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

Annotation of /FigKernelPackages/P2P.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (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.19 use DB_File;
22 :     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 :     # Open up the peg translation cache database (a DB_File) tied
119 :     # 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 :     $cache_handle = tie(%peg_cache, "DB_File", "$cache_dir/$cache_file",
133 :     O_CREAT | O_RDWR, 0666, $DB_HASH);
134 :     $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.19 $cache_handle->sync();
152 :     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 :     # $cache_handle DB_File handle corresponding to $peg_cache.
446 :     #
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 :     $cache_handle->sync();
669 :    
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 :     my $proxy = SOAP::Lite->uri($P2P::ns_relay)->proxy($url);
818 :    
819 :     my $self = {
820 :     url => $url,
821 :     proxy => $proxy,
822 :     };
823 :     return bless($self, $class);
824 :     }
825 :    
826 :     sub enumerate_annotation_systems
827 :     {
828 :     my($self) = @_;
829 :    
830 :     return $self->{proxy}->enumerate_annotation_systems()->result;
831 :     }
832 :    
833 :     sub fetch_queries
834 :     {
835 :     my($self, $id) = @_;
836 :    
837 :     my $reply = $self->{proxy}->fetch_queries($id);
838 :    
839 :     if ($reply->fault)
840 :     {
841 :     print "Failed to fetch queries: ", $reply->faultcode, " ", $reply->faultstring, "\n";
842 :     return undef;
843 :     }
844 :    
845 :     return $reply->result;
846 :     }
847 :    
848 :     sub deposit_answer
849 :     {
850 :     my($self, $id, $key, $answer) = @_;
851 :    
852 :     my $reply = $self->{proxy}->deposit_answer($id, $key,
853 :     SOAP::Data->type('base64')->value($answer));
854 :    
855 :     if ($reply->fault)
856 :     {
857 :     print "deposit_answer got fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
858 :     return undef;
859 :     }
860 :    
861 :     return $reply;
862 :     }
863 :    
864 :     =pod
865 :    
866 :     =head1 await_result
867 :    
868 :     Await the result from a possibly-asynchronous soap request.
869 :    
870 :     Look at the reply that we have. If it's a deferred reply, loop polling
871 :     the relay for the actual result.
872 :    
873 :     We determine if the reply is a deferred reply by examining the namespace
874 :     URI of the response. A response will be generated from the relay's namespace,
875 :     rather than that of the application itself.
876 :    
877 :     =cut
878 :    
879 :     sub await_result
880 :     {
881 :     my($self, $reply) = @_;
882 :    
883 :     while (1)
884 :     {
885 :     #
886 :     # Retrieve the namespace of the response, which is the first
887 :     # element in the body of the message.
888 :     #
889 :     my $ns = $reply->namespaceuriof('/Envelope/Body/[1]');
890 : olson 1.20 # print "Reply ns=$ns want $P2P::ns_relay\n";
891 : olson 1.1
892 :     if ($ns eq $P2P::ns_relay)
893 :     {
894 :     my $val = $reply->result;
895 : olson 1.20 # print "got val=", Dumper($val);
896 : olson 1.1 if ($val->[0] eq 'deferred')
897 :     {
898 :     #
899 :     # Sleep a little, then try to retrieve the response.
900 :     #
901 :    
902 :     sleep(1);
903 :     my $id = $val->[1];
904 :    
905 :     print "Retrieving reply\n";
906 :     $reply = $self->{proxy}->call_completed($id);
907 :     }
908 :     else
909 :     {
910 :     #
911 :     # We're not sure what to do here..
912 :     #
913 :     return undef;
914 :     }
915 :     }
916 :     else
917 :     {
918 :     #
919 :     # We got an actual response. Return it.
920 :     #
921 :    
922 :     return $reply;
923 :     }
924 :     }
925 :     }
926 :    
927 :     #############
928 :     #
929 :     # P2P Requestor
930 :     #
931 :     #############
932 :    
933 :     package P2P::Requestor;
934 :     use strict;
935 :    
936 :     use Data::Dumper;
937 : olson 1.15 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
938 : olson 1.1
939 :     use SOAP::Lite;
940 : olson 1.15
941 :     #use SOAP::Lite +trace => [qw(transport dispatch result debug)];
942 : olson 1.1 use P2P;
943 :    
944 :     #
945 :     # Create a new Requestor. It contains a reference to the FIG instance
946 :     # so that we can run the protocol completely from in here.
947 :     #
948 :    
949 :     sub new
950 :     {
951 :     my($class, $fig, $url, $peer_id, $relay) = @_;
952 :    
953 : olson 1.17 my $proxy = SOAP::Lite->uri($ns_p2p)->proxy($url, timeout => 3600);
954 : olson 1.1
955 :     my $self = {
956 :     fig => $fig,
957 :     url => $url,
958 :     peer_id => $peer_id,
959 :     proxy => $proxy,
960 :     relay => $relay,
961 :     };
962 :     return bless($self, $class);
963 :     }
964 :    
965 :     #
966 :     # First step: Request an update.
967 :     #
968 :     # We need to determine some notion of what our release is, since we are not
969 :     # currently tagging them explicitly. Until we delve into this more,
970 :     # I am going to return a null release, which means the same-release
971 :     # optimization won't be able to kick in.
972 :     #
973 :     # We also need to determine the last time we got an update from this
974 :     # system.
975 :     #
976 :    
977 :     sub request_update
978 :     {
979 : olson 1.20 my($self, $last_update, $update_thru) = @_;
980 : olson 1.1
981 : olson 1.18 my $rel = [$self->{fig}->get_release_info()];
982 : olson 1.1
983 :     if (!defined($last_update))
984 :     {
985 :     $last_update = $self->{fig}->get_peer_last_update($self->{peer_id});
986 :     }
987 : olson 1.17
988 :     print "Requesting update via $self->{proxy}\n";
989 : olson 1.20 my $reply = $self->{proxy}->request_update($rel, $last_update, $update_thru);
990 :     # print "Got reply ", Dumper($reply);
991 : olson 1.1
992 :     if ($self->{relay})
993 :     {
994 :     $reply = $self->{relay}->await_result($reply);
995 :     }
996 :    
997 :     if ($reply->fault)
998 :     {
999 :     print "request_update triggered fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
1000 :     return undef;
1001 :     }
1002 :    
1003 :     return $reply->result;
1004 :     }
1005 :    
1006 :     =pod
1007 :    
1008 :     =head1 get_pegs($session_id, $start, $length)
1009 :    
1010 :    
1011 :     =cut
1012 :    
1013 :     sub get_pegs
1014 :     {
1015 :     my($self, $session_id, $start, $length) = @_;
1016 :    
1017 :     return $self->call("get_pegs", $session_id, $start, $length);
1018 :     }
1019 :    
1020 : olson 1.6 sub finalize_pegs
1021 :     {
1022 :     my($self, $session_id, $request) = @_;
1023 :    
1024 :     return $self->call("finalize_pegs", $session_id, $request);
1025 :     }
1026 :    
1027 : olson 1.15 sub get_annotations
1028 :     {
1029 :     my($self, $session_id, $start, $length) = @_;
1030 :    
1031 :     return $self->call("get_annotations", $session_id, $start, $length);
1032 :     }
1033 :    
1034 : olson 1.19 sub get_assignments
1035 :     {
1036 :     my($self, $session_id, $start, $length) = @_;
1037 :    
1038 :     return $self->call("get_assignments", $session_id, $start, $length);
1039 :     }
1040 :    
1041 : olson 1.1 sub call
1042 :     {
1043 :     my($self, $func, @args) = @_;
1044 : olson 1.15
1045 :     my $t0 = [gettimeofday()];
1046 :     print "Calling $func\n";
1047 : olson 1.1 my $reply = $self->{proxy}->$func(@args);
1048 : olson 1.15 my $t1 = [gettimeofday()];
1049 :    
1050 :     my $elap = tv_interval($t0, $t1);
1051 :     print "Call to $func took $elap\n";
1052 : olson 1.1
1053 :     if ($self->{relay})
1054 :     {
1055 :     $reply = $self->{relay}->await_result($reply);
1056 :     }
1057 :    
1058 :     if ($reply->fault)
1059 :     {
1060 :     print "$func triggered fault: ", $reply->faultcode, " ", $reply->faultstring, "\n";
1061 :     return undef;
1062 :     }
1063 :    
1064 :     return $reply->result;
1065 :     }
1066 :    
1067 :    
1068 :     #############
1069 :     #
1070 :     # P2P Service
1071 :     #
1072 :     # Code in this module is invoked on the target on behalf of a requestor.
1073 :     #
1074 :     #############
1075 :    
1076 :     package P2P::Service;
1077 :    
1078 :     use Data::Dumper;
1079 :    
1080 :     use FIG;
1081 :     use FIG_Config;
1082 :     use strict;
1083 :    
1084 :     use File::Temp qw(tempdir);
1085 :     use File::Basename;
1086 :    
1087 :     sub request_update
1088 :     {
1089 : olson 1.20 my($class, $his_release, $last_update, $update_thru)= @_;
1090 : olson 1.1
1091 :     #
1092 :     # Verify input.
1093 :     #
1094 :    
1095 :     if ($last_update !~ /^\d+$/)
1096 :     {
1097 :     die "request_update: last_update must be a number (not '$last_update')\n";
1098 :     }
1099 :    
1100 : olson 1.20 if ($update_thru eq "")
1101 :     {
1102 :     $update_thru = time + 10000;
1103 :     }
1104 :    
1105 : olson 1.1 #
1106 :     # Create a new session id and a spool directory to use for storage
1107 :     # of information about it. This can go in the tempdir since it is
1108 :     # not persistent.
1109 :     #
1110 :    
1111 :     &FIG::verify_dir("$FIG_Config::temp/p2p_spool");
1112 : olson 1.22 my $spool_dir = tempdir(DIR => "$FIG_Config::temp/p2p_spool");
1113 : olson 1.1
1114 : olson 1.22 #my $spool_dir = "$FIG_Config::temp/p2p_spool/test";
1115 : olson 1.1 &FIG::verify_dir($spool_dir);
1116 :    
1117 :     my $session_id = basename($spool_dir);
1118 :     my $now = time;
1119 :    
1120 :     #
1121 :     # Gather the list of pegs and annotations for the update.
1122 :     #
1123 :    
1124 :     my $fig = new FIG;
1125 :    
1126 :     my $all_genomes = [$fig->genomes];
1127 :    
1128 :     my %all_genomes = map { $_ => 1 } @$all_genomes;
1129 :    
1130 :     my %pegs;
1131 : olson 1.15
1132 :     #
1133 :     # We keep track of usernames that have been seen, so that
1134 :     # we can both update our local user database and
1135 :     # we can report them to our peer.
1136 :     #
1137 :    
1138 :     my %users;
1139 : olson 1.1
1140 :     my $num_annos = 0;
1141 :     my $num_genomes = 0;
1142 :     my $num_pegs = 0;
1143 : olson 1.15 my $num_assignments = 0;
1144 : olson 1.1
1145 :     my $anno_fh;
1146 :     open($anno_fh, ">$spool_dir/annos");
1147 :    
1148 :     my $peg_fh;
1149 :     open($peg_fh, ">$spool_dir/pegs");
1150 :    
1151 :     my $genome_fh;
1152 :     open($genome_fh, ">$spool_dir/genomes");
1153 :    
1154 : olson 1.15 my $assign_fh;
1155 :     open($assign_fh, ">$spool_dir/assignments");
1156 :    
1157 : olson 1.1 for my $genome (@$all_genomes)
1158 :     {
1159 :     my $num_annos_for_genome = 0;
1160 : olson 1.15 my %assignment;
1161 : olson 1.1
1162 :     my $genome_dir = "$FIG_Config::organisms/$genome";
1163 :     next unless -d $genome_dir;
1164 :    
1165 :     my $afh;
1166 :     if (open($afh, "$genome_dir/annotations"))
1167 :     {
1168 :     my($fid, $anno_time, $who, $anno_text);
1169 :     local($/);
1170 :     $/ = "//\n";
1171 :     while (my $ann = <$afh>)
1172 :     {
1173 :     chomp $ann;
1174 :    
1175 :     if ((($fid, $anno_time, $who, $anno_text) =
1176 :     ($ann =~ /^(fig\|\d+\.\d+\.peg\.\d+)\n(\d+)\n(\S+)\n(.*\S)/s)) and
1177 : olson 1.20 $anno_time > $last_update and
1178 :     $anno_time < $update_thru)
1179 : olson 1.1
1180 :     {
1181 :     #
1182 : olson 1.15 # Update users list.
1183 :     #
1184 :    
1185 :     $users{$who}++;
1186 :    
1187 :     #
1188 : olson 1.1 # Look up aliases if we haven't seen this fid before.
1189 :     #
1190 :    
1191 :     if (!defined($pegs{$fid}))
1192 :     {
1193 :     my @aliases = $fig->feature_aliases($fid);
1194 :    
1195 :     print $peg_fh join("\t", $fid, $genome, @aliases), "\n";
1196 :     $num_pegs++;
1197 :     }
1198 :    
1199 :     print $anno_fh "$ann//\n";
1200 :    
1201 :     $pegs{$fid}++;
1202 :    
1203 :     $num_annos_for_genome++;
1204 :     $num_annos++;
1205 : olson 1.15
1206 :     #
1207 :     # While we're here, see if this is an assignment. We check in the
1208 :     # %assignment hash, which is keyed on fid, to see if we already
1209 :     # saw an assignment for this fid. If we have, we keep this one only if
1210 :     # the assignment time on it is later than the one we saw already.
1211 :     #
1212 :     # We are only looking at master assignments for now. We will need
1213 :     # to return to this issue and reexamine it, but in order to move
1214 :     # forward I am only matching master assignments.
1215 :     #
1216 :    
1217 :     if ($anno_text =~ /Set master function to\n(\S[^\n]+\S)/)
1218 :     {
1219 :     my $func = $1;
1220 :    
1221 :     my $other = $assignment{$fid};
1222 :    
1223 :     #
1224 :     # If we haven't seen an assignment for this fid,
1225 :     # or if it the other assignment has a timestamp that
1226 :     # is earlier than this one, set the assignment.
1227 :     #
1228 :    
1229 :     if (!defined($other) or
1230 :     ($other->[1] < $anno_time))
1231 :     {
1232 :     $assignment{$fid} = [$fid, $anno_time, $who, $func];
1233 :     }
1234 :     }
1235 : olson 1.1 }
1236 :     }
1237 :     close($afh);
1238 : olson 1.15
1239 :     #
1240 :     # Write out the assignments that remain.
1241 :     #
1242 :    
1243 :     for my $fid (sort keys(%assignment))
1244 :     {
1245 :     print $assign_fh join("\t", @{$assignment{$fid}}), "\n";
1246 :     $num_assignments++;
1247 :     }
1248 : olson 1.1 }
1249 : olson 1.15
1250 : olson 1.1
1251 :     #
1252 :     # Determine genome information if we have annotations for this one.
1253 :     #
1254 :    
1255 :     if ($num_annos_for_genome > 0)
1256 :     {
1257 :     $num_genomes++;
1258 :     if (open(my $cfh, "<$genome_dir/COUNTS"))
1259 :     {
1260 :     if ($_ = <$cfh>)
1261 :     {
1262 :     chomp;
1263 :     my($cgenome, $n_contigs, $total_nucs, $cksum) = split(/\t/, $_);
1264 :     if ($cgenome ne $genome)
1265 :     {
1266 :     warn "Hm, $genome has a COUNTS file with genome=$cgenome that does not match\n";
1267 :     }
1268 :     else
1269 :     {
1270 :     print $genome_fh join("\t",
1271 :     $genome, $num_annos_for_genome, $n_contigs,
1272 :     $total_nucs, $cksum), "\n";
1273 :     }
1274 :     }
1275 :     }
1276 :     }
1277 :    
1278 :     }
1279 :     close($anno_fh);
1280 :     close($peg_fh);
1281 :     close($genome_fh);
1282 : olson 1.15 close($assign_fh);
1283 : olson 1.1
1284 :     print "Pegs: $num_pegs\n";
1285 :     print "Genomes: $num_genomes\n";
1286 :     print "Annos: $num_annos\n";
1287 :    
1288 :     #
1289 :     # Check compatibility.
1290 :     #
1291 :    
1292 : olson 1.18 my $my_release = [$fig->get_release_info()];
1293 :    
1294 :     #
1295 :     # Release id is $my_release->[1].
1296 :     #
1297 :    
1298 :     my $compatible;
1299 :     if ($my_release->[1] ne "" and $his_release->[1] ne "")
1300 :     {
1301 :     #
1302 :     # Both releases must be defined for them to be compatible.
1303 :     #
1304 :     # At some point we need to consider the derived-release issue.
1305 :     #
1306 :    
1307 :     $compatible = $my_release->[1] eq $his_release->[1];
1308 :     }
1309 :     else
1310 :     {
1311 :     $compatible = 0;
1312 :     }
1313 : olson 1.1
1314 :     open(my $fh, ">$spool_dir/INFO");
1315 :     print $fh "requestor_release\t$his_release\n";
1316 :     print $fh "last_update\t$last_update\n";
1317 : olson 1.20 print $fh "update_thru\t$update_thru\n";
1318 : olson 1.1 print $fh "cur_update\t$now\n";
1319 :     print $fh "target_release\t$my_release\n";
1320 :     print $fh "compatible\t$compatible\n";
1321 :     print $fh "num_pegs\t$num_pegs\n";
1322 :     print $fh "num_genomes\t$num_genomes\n";
1323 :     print $fh "num_annos\t$num_annos\n";
1324 : olson 1.15 print $fh "num_assignments\t$num_assignments\n";
1325 : olson 1.1 close($fh);
1326 :    
1327 : olson 1.15 #
1328 :     # Construct list of users, and pdate local user database.
1329 :     #
1330 :    
1331 :     my @users = keys(%users);
1332 : olson 1.17 # $fig->ensure_users(\@users);
1333 : olson 1.15
1334 :     return [$session_id, $my_release, $num_assignments, $num_annos, $num_pegs, $num_genomes,
1335 : olson 1.16 $now, $compatible, \@users];
1336 : olson 1.1 }
1337 :    
1338 :    
1339 :     sub get_pegs
1340 :     {
1341 :     my($self, $session_id, $start, $len) = @_;
1342 :     my(%session_info);
1343 :    
1344 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1345 :    
1346 :     -d $spool_dir or die "Invalid session id $session_id";
1347 :    
1348 :     #
1349 :     # Read in the cached information for this session.
1350 :     #
1351 :    
1352 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1353 :     while (<$info_fh>)
1354 :     {
1355 :     chomp;
1356 :     my($var, $val) = split(/\t/, $_, 2);
1357 :     $session_info{$var} = $val;
1358 :     }
1359 :     close($info_fh);
1360 :    
1361 :     #
1362 :     # Sanity check start and length.
1363 :     #
1364 :    
1365 :     if ($start < 0 or $start >= $session_info{num_pegs})
1366 :     {
1367 :     die "Invalid start position $start";
1368 :     }
1369 :    
1370 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_pegs})
1371 :     {
1372 :     die "Invalid length $len";
1373 :     }
1374 :    
1375 :     #
1376 :     # Open file, spin to the starting line, then start reading.
1377 :     #
1378 :    
1379 :     open(my $peg_fh, "<$spool_dir/pegs") or die "Cannot open pegs file: $!";
1380 :    
1381 :     my $peg_output = [];
1382 :     my $genome_output = [];
1383 :    
1384 :     my $peg_num = 0;
1385 :     my $genomes_to_show = [];
1386 :     my %genomes_to_show;
1387 :    
1388 :     my($fid, $genome, @aliases);
1389 :    
1390 :     while (<$peg_fh>)
1391 :     {
1392 :     next if ($peg_num < $start);
1393 :    
1394 :     last if ($peg_num > ($start + $len));
1395 :    
1396 :     chomp;
1397 :    
1398 :     #
1399 :     # OK, this is a peg to process.
1400 :     # It's easy if we're compatible.
1401 :     #
1402 :    
1403 :     ($fid, $genome, @aliases) = split(/\t/, $_);
1404 :    
1405 :     if ($session_info{compatible})
1406 :     {
1407 :     push(@$peg_output, ['peg', $fid]);
1408 :     }
1409 :     else
1410 :     {
1411 :     if (!$genomes_to_show{$genome})
1412 :     {
1413 :     push(@$genomes_to_show, $genome);
1414 :     $genomes_to_show{$genome}++;
1415 :     }
1416 :     push(@$peg_output, ['peg_info', $fid, [@aliases], $genome]);
1417 :     }
1418 :     }
1419 :     continue
1420 :     {
1421 :     $peg_num++;
1422 :     }
1423 :    
1424 :     #
1425 :     # Read the genomes file, returning information about genomes referenced
1426 :     # in the pegs returned.
1427 :     #
1428 :    
1429 :     my $n_left = @$genomes_to_show;
1430 :    
1431 :     open(my $gfh, "<$spool_dir/genomes") or die "Cannot open genomes file: $!";
1432 :     while ($n_left > 0 and $_ = <$gfh>)
1433 :     {
1434 :     chomp;
1435 :    
1436 :     my($genome, $n_annos, $n_contigs, $n_nucs, $cksum) = split(/\t/);
1437 :    
1438 :     if ($genomes_to_show{$genome})
1439 :     {
1440 :     push(@$genome_output, [$genome, $n_contigs, $n_nucs, $cksum]);
1441 :     $n_left--;
1442 :     }
1443 :     }
1444 :     close($gfh);
1445 :    
1446 :     return [$peg_output, $genome_output];
1447 :     }
1448 : olson 1.6
1449 :     sub finalize_pegs
1450 :     {
1451 :     my($self, $session, $request) = @_;
1452 :     my($out);
1453 :    
1454 :     my $fig = new FIG;
1455 :    
1456 :     #
1457 :     # Walk the request handling appropriately. This is fairly easy, as it
1458 :     # is just a matter of pulling either sequence or location/contig data.
1459 :     #
1460 :    
1461 :     for my $item (@$request)
1462 :     {
1463 :     my($what, $peg) = @$item;
1464 :    
1465 :     if ($what eq "peg_genome")
1466 :     {
1467 :     #
1468 :     # Return the location and contig checksum for this peg.
1469 :     #
1470 : olson 1.13 # We also include the sequence in case the contig mapping doesn't work.
1471 :     #
1472 : olson 1.6
1473 :     my $loc = $fig->feature_location($peg);
1474 :     my $contig = $fig->contig_of($loc);
1475 : olson 1.7 my $cksum = $fig->contig_checksum($fig->genome_of($peg), $contig);
1476 : olson 1.13 my $seq = $fig->get_translation($peg);
1477 : olson 1.6
1478 :     push(@$out, ['peg_loc', $peg,
1479 : olson 1.13 $fig->strand_of($peg),
1480 : olson 1.6 $fig->beg_of($loc), $fig->end_of($loc),
1481 : olson 1.13 $cksum, $seq]);
1482 : olson 1.6
1483 :     }
1484 : olson 1.7 elsif ($what eq "peg_unknown")
1485 : olson 1.6 {
1486 :     my $seq = $fig->get_translation($peg);
1487 :     push(@$out, ['peg_seq', $peg, $seq]);
1488 :     }
1489 :     }
1490 :     return $out;
1491 :     }
1492 :    
1493 : olson 1.15
1494 :     sub get_annotations
1495 :     {
1496 :     my($self, $session_id, $start, $len) = @_;
1497 :    
1498 :     #
1499 :     # This is now easy; just run thru the saved annotations and return.
1500 :     #
1501 :    
1502 :     my(%session_info);
1503 :    
1504 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1505 :    
1506 :     -d $spool_dir or die "Invalid session id $session_id";
1507 :    
1508 :     #
1509 :     # Read in the cached information for this session.
1510 :     #
1511 :    
1512 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1513 :     while (<$info_fh>)
1514 :     {
1515 :     chomp;
1516 :     my($var, $val) = split(/\t/, $_, 2);
1517 :     $session_info{$var} = $val;
1518 :     }
1519 :     close($info_fh);
1520 :    
1521 :     #
1522 :     # Sanity check start and length.
1523 :     #
1524 :    
1525 :     if ($start < 0 or $start >= $session_info{num_annos})
1526 :     {
1527 :     die "Invalid start position $start";
1528 :     }
1529 :    
1530 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_annos})
1531 :     {
1532 :     die "Invalid length $len";
1533 :     }
1534 :    
1535 :     #
1536 :     # Open file, spin to the starting line, then start reading.
1537 :     #
1538 :    
1539 :     open(my $anno_fh, "<$spool_dir/annos") or die "Cannot open annos file: $!";
1540 :    
1541 :     my $anno_output = [];
1542 :    
1543 :     my $anno_num = 0;
1544 :    
1545 :     local $/ = "//\n";
1546 :     while (<$anno_fh>)
1547 :     {
1548 :     next if ($anno_num < $start);
1549 :    
1550 :     last if ($anno_num > ($start + $len));
1551 :    
1552 :     chomp;
1553 :    
1554 :     my($id, $date, $author, $anno) = split(/\n/, $_, 4);
1555 :    
1556 :     push(@$anno_output, [$id, $date, $author, $anno]);
1557 :     }
1558 :     continue
1559 :     {
1560 :     $anno_num++;
1561 :     }
1562 :    
1563 :     return $anno_output;
1564 :     }
1565 : olson 1.19
1566 :     sub get_assignments
1567 :     {
1568 :     my($self, $session_id, $start, $len) = @_;
1569 :    
1570 :     #
1571 :     # This is now easy; just run thru the saved assignments and return.
1572 :     #
1573 :    
1574 :     my(%session_info);
1575 :    
1576 :     my $spool_dir = "$FIG_Config::temp/p2p_spool/$session_id";
1577 :    
1578 :     -d $spool_dir or die "Invalid session id $session_id";
1579 :    
1580 :     #
1581 :     # Read in the cached information for this session.
1582 :     #
1583 :    
1584 :     open(my $info_fh, "<$spool_dir/INFO") or die "Cannot open INFO file: $!";
1585 :     while (<$info_fh>)
1586 :     {
1587 :     chomp;
1588 :     my($var, $val) = split(/\t/, $_, 2);
1589 :     $session_info{$var} = $val;
1590 :     }
1591 :     close($info_fh);
1592 :    
1593 :     #
1594 :     # Sanity check start and length.
1595 :     #
1596 :    
1597 :     if ($start < 0 or $start >= $session_info{num_assignments})
1598 :     {
1599 :     die "Invalid start position $start";
1600 :     }
1601 :    
1602 :     if ($len < 0 or ($start + $len - 1) >= $session_info{num_assignments})
1603 :     {
1604 :     die "Invalid length $len";
1605 :     }
1606 :    
1607 :     #
1608 :     # Open file, spin to the starting line, then start reading.
1609 :     #
1610 :    
1611 :     open(my $assign_fh, "<$spool_dir/assignments") or die "Cannot open assignments file: $!";
1612 :    
1613 :     my $assign_output = [];
1614 :    
1615 :     my $assign_num = 0;
1616 :    
1617 :     while (<$assign_fh>)
1618 :     {
1619 :     next if ($assign_num < $start);
1620 :    
1621 :     last if ($assign_num > ($start + $len));
1622 :    
1623 :     chomp;
1624 :    
1625 :     my($id, $date, $author, $func) = split(/\t/, $_, 4);
1626 :    
1627 :     push(@$assign_output, [$id, $date, $author, $func]);
1628 :     }
1629 :     continue
1630 :     {
1631 :     $assign_num++;
1632 :     }
1633 :    
1634 :     return $assign_output;
1635 :     }
1636 :    
1637 : olson 1.22 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3