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

Annotation of /FigKernelPackages/P2Pupdate.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.56 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : efrank 1.1 package P2Pupdate;
19 :    
20 : olson 1.33 use strict;
21 :    
22 : olson 1.41 use Safe;
23 : efrank 1.1 use FIG_Config;
24 :     use FIG;
25 :     use Carp;
26 :     use Data::Dumper;
27 :     use Cwd;
28 :    
29 :     =pod
30 :    
31 :     =head1 updating code
32 :    
33 :     This routine packages what is needed to upgrade an older system to the
34 :     current code. Code releases are numered
35 :    
36 :     p1n1.p2n2.p3n3...
37 :    
38 :     where "." is added at the point the code moved to another branch of
39 :     the tree. FIG, who provided the initial release of the SEED, will
40 :     number all of their code releases as
41 :    
42 :     FIGn
43 :    
44 :     where n is an integer. Suppose that between releases 13 and 14 a
45 :     second group (which we will term "Idiots" for convenience) took
46 :     release 13 and wished to branch the code tree. At that point, they
47 :     would name their first release as
48 :    
49 :     FIG13.Idiots1
50 :    
51 :     We are, of course, being both cavalier and nasty when we make such a
52 :     reference. We do, however, wish to express the view that it will
53 :     benefit everyone to attempt to reconcile differences and maintain a
54 :     single code progression as long as possible. There are often good
55 :     reasons to part ways, but we urge people to think carefully before
56 :     taking such a step.
57 :    
58 :     Two code releases
59 :    
60 :     i1.i2.i3...in
61 :     and j1.j2.j3...jm with m <= n
62 :    
63 :     are compatible iff
64 :    
65 :     ip == jp for p < m, and
66 :     jm and im have the same "source" and
67 :     jm <= im
68 :    
69 :     A new code release must have the property that it can bring any
70 :     "older" compatible release up to its release.
71 :    
72 :     Note that there is an issue relating to the code to build/install packages.
73 :     Since a system may be radically restructured between releases of code, the
74 :     code to build a "package" and the code to "install" a package are radically
75 :     separated. For example, the code in P2Pupdate.pm for building an assignment
76 :     package and the code for installing an assignment package both apply to the
77 :     release of code current on the system containing P2Pupdate.pm. In fact, the
78 :     code releases may be quite different on two synchronizing systems.
79 :    
80 :     To make things work the following rules must be observed:
81 :    
82 :     1. a code release is a tar file containing VERSION, Packages,
83 :     bin/ToolTemplates, and CGI/ToolTemplates. The installing system needs
84 :     to place these at the appropriate spots, and then run bring_system_up_to_date,
85 :     which is supposed to do any required restructuring.
86 :    
87 :     2. an assignments package is a tar file containing a single directory. The directory
88 :     contains subdirectories -- one per genome. Each genome subdirectory contains zero
89 :     or more files. The name of the file is the "user" and the contents will be the
90 :     assignments made by that user.
91 :    
92 :     3. an annotations package is a tar file containing a single directory. The files in
93 :     the directory are named by genome. They contain the annotations for the genome.
94 :    
95 :     =cut
96 :    
97 :     =pod
98 :    
99 :     =head1 what_code_do_I_have
100 :    
101 :     usage: &what_code_do_I_have($fig_base)
102 :    
103 :     This just returns the current version of the code.
104 :    
105 :     =cut
106 :    
107 :     sub what_code_do_I_have {
108 :     my($fig_base) = @_;
109 :    
110 : olson 1.34 my $version = &FIG::file_read("$fig_base/VERSION");
111 :     chomp $version;
112 : efrank 1.1 return $version;
113 :     }
114 :    
115 :     =pod
116 :    
117 :     =head1 updatable_code
118 :    
119 :     usage: &updatable_code_code($v1,$v2)
120 :    
121 :     This just returns true iff the two versions of code are compatible and $v1
122 :     is "more recent".
123 :    
124 :     =cut
125 :    
126 :     sub updatable_code {
127 :     my($v1,$v2) = @_;
128 :     my($i,$v1p,$v1n,$v2p,$v2n);
129 :    
130 :     my @v1 = split(/\./,$v1);
131 :     my @v2 = split(/\./,$v2);
132 :     if (@v1 < @v2) { return 0 }
133 :    
134 :     for ($i=0; ($i < $#v2) && ($v1[$i] eq $v2[$i]); $i++) {}
135 :     if ($i == $#v2)
136 :     {
137 :     $v1[$i] =~ /^(.*[^\d])(\d+)$/;
138 :     $v1p = $1;
139 :     $v1n = $2;
140 :    
141 :     $v2[$i] =~ /^(.*[^\d])(\d+)$/;
142 :     $v2p = $1;
143 :     $v2n = $2;
144 :    
145 :     return (($v2p eq $v1p) && ($v2n < $v1n));
146 :     }
147 :     return 0;
148 :     }
149 :    
150 :     =pod
151 :    
152 :     =head1 package_code
153 :    
154 :     usage: &package_code($fig_disk,$file)
155 :    
156 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
157 :     the updated code release will be taken.
158 :    
159 :     $file must be an absolute filename where the "code package" will be built.
160 :    
161 :     =cut
162 :    
163 :     sub package_code {
164 :     my($fig_disk,$file) = @_;
165 :    
166 :     &force_absolute($fig_disk);
167 :     &force_absolute($file);
168 : olson 1.34 my @tmp = &FIG::file_head("$fig_disk/CURRENT_RELEASE", 1);
169 : overbeek 1.2 my $current_release = $tmp[0];
170 : olson 1.34 chomp $current_release;
171 : overbeek 1.2
172 :     &FIG::run("cd $fig_disk/dist/releases; tar czf $file $current_release");
173 : efrank 1.1 }
174 :    
175 :     sub force_absolute {
176 :     my($file) = @_;
177 :    
178 :     if (substr($file,0,1) ne "/")
179 :     {
180 : overbeek 1.2 print "Error: Please use absolute file names (i.e., /Users/fig/... or /home/fig/...)\n";
181 :     exit;
182 : efrank 1.1 }
183 :     }
184 :    
185 :     =pod
186 :    
187 :     =head1 install_code
188 :    
189 :     usage: &install_code($fig_disk,$package)
190 :    
191 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
192 :    
193 :     $package must be an absolute filename where the "code package" from which to make
194 :     the update exists.
195 :    
196 :     Note that this routine does not check that the updated code is compatible, or even less
197 :     current. It is assumed that upper level logic is doing that.
198 :    
199 :     =cut
200 :    
201 :     sub install_code {
202 :     my($fig_disk,$package) = @_;
203 :     my $fig_base = "$fig_disk/FIG";
204 :     &force_absolute($fig_base);
205 :     &force_absolute($package);
206 :    
207 :     if (getcwd() !~ /FIGdisk$/) { print die "Sorry, you must run this while in $FIG_Config::fig_disk" }
208 :    
209 :    
210 :     (! -d "$fig_disk/BackupCode") || &FIG::run("rm -rf $fig_disk/BackupCode");
211 :     mkdir("$fig_disk/BackupCode",0777) || die "Could not make the BackupCode directory";
212 :     (! -d "$fig_disk/BackupEnv") || &FIG::run("rm -rf $fig_disk/BackupEnv");
213 :     mkdir("$fig_disk/BackupEnv",0777) || die "Could not make the BackupEnv directory";
214 :    
215 :     my $version = &what_code_do_I_have($fig_base);
216 :     &FIG::run("cd $fig_disk; mv README install lib man env src $fig_disk/BackupEnv");
217 :     &FIG::run("cd $fig_base; mv VERSION Packages CGI $fig_disk/BackupCode");
218 :     print STDERR "made backups\n";
219 :    
220 :     &FIG::run("cd $fig_disk; tar xzf $package");
221 :     print STDERR "untarred new code\n";
222 :    
223 :     &fix_config("$fig_base/Packages/FIG_Config.pm","$fig_disk/BackupCode/Packages/FIG_Config.pm");
224 :     &FIG::run("cd $fig_base/bin; touch ToolTemplates/*/*; make all");
225 :     &FIG::run("cd $fig_base/CGI; touch ToolTemplates/*/*; make all");
226 :     print STDERR "installed new bin and CGI\n";
227 :    
228 :     &FIG::run("bring_system_up_to_date $version");
229 :     }
230 :    
231 :     =pod
232 :    
233 :     =head1 package_lightweight_code
234 :    
235 :     usage: &package_lightweight_code($fig_disk,$file)
236 :    
237 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
238 :     the updated code release will be taken.
239 :    
240 :     $file must be an absolute filename where the "code package" will be built.
241 :    
242 :     =cut
243 :    
244 :     sub package_lightweight_code {
245 :     my($fig_disk,$file) = @_;
246 :    
247 :     &force_absolute($fig_disk);
248 :     &force_absolute($file);
249 : olson 1.34 my @tmp = &FIG::file_head("$fig_disk/CURRENT_RELEASE", 1);
250 : overbeek 1.2 my $current_release = $tmp[0];
251 : olson 1.34 chomp $current_release;
252 : overbeek 1.2
253 :     &FIG::run("cd $fig_disk/dist/releases; tar czf $file $current_release");
254 : efrank 1.1 }
255 :    
256 :     =pod
257 :    
258 :     =head1 install_lightweight_code
259 :    
260 :     usage: &install_lightweight_code($fig_disk,$package)
261 :    
262 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
263 :    
264 :     $package must be an absolute filename where the "code package" from which to make
265 :     the update exists.
266 :    
267 :     Note that this routine does not check that the updated code is compatible, or even less
268 :     current. It is assumed that upper level logic is doing that.
269 :    
270 :     =cut
271 :    
272 :     sub install_lightweight_code {
273 :     my($fig_disk,$package) = @_;
274 :     my $fig_base = "$fig_disk/FIG";
275 :     &force_absolute($fig_base);
276 :     &force_absolute($package);
277 :    
278 : overbeek 1.2 if (! mkdir("$fig_disk/Tmp$$",0777))
279 :     {
280 :     print "Error: could not make $fig_disk/Tmp$$\n";
281 :     exit;
282 :     }
283 : efrank 1.1
284 : overbeek 1.2 &FIG::run("cd $fig_disk/Tmp$$; tar xzf $package");
285 :     if (! opendir(TMP,"$fig_disk/Tmp$$"))
286 :     {
287 :     print "Error: could not open $fig_disk/Tmp$$\n";
288 :     exit;
289 :     }
290 : efrank 1.1
291 : overbeek 1.2 my @rels = grep { $_ !~ /^\./ } readdir(TMP);
292 :     closedir(TMP);
293 :     if (@rels != 1)
294 :     {
295 :     print "Error: Bad code package: $package\n";
296 :     exit;
297 :     }
298 : efrank 1.1
299 : overbeek 1.2 my $new_release = $rels[0];
300 :     if (-d "$fig_disk/dist/releases/$new_release")
301 :     {
302 :     print "Error: $new_release already exists; we are doing nothing\n";
303 :     exit;
304 :     }
305 : efrank 1.1
306 : efrank 1.3 &FIG::run("mv $fig_disk/Tmp$$/$new_release $fig_disk/dist/releases");
307 :     &FIG::run("rm -rf $fig_disk/Tmp$$");
308 : olson 1.22
309 :     #
310 :     # Ugh. For now, find the arch in the fig config file $fig_disk/config/fig-user-env.sh"
311 :     #
312 :    
313 :     my $arch;
314 :     open(FH, "<$fig_disk/config/fig-user-env.sh");
315 :     while (<FH>)
316 :     {
317 :     if (/RTARCH="(.*)"/)
318 :     {
319 :     $arch = $1;
320 :     last;
321 :     }
322 :     }
323 :     close(FH);
324 :    
325 :     if ($arch eq "")
326 :     {
327 :     die "Couldn't determine SEED install architecture, not switching to release.";
328 :     }
329 :    
330 :     $ENV{RTARCH} = $arch;
331 : olson 1.32
332 :     #
333 :     # Need to put the ext_bin in the path.
334 :     #
335 :    
336 :     $ENV{PATH} .= ":$FIG_Config::ext_bin";
337 : olson 1.22
338 : efrank 1.9 &FIG::run("$FIG_Config::bin/switch_to_release $new_release");
339 : efrank 1.1 }
340 :    
341 :    
342 :     sub fix_config {
343 :     my($new,$old) = @_;
344 :     my($line,$i);
345 :    
346 : olson 1.34 my @new = &FIG::file_read($new);
347 :     foreach $line (&FIG::file_read($old))
348 : efrank 1.1 {
349 :     if ($line =~ /^(\S+)\s+\=/)
350 :     {
351 : olson 1.33 my $var = $1;
352 :     my $varQ = quotemeta $var;
353 : efrank 1.1
354 :     for ($i=0; ($i < $#new) && ($new[$i] !~ /^$varQ\s+\=/); $i++) {}
355 :     if ($i == $#new)
356 :     {
357 :     splice(@new,$i,0,$line);
358 :     }
359 :     else
360 :     {
361 :     splice(@new,$i,1,$line);
362 :     }
363 :     }
364 :     }
365 :     open(NEW,">$new") || confess "could not overwrite $new";
366 :     foreach $line (@new)
367 :     {
368 :     print NEW $line;
369 :     }
370 :     close(NEW);
371 :     }
372 :    
373 :     =pod
374 :    
375 :     =head1 what_genomes_will_I_sync
376 :    
377 :     usage: &what_genomes_will_I_sync($fig_base,$who)
378 :    
379 :     This routine returns the list of genome IDs that you are willing to sync with $who.
380 :    
381 :     =cut
382 :    
383 :     sub what_genomes_will_I_sync {
384 :     my($fig_base,$who) = @_;
385 :    
386 :     # This is the promiscuous version - it will sync all genomes with anyone.
387 :    
388 :     opendir(GENOMES,"$fig_base/Data/Organisms") || die "could not open $fig_base/Data/Organisms";
389 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(GENOMES);
390 :     closedir(GENOMES);
391 :     return @genomes;
392 :     }
393 :    
394 :     =pod
395 :    
396 :     =head1 package_annotations
397 :    
398 : overbeek 1.29 usage: &package_annotations($fig,$genomes,$file)
399 : efrank 1.1
400 :     $genomes is a pointer to a list of genome IDs that will be exchanged.
401 :    
402 :     $file must be an absolute filename where the "annotation package" will be built.
403 :    
404 :     =cut
405 :    
406 :     sub package_annotations {
407 : olson 1.53 my($fig,$who,$date,$genomes,$file, %options) = @_;
408 : overbeek 1.29 my $fig_base = "$FIG_Config::fig_disk/FIG";
409 : efrank 1.1
410 : olson 1.52 if (!open(ANNOTATIONS,">$file"))
411 :     {
412 :     die "Cannot open annotations file $file for writing: $!";
413 :     }
414 :    
415 :    
416 :     my $annos = $fig->annotations_made_fast($genomes, $date, undef, $who);
417 :    
418 :     #
419 :     # $annos is a list of pairs [$genome, $genomeannos]
420 :     # $genomeannos is a hash keyed on peg. value is a list of lists [$peg, $time, $who, $anno].
421 :     #
422 :    
423 : olson 1.55 my @annos = sort { &FIG::by_genome_id($a->[0], $b->[0]) } @$annos;
424 : olson 1.52
425 :     for my $gent (@annos)
426 :     {
427 :     my($genome, $alist) = @$gent;
428 :    
429 :     for my $peg (sort { &FIG::by_fig_id($a, $b) } keys %$alist)
430 :     {
431 :     for my $aent (@{$alist->{$peg}})
432 :     {
433 : olson 1.55 print ANNOTATIONS $aent->as_text() . "\n///\n";
434 : olson 1.52 }
435 :     }
436 :    
437 :     }
438 : olson 1.53
439 :     print ANNOTATIONS "//\n";
440 :    
441 :     if (!$options{skip_aliases})
442 : olson 1.52 {
443 : olson 1.53 for my $gent (@annos)
444 : olson 1.52 {
445 : olson 1.53 my($genome, $alist) = @$gent;
446 :     my $gs = $fig->genus_species($genome);
447 :    
448 :     for my $peg (sort { &FIG::by_fig_id($a, $b) } keys %$alist)
449 : olson 1.52 {
450 : olson 1.53 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
451 :     print ANNOTATIONS join("\t",($peg,join(",",@aliases),$gs,scalar $fig->function_of($peg))) . "\n";
452 : olson 1.52 }
453 :     }
454 : olson 1.53 }
455 : olson 1.52
456 :     print ANNOTATIONS "//\n";
457 : olson 1.53 if (!$options{skip_sequences})
458 : olson 1.52 {
459 : olson 1.53 for my $gent (@annos)
460 : olson 1.52 {
461 : olson 1.53 my($genome, $alist) = @$gent;
462 :    
463 :     for my $peg (sort { &FIG::by_fig_id($a, $b) } keys %$alist)
464 :     {
465 :     my $seq = $fig->get_translation($peg);
466 :     &FIG::display_id_and_seq($peg,\$seq,\*ANNOTATIONS);
467 :     }
468 :    
469 : olson 1.52 }
470 :     }
471 :    
472 :     close(ANNOTATIONS);
473 :     }
474 :    
475 : olson 1.54 #
476 :     # This was the original version.
477 :     #
478 : olson 1.52 sub package_annotations2 {
479 :     my($fig,$who,$date,$genomes,$file) = @_;
480 :     my $fig_base = "$FIG_Config::fig_disk/FIG";
481 :    
482 : overbeek 1.29 if (open(ANNOTATIONS,">$file"))
483 : efrank 1.1 {
484 : overbeek 1.29 my @annotations = sort { $a->[0] cmp $b->[0] } $fig->annotations_made($genomes,$who,$date);
485 : olson 1.33 foreach my $x (@annotations)
486 : efrank 1.1 {
487 : overbeek 1.45 my $ann = join("\n",@$x);
488 :     if (($ann =~ /^fig\|\d+\.\d+\.peg\.\d+\n\d+\n/s) && ($ann !~ /\n\/\/\n/s))
489 :     {
490 :     print ANNOTATIONS join("\n",@$x),"\n///\n";
491 :     }
492 : efrank 1.1 }
493 : overbeek 1.29 print ANNOTATIONS "//\n";
494 : efrank 1.15
495 : olson 1.33 foreach my $x (@annotations)
496 : efrank 1.15 {
497 : olson 1.33 my $peg = $x->[0];
498 : overbeek 1.30 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
499 :     print ANNOTATIONS join("\t",($peg,join(",",@aliases),$fig->genus_species($fig->genome_of($peg)),scalar $fig->function_of($peg))) . "\n";
500 : efrank 1.15 }
501 : overbeek 1.29 print ANNOTATIONS "//\n";
502 :    
503 : olson 1.33 foreach my $x (@annotations)
504 : efrank 1.15 {
505 : olson 1.33 my $peg;
506 : overbeek 1.29 ($peg,undef) = @$x;
507 : overbeek 1.30 my $seq = $fig->get_translation($peg);
508 : overbeek 1.29 &FIG::display_id_and_seq($peg,\$seq,\*ANNOTATIONS);
509 : efrank 1.15 }
510 : overbeek 1.29 close(ANNOTATIONS);
511 : efrank 1.15 }
512 : efrank 1.1 }
513 :    
514 : overbeek 1.29
515 : efrank 1.1 =pod
516 :    
517 :     =head1 install_annotations
518 :    
519 :     usage: &install_annotations($fig_disk,$package)
520 :    
521 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
522 :    
523 :     $package must be an absolute filename where the "annotations package" from which to make
524 :     the update exists.
525 :    
526 :     =cut
527 :    
528 :     sub install_annotations {
529 : overbeek 1.29 my($fig,$package) = @_;
530 :     my($user,$who,$date,$userR,@assignments,$peg,$aliases,$org,$func);
531 :     my(%pegs,%seq_of,@seq,$peg_to,$trans_pegs,$seq,$line,@ann,$ann);
532 : efrank 1.1 my($genome);
533 :    
534 : overbeek 1.29 my $fig_disk = $FIG_Config::fig_disk;
535 :     open(IN,"<$package") || die "could not open $package";
536 :     $/ = "\n//\n";
537 :     if (defined($line = <IN>))
538 :     {
539 : olson 1.33 my(@annotations);
540 :    
541 : overbeek 1.29 $line =~ s/\n\/\/\n/\n/s;
542 :     @ann = split(/\n\/\/\/\n/,$line);
543 :     foreach $ann (@ann)
544 :     {
545 :     if ($ann =~ /^(fig\|\d+\.\d+\.peg\.\d+)\n(\d+)\n(\S+)\n(.*)/s)
546 :     {
547 :     push(@annotations,[$1,$2,$3,$4]);
548 :     }
549 :     }
550 :     $/ = "\n";
551 :     while ($line && defined($line = <IN>) && ($line !~ /^\/\//))
552 :     {
553 : olson 1.34 chomp $line;
554 : overbeek 1.29 ($peg,$aliases,$org,$func) = split(/\t/,$line);
555 :     $pegs{$peg} = [$aliases,$org,$func];
556 :     }
557 :    
558 :     if ($line) { $line = <IN> }
559 :     while (defined($line) && ($line !~ /^\/\//))
560 :     {
561 :     if ($line =~ /^>(\S+)/)
562 :     {
563 :     $peg = $1;
564 :     @seq = ();
565 : overbeek 1.44 $line = <IN>;
566 :     while ($line && ($line !~ /^[>\/]/) && ($line !~ /^\/\//))
567 : overbeek 1.29 {
568 :     push(@seq,$line);
569 :     $line = <IN>;
570 :     }
571 :     $seq = join("",@seq);
572 :     $seq =~ s/[ \n\t]//gs;
573 :     $seq_of{$peg} = uc $seq;
574 :     }
575 : overbeek 1.46 else
576 :     {
577 :     $line = <IN>;
578 :     }
579 : overbeek 1.29 }
580 :     close(IN);
581 :     $trans_pegs = $fig->translate_pegs(\%pegs,\%seq_of);
582 :     @annotations = sort { ($a->[0] cmp $b->[0]) or ($a->[1] <=> $b->[1]) }
583 :     map { ($peg = $trans_pegs->{$_->[0]}) ? [$peg,$_->[1],$_->[2],$_->[3]] : () }
584 :     @annotations;
585 :    
586 :     if (-d "$fig_disk/BackupAnnotations") { system "rm -rf $fig_disk/BackupAnnotations" }
587 :     mkdir("$fig_disk/BackupAnnotations",0777);
588 :     mkdir("$fig_disk/BackupAnnotations/New",0777);
589 : olson 1.33 my $i;
590 :     for ($i=0; ($i < @annotations); $i++)
591 : overbeek 1.29 {
592 :     if (($i == 0) || ($fig->genome_of($annotations[$i]->[0]) ne $fig->genome_of($annotations[$i-1]->[0])))
593 :     {
594 :     if ($i != 0)
595 :     {
596 :     close(OUT);
597 :     }
598 :     $genome = $fig->genome_of($annotations[$i]->[0]);
599 :     open(OUT,">$fig_disk/BackupAnnotations/New/$genome")
600 :     || die "could not open $fig_disk/BackupAnnotations/New/$genome";
601 :     }
602 :     print OUT join("\n",@{$annotations[$i]}),"\n//\n";
603 :     }
604 :     if ($i > 0) { close(OUT) }
605 :     }
606 : efrank 1.15
607 : efrank 1.1 opendir(TMP,"$fig_disk/BackupAnnotations/New") || die "could not open $fig_disk/BackupAnnotations/New";
608 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(TMP);
609 :     closedir(TMP);
610 :     foreach $genome (@genomes)
611 :     {
612 : overbeek 1.43 next if (! -d "$fig_disk/FIG/Data/Organisms/$genome");
613 : efrank 1.13
614 : efrank 1.1 print STDERR "installing $fig_disk/FIG/Data/Organisms/$genome/annotations\n";
615 :     if (-s "$fig_disk/FIG/Data/Organisms/$genome/annotations")
616 :     {
617 :     &FIG::run("cp -p $fig_disk/FIG/Data/Organisms/$genome/annotations $fig_disk/BackupAnnotations/$genome");
618 : efrank 1.9 &FIG::run("$FIG_Config::bin/merge_annotations $fig_disk/BackupAnnotations/$genome $fig_disk/BackupAnnotations/New/$genome > $fig_disk/FIG/Data/Organisms/$genome/annotations");
619 : efrank 1.1 }
620 :     else
621 :     {
622 :     &FIG::run("cp $fig_disk/BackupAnnotations/New/$genome $fig_disk/FIG/Data/Organisms/$genome/annotations");
623 :     }
624 : olson 1.37 chmod 0777,"$fig_disk/FIG/Data/Organisms/$genome/annotations";
625 : efrank 1.1 }
626 : efrank 1.9 &FIG::run("$FIG_Config::bin/index_annotations");
627 : efrank 1.1 }
628 : olson 1.47
629 :    
630 :     =pod
631 :    
632 :     =head1 install_annotations_gff
633 :    
634 :     Install a set of annotations contained in a GFF3 file package.
635 :    
636 :     We parse using the FigGFF::GFFParser GFF parser. This returns a GFFFile object
637 :     that contains the parsed contents of the file.
638 :    
639 :     =cut
640 :    
641 :     sub install_annotations_gff
642 :     {
643 :     my($fig, $gff_file) = @_;
644 :    
645 :     my $db = $fig->db_handle;
646 :    
647 :     my $parser = new GFFParser($fig);
648 :    
649 :     my $fobj = $parser->parse($gff_file);
650 :    
651 :     #
652 :     # We assume that we only have one genome per GFF file, but we
653 :     # get the list of genomes and checksums via a general accessor anyway.
654 :     #
655 :    
656 :     for my $ent (@{$fobj->genome_checksums()})
657 :     {
658 :     my($genome, $checksum) = @$ent;
659 :    
660 :     #
661 :     # Determine if we have the same version of this genome.
662 :     #
663 :    
664 :     my $local_genome = $fig->genome_with_md5sum($checksum);
665 :     print "Local genome=$local_genome cksum=$checksum\n";
666 :    
667 :     #
668 :     # Walk the features, looking for matching features in the local SEED,
669 :     # and install the annotations if possible.
670 :     #
671 :    
672 :     my @annos;
673 :    
674 :     print "Walking $genome\n";
675 :     for my $feature (@{$fobj->features_for_genome($genome)})
676 :     {
677 :     my($local_id);
678 :    
679 :     my @local_ids = $feature->find_local_feature($local_genome);
680 :    
681 :     print "Mapped to @local_ids\n";
682 :     }
683 :     }
684 :     }
685 :    
686 : efrank 1.1
687 :     =pod
688 :    
689 :     =head1 restore_annotations
690 :    
691 :     usage: &restore_annotations($fig_disk);
692 :    
693 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
694 :    
695 :     =cut
696 :    
697 :     sub restore_annotations {
698 :     my($fig_disk) = @_;
699 :    
700 :     &force_absolute($fig_disk);
701 :     (-d "$fig_disk/BackupAnnotations") || die "could not find an active backup";
702 :     opendir(TMP,"$fig_disk/BackupAnnotations") || die "could not open $fig_disk/BackupAnnotations";
703 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(TMP);
704 :     closedir(TMP);
705 : olson 1.33 foreach my $genome (@genomes)
706 : efrank 1.1 {
707 :     unlink("$fig_disk/FIG/Data/Organisms/$genome/annotations");
708 :     &FIG::run("cp $fig_disk/BackupAnnotations/$genome $fig_disk/FIG/Data/Organisms/$genome/annotations");
709 : olson 1.37 system "chmod 777 $fig_disk/FIG/Data/Organisms/$genome/annotations";
710 : efrank 1.1 }
711 : efrank 1.9 &FIG::run("$FIG_Config::bin/index_annotations");
712 : efrank 1.1 }
713 :    
714 :     =pod
715 :    
716 :     =head1 package_aassignments
717 :    
718 : overbeek 1.20 usage: package_assignments($fig,$user,$who,$date,$genomes,$file)
719 : efrank 1.1
720 : overbeek 1.20 $user designates the user wishing to get the assignments
721 : efrank 1.1
722 : overbeek 1.5 $who designates whose assignments you want (defaults to "master")
723 :    
724 :     $date if given indicates a point in time (get assignments after that point)
725 :    
726 : efrank 1.1 $genomes is a pointer to a list of genome IDs that will be exchanged.
727 :    
728 :     $file must be an absolute filename where the "assignment package" will be built.
729 :    
730 :     =cut
731 :    
732 :     sub package_assignments {
733 : overbeek 1.20 my($fig,$user,$who,$date,$genomes,$file) = @_;
734 : overbeek 1.27 my($genome,$x,$org,$curr,$peg);
735 : overbeek 1.4 $who = $who ? $who : "master";
736 : overbeek 1.7 $date = $date ? $date : 0;
737 : overbeek 1.27
738 : overbeek 1.20 if (open(ASSIGNMENTS,">$file"))
739 : efrank 1.1 {
740 : overbeek 1.20 print ASSIGNMENTS "$user\t$who\t$date\n";
741 : olson 1.33 my @assignments = sort { $a->[0] cmp $b->[0] } $fig->assignments_made_full($genomes,$who,$date);
742 : overbeek 1.36 my @curr_assignments = ();
743 : overbeek 1.27 foreach $x (@assignments)
744 : efrank 1.1 {
745 : overbeek 1.36 my($peg, $function, undef, undef) = @$x;
746 :     if ($function eq $fig->function_of($peg,$who))
747 :     {
748 :     print ASSIGNMENTS join("\t", $peg, $function),"\n";
749 :     push(@curr_assignments,$x);
750 :     }
751 : overbeek 1.4 }
752 : overbeek 1.27 print ASSIGNMENTS "//\n";
753 :    
754 : overbeek 1.36 foreach $x (@curr_assignments)
755 : overbeek 1.27 {
756 :     ($peg,undef) = @$x;
757 : overbeek 1.30 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
758 : olson 1.33
759 :     my $alias_txt = join(",",@aliases);
760 :     my $gs_txt = $fig->genus_species($fig->genome_of($peg));
761 :     my $func_txt = scalar $fig->function_of($peg);
762 :    
763 :     print ASSIGNMENTS join("\t",($peg,
764 :     $alias_txt,
765 :     $gs_txt,
766 :     $func_txt)) . "\n";
767 : overbeek 1.28 }
768 :     print ASSIGNMENTS "//\n";
769 :    
770 : overbeek 1.36 foreach $x (@curr_assignments)
771 : overbeek 1.28 {
772 :     ($peg,undef) = @$x;
773 : overbeek 1.30 my $seq = $fig->get_translation($peg);
774 : overbeek 1.27 &FIG::display_id_and_seq($peg,\$seq,\*ASSIGNMENTS);
775 :     }
776 :     close(ASSIGNMENTS);
777 : efrank 1.1 }
778 :     }
779 :    
780 :     =pod
781 :    
782 :     =head1 install_assignments
783 :    
784 : overbeek 1.20 usage: &install_assignments($package)
785 : efrank 1.1
786 : overbeek 1.20 $package must be a filename where the "assignments package" from which to make
787 :     the assignment set exists
788 : efrank 1.1
789 :     =cut
790 :    
791 :     sub install_assignments {
792 : overbeek 1.35 my($fig,$package,$make_assignments) = @_;
793 : overbeek 1.29 my($user,$who,$date,$userR,@assignments,$peg,$aliases,$org,$func);
794 :     my(%pegs,%seq_of,@seq,$peg_to,$trans_pegs,$seq);
795 : efrank 1.1
796 : overbeek 1.20 open(IN,"<$package") || die "could not open $package";
797 :     my $line = <IN>;
798 : olson 1.34 chomp $line;
799 : overbeek 1.20 ($user,$who,$date) = split(/\t/,$line);
800 : olson 1.26 $userR = $user;
801 :     $userR =~ s/^master://;
802 : overbeek 1.29
803 : overbeek 1.30 while (defined($line = <IN>) && ($line !~ /^\/\//))
804 : overbeek 1.29 {
805 :     if ($line =~ /^(fig\|\d+\.\d+\.peg\.\d+)\t(\S.*\S)/)
806 :     {
807 :     push(@assignments,[$1,$2]);
808 :     }
809 :     }
810 :     while ($line && defined($line = <IN>) && ($line !~ /^\/\//))
811 :     {
812 : olson 1.34 chomp $line;
813 : overbeek 1.29 ($peg,$aliases,$org,$func) = split(/\t/,$line);
814 :     $pegs{$peg} = [$aliases,$org,$func];
815 :     }
816 :    
817 :     if ($line) { $line = <IN> }
818 :     while (defined($line) && ($line !~ /^\/\//))
819 :     {
820 :     if ($line =~ /^>(\S+)/)
821 :     {
822 :     $peg = $1;
823 :     @seq = ();
824 : overbeek 1.44 $line = <IN>;
825 :     while ($line && ($line !~ /^[>\/]/) && ($line !~ /^\/\//))
826 : overbeek 1.29 {
827 :     push(@seq,$line);
828 :     $line = <IN>;
829 :     }
830 :     $seq = join("",@seq);
831 :     $seq =~ s/[ \n\t]//gs;
832 :     $seq_of{$peg} = uc $seq;
833 :     }
834 : overbeek 1.46 else
835 :     {
836 :     $line = <IN>;
837 : olson 1.47 }
838 : overbeek 1.29 }
839 :     close(IN);
840 :     $trans_pegs = $fig->translate_pegs(\%pegs,\%seq_of);
841 :    
842 : olson 1.26 &FIG::verify_dir("$FIG_Config::data/Assignments/$userR");
843 : overbeek 1.29 my $file = &FIG::epoch_to_readable($date) . ":$who:imported";
844 : overbeek 1.20 $file =~ s/\//-/g;
845 : overbeek 1.35
846 :     if (! $make_assignments)
847 :     {
848 :     open(OUT,">$FIG_Config::data/Assignments/$userR/$file")
849 :     || die "could not open $FIG_Config::data/Assignments/$userR/$file";
850 :     }
851 : overbeek 1.29
852 :     foreach $peg (keys(%$trans_pegs))
853 : overbeek 1.20 {
854 : overbeek 1.29 $peg_to = $trans_pegs->{$peg};
855 :     $func = $pegs{$peg}->[2];
856 :     if ($fig->function_of($peg_to) ne $func)
857 :     {
858 : overbeek 1.35 if ($make_assignments)
859 :     {
860 :     if ($user =~ /master:(.*)/)
861 :     {
862 :     $userR = $1;
863 :     $fig->assign_function($peg_to,"master",$func,"");
864 :     if ($userR ne "none")
865 :     {
866 :     $fig->add_annotation($peg_to,$userR,"Set master function to\n$func\n");
867 :     }
868 :     }
869 :     else
870 :     {
871 :     $fig->assign_function($peg_to,$user,$func,"");
872 :     if ($user ne "none")
873 :     {
874 :     $fig->add_annotation($peg_to,$user,"Set function to\n$func\n");
875 :     }
876 :     }
877 :     }
878 :     else
879 :     {
880 :     print OUT "$peg_to\t$func\n";
881 :     }
882 : overbeek 1.29 }
883 : overbeek 1.20 }
884 : overbeek 1.35 if (! $make_assignments)
885 :     {
886 :     close(OUT);
887 :     if (! -s "$FIG_Config::data/Assignments/$userR/$file") { unlink("$FIG_Config::data/Assignments/$userR/$file") }
888 :     }
889 : efrank 1.1 }
890 :    
891 :     =pod
892 :    
893 :     =head1 package_translation_rules
894 :    
895 :     usage: &package_translation_rules($fig_base,$file)
896 :    
897 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
898 :     the updated code release will be taken.
899 :    
900 :     $file must be an absolute filename where the "translation_rules package" will be built.
901 :    
902 :     =cut
903 :    
904 :     sub package_translation_rules {
905 :     my($fig_base,$file) = @_;
906 :    
907 :     &FIG::run("cp $fig_base/Data/Global/function.synonyms $file");
908 :     }
909 :    
910 :     =pod
911 :    
912 :     =head1 install_translation_rules
913 :    
914 : efrank 1.10 usage: &install_translation_rules($fig_disk,$from,$package)
915 : efrank 1.1
916 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
917 :    
918 :     $package must be an absolute filename where the "translation_rules package" from which to make
919 :     the update exists.
920 :    
921 :     =cut
922 :    
923 :     sub install_translation_rules {
924 : efrank 1.10 my($fig_disk,$from,$package) = @_;
925 : efrank 1.1
926 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
927 :     &force_absolute($fig_disk);
928 :     if (-d "$fig_disk/BackupTranslation_Rules") { system "rm -rf $fig_disk/BackupTranslation_Rules" }
929 :     mkdir("$fig_disk/BackupTranslation_Rules",0777);
930 : disz 1.31 chmod 02777,"$fig_disk/BackupTranslation_Rules";
931 : efrank 1.1 if (-s $file)
932 :     {
933 :     &FIG::run("cp $file $fig_disk/BackupTranslation_Rules");
934 :     }
935 : efrank 1.10 &FIG::run("$FIG_Config::bin/merge_translation_rules $fig_disk/BackupTranslation_Rules/function.synonyms $package $from > $file");
936 : disz 1.31 chmod 02777,$file;
937 : efrank 1.1 }
938 :    
939 :     =pod
940 :    
941 :     =head1 restore_translation_rules
942 :    
943 :     usage: &restore_translation_rules($fig_disk);
944 :    
945 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
946 :    
947 :     =cut
948 :    
949 :     sub restore_translation_rules {
950 :     my($fig_disk) = @_;
951 :    
952 :     &force_absolute($fig_disk);
953 :    
954 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
955 :     (-s "$fig_disk/BackupTranslation_Rules/function.synonyms") || die "could not find an active backup";
956 :     if (-s "$fig_disk/BackupTranslation_Rules/function.synonyms")
957 :     {
958 :     &FIG::run("cp $fig_disk/BackupTranslation_Rules/function.synonyms $file");
959 : olson 1.37 chmod 0777, $file;
960 : efrank 1.1 }
961 :     }
962 :    
963 : overbeek 1.23 sub package_subsystems {
964 : overbeek 1.57 my($fig,$file,$just_exchangable,$just_these) = @_;
965 :     my($ssa,@exchangable);
966 : overbeek 1.27
967 : overbeek 1.57 if (@$just_these > 0)
968 :     {
969 :     @exchangable = @$just_these;
970 :     }
971 :     else
972 :     {
973 :     $just_exchangable = defined($just_exchangable) ? $just_exchangable : 1;
974 :     @exchangable = grep { (! $just_exchangable) || $fig->is_exchangable_subsystem($_) }
975 :     $fig->all_subsystems;
976 :     }
977 : overbeek 1.23 my $fig = new FIG;
978 : overbeek 1.24 if ((@exchangable > 0) && open(SUB,">$file"))
979 : overbeek 1.23 {
980 : overbeek 1.24 foreach $ssa (@exchangable)
981 : overbeek 1.23 {
982 : overbeek 1.25 # print STDERR "writing $ssa to $file\n";
983 :     my($spreadsheet,$notes) = $fig->exportable_subsystem($ssa);
984 : overbeek 1.23 print SUB join("",@$spreadsheet),join("",@$notes),"########################\n";
985 :     }
986 :     close(SUB);
987 :     }
988 : overbeek 1.25 else
989 :     {
990 :     # print STDERR &Dumper(\@exchangable,$file);
991 :     }
992 : overbeek 1.23 }
993 :    
994 : overbeek 1.30 sub install_subsystems {
995 :     my($fig,$package) = @_;
996 :    
997 : disz 1.31 &FIG::run("$FIG_Config::bin/import_subsystems master last_release < $package");
998 : overbeek 1.30 }
999 :    
1000 : olson 1.37
1001 :     =pod
1002 :    
1003 :     =head2 unpack_packaged_subsystem
1004 :    
1005 :     Unpack a packaged subsystem (from the clearinghouse or a p2p transfer)
1006 :     into a directory; this will create a directory named as the subsystem
1007 :     and formatted like the standard subsystem directories, as well as a
1008 :     file of assignments and a file of sequences in fasta format.
1009 :    
1010 :     Returns the name of the subsystem.
1011 :    
1012 :     =cut
1013 :    
1014 :     sub unpack_packaged_subsystem
1015 :     {
1016 :     my($fig, $file, $target_dir) = @_;
1017 :    
1018 :     my $user = $fig->get_user();
1019 :    
1020 :     &FIG::verify_dir($target_dir);
1021 :    
1022 :     my $fh;
1023 :    
1024 :     if (!open($fh, "<$file"))
1025 :     {
1026 :     warn "unpack_packaged_subsystem: cannot open $file: $!";
1027 :     return undef;
1028 :     }
1029 :    
1030 :     #
1031 :     # We scan the file, breaking it up into sections and writing
1032 :     # to the appropriate places.
1033 :     #
1034 :     # First the header.
1035 :     #
1036 :    
1037 :     local $/ = "\n//\n";
1038 :    
1039 :     my $header = <$fh>;
1040 :     chomp $header;
1041 :    
1042 :     my ($name, $version, $exchangable, $curation) = split(/\n/, $header);
1043 :    
1044 :     print "Importing name=$name version=$version exch=$exchangable curation='$curation'\n";
1045 :    
1046 :     #
1047 :     # Pull in roles, subsets, and spreadsheet. These will be written to the new
1048 :     # spreadsheet file.
1049 :     #
1050 :    
1051 :     my $roles = <$fh>;
1052 :     chomp $roles;
1053 :    
1054 :     my $subsets = <$fh>;
1055 :     chomp $subsets;
1056 :    
1057 :     my $spreadsheet = <$fh>;
1058 :     chomp $spreadsheet;
1059 :    
1060 :    
1061 :     #
1062 :     # Pull the assignments and sequences. These go to their own files.
1063 :     #
1064 :    
1065 :     my $assignments = <$fh>;
1066 :     chomp $assignments;
1067 :    
1068 :     my $sequences = <$fh>;
1069 :     chomp $sequences;
1070 :    
1071 :     #
1072 :     # And the notes; these will be written to the subsystem dir.
1073 :     #
1074 :    
1075 :     my $notes = <$fh>;
1076 :     chomp $notes;
1077 :    
1078 :     close($fh);
1079 :    
1080 :     #
1081 :     # Everything is read. Now to write it all back out again.
1082 :     #
1083 :    
1084 :     #
1085 :     # First the subsystem.
1086 :     #
1087 :    
1088 :     my $ss_path = "$target_dir/subsystem";
1089 :     &FIG::verify_dir($ss_path);
1090 :    
1091 :     open($fh, ">$ss_path/EXCHANGABLE");
1092 :     print $fh "$exchangable\n";
1093 :     close($fh);
1094 :    
1095 :     open($fh, ">$ss_path/VERSION");
1096 :     print $fh "$version\n";
1097 :     close($fh);
1098 :    
1099 :     open($fh, ">$ss_path/curation.log");
1100 :     print $fh "$curation\n";
1101 :     my $now = time;
1102 :     print $fh "$now\t$user\timported\n";
1103 :     close($fh);
1104 :    
1105 :     open($fh, ">$ss_path/notes");
1106 :     print $fh "$notes\n";
1107 :     close($fh);
1108 :    
1109 :     open($fh, ">$ss_path/spreadsheet");
1110 :     print $fh "$roles\n";
1111 :     print $fh "//\n";
1112 :     print $fh "$subsets\n";
1113 :     print $fh "//\n";
1114 :     print $fh "$spreadsheet\n";
1115 :     close($fh);
1116 :    
1117 :     open($fh, ">$target_dir/subsystem_name");
1118 :     print $fh "$name\n";
1119 :     close($fh);
1120 :    
1121 :     open($fh, ">$target_dir/assignments");
1122 :     print $fh "$assignments\n";
1123 :     close($fh);
1124 :    
1125 :     open($fh, ">$target_dir/seqs.fasta");
1126 :     print $fh "$sequences\n";
1127 :     close($fh);
1128 :    
1129 :     return $name;
1130 :     }
1131 :    
1132 : olson 1.33 package SubsystemFile;
1133 :    
1134 :     use Data::Dumper;
1135 :     use strict;
1136 : olson 1.50 use Carp;
1137 :     use MIME::Base64;
1138 :    
1139 : olson 1.33 sub new
1140 :     {
1141 :     my($class, $qdir, $file, $fig) = @_;
1142 :     my(@info);
1143 :    
1144 : olson 1.50 my $use_cache = defined($qdir);
1145 :    
1146 : olson 1.33 @info = FIG::file_head($file, 4);
1147 :     if (!@info)
1148 :     {
1149 :     warn "Cannot open $file\n";
1150 :     return undef;
1151 :     }
1152 :    
1153 :     chomp(@info);
1154 :    
1155 :     my $name = $info[0];
1156 :     my $version = $info[1];
1157 :     my $exc = $info[2];
1158 :    
1159 :     my @c = split(/\t/, $info[3]);
1160 :    
1161 :     my $curator = $c[1];
1162 :    
1163 :     my $self = {
1164 :     qdir => $qdir,
1165 : olson 1.50 use_cache => $use_cache,
1166 : olson 1.33 file => $file,
1167 :     name => $name,
1168 :     version => $version,
1169 :     exchangable => $exc,
1170 :     curator => $curator,
1171 : olson 1.39 curation_log => $info[3],
1172 : olson 1.33 fig => $fig,
1173 :     };
1174 :    
1175 :     return bless($self, $class);
1176 :    
1177 :     }
1178 :    
1179 :     #
1180 :     # Load the export file into internal data structures.
1181 :     #
1182 :     # It's structured as
1183 :     #
1184 :     # name
1185 :     # version
1186 :     # exchangable
1187 :     # creation date <tab> curator <tab> "started"
1188 :     # //
1189 :     # roles
1190 :     # //
1191 :     # subsets
1192 :     # //
1193 :     # spreadsheet
1194 :     # //
1195 :     # assignments
1196 :     # //
1197 :     # sequences
1198 :     # //
1199 :     # notes
1200 :     # //
1201 : olson 1.49 # reactions
1202 : olson 1.33 #
1203 :     # Subsections:
1204 :     #
1205 :     # roles:
1206 :     #
1207 :     # abbr <tab> role-name
1208 :     #
1209 :     # subsets has meaning to the acutal subsystems, but we'll use it as a string.
1210 :     #
1211 :     # spreadsheet:
1212 :     #
1213 :     # genome <tab> variant <tab> items
1214 :     #
1215 :     # Where items is tab-separated columns, each of which is comma-separated peg number in the genome
1216 :     #
1217 :     # assignments:
1218 :     #
1219 :     # fid <tab> aliases <tab> organism <tab> function
1220 :     #
1221 :     # sequences:
1222 :     #
1223 :     # list of fasta's
1224 :     #
1225 :     # notes:
1226 :     #
1227 :     # plain text
1228 :     #
1229 :     sub load
1230 :     {
1231 :     my($self) = @_;
1232 :    
1233 :     my $fig = $self->{fig};
1234 :    
1235 :     my($fh);
1236 :    
1237 :     open($fh, "<$self->{file}") or die "Cannot open $self->{file}: $!\n";
1238 :    
1239 :     #
1240 : olson 1.39 # Skip intro section - we already read this information in the constructor.
1241 : olson 1.33 #
1242 :    
1243 :     while (<$fh>)
1244 :     {
1245 :     chomp;
1246 :     last if m,^//,;
1247 :     }
1248 :    
1249 :     #
1250 :     # Read the roles.
1251 :     #
1252 :    
1253 :    
1254 :     my $nroles;
1255 :    
1256 :     while (<$fh>)
1257 :     {
1258 :     last if m,^//,;
1259 :    
1260 :     $self->{role_text} .= $_;
1261 :     chomp $_;
1262 :    
1263 :     my($abbr, $role) = split(/\t/);
1264 :    
1265 :     warn "Have role $role\n";
1266 :    
1267 :     push(@{$self->{roles}}, $role);
1268 :     push(@{$self->{abbrs}}, $abbr);
1269 :    
1270 :     $nroles++;
1271 :     }
1272 :    
1273 :     #
1274 :     # Read in subsets as a string.
1275 :     #
1276 :    
1277 :     while (<$fh>)
1278 :     {
1279 :     last if m,^//,;
1280 :    
1281 :     $self->{subsets_text} .= $_;
1282 :     }
1283 :    
1284 :     #
1285 :     # Read the spreadsheet.
1286 :     #
1287 :    
1288 :     while (<$fh>)
1289 :     {
1290 :     last if m,^//,;
1291 :    
1292 :     $self->{spreadsheet_text} .= $_;
1293 :    
1294 :     chomp;
1295 :    
1296 :     my($genome, $variant, @items) = split(/\t/, $_, $nroles + 2);
1297 :    
1298 :     push(@{$self->{genomes}}, $genome);
1299 :    
1300 :     my $gobj = GenomeObj->new($self, $fig, $genome, $variant, [@items]);
1301 :    
1302 :     $self->{genome_objs}->{$genome} = $gobj;
1303 :     }
1304 :    
1305 :     #
1306 :     # Read PEG info
1307 :     #
1308 :    
1309 :     while (<$fh>)
1310 :     {
1311 :     last if m,^//,;
1312 :    
1313 :     chomp;
1314 :    
1315 :     my ($peg, $aliases, $org, $func) = split(/\t/);
1316 :    
1317 :     push(@{$self->{pegs}}, [$peg, $aliases, $org, $func]);
1318 :     }
1319 :    
1320 :     #
1321 :     # Read sequence info
1322 :     #
1323 :    
1324 :     my($cur, $cur_peg);
1325 :    
1326 :     while (<$fh>)
1327 :     {
1328 :     if (/^>(fig\|\d+\.\d+\.peg\.\d+)/)
1329 :     {
1330 :     if ($cur)
1331 :     {
1332 :     $cur =~ s/\s+//gs;
1333 :     $self->{peg_seq}->{$cur_peg} = $cur;
1334 :     }
1335 :     $cur_peg = $1;
1336 :     $cur = '';
1337 :     }
1338 :     elsif (m,^//,)
1339 :     {
1340 :     $cur =~ s/\s+//gs;
1341 :     $self->{peg_seq}->{$cur_peg} = $cur;
1342 :     last;
1343 :     }
1344 :     else
1345 :     {
1346 :     $cur .= $_;
1347 :     }
1348 :     }
1349 :    
1350 :     #
1351 :     # Read notes as a string
1352 :     #
1353 :    
1354 :     while (<$fh>)
1355 :     {
1356 :     last if m,^//,;
1357 :    
1358 :     $self->{notes_txt} .= $_;
1359 :     }
1360 : olson 1.49
1361 :     #
1362 :     # Anything left here is reaction data.
1363 :     #
1364 :    
1365 :     my $reactions;
1366 :    
1367 :     while (<$fh>)
1368 :     {
1369 :     last if m,^//,;
1370 :    
1371 :     if (/^([^\t]+)\t([^\t]+)/)
1372 :     {
1373 :     $reactions .= $_;
1374 :     }
1375 :     }
1376 :    
1377 :     $self->{reactions} = $reactions if $reactions ne "";
1378 :    
1379 : olson 1.50 #
1380 :     # Additional sections. If $_ is //<something>, go ahead and process the blocks.
1381 :     #
1382 :     #
1383 :    
1384 :     my @blocks = ();
1385 :    
1386 :     if (m,^//(.*)$,)
1387 :     {
1388 :     chomp;
1389 : olson 1.51 my $cur_block;
1390 : olson 1.50 my $cur_tag = $1;
1391 :     while (<$fh>)
1392 :     {
1393 :     if (m,^//end$,)
1394 :     {
1395 : olson 1.51 push(@blocks, [$cur_tag, $cur_block]);
1396 : olson 1.50 }
1397 :     elsif (m,^//(.*)$,)
1398 :     {
1399 :     chomp;
1400 : olson 1.51 $cur_block = [];
1401 : olson 1.50 $cur_tag = $1;
1402 :     }
1403 :     else
1404 :     {
1405 : olson 1.51 push(@$cur_block, $_);
1406 : olson 1.50 }
1407 :     }
1408 :     }
1409 :     $self->{blocks} = \@blocks;
1410 : olson 1.33 }
1411 :    
1412 :     #
1413 : olson 1.39 # Compute or load from cache the PEG translations for this subsystem.
1414 :     #
1415 :     sub ensure_peg_translations
1416 :     {
1417 :     my($self) = @_;
1418 :    
1419 :     #
1420 :     # First we map the PEGs in this subsystem to PEGs in the
1421 :     # local SEED.
1422 :     #
1423 :     # translate_pegs requires a hash of peg->[aliases] as the first argument,
1424 :     # and a hash of peg->sequence as the second argument.
1425 :     #
1426 :    
1427 :     my $fig = $self->{fig};
1428 :    
1429 :     my %pegs;
1430 :     my %seqs_of;
1431 :    
1432 :     for my $pegent (@{$self->{pegs}})
1433 :     {
1434 :     my($peg, $aliases, $org, $func) = @$pegent;
1435 :     $pegs{$peg} = [$aliases, $org, $func];
1436 :     $seqs_of{$peg} = $self->{peg_seq}->{$peg};
1437 :     }
1438 :    
1439 :     sub show_cb
1440 :     {
1441 :     print "$_[0]<p>\n";
1442 :     }
1443 :    
1444 :     my $cached_translation_file = "$self->{qdir}/peg_translation";
1445 :    
1446 :     my $tran_peg;
1447 :    
1448 : olson 1.50 if ($self->{use_cache} and -f $cached_translation_file and -s $cached_translation_file > 0)
1449 : olson 1.39 {
1450 :     #
1451 :     # Read the cached translations.
1452 :     #
1453 :    
1454 :     if (open(my $fh, "<$cached_translation_file"))
1455 :     {
1456 :     warn "Reading cached peg translations\n";
1457 :     $tran_peg = {};
1458 :     while (<$fh>)
1459 :     {
1460 :     chomp;
1461 :     my($k, $v) = split(/\t/);
1462 :     $tran_peg->{$k} = $v;
1463 :     }
1464 :     close($fh);
1465 :     }
1466 :     }
1467 :    
1468 :     if (!$tran_peg)
1469 :     {
1470 :     $tran_peg = $fig->translate_pegs(\%pegs, \%seqs_of, \&show_cb);
1471 :    
1472 :     #
1473 :     # tran_peg is now a hash from subsystem_peg->local_peg
1474 :     #
1475 :    
1476 :     #
1477 :     # Write the translations out to a file in the queue directory
1478 :     # for use during installation.
1479 :     #
1480 :    
1481 : olson 1.50 if ($self->{use_cache} and open(my $fh, ">$self->{qdir}/peg_translation"))
1482 : olson 1.39 {
1483 :     for my $p (keys(%$tran_peg))
1484 :     {
1485 :     my $tp = $tran_peg->{$p};
1486 :     print $fh "$p\t$tp\n";
1487 :     }
1488 :     close($fh);
1489 :     }
1490 :     }
1491 :     $self->{tran_peg} = $tran_peg;
1492 :     return $tran_peg;
1493 :     }
1494 :    
1495 :     #
1496 : olson 1.33 # Analyze this subsystem for compatibility with this SEED install.
1497 :     #
1498 :     # Returns three lists:
1499 :     #
1500 :     # A major conflict list, consisting of tuples
1501 :     # [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs] where $ss_peg
1502 :     # is the peg in the subsystem being analyzied, and $ss_func is
1503 :     # its assigned function in that subsystem. $loc_peg is the peg
1504 :     # in the local SEED, and $loc_func its local assignment. $subs is
1505 :     # the list of pairs [$subsystem_name, $role] denoting the subsystem(s)
1506 :     # that $loc_peg particpates in.
1507 :     #
1508 :     # A conflict is flagged if the local function is different than
1509 :     # the one being imported, and if the local peg is in a subsystem.
1510 :     #
1511 :     # A minor conflict list, consisting of tuples [$ss_peg, $ss_func, $loc_peg, $loc_func].
1512 :     #
1513 :     #
1514 :     # The second list is a list of subsystem pegs that do not have
1515 :     # a local equivalent. Each entry is a triple
1516 :     # [peg, orgname, function].
1517 :     #
1518 :    
1519 :     sub analyze
1520 :     {
1521 :     my($self) = @_;
1522 :     my $fig = $self->{fig};
1523 :    
1524 : olson 1.39 my $tran_peg = $self->ensure_peg_translations();
1525 :    
1526 :     #
1527 :     # Now we walk the PEGs, determining a) which are missing
1528 :     # in the local SEED, and b) which have a conflicting assignment.
1529 :     #
1530 : olson 1.33 #
1531 : olson 1.39 # We also need to determine if this assignment will cause
1532 :     # pegs to be filled into subsystem roles that were not
1533 :     # otherwise going to be added.
1534 : olson 1.33 #
1535 : olson 1.39 # To enable this, we determine from the subsystem index
1536 :     # the list all roles that are present in subsystems on
1537 :     # this SEED.
1538 : olson 1.33 #
1539 :    
1540 : olson 1.39 my $sub_name = $self->name();
1541 :     my $subsystem_roles = $fig->subsystem_roles();
1542 :    
1543 :     my(@conflict, $minor_conflict, $missing);
1544 :    
1545 : olson 1.40 #
1546 :     # Hashes for accumulating aggregate counts of conflicts.
1547 :     #
1548 :    
1549 :     my(%subs_in, %subs_out, %roles_in, %roles_out);
1550 :    
1551 : olson 1.39 $missing = [];
1552 :    
1553 :     print "Determining conflicts...<p>\n";
1554 : olson 1.33
1555 :     for my $pegent (@{$self->{pegs}})
1556 :     {
1557 : olson 1.39 my($ss_peg, undef, $ss_org, $ss_func) = @$pegent;
1558 :    
1559 :     #
1560 :     # If this peg has a local translation, determine if
1561 :     # the associated assignment conflicts with a local assignment.
1562 :     #
1563 :     # One type of conflict occurs when the new assignment would cause
1564 :     # the peg to be removed from a subsystem. This occurs when the
1565 :     # new functional assignment is different from the current
1566 :     # assignment, and the peg is already in a subsystem.
1567 :     #
1568 :     # Another type of conflict occurs when the new assignment
1569 :     # for a peg matches a role that exists in a locally-installed
1570 :     # subsystem. This will cause the peg to be added to a
1571 :     # subsystem upon refill of that subsystem.
1572 :     #
1573 :     # It is possible for both the above conditions to hold,
1574 :     # in which case a peg would be moved out of one
1575 :     # subsystem into another.
1576 :     #
1577 :     # We denote these cases in the generated conflict list by
1578 :     # annotating the entry with the list of subsystems from which
1579 :     # the peg would be removed if the assignment were to be
1580 :     # accepted, and the list of subsystems to which the
1581 :     # peg would be added.
1582 :     #
1583 :    
1584 :     if (my $loc_peg = $tran_peg->{$ss_peg})
1585 :     {
1586 :     my $subs_removed = [];
1587 :     my $subs_added = [];
1588 :    
1589 :     #
1590 :     # Determine what our locally-assigned function is, and what
1591 :     # subsystem this peg appears in.
1592 :     #
1593 :    
1594 :     my $loc_func = $fig->function_of($loc_peg);
1595 :    
1596 :     #
1597 :     # If the functions don't match, it's a conflict.
1598 :     # If the local function is in a subsystem, it's a major
1599 :     # conflict. If it's not, it's a minor conflict.
1600 :     #
1601 :     # We actually let the major/minor determination be done by
1602 :     # the analysis display code, since the difference is only in whether
1603 :     # there are subsystems.
1604 :     #
1605 :    
1606 :     if ($loc_func ne $ss_func)
1607 :     {
1608 :    
1609 :     #
1610 :     # If the function defined in the new subsystem is different than
1611 :     # the current function, we mark a conflict. Along with the conflict
1612 :     # we include a list of the subsystems in which the local peg
1613 :     # is included.
1614 :     #
1615 :     # We use the subsystems_for_peg method to determine local subsystems
1616 :     # associated with a peg. It returns a list of pairs [subsystem, rolename].
1617 :     #
1618 :    
1619 :     #
1620 :     # What if we are loading a new version of an existing subsystem, and
1621 :     # a role name has changed?
1622 :     #
1623 :     # In this case, $loc_func ne $ss_func, $loc_peg will appear in the local copy of
1624 :     # the subsystem we are loading, and hence as a candidate for removal from that subsystem.
1625 :     # This may be thought of as a spurious message, and leads me to want to remove
1626 :     # such warnings (if I'm updating a subsystem, I can expect that the pegs in that
1627 :     # subsystem will change).
1628 :     #
1629 : olson 1.40 # subsystems_for_peg returns a list of pairs [subsystem, role].
1630 :     #
1631 :     # There might be somethign of a discrepancy here. This only
1632 :     # measures the subsystems the peg is actually currently part of, not
1633 :     # the number of subsystems that have a role corresponding to the peg's
1634 :     # current assignment.
1635 :     #
1636 : olson 1.39
1637 :     my @removed = $fig->subsystems_for_peg($loc_peg);
1638 : olson 1.40
1639 :     for my $r (@removed)
1640 :     {
1641 :     my($rsub, $rrole) = @$r;
1642 :    
1643 :     #
1644 :     # Skip the numbers for an existing subsystem.
1645 :     #
1646 :     next if $rsub eq $sub_name;
1647 :    
1648 :     $roles_out{$rrole}++;
1649 :     $subs_out{$rsub}++;
1650 :    
1651 :     push(@$subs_removed, $r);
1652 :     }
1653 : olson 1.39
1654 :     #
1655 :     # We also check to see if the new function is present
1656 :     # as a role in any local subsystem. If it is, then when that subsystem
1657 :     # is refilled, this peg will appear in it.
1658 :     #
1659 : olson 1.41 # $subsystem_roles is a hash keyed on role name with each value
1660 : olson 1.40 # a list of subsystem names.
1661 : olson 1.39 #
1662 :    
1663 :     if (my $loc_ss = $subsystem_roles->{$ss_func})
1664 :     {
1665 : olson 1.40 #
1666 :     # $loc_ss is the set of subsystems that have the new
1667 :     # function assignment as a role name.
1668 :     #
1669 : olson 1.41 my @subs = grep { $_ ne $sub_name} @$loc_ss;
1670 : olson 1.40
1671 : olson 1.41 if (@subs)
1672 :     {
1673 :     push(@$subs_added, @subs);
1674 :    
1675 :     map { $subs_in{$_}++ } @subs;
1676 :     $roles_in{$ss_func}++;
1677 :     }
1678 : olson 1.39 }
1679 :    
1680 :     push(@conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs_removed, $subs_added]);
1681 :     }
1682 :    
1683 :     }
1684 :     else
1685 :     {
1686 :     push(@$missing, [$ss_peg, $ss_org, $ss_func]);
1687 :     }
1688 :     }
1689 :    
1690 :     my $conflict = [sort { @{$b->[4]} + @{$b->[5]} <=> @{$a->[4]} + @{$a->[5]} } @conflict];
1691 :    
1692 : olson 1.40 my $aggreg = {
1693 :     roles_in => [keys(%roles_in)],
1694 :     roles_out => [keys(%roles_out)],
1695 :     subs_in => [keys(%subs_in)],
1696 :     subs_out => [keys(%subs_out)],
1697 :     };
1698 :    
1699 :     return ($conflict, $missing, $aggreg);
1700 : olson 1.39 }
1701 :    
1702 :     sub read_cached_analysis
1703 :     {
1704 :     my($self) = @_;
1705 :    
1706 :     my $cfile = "$self->{qdir}/conflicts";
1707 :     my $mfile = "$self->{qdir}/missing";
1708 :    
1709 :     my($conflict, $missing);
1710 :     $conflict = [];
1711 :     $missing = [];
1712 :    
1713 :     if (open(my $fh, "<$cfile"))
1714 :     {
1715 :    
1716 :     while (<$fh>)
1717 :     {
1718 :     chomp;
1719 :    
1720 :     my($ss_peg, $ss_func, $loc_peg, $loc_func) = split(/\t/);
1721 :    
1722 :     my $subs_removed = <$fh>;
1723 :     my $subs_added = <$fh>;
1724 :    
1725 :     chomp($subs_removed);
1726 :     chomp($subs_added);
1727 :    
1728 :     my @subs_removed_raw = split(/\t/, $subs_removed);
1729 :     my $subs_added_list = [split(/\t/, $subs_added)];
1730 :    
1731 :     my $subs_removed_list = [];
1732 :    
1733 :     while (@subs_removed_raw)
1734 :     {
1735 :     my($v1, $v2, @rest) = @subs_removed_raw;
1736 :     @subs_removed_raw = @rest;
1737 :     push(@$subs_removed_list, [$v1, $v2]);
1738 :     }
1739 :    
1740 :     push(@$conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs_removed_list, $subs_added_list]);
1741 :     }
1742 :     }
1743 :    
1744 :     if (open(my $fh, "<$mfile"))
1745 :     {
1746 :    
1747 :     while (<$fh>)
1748 :     {
1749 :     chomp;
1750 :    
1751 :     my(@a) = split(/\t/);
1752 :     push(@$missing, [@a]);
1753 :     }
1754 :     }
1755 :    
1756 :     return($conflict, $missing);
1757 :     }
1758 :    
1759 :     #
1760 :     # Install this subsystem.
1761 :     #
1762 : olson 1.42 # $dont_assign is a list of subsytem PEGs that should not have their assignments overwritten.
1763 : olson 1.39 #
1764 :     # We return a list of for-the-installer messages that will be presented when the install completes.
1765 :     #
1766 : olson 1.50 # If $assignments_file is set, assignments will be written to that file
1767 :     # instead of being installed.
1768 :     #
1769 : olson 1.39 sub install
1770 :     {
1771 : olson 1.50 my($self, $dont_assign, $imported_from, $assignments_file) = @_;
1772 : olson 1.39
1773 :     my @messages;
1774 :    
1775 :     my $fig = $self->{fig};
1776 :     my $subsystems_dir = "$FIG_Config::data/Subsystems";
1777 :    
1778 :     my $sub_name = $self->name();
1779 :     $sub_name =~ s/ /_/g;
1780 :     my $sub_dir = "$subsystems_dir/$sub_name";
1781 :     my $ver = $self->version();
1782 :    
1783 :     #
1784 :     # First check to see if we already have this subsystem installed.
1785 :     #
1786 :    
1787 :     if (-d $sub_dir and (my $cur_ver = $fig->subsystem_version($sub_name)) >= $ver)
1788 :     {
1789 :     warn "Not importing $sub_name: current version $cur_ver >= imported version $ver";
1790 : olson 1.50 push(@messages, "Not importing $sub_name: current version $cur_ver >= imported version $ver\n");
1791 :     return @messages;
1792 : olson 1.39 }
1793 :    
1794 :     warn "Importing $sub_name version $ver\n";
1795 :     push(@messages, "Importing $sub_name version $ver\n");
1796 :    
1797 :     if (! -d $sub_dir)
1798 :     {
1799 :     mkdir($sub_dir, 0777) or die "Cannot mkdir $sub_dir: $!";
1800 : olson 1.33 }
1801 :    
1802 : olson 1.39 #
1803 :     # Write the header/meta information.
1804 :     #
1805 :    
1806 :     my $fh;
1807 : olson 1.50 $imported_from = "???" unless $imported_from ne '';
1808 : olson 1.39
1809 :     open($fh, ">$sub_dir/VERSION") or die "Cannot open $sub_dir/VERSION for writing: $!";
1810 :     print $fh "$ver\n";
1811 :     close($fh);
1812 :     chmod(0666, "$sub_dir/VERSION");
1813 :    
1814 :     open($fh, ">$sub_dir/EXCHANGABLE") or die "Cannot open $sub_dir/EXCHANGABLE for writing: $!";
1815 :     print $fh $self->exchangable() . "\n";
1816 :     close($fh);
1817 :     chmod(0666, "$sub_dir/EXCHANGABLE");
1818 :    
1819 :     open($fh, ">$sub_dir/curation.log") or die "Cannot open $sub_dir/curation.logt for writing: $!";
1820 :     print $fh "$self->{curation_log}\n";
1821 :     my $time = time;
1822 :     print $fh "$time\t$imported_from\timported_from\n";
1823 :     close($fh);
1824 :     chmod(0666, "$sub_dir/curation.log");
1825 :    
1826 :     open($fh, ">$sub_dir/notes") or die "Cannot open $sub_dir/notes for writing: $!";
1827 :     print $fh $self->{notes_txt} . "\n";
1828 :     close($fh);
1829 :     chmod(0666, "$sub_dir/notes");
1830 : olson 1.49
1831 :     if ($self->{reactions})
1832 :     {
1833 :     open($fh, ">$sub_dir/reactions") or die "Cannot open $sub_dir/reactions for writing: $!";
1834 :     print $fh $self->{reactions} . "\n";
1835 :     close($fh);
1836 :     chmod(0666, "$sub_dir/reactions");
1837 :     }
1838 : olson 1.39
1839 :     my $tran_peg = $self->ensure_peg_translations();
1840 : olson 1.33
1841 :     #
1842 : olson 1.39 # We can start writing the spreadsheet.
1843 : olson 1.33 #
1844 :    
1845 : olson 1.39 my $ssa_fh;
1846 :     open($ssa_fh, ">$sub_dir/spreadsheet") or die "Cannot open $sub_dir/spreadsheet for writing: $!";
1847 :    
1848 : olson 1.33 #
1849 : olson 1.39 # Start with the roles and subsets.
1850 : olson 1.33 #
1851 : olson 1.39
1852 :     print $ssa_fh $self->{role_text};
1853 :     print $ssa_fh "//\n";
1854 :    
1855 :     print $ssa_fh $self->{subsets_text};
1856 :     print $ssa_fh "//\n";
1857 : olson 1.33
1858 : olson 1.39 for my $g (@{$self->{genomes}})
1859 : olson 1.33 {
1860 : olson 1.39 my $gobj = $self->{genome_objs}->{$g};
1861 :     my ($trans_genome, @row) = $gobj->translate($tran_peg);
1862 :    
1863 :     if ($trans_genome)
1864 : olson 1.33 {
1865 : olson 1.39 print $ssa_fh join("\t", $trans_genome, $gobj->{variant}, @row), "\n";
1866 : olson 1.33 }
1867 :     }
1868 : olson 1.39
1869 :     close($ssa_fh);
1870 :    
1871 :     #
1872 : olson 1.50 # The subsystem itself is now in place. Depending on how we were
1873 :     # invoked, write the assignments to a file, or install them on
1874 :     # the system.
1875 :     #
1876 : olson 1.58 # If dont_assign is not a list but is true, save no assignments at all.
1877 :     #
1878 :     if (!ref($dont_assign) and $dont_assign)
1879 :     {
1880 :     # Skip assignments
1881 :     }
1882 :     elsif (defined($assignments_file))
1883 : olson 1.50 {
1884 :     $self->write_assignments_to_file(\@messages, $tran_peg, $assignments_file);
1885 :     }
1886 :     else
1887 :     {
1888 :     $self->install_assignments(\@messages, $tran_peg, $dont_assign);
1889 :     }
1890 :    
1891 :     $self->install_blocks(\@messages, $sub_dir);
1892 :    
1893 :     return @messages;
1894 :     }
1895 :    
1896 :     #
1897 :     # Install any other block-data code that's in the package. Right now this is just
1898 :     # the diagrams.
1899 :     #
1900 :     sub install_blocks
1901 :     {
1902 :     my($self, $messages, $sub_dir) = @_;
1903 :     my $fig = $self->{fig};
1904 :    
1905 :     #
1906 :     # At this point, the rest of the subsystem is written to disk. We can
1907 :     # use the Subsys.pm mechanism to write this stuff out.
1908 :     #
1909 :    
1910 :     my $sub = $fig->get_subsystem($self->name());
1911 :    
1912 :     for my $block (@{$self->{blocks}})
1913 :     {
1914 :     my($block_hdr, $block_data) = @$block;
1915 :    
1916 :     if ($block_hdr =~ /^diagram:([^:]+):name\t(\S+)/)
1917 :     {
1918 :     #
1919 :     # The diagram output format ensures this is the first block, so just
1920 :     # create the diagram.
1921 :     #
1922 :    
1923 :     my $diagram_id = $1;
1924 :     my $diagram_name = $2;
1925 :    
1926 : olson 1.51 $sub->create_new_diagram(undef, undef, $diagram_name, $diagram_id);
1927 : olson 1.50 }
1928 :     elsif ($block_hdr =~ m,^diagram:([^:]+):diagram=([^\s/]+)\t(\d+),)
1929 :     {
1930 :     my $diagram_id = $1;
1931 :     my $img_file = $2;
1932 :     my $size = $3;
1933 :    
1934 :     my $ddir = "$sub_dir/diagrams/$diagram_id";
1935 :    
1936 :     if (! -d $ddir)
1937 :     {
1938 :     push(@$messages, "Invalid diagrams: diagram directory for $diagram_id did not exist while parsing diagram file\n");
1939 :     next;
1940 :     }
1941 :    
1942 :     if (!open(FH, ">$ddir/$img_file"))
1943 :     {
1944 :     push(@$messages, "Cannot open image file $ddir/$img_file for writing: $!\n");
1945 :     next;
1946 :     }
1947 :    
1948 :     for my $line (@$block_data)
1949 :     {
1950 :     $line =~ s/^B://;
1951 :     my $dec = decode_base64($line);
1952 :     print FH $dec;
1953 :     }
1954 :     close(FH);
1955 :    
1956 :     my $fsize = -s "$ddir/$img_file";
1957 :     if ($fsize != $size)
1958 :     {
1959 :     push(@$messages, "Diagram image $img_file size $fsize does not match declared size $size\n");
1960 :     warn "Diagram image $img_file size $fsize does not match declared size $size";
1961 :     }
1962 :     }
1963 :     }
1964 :     }
1965 :    
1966 :    
1967 :    
1968 :     sub write_assignments_to_file
1969 :     {
1970 :     my($self, $messages, $tran_peg, $filename) = @_;
1971 :     my $fig = $self->{fig};
1972 :    
1973 :     my $fh;
1974 :    
1975 :     for my $pegent (@{$self->{pegs}})
1976 :     {
1977 :     my($peg, $aliases, $org, $func) = @$pegent;
1978 :     my $tpeg = $tran_peg->{$peg};
1979 :    
1980 :     if (!$tpeg)
1981 :     {
1982 :     warn "Couldn't translate $peg (from $org)\n";
1983 :     push(@$messages, "Couldn't translate $peg (from $org)");
1984 :     next;
1985 :     }
1986 :    
1987 :     my $old = $fig->function_of($tpeg);
1988 :    
1989 :     if ($old ne $func)
1990 :     {
1991 :     #
1992 :     # Only open the file if we have assignments to write.
1993 :     #
1994 :    
1995 :     if (!defined($fh))
1996 :     {
1997 :     open($fh, ">$filename") or confess "Error opening $filename for writing: $!";
1998 :     }
1999 :     print $fh "$tpeg\t$func\n";
2000 :     }
2001 :     }
2002 :     if (defined($fh))
2003 :     {
2004 :     close($fh);
2005 :     }
2006 :     }
2007 :    
2008 :    
2009 :     sub install_assignments
2010 :     {
2011 :     my($self, $messages, $tran_peg, $dont_assign) = @_;
2012 :     my $fig = $self->{fig};
2013 :    
2014 : olson 1.33 #
2015 : olson 1.39 # Enter the new assignments, saving the old assignments in the spool dir.
2016 : olson 1.33 #
2017 :    
2018 : olson 1.39 my $now = time;
2019 :    
2020 :     my $old_funcs_fh;
2021 :     open($old_funcs_fh, ">>$self->{qdir}/old_assignments.$now");
2022 :    
2023 :     my $curator = $self->curator();
2024 : olson 1.33
2025 : olson 1.42 my %dont_assign;
2026 :    
2027 :     map { $dont_assign{$_}++ } @$dont_assign;
2028 :    
2029 : olson 1.33 for my $pegent (@{$self->{pegs}})
2030 :     {
2031 : olson 1.39 my($peg, $aliases, $org, $func) = @$pegent;
2032 :     my $tpeg = $tran_peg->{$peg};
2033 :    
2034 :     if (!$tpeg)
2035 :     {
2036 :     warn "Couldn't translate $peg (from $org)\n";
2037 : olson 1.50 push(@$messages, "Couldn't translate $peg (from $org)");
2038 : olson 1.39 next;
2039 :     }
2040 :    
2041 : olson 1.42 if ($dont_assign{$peg})
2042 :     {
2043 :     warn "Skipping assignment of $peg ($tpeg locally)\n";
2044 :     next;
2045 :     }
2046 :    
2047 : olson 1.39 my $old = $fig->function_of($tpeg);
2048 :    
2049 :     if ($old ne $func)
2050 : olson 1.33 {
2051 : olson 1.39 print $old_funcs_fh "$tpeg\t$old\t$curator\t$func\n";
2052 :     $fig->add_annotation($tpeg, $curator,
2053 :     "Assigning function $func based on installation of subsystem $self->{name}");
2054 : olson 1.33
2055 : olson 1.39 if ($curator =~ /master:(.*)/)
2056 :     {
2057 :     my $user = $1;
2058 :     $fig->assign_function($tpeg, "master", $func, "");
2059 : olson 1.48 $fig->add_annotation($tpeg, $user, "Set master function to\n$func\n");
2060 : olson 1.39 }
2061 :     else
2062 : olson 1.33 {
2063 : olson 1.39 $fig->assign_function($tpeg, $curator, $func, "");
2064 : olson 1.48 $fig->add_annotation($tpeg, $curator, "Set function to\n$func\n");
2065 : olson 1.33 }
2066 :     }
2067 :     else
2068 :     {
2069 : olson 1.39 # print "$tpeg already has assignment $func\n";
2070 : olson 1.33 }
2071 :     }
2072 : olson 1.39 close($old_funcs_fh);
2073 : olson 1.33 }
2074 :    
2075 : olson 1.41 #
2076 :     # Read the aggregate analysis results.
2077 :     #
2078 :    
2079 :     sub aggregate_analysis
2080 :     {
2081 :     my($self) = @_;
2082 :    
2083 :     if (open(my $fh, "<$self->{qdir}/aggregate"))
2084 :     {
2085 :     local($/);
2086 :     my $txt = <$fh>;
2087 :     close($fh);
2088 :    
2089 :     my $VAR1; # For the Dumper'd data.
2090 :    
2091 :     my $compartment = new Safe;
2092 :     my $aggr = $compartment->reval($txt);
2093 :     if ($@)
2094 :     {
2095 :     warn "aggregate_analysis: error parsing saved data: $@";
2096 :     return undef;
2097 :     }
2098 :     return $aggr;
2099 :     }
2100 :     else
2101 :     {
2102 :     return undef;
2103 :     }
2104 :     }
2105 :    
2106 : olson 1.33 sub name
2107 :     {
2108 :     my($self) = @_;
2109 :     return $self->{name};
2110 :     }
2111 :    
2112 :    
2113 :     sub version
2114 :     {
2115 :     my($self) = @_;
2116 :     return $self->{version};
2117 :     }
2118 :    
2119 :     sub exchangable
2120 :     {
2121 :     my($self) = @_;
2122 :     return $self->{exchangable};
2123 :     }
2124 :    
2125 :     sub curator
2126 :     {
2127 :     my($self) = @_;
2128 :     return $self->{curator};
2129 :     }
2130 :    
2131 : olson 1.39 sub analysis_complete
2132 :     {
2133 :     my($self) = @_;
2134 :    
2135 :     return -f "$self->{qdir}/analysis_complete";
2136 :     }
2137 :    
2138 :     sub analysis_jobid
2139 :     {
2140 :     my($self) = @_;
2141 :    
2142 :     my $jid_file = "$self->{qdir}/analysis_jobid";
2143 :    
2144 :     return &FIG::file_head($jid_file, 1);
2145 :     }
2146 :    
2147 : olson 1.33 package GenomeObj;
2148 :    
2149 : olson 1.39 use strict;
2150 :     use Data::Dumper;
2151 :    
2152 :     #
2153 :     # A genomeobj is a small datatype that holds the data in a row of a
2154 :     # spreadsheet file.
2155 :     #
2156 :    
2157 : olson 1.33 sub new
2158 :     {
2159 :     my($class, $subfile, $fig, $genome, $variant, $items) = @_;
2160 :    
2161 :     my $self = {
2162 :     fig => $fig,
2163 :     subfile => $subfile,
2164 :     genome => $genome,
2165 :     variant => $variant,
2166 :     items => $items,
2167 :     };
2168 :     return bless($self, $class);
2169 :    
2170 :     }
2171 :    
2172 : olson 1.39 #
2173 :     # Translate this row to a new context.
2174 :     #
2175 :     # $trans_peg is a hash mapping from spreadsheet PEG to local PEG
2176 :     #
2177 :     sub translate
2178 :     {
2179 :     my($self, $trans_peg) = @_;
2180 :     my $fig = $self->{fig};
2181 :    
2182 :     my $genome = $self->{genome};
2183 :    
2184 :     my $parsed_items = [];
2185 :     $self->{parsed_items} = $parsed_items;
2186 :     my $trans_items = [];
2187 :     $self->{trans_items} = $trans_items;
2188 :    
2189 :     #
2190 :     # Hash of genomes seen in this row.
2191 :     my %genomes;
2192 :    
2193 :     for my $item (@{$self->{items}})
2194 :     {
2195 :     my $l = [ map { $_ eq '' ? undef : "fig|$genome.peg.$_" } split(/,/, $item) ];
2196 :     my $t = [ map { $trans_peg->{$_} } @$l ];
2197 :    
2198 :     push(@$parsed_items, $l);
2199 :     push(@$trans_items, $t);
2200 :    
2201 :     #
2202 :     # Count the genomes that are seen in the translated pegs.
2203 :     #
2204 :    
2205 :     for my $tpeg (@$t)
2206 :     {
2207 :     my $tg = $fig->genome_of($tpeg);
2208 :     $genomes{$tg}++ if $tg ne "";
2209 :     }
2210 :    
2211 :     }
2212 :    
2213 :     #
2214 :     # Now determine the dominant organism for this translated row.
2215 :     #
2216 :    
2217 :     my @orgs = sort { $genomes{$b} <=> $genomes{$a} } keys(%genomes);
2218 :    
2219 :     # print "@{$self->{items}}\n";
2220 :     # print join(" ", map { "$_: $genomes{$_} " } @orgs ), "\n";
2221 :    
2222 :     unless (@orgs == 1 # Single organism
2223 :     or
2224 :     (@orgs > 1 and $genomes{$orgs[0]} > (2 * $genomes{$orgs[1]})) # First org has > 2x the second org
2225 :     )
2226 :     {
2227 :     warn "Could not determine translation for $genome\n";
2228 :     return undef;
2229 :     }
2230 :    
2231 :     #
2232 :     # The dominant organism is the first in the list.
2233 :     #
2234 :    
2235 :     my $dom = $orgs[0];
2236 :    
2237 :     #
2238 :     # Run through the translated pegs, and remove the ones that are
2239 :     # not in the dominant organism.
2240 :     #
2241 :    
2242 :     my @res;
2243 :     for my $i (0..@$trans_items - 1)
2244 :     {
2245 :     my $t = $trans_items->[$i];
2246 :    
2247 :     my @nt;
2248 :     for my $peg (@$t)
2249 :     {
2250 :     if ($peg =~ /^fig\|(\d+\.\d+)\.peg\.(\d+)$/)
2251 :     {
2252 :     if ($1 eq $dom)
2253 :     {
2254 :     push(@nt, $2);
2255 :     }
2256 :     }
2257 :     }
2258 :     push(@res, join(",", @nt));
2259 :     }
2260 :     return $dom, @res;
2261 :     }
2262 : efrank 1.1 1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3