[Bio] / FigKernelPackages / P2P.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/P2P.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (view) (download) (as text)

1 : olson 1.1 #
2 :     # This module contains the code for the P2P update protocol.
3 :     #
4 :     # Package P2P contains the namespace declarations, and possibly toplevel utility
5 :     # routines. (get_relay ?)
6 :     #
7 :     # Package P2P::Relay contains methods for contacting the P2P relay service. The actual
8 :     # implementation of the relay service is not contained here - it is a standalone module
9 :     # that can be installed on a web server that does not have a full SEED installed.
10 :     #
11 :     # Package P2P::Requestor contains the requestor-side code for the update protocol.
12 :     #
13 :     # Package P2P::Service contains the web service implementation routines for the
14 :     # protocol.
15 :     #
16 :    
17 :     package P2P;
18 :    
19 :     use FIG_Config;
20 :    
21 : olson 1.24 use AnyDBM_File;
22 : olson 1.19 use Fcntl;
23 :    
24 : olson 1.1 use strict;
25 :     use Exporter;
26 :     use base qw(Exporter);
27 :    
28 : olson 1.15 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
29 :    
30 : olson 1.1 use Data::Dumper;
31 :    
32 :     use vars qw(@EXPORT @EXPORT_OK);
33 :     @EXPORT = ();
34 :     @EXPORT_OK = qw($ns_p2p $ns_relay);
35 :    
36 :     our $ns_p2p = "http://thefig.info/schemas/p2p_update";
37 :     our $ns_relay = "http://thefig.info/schemas/p2p_relay";
38 :    
39 : olson 1.21 my $peg_batch_size = 1000;
40 :     my $anno_batch_size = 1000;
41 :     my $assign_batch_size = 1000;
42 : olson 1.23 my $fin_batch_size = 1000;
43 : olson 1.21
44 : olson 1.22 my $log_fh;
45 : olson 1.23 my $html_fh;
46 : olson 1.22
47 : olson 1.1 =pod
48 :    
49 : olson 1.25 =head1 perform_update($peer, $last_update, $skip_tough_search, $update_thru, $log_file, $html_file, $assignment_policy))
50 : olson 1.1
51 :     Perform a peer-to-peer update with the given peer. $peer is an instance of
52 :     P2P::Requestor which can connect to the peer. It is expected that the
53 :     SEED infrastructure will create this requestor appropriately for the
54 :     particular circumstance (direct connection, thru relay, etc).
55 :    
56 :     This code executes the high-level protocol, maintaining state between
57 :     calls to the peer to exchange the actual information.
58 :    
59 : olson 1.25 $last_update: Search for updates since this time.
60 :     $skip_tough_search: Do not use the time-consuming $fig->tough_search method as a last resort for peg mapping.
61 :     $update_thru: Search for updates until this time. Undef means to search for all since $last_update.
62 :     $log_file: Write logging information to this file.
63 :     $html_file: Write a HTML summary to this file.
64 :     $assignment_policy: If a list reference, contains the list of users from which we will accept assignments. If a code ref, a predicate that is passed ($peg, $timestamp, $author, $function) and returns true if the assignment should be made.
65 :    
66 : olson 1.1 =cut
67 :    
68 :     sub perform_update
69 :     {
70 : olson 1.25 my($fig, $peer, $last_update, $skip_tough_search, $update_thru, $log_file, $html_file,
71 :     $assignment_policy) = @_;
72 :    
73 :     my $allow_assignment;
74 : olson 1.22
75 :     $log_file = "/dev/null" unless $log_file ne "";
76 :     open($log_fh, ">>$log_file") or die "Cannot open logfile $log_file: $!\n";
77 :     $log_fh->autoflush(1);
78 :    
79 : olson 1.23 $html_file = "/dev/null" unless $html_file ne "";
80 :     open($html_fh, ">$html_file") or die "Cannot open htmlfile $html_file: $!\n";
81 :     $html_fh->autoflush(1);
82 :    
83 : olson 1.25 if (ref($assignment_policy) eq "CODE")
84 :     {
85 :     $allow_assignment = $assignment_policy;
86 :     }
87 :     elsif (ref($assignment_policy) eq "ARRAY")
88 :     {
89 :     my $ahash = {};
90 :     map { $ahash->{$_}++; } @$assignment_policy;
91 :     $allow_assignment = sub {
92 :     return $ahash->{$_[2]};
93 :     };
94 :     }
95 :     elsif (ref($assignment_policy) eq "HASH")
96 :     {
97 :     $allow_assignment = sub {
98 :     return $assignment_policy->{$_[2]};
99 :     };
100 :     }
101 :     else
102 :     {
103 :     print $log_fh "Invalid assignment policy $assignment_policy\n";
104 :     die "Invalid assignment policy $assignment_policy\n";
105 :     }
106 :    
107 : olson 1.23 my $now = localtime();
108 :     my $last_str = localtime($last_update);
109 :     print $html_fh <<END;
110 :     <h1>P2P Update at $now</h1>
111 :     Peer URL $peer->{url}<br>
112 :     Update from: $last_str<br>
113 :     END
114 :    
115 :     print $log_fh "Beginning P2P update at $now\n";
116 : olson 1.22 print $log_fh " Peer URL: $peer->{url}\n";
117 : olson 1.23 print $log_fh " Update from: $last_str\n";
118 : olson 1.22 print $log_fh "\n";
119 : olson 1.1
120 : olson 1.20 my $ret = $peer->request_update($last_update, $update_thru);
121 : olson 1.1
122 :     if (!$ret or ref($ret) ne "ARRAY")
123 :     {
124 : olson 1.18 die "perform_update: request_update failed\n";
125 : olson 1.1 }
126 :    
127 : olson 1.15 my($session, $target_release, $num_assignments, $num_annos, $num_pegs, $num_genomes,
128 : olson 1.1 $target_time, $compatible) = @$ret;
129 :    
130 : olson 1.18 print "perform_update: session=$session target=@$target_release num_annos=$num_annos\n";
131 : olson 1.1 print " num_pegs=$num_pegs num_genomes=$num_genomes target_time=$target_time compat=$compatible\n";
132 :    
133 : olson 1.22 my @my_release = $fig->get_release_info();
134 :    
135 :     print $log_fh "Session id = $session\n";
136 :     print $log_fh "Target release information: \n\t", join("\n\t", @$target_release), "\n";
137 :     print $log_fh "My release information: \n\t", join("\n\t", @my_release), "\n";
138 :     print $log_fh "$num_annos annotations\n";
139 :     print $log_fh "$num_assignments assignments\n";
140 :     print $log_fh "$num_pegs pegs\n";
141 :    
142 : olson 1.23 print $html_fh "Session id = $session<br>\n";
143 :     print $html_fh "Target release information: <br>\n\t", join("<br>\n\t", @$target_release), "<br>\n";
144 :     print $html_fh "My release information: <br>\n\t", join("<br>\n\t", @my_release), "<br>\n";
145 :     print $html_fh "$num_annos annotations<br>\n";
146 :     print $html_fh "$num_assignments assignments<br>\n";
147 :     print $html_fh "$num_pegs pegs<br>\n";
148 :    
149 : olson 1.1 #
150 : olson 1.19 # We now know the data release for our peer.
151 :     #
152 : olson 1.24 # Open up the peg translation cache database (a AnyDBM_File) tied
153 : olson 1.19 # to %peg_cache. We needn't worry about keeping it in a directory
154 :     # based on our current release, as it the cache directory is kept *in*
155 :     # the current data release directory.
156 :     #
157 :    
158 :     my $cache_handle;
159 :     my %peg_cache;
160 :     if ($target_release->[1] ne "")
161 :     {
162 :     my $cache_file = "pegcache.$target_release->[1].db";
163 :     my $cache_dir = "$FIG_Config::data/P2PQueue";
164 :     $fig->verify_dir($cache_dir);
165 :    
166 : olson 1.24 $cache_handle = tie(%peg_cache, "AnyDBM_File", "$cache_dir/$cache_file",
167 :     O_CREAT | O_RDWR, 0666);
168 : olson 1.19 $cache_handle or warn "Could not tie peg_cache to $cache_dir/$cache_file: $!\n";
169 :     }
170 :    
171 :     #
172 : olson 1.21 # peg_mapping is the local mapping from remote->local peg. This might
173 :     # be replacable by peg_cache from above.
174 : olson 1.1 #
175 : olson 1.21 my %peg_mapping;
176 : olson 1.1
177 : olson 1.21
178 : olson 1.1 #
179 : olson 1.21 # We have the information now to begin the update process. Retrieve the pegs.
180 : olson 1.1 #
181 :    
182 : olson 1.21 _compute_peg_mapping($fig, $peer, $session, $num_pegs, \%peg_mapping, \%peg_cache, $cache_handle,
183 :     $skip_tough_search);
184 : olson 1.7
185 : olson 1.24 eval { $cache_handle->sync();};
186 : olson 1.19 untie %peg_cache;
187 :    
188 :     #
189 : olson 1.15 # Create a list of locally-mapped annotations on a per-genome
190 :     # basis.
191 :     #
192 :    
193 :     my %genome_annos;
194 : olson 1.19
195 :     #
196 :     # %genome_assignments is a hash mapping from genome to a hashref
197 :     # that maps peg to function (since assignments are unique).
198 :     #
199 :     # (Hm. Unless two remote pegs map to the same local peg; unclear what to do
200 :     # then. Punt for now).
201 :     #
202 :     my %genome_assignments;
203 : olson 1.15
204 : olson 1.21 #
205 :     # Retrieve the annotations, and generate a list of mapped annotations.
206 :     #
207 :    
208 :     for (my $anno_start = 0; $anno_start < $num_annos; $anno_start += $anno_batch_size)
209 : olson 1.15 {
210 : olson 1.21 my $anno_req_len = $num_annos - $anno_start;
211 :     $anno_req_len = $anno_batch_size if $anno_req_len > $anno_batch_size;
212 : olson 1.15
213 : olson 1.21 print "Retrieve $anno_req_len annos at $anno_start\n";
214 : olson 1.22 print $log_fh "Retrieve $anno_req_len annos at $anno_start\n";
215 : olson 1.21
216 :     my $annos = $peer->get_annotations($session, $anno_start, $anno_req_len);
217 : olson 1.15
218 : olson 1.21 for my $anno (@$annos)
219 :     {
220 :     my($his_id, $ts, $author, $anno) = @$anno;
221 :    
222 :     my $my_id = $peg_mapping{$his_id};
223 :     next unless $my_id;
224 : olson 1.15
225 : olson 1.21 my $genome = $fig->genome_of($my_id);
226 :    
227 :     push(@{$genome_annos{$genome}}, [$my_id, $ts, $author, $anno]);
228 :     }
229 : olson 1.15 }
230 :    
231 : olson 1.19 #
232 :     # Do the same for the assignments
233 :     #
234 :    
235 : olson 1.20 # print Dumper($assignments);
236 :    
237 : olson 1.21
238 :     for (my $assign_start = 0; $assign_start < $num_assignments; $assign_start += $assign_batch_size)
239 : olson 1.19 {
240 : olson 1.21 my $assign_req_len = $num_assignments - $assign_start;
241 :     $assign_req_len = $assign_batch_size if $assign_req_len > $assign_batch_size;
242 : olson 1.19
243 : olson 1.21 print "Retrieve $assign_req_len assigns at $assign_start\n";
244 : olson 1.22 print $log_fh "Retrieve $assign_req_len assigns at $assign_start\n";
245 : olson 1.21
246 :     my $assignments = $peer->get_assignments($session, $assign_start, $assign_req_len);
247 :    
248 :     for my $assign (@$assignments)
249 :     {
250 :     my($his_id, $ts, $author, $func) = @$assign;
251 : olson 1.19
252 : olson 1.21 my $my_id = $peg_mapping{$his_id};
253 :     next unless $my_id;
254 : olson 1.19
255 : olson 1.21 my $genome = $fig->genome_of($my_id);
256 : olson 1.19
257 : olson 1.21 $genome_assignments{$genome}->{$my_id} = [$my_id, $ts, $author, $func];
258 :     }
259 : olson 1.19 }
260 :    
261 :     # print Dumper(\%genome_annos);
262 : olson 1.15
263 :     #
264 :     # Now install annotations.
265 :     #
266 :    
267 :     for my $genome (keys(%genome_annos))
268 :     {
269 : olson 1.19 #
270 :     # Plan: Apply the merge_annotations.pl logic. Read the annotations
271 :     # from the per-org annotations file, add the new ones here, sort, and remove duplicates.
272 :     # Write the results to the annotations file.
273 :     #
274 :     # When we are all done, rerun the index_annotations script.
275 :     #
276 :     # Why not do that incrementally? Partly because the annotation_seeks table doesn't
277 :     # have a column for the genome id, so a removal of old data would require a
278 :     # string-match query; since a complete reindex of the annotations is pretty
279 :     # fast (60 sec on a G4 laptop on a firewire disk), it's not clear whether the incremental
280 :     # update would actually be a win.
281 :     #
282 :    
283 :     my @annos = @{$genome_annos{$genome}};
284 :     my $assignments = $genome_assignments{$genome};
285 :     #
286 :     # %assignment_annos is a hash from peg to the list
287 :     # of annotations for that peg.
288 :     #
289 :     my %assignment_annos;
290 :    
291 :     my $dir = "$FIG_Config::organisms/$genome";
292 :     my $anno_file = "$dir/annotations";
293 :     my $anno_bak = "$dir/annotations." . time;
294 :    
295 :     my $new_count = @annos;
296 :    
297 :     #
298 :     # Rename the annotations file to a new name based on the current time.
299 :     #
300 :    
301 : olson 1.23 my $gs = $fig->genus_species($genome);
302 :     print $html_fh "<h1>Updates for $genome ($gs)</h1>\n";
303 :    
304 : olson 1.19 if (-f $anno_file)
305 :     {
306 :     rename($anno_file, $anno_bak) or die "Cannot rename $anno_file to $anno_bak: $!";
307 : olson 1.22 print $log_fh "Moved annotations file $anno_file to backup $anno_bak\n";
308 : olson 1.19 }
309 :    
310 :     if (open(my $fh, "<$anno_bak"))
311 :     {
312 :     #
313 :     # While we are scanning here, we look for the latest local assignment
314 :     # for any peg for which we are installing an assignment.
315 :     #
316 :     local($/) = "\n//\n";
317 :    
318 :     my($chunk, $peg, $ts, $author, $anno);
319 :    
320 :     while (defined($chunk = <$fh>))
321 :     {
322 :     chomp $chunk;
323 :     ($peg, $ts, $author, $anno) = split(/\n/, $chunk, 4);
324 :    
325 :     if ($peg =~ /^fig\|/ and $ts =~ /^\d+$/)
326 :     {
327 :     my $ent = [$peg, $ts, $author, $anno];
328 :     push(@annos, $ent);
329 :    
330 :     if (defined($assignments->{$peg}))
331 :     {
332 :     #
333 :     # We have an incoming assignment for this peg.
334 :     # Don't parse anything yet, but push the annotation
335 :     # on a list so we can sort by date.
336 :     #
337 :     push(@{$assignment_annos{$peg}}, $ent);
338 :     }
339 :     }
340 :     }
341 :     close($fh);
342 :     }
343 :    
344 :     #
345 :     # Determine if we are going to install an assignment.
346 :     #
347 :    
348 : olson 1.23 my $cgi_url = &FIG::cgi_url();
349 :     print $html_fh "<h2>Assignments made</h2>\n";
350 : olson 1.25 print $html_fh "<table border=\"1\">\n";
351 : olson 1.23 print $html_fh "<tr><th>PEG</th><th>Old assignment</th><th>New assignment</th><tr>\n";
352 :    
353 : olson 1.19 for my $peg (keys %$assignments)
354 :     {
355 : olson 1.20 my(undef, $ts, $author, $func) = @{$assignments->{$peg}};
356 :    
357 :     #
358 :     # Sort the existing annotations for this peg by date.
359 :     #
360 :     # Recall that this list has entries [$peg, $timestamp, $author, $anno]
361 :     #
362 :    
363 :     my @eannos;
364 :     if (ref($assignment_annos{$peg}))
365 :     {
366 :     @eannos = sort { $b->[1] <=> $a->[1] } @{$assignment_annos{$peg}};
367 :     }
368 :     else
369 :     {
370 :     #
371 :     # No assignment annotations found.
372 :     #
373 :     @eannos = ();
374 :     }
375 :    
376 :     # print "Assignment annos for $peg: ", Dumper(\@eannos);
377 : olson 1.19
378 :     #
379 : olson 1.20 # Filter out just the master assignments that are newer than
380 :     # the one we are contemplating putting in place.
381 : olson 1.19 #
382 :    
383 : olson 1.20 my @cand = grep {
384 :     ($_->[1] > $ts) and ($_->[3] =~ /Set master function to/)
385 :     } @eannos;
386 :    
387 :     if (@cand > 0)
388 :     {
389 :     #
390 :     # Here is were some policy needs to be put in place --
391 :     # we have a more recent annotation on the current system.
392 :     #
393 :     # For now, we will not install an assignment if there is any
394 :     # newer assignment in place.
395 :     #
396 :    
397 :     warn "Skipping assignment for $peg $func due to more recent assignment $cand[0]->[3]\n";
398 : olson 1.22 print $log_fh "Skipping assignment for $peg $func due to more recent assignment $cand[0]->[3]\n";
399 : olson 1.20 }
400 :     else
401 :     {
402 :     #
403 :     # Nothing is blocking us. While we are testing, just slam this assignment in.
404 :     #
405 : olson 1.19
406 : olson 1.20 my $old = $fig->function_of($peg, 'master');
407 :    
408 : olson 1.25 if ($old ne $func and &$allow_assignment($peg, $ts, $author, $func))
409 : olson 1.21 {
410 : olson 1.23 my $l = "$cgi_url/protein.cgi?prot=$peg";
411 :     print $html_fh "<tr><td><a href=\"$l\">$peg</a></td><td>$old</td><td>$func</td></tr>\n";
412 :    
413 : olson 1.21 print "Assign $peg $func\n";
414 : olson 1.22 print $log_fh "Assign $peg $func\n";
415 :     print $log_fh " was $old\n";
416 : olson 1.21 $fig->assign_function($peg, 'master', $func);
417 : olson 1.23
418 : olson 1.21 }
419 : olson 1.20 }
420 : olson 1.19 }
421 :    
422 : olson 1.23 print $html_fh "</table>\n";
423 :    
424 :     print $html_fh "<h2>Annotations added</h2>\n";
425 : olson 1.25 print $html_fh "<table border=\"1\">\n";
426 : olson 1.23 print $html_fh "<tr><th>PEG</th><th>Time</th><th>Author</th><th>Annotation</th></tr>\n";
427 :    
428 : olson 1.19 open(my $outfh, ">$anno_file") or die "Cannot open new annotation file $anno_file: $!\n";
429 :    
430 :     my $last;
431 :     my @sorted = sort { ($a->[0] cmp $b->[0]) or ($a->[1] <=> $b->[1]) } @annos;
432 :     my $inst = 0;
433 :     my $dup = 0;
434 :     foreach my $ann (@sorted)
435 :     {
436 :     my $txt = join("\n", @$ann);
437 :     #
438 :     # Drop the trailing \n if there is one; we will add it back when we print and
439 :     # want to ensure the file format remains sane.
440 :     #
441 :     chomp $txt;
442 :     if ($txt ne $last)
443 :     {
444 : olson 1.23 my $peg = $ann->[0];
445 :     my $l = "$cgi_url/protein.cgi?prot=$peg";
446 :     print $html_fh "<tr>" . join("\n", map { "<td>$_</td>" }
447 :     "<a href=\"$l\">$peg</a>",
448 :     scalar(localtime($ann->[1])), $ann->[2], $ann->[3])
449 :     . "</tr>\n";
450 :    
451 : olson 1.19 print $outfh "$txt\n//\n";
452 :     $last = $txt;
453 : olson 1.21 # print "Inst $ann->[0] $ann->[1] $ann->[2]\n";
454 : olson 1.19 $inst++;
455 :     }
456 :     else
457 :     {
458 : olson 1.21 # print "Dup $ann->[0] $ann->[1] $ann->[2]\n";
459 : olson 1.19 $dup++;
460 :     }
461 :     }
462 : olson 1.23 print $html_fh "</table>\n";
463 : olson 1.19 close($outfh);
464 :     chmod(0666, $anno_file) or warn "Cannot chmod 0666 $anno_file: $!\n";
465 :     print "Wrote $anno_file. $new_count new annos, $inst installed, $dup duplicates\n";
466 : olson 1.22 print $log_fh "Wrote $anno_file. $new_count new annos, $inst installed, $dup duplicates\n";
467 : olson 1.15 }
468 : olson 1.23 close($html_fh);
469 : olson 1.1 }
470 :    
471 : olson 1.21 #
472 :     # Compute the peg mapping for a session.
473 :     #
474 :     # $fig Active FIG instance
475 :     # $peer P2P peer for this session.
476 :     # $session P2P session ID
477 :     # $peg_mapping Hash ref for the remote -> local PEG mapping
478 :     # $peg_cache Hash ref for the persistent remote -> local PEG mapping cache db.
479 : olson 1.24 # $cache_handle AnyDBM_File handle corresponding to $peg_cache.
480 : olson 1.21 #
481 :     sub _compute_peg_mapping
482 :     {
483 :     my($fig, $peer, $session, $num_pegs, $peg_mapping, $peg_cache, $cache_handle, $skip_tough_search) = @_;
484 :    
485 :     #
486 :     # genome_map is a hash mapping from target genome id to a list of
487 :     # pegs on the target. This is used to construct a finalize_pegs request after
488 :     # the first phase of peg mapping.
489 :     #
490 :    
491 :     my %genome_map;
492 :    
493 :     #
494 :     # target_genome_info is a hash mapping from target genome
495 :     # identifier to the target-side information on the genome -
496 :     # number of contigs, number of nucleotides, checksum.
497 :     #
498 :     # We accumulate it here across possibly multiple batches of
499 :     # peg retrievals in order to create a single finalization
500 :     # list.
501 :     #
502 :    
503 :     my %target_genome_info;
504 :    
505 :     #
506 :     # For very large transfers, we need to batch the peg processing.
507 :     #
508 :    
509 :     for (my $peg_start = 0; $peg_start < $num_pegs; $peg_start += $peg_batch_size)
510 :     {
511 :     my $peg_req_len = $num_pegs - $peg_start;
512 :     $peg_req_len = $peg_batch_size if $peg_req_len > $peg_batch_size;
513 :    
514 :     print "Getting $peg_req_len pegs at $peg_start\n";
515 : olson 1.22 print $log_fh "Getting $peg_req_len pegs at $peg_start\n";
516 : olson 1.21 my $ret = $peer->get_pegs($session, $peg_start, $peg_req_len);
517 :    
518 :     if (!$ret or ref($ret) ne "ARRAY")
519 :     {
520 :     die "perform_update: get_pegs failed\n";
521 :     }
522 :    
523 :     my($peg_list, $genome_list) = @$ret;
524 :    
525 :     for my $gent (@$genome_list)
526 :     {
527 :     $target_genome_info{$gent->[0]} = $gent;
528 :     }
529 :    
530 :     _compute_peg_mapping_batch($fig, $peer, $session, $peg_mapping, $peg_cache, $cache_handle,
531 :     $peg_list, \%genome_map);
532 :     }
533 :    
534 :     #
535 :     # We have finished first pass. Now go over the per-genome mappings that need to be made.
536 :     #
537 :     # $genome_map{$genome_id} is a list of pegs that reside on that genome.
538 :     # The pegs and genome id are both target-based identifiers.
539 :     #
540 :     # %target_genome_info defines the list of genome information we have on the remote
541 :     # side.
542 :     #
543 :     # We build a request to be passed to finalize_pegs. Each entry in the request is either
544 :     # ['peg_genome', $peg] which means that we have a genome that corresponds to the
545 :     # genome the peg is in. We can attempt to map via contig locations.
546 :     #
547 :     # If that is not the case, we pass a request entry of ['peg_unknown', $peg]
548 :     # which will result in the sequence data being returned.
549 :     #
550 :    
551 :     my @finalize_req = ();
552 :    
553 :     #
554 :     # local_genome maps a target peg identifier to the local genome id it translates to.
555 :     #
556 :     my %local_genome;
557 :    
558 :     for my $genome (keys(%target_genome_info))
559 :     {
560 :     my($tg, $n_contigs, $n_nucs, $cksum) = @{$target_genome_info{$genome}};
561 :    
562 :     $tg eq $genome or die "Invalid entry in target_genome_info for $genome => $tg, $n_contigs, $n_nucs, $cksum";
563 :    
564 :     #
565 :     # Don't bother unless we have any pegs to look up.
566 :     #
567 :     next unless defined($genome_map{$genome});
568 :    
569 :     #
570 :     # Determine if we have a local genome installed that matches precisely the
571 :     # genome on the target side.
572 :     #
573 :     my $my_genome = $fig->find_genome_by_content($genome, $n_contigs, $n_nucs, $cksum);
574 :    
575 :     my $pegs = $genome_map{$genome};
576 :    
577 :     if ($my_genome)
578 :     {
579 :     #
580 :     # We do have such a local genome. Generate a peg_genome request to
581 :     # get the location information from the target side.
582 :     #
583 :     # Also remember the local genome mapping for this peg.
584 :     #
585 :    
586 :     print "$genome mapped to $my_genome\n";
587 : olson 1.22 print $log_fh "$genome mapped to $my_genome\n";
588 : olson 1.21 for my $peg (@$pegs)
589 :     {
590 :     push(@finalize_req, ['peg_genome', $peg]);
591 :     $local_genome{$peg} = $my_genome;
592 :     }
593 :    
594 :     }
595 :     else
596 :     {
597 :     #
598 :     # We don't have such a genome. We need to retrieve the
599 :     # sequence data in order to finish mapping.
600 :     #
601 :     push(@finalize_req, map { ['peg_unknown', $_] } @$pegs);
602 :     }
603 :     }
604 :    
605 :     #
606 :     # We've built our finalization request. Handle it (possibly with batching here too).
607 :     #
608 :    
609 :     _process_finalization_request($fig, $peer, $session, $peg_mapping, $peg_cache, $cache_handle,
610 :     \%local_genome, \@finalize_req, $skip_tough_search);
611 :    
612 :     }
613 :    
614 :     #
615 :     # Process one batch of PEGs.
616 :     #
617 :     # Same args as _compute_peg_mapping, with the addition of:
618 :     #
619 :     # $peg_list List of pegs to be processed
620 :     # $genome_map Hash maintaining list of genomes with their pegs.
621 :     # $target_genome_info Hash maintaining overall list of target-side genome information.
622 :     #
623 :     sub _compute_peg_mapping_batch
624 :     {
625 :     my($fig, $peer, $session, $peg_mapping, $peg_cache, $cache_handle,
626 :     $peg_list, $genome_map, $target_genome_info) = @_;
627 :    
628 :     #
629 :     # Walk the list of pegs as returned from get_pegs() and determine what has to
630 :     # be done.
631 :     #
632 :     # If the entry is ['peg', $peg], we can use the peg ID as is.
633 :     #
634 :     # If the entry is ['peg_info', $peg, $alias_list, $genome], the peg
635 :     # has the given aliases, and is in the given genome.
636 :     #
637 :     for my $peg_info (@$peg_list)
638 :     {
639 :     my($key, $peg, @rest) = @$peg_info;
640 :    
641 :     if ($key eq 'peg')
642 :     {
643 :     #
644 :     # Peg id is directly usable.
645 :     #
646 :     $peg_mapping->{$peg} = $peg;
647 :     }
648 :     elsif ($key eq 'peg_info')
649 :     {
650 :     #
651 :     # Peg id not directly usable. See if we have it in the cache.
652 :     #
653 :    
654 :     if ((my $cached = $peg_cache->{$peg}) ne "")
655 :     {
656 :     #
657 :     # Cool, we've cached the result. Use it.
658 :     #
659 :    
660 :     $peg_mapping->{$peg} = $cached;
661 :     # warn "Found cached mapping $peg => $cached\n";
662 :     next;
663 :     }
664 :    
665 :     #
666 :     # It is not cached. Attempt to resolve by means of alias IDs.
667 :     #
668 :    
669 :     my($alias_list, $genome_id) = @rest;
670 :    
671 :     for my $alias (@$alias_list)
672 :     {
673 :     my $mapped = $fig->by_alias($alias);
674 :     if ($mapped)
675 :     {
676 :     print "$peg maps to $mapped via $alias\n";
677 : olson 1.22 print $log_fh "$peg maps to $mapped via $alias\n";
678 : olson 1.21 $peg_mapping->{$peg}= $mapped;
679 :     $peg_cache->{$peg} = $mapped;
680 :     last;
681 :     }
682 :     }
683 :    
684 :     #
685 :     # If we weren't able to resolve by ID,
686 :     # add to %genome_map as a PEG that will need
687 :     # to be resolved by means of contig location.
688 :     #
689 :    
690 :     if (!defined($peg_mapping->{$peg}))
691 :     {
692 :     push(@{$genome_map->{$genome_id}}, $peg);
693 :     print "$peg did not map on first pass\n";
694 : olson 1.22 print $log_fh "$peg did not map on first pass\n";
695 : olson 1.21 }
696 :     }
697 :     }
698 :    
699 :     #
700 :     # Flush the cache to write out any computed mappings.
701 :     #
702 : olson 1.24 eval { $cache_handle->sync();};
703 : olson 1.21
704 :     }
705 :    
706 :     sub _process_finalization_request
707 :     {
708 :     my($fig, $peer, $session, $peg_mapping, $peg_cache, $cache_handle,
709 :     $local_genome, $finalize_req, $skip_tough_search) = @_;
710 :    
711 :     #
712 :     # Immediately return unless there's something to do.
713 :     #
714 :     return unless ref($finalize_req) and @$finalize_req > 0;
715 : olson 1.1
716 : olson 1.21 while (@$finalize_req > 0)
717 :     {
718 :     my @req = splice(@$finalize_req, 0, $fin_batch_size);
719 :    
720 :     print "Invoking finalize_pegs on ", int(@req), " pegs\n";
721 : olson 1.22 print $log_fh "Invoking finalize_pegs on ", int(@req), " pegs\n";
722 : olson 1.21 my $ret = $peer->finalize_pegs($session, \@req);
723 :    
724 :     if (!$ret or ref($ret) ne "ARRAY")
725 :     {
726 :     die "perform_update: finalize_pegs failed\n";
727 :     }
728 :    
729 :     #
730 :     # The return is a list of either location entries or
731 :     # sequence data. Attempt to finish up the mapping.
732 :     #
733 :    
734 :     my(%sought, %sought_seq);
735 :    
736 :    
737 :     my $dbh = $fig->db_handle();
738 :     for my $entry (@$ret)
739 :     {
740 :     my($what, $peg, @rest) = @$entry;
741 :    
742 :     if ($what eq "peg_loc")
743 :     {
744 :     my($strand, $start, $end, $cksum, $seq) = @rest;
745 :    
746 :     #
747 :     # We have a contig location. Try to find a matching contig
748 :     # here, and see if it maps to something.
749 :     #
750 :    
751 :     my $my_genome = $local_genome->{$peg};
752 :     my $local_contig = $fig->find_contig_with_checksum($my_genome, $cksum);
753 :     if ($local_contig)
754 :     {
755 :     #
756 :     # Now look up the local peg. We match on the end location; depending on the strand
757 :     # the feature is on, we want to look at either minloc or maxloc.
758 :     #
759 :    
760 :     my $whichloc = $strand eq '-' ? "minloc" : "maxloc";
761 :    
762 :     my $res = $dbh->SQL(qq!SELECT id from features
763 :     WHERE $whichloc = $end and genome = '$my_genome' and
764 :     contig = '$local_contig'
765 :     !);
766 :    
767 :     if ($res and @$res > 0)
768 :     {
769 :     my(@ids) = map { $_->[0] } @$res;
770 :     my $id = $ids[0];
771 :     $peg_mapping->{$peg} = $id;
772 :     $peg_cache->{$peg} = $id;
773 :     print "Mapped $peg to $id via contigs\n";
774 :     if (@$res > 1)
775 :     {
776 :     warn "Multiple mappings found for $peg: @ids\n";
777 : olson 1.22 print $log_fh "Multiple mappings found for $peg: @ids\n";
778 : olson 1.21 }
779 :     }
780 :     else
781 :     {
782 :     print "failed: $peg $my_genome and contig $local_contig start=$start end=$end strand=$strand\n";
783 : olson 1.22 print $log_fh "failed: $peg $my_genome and contig $local_contig start=$start end=$end strand=$strand\n";
784 : olson 1.23 print $html_fh "Contig match failed: $peg $my_genome contig $local_contig start $start end $end strand $strand<br>\n";
785 : olson 1.21 $sought{$peg}++;
786 :     $sought_seq{$peg} = $seq;
787 :     }
788 :     }
789 :     else
790 :     {
791 :     print "Mapping failed for $my_genome checksum $cksum\n";
792 : olson 1.22 print $log_fh "Mapping failed for $my_genome checksum $cksum\n";
793 : olson 1.23 print $html_fh "Mapping failed for $my_genome checksum $cksum<br>\n";
794 : olson 1.21 $sought{$peg}++;
795 :     $sought_seq{$peg} = $seq;
796 :     }
797 :     }
798 :     elsif ($what eq "peg_seq")
799 :     {
800 :     my($seq) = @rest;
801 :    
802 :     $sought{$peg}++;
803 :     $sought_seq{$peg} = $seq;
804 :     }
805 :     }
806 :    
807 :     #
808 :     # Now see if we need to do a tough search.
809 :     #
810 :    
811 :     if (keys(%sought) > 0 and !$skip_tough_search)
812 :     {
813 :     my %trans;
814 :    
815 :     print "Starting tough search\n";
816 : olson 1.22 print $log_fh "Starting tough search\n";
817 : olson 1.21
818 :     $fig->tough_search(undef, \%sought_seq, \%trans, \%sought);
819 :     print "Tough search translated: \n";
820 : olson 1.22 print $log_fh "Tough search translated: \n";
821 : olson 1.21 while (my($tpeg, $ttrans) = each(%trans))
822 :     {
823 :     print " $tpeg -> $ttrans\n";
824 : olson 1.22 print $log_fh " $tpeg -> $ttrans\n";
825 : olson 1.21 $peg_mapping->{$tpeg} = $ttrans;
826 :     $peg_cache->{$tpeg} = $ttrans;
827 :     }
828 :     }
829 :     }
830 :     }
831 : olson 1.15
832 : olson 1.1 #############
833 :     #
834 :     # P2P Relay
835 :     #
836 :     #############
837 :    
838 :    
839 :     package P2P::Relay;
840 :     use strict;
841 :    
842 :     use Data::Dumper;
843 :     use SOAP::Lite;
844 :    
845 :     use P2P;
846 :    
847 :     sub new
848 :     {
849 :     my($class, $url) = @_;
850 :    
851 : olson 1.25 my $creds = [];
852 : olson 1.24
853 :     my $proxy = SOAP::Lite->uri($P2P::ns_relay)->proxy([$url,
854 :     credentials => $creds]);
855 : olson 1.1
856 :     my $self = {
857 :     url => $url,
858 :     proxy => $proxy,
859 :     };
860 :     return bless($self, $class);
861 :     }
862 :    
863 :     sub enumerate_annotation_systems
864 :     {
865 :     my($self) = @_;
866 :    
867 :     return $self->{proxy}->enumerate_annotation_systems()->result;
868 :     }
869 :    
870 :     sub fetch_queries
871 :     {
872 :     my($self, $id) = @_;
873 :    
874 :     my $reply = $self->{proxy}->fetch_queries($id);
875 :    
876 :     if ($reply->fault)
877 :     {
878 :     print "Failed to fetch queries: ", $reply->faultcode, " ", $reply->faultstring, "\n";
879 :     return undef;
880 :     }
881 :    
882 :     return $reply->result;
883 :     }
884 :    
885 :     sub deposit_answer
886 :     {
887 :     my($self, $id, $key, $answer) = @_;
888 :    
889 :     my $reply = $self->{proxy}->deposit_answer($id, $key,
890 :     SOAP::Data->type('base64')->value($answer));
891 :    
892 :     if ($reply->fault)
893 :     {
894 :     print "deposit_answer got fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
895 :     return undef;
896 :     }
897 :    
898 :     return $reply;
899 :     }
900 :    
901 :     =pod
902 :    
903 :     =head1 await_result
904 :    
905 :     Await the result from a possibly-asynchronous soap request.
906 :    
907 :     Look at the reply that we have. If it's a deferred reply, loop polling
908 :     the relay for the actual result.
909 :    
910 :     We determine if the reply is a deferred reply by examining the namespace
911 :     URI of the response. A response will be generated from the relay's namespace,
912 :     rather than that of the application itself.
913 :    
914 :     =cut
915 :    
916 :     sub await_result
917 :     {
918 :     my($self, $reply) = @_;
919 :    
920 :     while (1)
921 :     {
922 :     #
923 :     # Retrieve the namespace of the response, which is the first
924 :     # element in the body of the message.
925 :     #
926 :     my $ns = $reply->namespaceuriof('/Envelope/Body/[1]');
927 : olson 1.20 # print "Reply ns=$ns want $P2P::ns_relay\n";
928 : olson 1.1
929 :     if ($ns eq $P2P::ns_relay)
930 :     {
931 :     my $val = $reply->result;
932 : olson 1.20 # print "got val=", Dumper($val);
933 : olson 1.1 if ($val->[0] eq 'deferred')
934 :     {
935 :     #
936 :     # Sleep a little, then try to retrieve the response.
937 :     #
938 :    
939 :     sleep(1);
940 :     my $id = $val->[1];
941 :    
942 :     print "Retrieving reply\n";
943 :     $reply = $self->{proxy}->call_completed($id);
944 :     }
945 :     else
946 :     {
947 :     #
948 :     # We're not sure what to do here..
949 :     #
950 :     return undef;
951 :     }
952 :     }
953 :     else
954 :     {
955 :     #
956 :     # We got an actual response. Return it.
957 :     #
958 :    
959 :     return $reply;
960 :     }
961 :     }
962 :     }
963 :    
964 :     #############
965 :     #
966 :     # P2P Requestor
967 :     #
968 :     #############
969 :    
970 :     package P2P::Requestor;
971 :     use strict;
972 :    
973 :     use Data::Dumper;
974 : olson 1.15 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
975 : olson 1.1
976 :     use SOAP::Lite;
977 : olson 1.15
978 :     #use SOAP::Lite +trace => [qw(transport dispatch result debug)];
979 : olson 1.1 use P2P;
980 :    
981 :     #
982 :     # Create a new Requestor. It contains a reference to the FIG instance
983 :     # so that we can run the protocol completely from in here.
984 :     #
985 :    
986 :     sub new
987 :     {
988 : olson 1.24 my($class, $fig, $url, $peer_id, $relay, $credentials) = @_;
989 : olson 1.1
990 : olson 1.24 $credentials = [] unless ref($credentials);
991 :    
992 :     my $proxy = SOAP::Lite->uri($ns_p2p)->proxy($url, timeout => 3600,
993 :     credentials => $credentials);
994 : olson 1.1
995 :     my $self = {
996 :     fig => $fig,
997 :     url => $url,
998 :     peer_id => $peer_id,
999 :     proxy => $proxy,
1000 :     relay => $relay,
1001 :     };
1002 :     return bless($self, $class);
1003 :     }
1004 :    
1005 :     #
1006 :     # First step: Request an update.
1007 :     #
1008 :     # We need to determine some notion of what our release is, since we are not
1009 :     # currently tagging them explicitly. Until we delve into this more,
1010 :     # I am going to return a null release, which means the same-release
1011 :     # optimization won't be able to kick in.
1012 :     #
1013 :     # We also need to determine the last time we got an update from this
1014 :     # system.
1015 :     #
1016 :    
1017 :     sub request_update
1018 :     {
1019 : olson 1.20 my($self, $last_update, $update_thru) = @_;
1020 : olson 1.1
1021 : olson 1.18 my $rel = [$self->{fig}->get_release_info()];
1022 : olson 1.1
1023 :     if (!defined($last_update))
1024 :     {
1025 :     $last_update = $self->{fig}->get_peer_last_update($self->{peer_id});
1026 :     }
1027 : olson 1.17
1028 :     print "Requesting update via $self->{proxy}\n";
1029 : olson 1.20 my $reply = $self->{proxy}->request_update($rel, $last_update, $update_thru);
1030 :     # print "Got reply ", Dumper($reply);
1031 : olson 1.1
1032 :     if ($self->{relay})
1033 :     {
1034 :     $reply = $self->{relay}->await_result($reply);
1035 :     }
1036 :    
1037 :     if ($reply->fault)
1038 :     {
1039 :     print "request_update triggered fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
1040 :     return undef;
1041 :     }
1042 :    
1043 :     return $reply->result;
1044 :     }
1045 :    
1046 :     =pod
1047 :    
1048 :     =head1 get_pegs($session_id, $start, $length)
1049 :    
1050 :    
1051 :     =cut
1052 :    
1053 :     sub get_pegs
1054 :     {
1055 :     my($self, $session_id, $start, $length) = @_;
1056 :    
1057 :     return $self->call("get_pegs", $session_id, $start, $length);
1058 :     }
1059 :    
1060 : olson 1.6 sub finalize_pegs
1061 :     {
1062 :     my($self, $session_id, $request) = @_;
1063 :    
1064 :     return $self->call("finalize_pegs", $session_id, $request);
1065 :     }
1066 :    
1067 : olson 1.15 sub get_annotations
1068 :     {
1069 :     my($self, $session_id, $start, $length) = @_;
1070 :    
1071 :     return $self->call("get_annotations", $session_id, $start, $length);
1072 :     }
1073 :    
1074 : olson 1.19 sub get_assignments
1075 :     {
1076 :     my($self, $session_id, $start, $length) = @_;
1077 :    
1078 :     return $self->call("get_assignments", $session_id, $start, $length);
1079 :     }
1080 :    
1081 : olson 1.1 sub call
1082 :     {
1083 :     my($self, $func, @args) = @_;
1084 : olson 1.15
1085 :     my $t0 = [gettimeofday()];
1086 :     print "Calling $func\n";
1087 : olson 1.1 my $reply = $self->{proxy}->$func(@args);
1088 : olson 1.15 my $t1 = [gettimeofday()];
1089 :    
1090 :     my $elap = tv_interval($t0, $t1);
1091 :     print "Call to $func took $elap\n";
1092 : olson 1.1
1093 :     if ($self->{relay})
1094 :     {
1095 :     $reply = $self->{relay}->await_result($reply);
1096 :     }
1097 :    
1098 :     if ($reply->fault)
1099 :     {
1100 :     print "$func triggered fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
1101 :     return undef;
1102 :     }
1103 :    
1104 :     return $reply->result;
1105 :     }
1106 :    
1107 :    
1108 :     #############
1109 :     #
1110 :     # P2P Service
1111 :     #
1112 :     # Code in this module is invoked on the target on behalf of a requestor.
1113 :     #
1114 :     #############
1115 :    
1116 :     package P2P::Service;
1117 :    
1118 :     use Data::Dumper;
1119 :    
1120 :     use FIG;
1121 :     use FIG_Config;
1122 :     use strict;
1123 :    
1124 :     use File::Temp qw(tempdir);
1125 :     use File::Basename;
1126 :    
1127 :     sub request_update
1128 :     {
1129 : olson 1.20 my($class, $his_release, $last_update, $update_thru)= @_;
1130 : olson 1.1
1131 :     #
1132 :     # Verify input.
1133 :     #
1134 :    
1135 :     if ($last_update !~ /^\d+$/)
1136 :     {
1137 :     die "request_update: last_update must be a number (not '$last_update')\n";
1138 :     }
1139 :    
1140 : olson 1.20 if ($update_thru eq "")
1141 :     {
1142 :     $update_thru = time + 10000;
1143 :     }
1144 :    
1145 : olson 1.1 #
1146 :     # Create a new session id and a spool directory to use for storage
1147 :     # of information about it. This can go in the tempdir since it is
1148 :     # not persistent.
1149 :     #
1150 :    
1151 :     &FIG::verify_dir("$FIG_Config::temp/p2p_spool");
1152 : olson 1.22 my $spool_dir = tempdir(DIR => "$FIG_Config::temp/p2p_spool");
1153 : olson 1.1
1154 : olson 1.22 #my $spool_dir = "$FIG_Config::temp/p2p_spool/test";
1155 : olson 1.1 &FIG::verify_dir($spool_dir);
1156 :    
1157 :     my $session_id = basename($spool_dir);
1158 :     my $now = time;
1159 :    
1160 :     #
1161 :     # Gather the list of pegs and annotations for the update.
1162 :     #
1163 :    
1164 :     my $fig = new FIG;
1165 :    
1166 :     my $all_genomes = [$fig->genomes];
1167 :    
1168 :     my %all_genomes = map { $_ => 1 } @$all_genomes;
1169 :    
1170 :     my %pegs;
1171 : olson 1.15
1172 :     #
1173 :     # We keep track of usernames that have been seen, so that
1174 :     # we can both update our local user database and
1175 :     # we can report them to our peer.
1176 :     #
1177 :    
1178 :     my %users;
1179 : olson 1.1
1180 :     my $num_annos = 0;
1181 :     my $num_genomes = 0;
1182 :     my $num_pegs = 0;
1183 : olson 1.15 my $num_assignments = 0;
1184 : olson 1.1
1185 :     my $anno_fh;
1186 :     open($anno_fh, ">$spool_dir/annos");
1187 :    
1188 :     my $peg_fh;
1189 :     open($peg_fh, ">$spool_dir/pegs");
1190 :    
1191 :     my $genome_fh;
1192 :     open($genome_fh, ">$spool_dir/genomes");
1193 :    
1194 : olson 1.15 my $assign_fh;
1195 :     open($assign_fh, ">$spool_dir/assignments");
1196 :    
1197 : olson 1.25 #
1198 :     # We originally used a query to get the PEGs that needed to have annotations
1199 :     # sent. Unfortunately, this performed very poorly due to all of the resultant
1200 :     # seeking around in the annotations files.
1201 :     #
1202 :     # The code below just runs through all of the anno files looking for annos.
1203 :     #
1204 :     # A better way to do this would be to do a query to retrieve the genome id's for
1205 :     # genomes that have updates. The problem here is that the annotation_seeks
1206 :     # table doesn't have an explicit genome field.
1207 :     #
1208 :     # Surprisingly, to me anyway, the following query appers to run quickly, in both
1209 :     # postgres and mysql:
1210 :     #
1211 :     # SELECT distinct(substring(fid from 5 for position('.peg.' in fid) - 5))
1212 :     # FROM annotation_seeks
1213 :     # WHERE dateof > some-date.
1214 :     #
1215 :     # The output of that can be parsed to get the genome id and just those
1216 :     # annotations files searched.
1217 :     #
1218 :    
1219 : olson 1.1 for my $genome (@$all_genomes)
1220 :     {
1221 :     my $num_annos_for_genome = 0;
1222 : olson 1.15 my %assignment;
1223 : olson 1.1
1224 :     my $genome_dir = "$FIG_Config::organisms/$genome";
1225 :     next unless -d $genome_dir;
1226 :    
1227 :     my $afh;
1228 :     if (open($afh, "$genome_dir/annotations"))
1229 :     {
1230 :     my($fid, $anno_time, $who, $anno_text);
1231 :     local($/);
1232 :     $/ = "//\n";
1233 :     while (my $ann = <$afh>)
1234 :     {
1235 :     chomp $ann;
1236 :    
1237 :     if ((($fid, $anno_time, $who, $anno_text) =
1238 :     ($ann =~ /^(fig\|\d+\.\d+\.peg\.\d+)\n(\d+)\n(\S+)\n(.*\S)/s)) and
1239 : olson 1.20 $anno_time > $last_update and
1240 :     $anno_time < $update_thru)
1241 : olson 1.1
1242 :     {
1243 :     #
1244 : olson 1.15 # Update users list.
1245 :     #
1246 :    
1247 :     $users{$who}++;
1248 :    
1249 :     #
1250 : olson 1.1 # Look up aliases if we haven't seen this fid before.
1251 :     #
1252 :    
1253 :     if (!defined($pegs{$fid}))
1254 :     {
1255 :     my @aliases = $fig->feature_aliases($fid);
1256 :    
1257 :     print $peg_fh join("\t", $fid, $genome, @aliases), "\n";
1258 :     $num_pegs++;
1259 :     }
1260 :    
1261 :     print $anno_fh "$ann//\n";
1262 :    
1263 :     $pegs{$fid}++;
1264 :    
1265 :     $num_annos_for_genome++;
1266 :     $num_annos++;
1267 : olson 1.15
1268 :     #
1269 :     # While we're here, see if this is an assignment. We check in the
1270 :     # %assignment hash, which is keyed on fid, to see if we already
1271 :     # saw an assignment for this fid. If we have, we keep this one only if
1272 :     # the assignment time on it is later than the one we saw already.
1273 :     #
1274 :     # We are only looking at master assignments for now. We will need
1275 :     # to return to this issue and reexamine it, but in order to move
1276 :     # forward I am only matching master assignments.
1277 :     #
1278 :    
1279 :     if ($anno_text =~ /Set master function to\n(\S[^\n]+\S)/)
1280 :     {
1281 :     my $func = $1;
1282 :    
1283 :     my $other = $assignment{$fid};
1284 :    
1285 :     #
1286 :     # If we haven't seen an assignment for this fid,
1287 :     # or if it the other assignment has a timestamp that
1288 :     # is earlier than this one, set the assignment.
1289 :     #
1290 :    
1291 :     if (!defined($other) or
1292 :     ($other->[1] < $anno_time))
1293 :     {
1294 :     $assignment{$fid} = [$fid, $anno_time, $who, $func];
1295 :     }
1296 :     }
1297 : olson 1.1 }
1298 :     }
1299 :     close($afh);
1300 : olson 1.15
1301 :     #
1302 :     # Write out the assignments that remain.
1303 :     #
1304 :    
1305 :     for my $fid (sort keys(%assignment))
1306 :     {
1307 :     print $assign_fh join("\t", @{$assignment{$fid}}), "\n";
1308 :     $num_assignments++;
1309 :     }
1310 : olson 1.1 }
1311 : olson 1.15
1312 : olson 1.1
1313 :     #
1314 :     # Determine genome information if we have annotations for this one.
1315 :     #
1316 :    
1317 :     if ($num_annos_for_genome > 0)
1318 :     {
1319 :     $num_genomes++;
1320 :     if (open(my $cfh, "<$genome_dir/COUNTS"))
1321 :     {
1322 :     if ($_ = <$cfh>)
1323 :     {
1324 :     chomp;
1325 :     my($cgenome, $n_contigs, $total_nucs, $cksum) = split(/\t/, $_);
1326 :     if ($cgenome ne $genome)
1327 :     {
1328 :     warn "Hm, $genome has a COUNTS file with genome=$cgenome that does not match\n";
1329 :     }
1330 :     else
1331 :     {
1332 :     print $genome_fh join("\t",
1333 :     $genome, $num_annos_for_genome, $n_contigs,
1334 :     $total_nucs, $cksum), "\n";
1335 :     }
1336 :     }
1337 :     }
1338 :     }
1339 :    
1340 :     }
1341 :     close($anno_fh);
1342 :     close($peg_fh);
1343 :     close($genome_fh);
1344 : olson 1.15 close($assign_fh);
1345 : olson 1.1
1346 :     print "Pegs: $num_pegs\n";
1347 :     print "Genomes: $num_genomes\n";
1348 :     print "Annos: $num_annos\n";
1349 :    
1350 :     #
1351 :     # Check compatibility.
1352 :     #
1353 :    
1354 : olson 1.18 my $my_release = [$fig->get_release_info()];
1355 :    
1356 :     #
1357 :     # Release id is $my_release->[1].
1358 :     #
1359 :    
1360 :     my $compatible;
1361 :     if ($my_release->[1] ne "" and $his_release->[1] ne "")
1362 :     {
1363 :     #
1364 :     # Both releases must be defined for them to be compatible.
1365 :     #
1366 :     # At some point we need to consider the derived-release issue.
1367 :     #
1368 :    
1369 :     $compatible = $my_release->[1] eq $his_release->[1];
1370 :     }
1371 :     else
1372 :     {
1373 :     $compatible = 0;
1374 :     }
1375 : olson 1.1
1376 :     open(my $fh, ">$spool_dir/INFO");
1377 :     print $fh "requestor_release\t$his_release\n";
1378 :     print $fh "last_update\t$last_update\n";
1379 : olson 1.20 print $fh "update_thru\t$update_thru\n";
1380 : olson 1.1 print $fh "cur_update\t$now\n";
1381 :     print $fh "target_release\t$my_release\n";
1382 :     print $fh "compatible\t$compatible\n";
1383 :     print $fh "num_pegs\t$num_pegs\n";
1384 :     print $fh "num_genomes\t$num_genomes\n";
1385 :     print $fh "num_annos\t$num_annos\n";
1386 : olson 1.15 print $fh "num_assignments\t$num_assignments\n";
1387 : olson 1.1 close($fh);
1388 :    
1389 : olson 1.15 #
1390 :     # Construct list of users, and pdate local user database.
1391 :     #
1392 :    
1393 :     my @users = keys(%users);
1394 : olson 1.17 # $fig->ensure_users(\@users);
1395 : olson 1.15
1396 :     return [$session_id, $my_release, $num_assignments, $num_annos, $num_pegs, $num_genomes,
1397 : olson 1.16 $now, $compatible, \@users];
1398 : olson 1.1 }
1399 :    
1400 :    
1401 :     sub get_pegs
1402 :     {
1403 :     my($self, $session_id, $start, $len) = @_;
1404 :     my(%session_info);
1405 :    
1406 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1407 :    
1408 :     -d $spool_dir or die "Invalid session id $session_id";
1409 :    
1410 :     #
1411 :     # Read in the cached information for this session.
1412 :     #
1413 :    
1414 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1415 :     while (<$info_fh>)
1416 :     {
1417 :     chomp;
1418 :     my($var, $val) = split(/\t/, $_, 2);
1419 :     $session_info{$var} = $val;
1420 :     }
1421 :     close($info_fh);
1422 :    
1423 :     #
1424 :     # Sanity check start and length.
1425 :     #
1426 :    
1427 :     if ($start < 0 or $start >= $session_info{num_pegs})
1428 :     {
1429 :     die "Invalid start position $start";
1430 :     }
1431 :    
1432 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_pegs})
1433 :     {
1434 :     die "Invalid length $len";
1435 :     }
1436 :    
1437 :     #
1438 :     # Open file, spin to the starting line, then start reading.
1439 :     #
1440 :    
1441 :     open(my $peg_fh, "<$spool_dir/pegs") or die "Cannot open pegs file: $!";
1442 :    
1443 :     my $peg_output = [];
1444 :     my $genome_output = [];
1445 :    
1446 :     my $peg_num = 0;
1447 :     my $genomes_to_show = [];
1448 :     my %genomes_to_show;
1449 :    
1450 :     my($fid, $genome, @aliases);
1451 :    
1452 :     while (<$peg_fh>)
1453 :     {
1454 :     next if ($peg_num < $start);
1455 :    
1456 :     last if ($peg_num > ($start + $len));
1457 :    
1458 :     chomp;
1459 :    
1460 :     #
1461 :     # OK, this is a peg to process.
1462 :     # It's easy if we're compatible.
1463 :     #
1464 :    
1465 :     ($fid, $genome, @aliases) = split(/\t/, $_);
1466 :    
1467 :     if ($session_info{compatible})
1468 :     {
1469 :     push(@$peg_output, ['peg', $fid]);
1470 :     }
1471 :     else
1472 :     {
1473 :     if (!$genomes_to_show{$genome})
1474 :     {
1475 :     push(@$genomes_to_show, $genome);
1476 :     $genomes_to_show{$genome}++;
1477 :     }
1478 :     push(@$peg_output, ['peg_info', $fid, [@aliases], $genome]);
1479 :     }
1480 :     }
1481 :     continue
1482 :     {
1483 :     $peg_num++;
1484 :     }
1485 :    
1486 :     #
1487 :     # Read the genomes file, returning information about genomes referenced
1488 :     # in the pegs returned.
1489 :     #
1490 :    
1491 :     my $n_left = @$genomes_to_show;
1492 :    
1493 :     open(my $gfh, "<$spool_dir/genomes") or die "Cannot open genomes file: $!";
1494 :     while ($n_left > 0 and $_ = <$gfh>)
1495 :     {
1496 :     chomp;
1497 :    
1498 :     my($genome, $n_annos, $n_contigs, $n_nucs, $cksum) = split(/\t/);
1499 :    
1500 :     if ($genomes_to_show{$genome})
1501 :     {
1502 :     push(@$genome_output, [$genome, $n_contigs, $n_nucs, $cksum]);
1503 :     $n_left--;
1504 :     }
1505 :     }
1506 :     close($gfh);
1507 :    
1508 :     return [$peg_output, $genome_output];
1509 :     }
1510 : olson 1.6
1511 :     sub finalize_pegs
1512 :     {
1513 :     my($self, $session, $request) = @_;
1514 :     my($out);
1515 :    
1516 :     my $fig = new FIG;
1517 :    
1518 :     #
1519 :     # Walk the request handling appropriately. This is fairly easy, as it
1520 :     # is just a matter of pulling either sequence or location/contig data.
1521 :     #
1522 :    
1523 :     for my $item (@$request)
1524 :     {
1525 :     my($what, $peg) = @$item;
1526 :    
1527 :     if ($what eq "peg_genome")
1528 :     {
1529 :     #
1530 :     # Return the location and contig checksum for this peg.
1531 :     #
1532 : olson 1.13 # We also include the sequence in case the contig mapping doesn't work.
1533 :     #
1534 : olson 1.6
1535 :     my $loc = $fig->feature_location($peg);
1536 :     my $contig = $fig->contig_of($loc);
1537 : olson 1.7 my $cksum = $fig->contig_checksum($fig->genome_of($peg), $contig);
1538 : olson 1.13 my $seq = $fig->get_translation($peg);
1539 : olson 1.6
1540 :     push(@$out, ['peg_loc', $peg,
1541 : olson 1.13 $fig->strand_of($peg),
1542 : olson 1.6 $fig->beg_of($loc), $fig->end_of($loc),
1543 : olson 1.13 $cksum, $seq]);
1544 : olson 1.6
1545 :     }
1546 : olson 1.7 elsif ($what eq "peg_unknown")
1547 : olson 1.6 {
1548 :     my $seq = $fig->get_translation($peg);
1549 :     push(@$out, ['peg_seq', $peg, $seq]);
1550 :     }
1551 :     }
1552 :     return $out;
1553 :     }
1554 :    
1555 : olson 1.15
1556 :     sub get_annotations
1557 :     {
1558 :     my($self, $session_id, $start, $len) = @_;
1559 :    
1560 :     #
1561 :     # This is now easy; just run thru the saved annotations and return.
1562 :     #
1563 :    
1564 :     my(%session_info);
1565 :    
1566 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1567 :    
1568 :     -d $spool_dir or die "Invalid session id $session_id";
1569 :    
1570 :     #
1571 :     # Read in the cached information for this session.
1572 :     #
1573 :    
1574 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1575 :     while (<$info_fh>)
1576 :     {
1577 :     chomp;
1578 :     my($var, $val) = split(/\t/, $_, 2);
1579 :     $session_info{$var} = $val;
1580 :     }
1581 :     close($info_fh);
1582 :    
1583 :     #
1584 :     # Sanity check start and length.
1585 :     #
1586 :    
1587 :     if ($start < 0 or $start >= $session_info{num_annos})
1588 :     {
1589 :     die "Invalid start position $start";
1590 :     }
1591 :    
1592 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_annos})
1593 :     {
1594 :     die "Invalid length $len";
1595 :     }
1596 :    
1597 :     #
1598 :     # Open file, spin to the starting line, then start reading.
1599 :     #
1600 :    
1601 :     open(my $anno_fh, "<$spool_dir/annos") or die "Cannot open annos file: $!";
1602 :    
1603 :     my $anno_output = [];
1604 :    
1605 :     my $anno_num = 0;
1606 :    
1607 :     local $/ = "//\n";
1608 :     while (<$anno_fh>)
1609 :     {
1610 :     next if ($anno_num < $start);
1611 :    
1612 :     last if ($anno_num > ($start + $len));
1613 :    
1614 :     chomp;
1615 :    
1616 :     my($id, $date, $author, $anno) = split(/\n/, $_, 4);
1617 :    
1618 :     push(@$anno_output, [$id, $date, $author, $anno]);
1619 :     }
1620 :     continue
1621 :     {
1622 :     $anno_num++;
1623 :     }
1624 :    
1625 :     return $anno_output;
1626 :     }
1627 : olson 1.19
1628 :     sub get_assignments
1629 :     {
1630 :     my($self, $session_id, $start, $len) = @_;
1631 :    
1632 :     #
1633 :     # This is now easy; just run thru the saved assignments and return.
1634 :     #
1635 :    
1636 :     my(%session_info);
1637 :    
1638 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1639 :    
1640 :     -d $spool_dir or die "Invalid session id $session_id";
1641 :    
1642 :     #
1643 :     # Read in the cached information for this session.
1644 :     #
1645 :    
1646 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1647 :     while (<$info_fh>)
1648 :     {
1649 :     chomp;
1650 :     my($var, $val) = split(/\t/, $_, 2);
1651 :     $session_info{$var} = $val;
1652 :     }
1653 :     close($info_fh);
1654 :    
1655 :     #
1656 :     # Sanity check start and length.
1657 :     #
1658 :    
1659 :     if ($start < 0 or $start >= $session_info{num_assignments})
1660 :     {
1661 :     die "Invalid start position $start";
1662 :     }
1663 :    
1664 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_assignments})
1665 :     {
1666 :     die "Invalid length $len";
1667 :     }
1668 :    
1669 :     #
1670 :     # Open file, spin to the starting line, then start reading.
1671 :     #
1672 :    
1673 :     open(my $assign_fh, "<$spool_dir/assignments") or die "Cannot open assignments file: $!";
1674 :    
1675 :     my $assign_output = [];
1676 :    
1677 :     my $assign_num = 0;
1678 :    
1679 :     while (<$assign_fh>)
1680 :     {
1681 :     next if ($assign_num < $start);
1682 :    
1683 :     last if ($assign_num > ($start + $len));
1684 :    
1685 :     chomp;
1686 :    
1687 :     my($id, $date, $author, $func) = split(/\t/, $_, 4);
1688 :    
1689 :     push(@$assign_output, [$id, $date, $author, $func]);
1690 :     }
1691 :     continue
1692 :     {
1693 :     $assign_num++;
1694 :     }
1695 :    
1696 :     return $assign_output;
1697 :     }
1698 :    
1699 : olson 1.22 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3