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

Annotation of /FigKernelPackages/P2P.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3