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

Annotation of /FigKernelPackages/P2Pupdate.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (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.30 my($fig,$file,$just_exchangable) = @_;
965 : overbeek 1.23 my($ssa);
966 :    
967 : overbeek 1.27 $just_exchangable = defined($just_exchangable) ? $just_exchangable : 1;
968 :     my @exchangable = grep { (! $just_exchangable) || $fig->is_exchangable_subsystem($_) }
969 :     $fig->all_subsystems;
970 :    
971 : overbeek 1.23 my $fig = new FIG;
972 : overbeek 1.24 if ((@exchangable > 0) && open(SUB,">$file"))
973 : overbeek 1.23 {
974 : overbeek 1.24 foreach $ssa (@exchangable)
975 : overbeek 1.23 {
976 : overbeek 1.25 # print STDERR "writing $ssa to $file\n";
977 :     my($spreadsheet,$notes) = $fig->exportable_subsystem($ssa);
978 : overbeek 1.23 print SUB join("",@$spreadsheet),join("",@$notes),"########################\n";
979 :     }
980 :     close(SUB);
981 :     }
982 : overbeek 1.25 else
983 :     {
984 :     # print STDERR &Dumper(\@exchangable,$file);
985 :     }
986 : overbeek 1.23 }
987 :    
988 : overbeek 1.30 sub install_subsystems {
989 :     my($fig,$package) = @_;
990 :    
991 : disz 1.31 &FIG::run("$FIG_Config::bin/import_subsystems master last_release < $package");
992 : overbeek 1.30 }
993 :    
994 : olson 1.37
995 :     =pod
996 :    
997 :     =head2 unpack_packaged_subsystem
998 :    
999 :     Unpack a packaged subsystem (from the clearinghouse or a p2p transfer)
1000 :     into a directory; this will create a directory named as the subsystem
1001 :     and formatted like the standard subsystem directories, as well as a
1002 :     file of assignments and a file of sequences in fasta format.
1003 :    
1004 :     Returns the name of the subsystem.
1005 :    
1006 :     =cut
1007 :    
1008 :     sub unpack_packaged_subsystem
1009 :     {
1010 :     my($fig, $file, $target_dir) = @_;
1011 :    
1012 :     my $user = $fig->get_user();
1013 :    
1014 :     &FIG::verify_dir($target_dir);
1015 :    
1016 :     my $fh;
1017 :    
1018 :     if (!open($fh, "<$file"))
1019 :     {
1020 :     warn "unpack_packaged_subsystem: cannot open $file: $!";
1021 :     return undef;
1022 :     }
1023 :    
1024 :     #
1025 :     # We scan the file, breaking it up into sections and writing
1026 :     # to the appropriate places.
1027 :     #
1028 :     # First the header.
1029 :     #
1030 :    
1031 :     local $/ = "\n//\n";
1032 :    
1033 :     my $header = <$fh>;
1034 :     chomp $header;
1035 :    
1036 :     my ($name, $version, $exchangable, $curation) = split(/\n/, $header);
1037 :    
1038 :     print "Importing name=$name version=$version exch=$exchangable curation='$curation'\n";
1039 :    
1040 :     #
1041 :     # Pull in roles, subsets, and spreadsheet. These will be written to the new
1042 :     # spreadsheet file.
1043 :     #
1044 :    
1045 :     my $roles = <$fh>;
1046 :     chomp $roles;
1047 :    
1048 :     my $subsets = <$fh>;
1049 :     chomp $subsets;
1050 :    
1051 :     my $spreadsheet = <$fh>;
1052 :     chomp $spreadsheet;
1053 :    
1054 :    
1055 :     #
1056 :     # Pull the assignments and sequences. These go to their own files.
1057 :     #
1058 :    
1059 :     my $assignments = <$fh>;
1060 :     chomp $assignments;
1061 :    
1062 :     my $sequences = <$fh>;
1063 :     chomp $sequences;
1064 :    
1065 :     #
1066 :     # And the notes; these will be written to the subsystem dir.
1067 :     #
1068 :    
1069 :     my $notes = <$fh>;
1070 :     chomp $notes;
1071 :    
1072 :     close($fh);
1073 :    
1074 :     #
1075 :     # Everything is read. Now to write it all back out again.
1076 :     #
1077 :    
1078 :     #
1079 :     # First the subsystem.
1080 :     #
1081 :    
1082 :     my $ss_path = "$target_dir/subsystem";
1083 :     &FIG::verify_dir($ss_path);
1084 :    
1085 :     open($fh, ">$ss_path/EXCHANGABLE");
1086 :     print $fh "$exchangable\n";
1087 :     close($fh);
1088 :    
1089 :     open($fh, ">$ss_path/VERSION");
1090 :     print $fh "$version\n";
1091 :     close($fh);
1092 :    
1093 :     open($fh, ">$ss_path/curation.log");
1094 :     print $fh "$curation\n";
1095 :     my $now = time;
1096 :     print $fh "$now\t$user\timported\n";
1097 :     close($fh);
1098 :    
1099 :     open($fh, ">$ss_path/notes");
1100 :     print $fh "$notes\n";
1101 :     close($fh);
1102 :    
1103 :     open($fh, ">$ss_path/spreadsheet");
1104 :     print $fh "$roles\n";
1105 :     print $fh "//\n";
1106 :     print $fh "$subsets\n";
1107 :     print $fh "//\n";
1108 :     print $fh "$spreadsheet\n";
1109 :     close($fh);
1110 :    
1111 :     open($fh, ">$target_dir/subsystem_name");
1112 :     print $fh "$name\n";
1113 :     close($fh);
1114 :    
1115 :     open($fh, ">$target_dir/assignments");
1116 :     print $fh "$assignments\n";
1117 :     close($fh);
1118 :    
1119 :     open($fh, ">$target_dir/seqs.fasta");
1120 :     print $fh "$sequences\n";
1121 :     close($fh);
1122 :    
1123 :     return $name;
1124 :     }
1125 :    
1126 : olson 1.33 package SubsystemFile;
1127 :    
1128 :     use Data::Dumper;
1129 :     use strict;
1130 : olson 1.50 use Carp;
1131 :     use MIME::Base64;
1132 :    
1133 : olson 1.33 sub new
1134 :     {
1135 :     my($class, $qdir, $file, $fig) = @_;
1136 :     my(@info);
1137 :    
1138 : olson 1.50 my $use_cache = defined($qdir);
1139 :    
1140 : olson 1.33 @info = FIG::file_head($file, 4);
1141 :     if (!@info)
1142 :     {
1143 :     warn "Cannot open $file\n";
1144 :     return undef;
1145 :     }
1146 :    
1147 :     chomp(@info);
1148 :    
1149 :     my $name = $info[0];
1150 :     my $version = $info[1];
1151 :     my $exc = $info[2];
1152 :    
1153 :     my @c = split(/\t/, $info[3]);
1154 :    
1155 :     my $curator = $c[1];
1156 :    
1157 :     my $self = {
1158 :     qdir => $qdir,
1159 : olson 1.50 use_cache => $use_cache,
1160 : olson 1.33 file => $file,
1161 :     name => $name,
1162 :     version => $version,
1163 :     exchangable => $exc,
1164 :     curator => $curator,
1165 : olson 1.39 curation_log => $info[3],
1166 : olson 1.33 fig => $fig,
1167 :     };
1168 :    
1169 :     return bless($self, $class);
1170 :    
1171 :     }
1172 :    
1173 :     #
1174 :     # Load the export file into internal data structures.
1175 :     #
1176 :     # It's structured as
1177 :     #
1178 :     # name
1179 :     # version
1180 :     # exchangable
1181 :     # creation date <tab> curator <tab> "started"
1182 :     # //
1183 :     # roles
1184 :     # //
1185 :     # subsets
1186 :     # //
1187 :     # spreadsheet
1188 :     # //
1189 :     # assignments
1190 :     # //
1191 :     # sequences
1192 :     # //
1193 :     # notes
1194 :     # //
1195 : olson 1.49 # reactions
1196 : olson 1.33 #
1197 :     # Subsections:
1198 :     #
1199 :     # roles:
1200 :     #
1201 :     # abbr <tab> role-name
1202 :     #
1203 :     # subsets has meaning to the acutal subsystems, but we'll use it as a string.
1204 :     #
1205 :     # spreadsheet:
1206 :     #
1207 :     # genome <tab> variant <tab> items
1208 :     #
1209 :     # Where items is tab-separated columns, each of which is comma-separated peg number in the genome
1210 :     #
1211 :     # assignments:
1212 :     #
1213 :     # fid <tab> aliases <tab> organism <tab> function
1214 :     #
1215 :     # sequences:
1216 :     #
1217 :     # list of fasta's
1218 :     #
1219 :     # notes:
1220 :     #
1221 :     # plain text
1222 :     #
1223 :     sub load
1224 :     {
1225 :     my($self) = @_;
1226 :    
1227 :     my $fig = $self->{fig};
1228 :    
1229 :     my($fh);
1230 :    
1231 :     open($fh, "<$self->{file}") or die "Cannot open $self->{file}: $!\n";
1232 :    
1233 :     #
1234 : olson 1.39 # Skip intro section - we already read this information in the constructor.
1235 : olson 1.33 #
1236 :    
1237 :     while (<$fh>)
1238 :     {
1239 :     chomp;
1240 :     last if m,^//,;
1241 :     }
1242 :    
1243 :     #
1244 :     # Read the roles.
1245 :     #
1246 :    
1247 :    
1248 :     my $nroles;
1249 :    
1250 :     while (<$fh>)
1251 :     {
1252 :     last if m,^//,;
1253 :    
1254 :     $self->{role_text} .= $_;
1255 :     chomp $_;
1256 :    
1257 :     my($abbr, $role) = split(/\t/);
1258 :    
1259 :     warn "Have role $role\n";
1260 :    
1261 :     push(@{$self->{roles}}, $role);
1262 :     push(@{$self->{abbrs}}, $abbr);
1263 :    
1264 :     $nroles++;
1265 :     }
1266 :    
1267 :     #
1268 :     # Read in subsets as a string.
1269 :     #
1270 :    
1271 :     while (<$fh>)
1272 :     {
1273 :     last if m,^//,;
1274 :    
1275 :     $self->{subsets_text} .= $_;
1276 :     }
1277 :    
1278 :     #
1279 :     # Read the spreadsheet.
1280 :     #
1281 :    
1282 :     while (<$fh>)
1283 :     {
1284 :     last if m,^//,;
1285 :    
1286 :     $self->{spreadsheet_text} .= $_;
1287 :    
1288 :     chomp;
1289 :    
1290 :     my($genome, $variant, @items) = split(/\t/, $_, $nroles + 2);
1291 :    
1292 :     push(@{$self->{genomes}}, $genome);
1293 :    
1294 :     my $gobj = GenomeObj->new($self, $fig, $genome, $variant, [@items]);
1295 :    
1296 :     $self->{genome_objs}->{$genome} = $gobj;
1297 :     }
1298 :    
1299 :     #
1300 :     # Read PEG info
1301 :     #
1302 :    
1303 :     while (<$fh>)
1304 :     {
1305 :     last if m,^//,;
1306 :    
1307 :     chomp;
1308 :    
1309 :     my ($peg, $aliases, $org, $func) = split(/\t/);
1310 :    
1311 :     push(@{$self->{pegs}}, [$peg, $aliases, $org, $func]);
1312 :     }
1313 :    
1314 :     #
1315 :     # Read sequence info
1316 :     #
1317 :    
1318 :     my($cur, $cur_peg);
1319 :    
1320 :     while (<$fh>)
1321 :     {
1322 :     if (/^>(fig\|\d+\.\d+\.peg\.\d+)/)
1323 :     {
1324 :     if ($cur)
1325 :     {
1326 :     $cur =~ s/\s+//gs;
1327 :     $self->{peg_seq}->{$cur_peg} = $cur;
1328 :     }
1329 :     $cur_peg = $1;
1330 :     $cur = '';
1331 :     }
1332 :     elsif (m,^//,)
1333 :     {
1334 :     $cur =~ s/\s+//gs;
1335 :     $self->{peg_seq}->{$cur_peg} = $cur;
1336 :     last;
1337 :     }
1338 :     else
1339 :     {
1340 :     $cur .= $_;
1341 :     }
1342 :     }
1343 :    
1344 :     #
1345 :     # Read notes as a string
1346 :     #
1347 :    
1348 :     while (<$fh>)
1349 :     {
1350 :     last if m,^//,;
1351 :    
1352 :     $self->{notes_txt} .= $_;
1353 :     }
1354 : olson 1.49
1355 :     #
1356 :     # Anything left here is reaction data.
1357 :     #
1358 :    
1359 :     my $reactions;
1360 :    
1361 :     while (<$fh>)
1362 :     {
1363 :     last if m,^//,;
1364 :    
1365 :     if (/^([^\t]+)\t([^\t]+)/)
1366 :     {
1367 :     $reactions .= $_;
1368 :     }
1369 :     }
1370 :    
1371 :     $self->{reactions} = $reactions if $reactions ne "";
1372 :    
1373 : olson 1.50 #
1374 :     # Additional sections. If $_ is //<something>, go ahead and process the blocks.
1375 :     #
1376 :     #
1377 :    
1378 :     my @blocks = ();
1379 :    
1380 :     if (m,^//(.*)$,)
1381 :     {
1382 :     chomp;
1383 : olson 1.51 my $cur_block;
1384 : olson 1.50 my $cur_tag = $1;
1385 :     while (<$fh>)
1386 :     {
1387 :     if (m,^//end$,)
1388 :     {
1389 : olson 1.51 push(@blocks, [$cur_tag, $cur_block]);
1390 : olson 1.50 }
1391 :     elsif (m,^//(.*)$,)
1392 :     {
1393 :     chomp;
1394 : olson 1.51 $cur_block = [];
1395 : olson 1.50 $cur_tag = $1;
1396 :     }
1397 :     else
1398 :     {
1399 : olson 1.51 push(@$cur_block, $_);
1400 : olson 1.50 }
1401 :     }
1402 :     }
1403 :     $self->{blocks} = \@blocks;
1404 : olson 1.33 }
1405 :    
1406 :     #
1407 : olson 1.39 # Compute or load from cache the PEG translations for this subsystem.
1408 :     #
1409 :     sub ensure_peg_translations
1410 :     {
1411 :     my($self) = @_;
1412 :    
1413 :     #
1414 :     # First we map the PEGs in this subsystem to PEGs in the
1415 :     # local SEED.
1416 :     #
1417 :     # translate_pegs requires a hash of peg->[aliases] as the first argument,
1418 :     # and a hash of peg->sequence as the second argument.
1419 :     #
1420 :    
1421 :     my $fig = $self->{fig};
1422 :    
1423 :     my %pegs;
1424 :     my %seqs_of;
1425 :    
1426 :     for my $pegent (@{$self->{pegs}})
1427 :     {
1428 :     my($peg, $aliases, $org, $func) = @$pegent;
1429 :     $pegs{$peg} = [$aliases, $org, $func];
1430 :     $seqs_of{$peg} = $self->{peg_seq}->{$peg};
1431 :     }
1432 :    
1433 :     sub show_cb
1434 :     {
1435 :     print "$_[0]<p>\n";
1436 :     }
1437 :    
1438 :     my $cached_translation_file = "$self->{qdir}/peg_translation";
1439 :    
1440 :     my $tran_peg;
1441 :    
1442 : olson 1.50 if ($self->{use_cache} and -f $cached_translation_file and -s $cached_translation_file > 0)
1443 : olson 1.39 {
1444 :     #
1445 :     # Read the cached translations.
1446 :     #
1447 :    
1448 :     if (open(my $fh, "<$cached_translation_file"))
1449 :     {
1450 :     warn "Reading cached peg translations\n";
1451 :     $tran_peg = {};
1452 :     while (<$fh>)
1453 :     {
1454 :     chomp;
1455 :     my($k, $v) = split(/\t/);
1456 :     $tran_peg->{$k} = $v;
1457 :     }
1458 :     close($fh);
1459 :     }
1460 :     }
1461 :    
1462 :     if (!$tran_peg)
1463 :     {
1464 :     $tran_peg = $fig->translate_pegs(\%pegs, \%seqs_of, \&show_cb);
1465 :    
1466 :     #
1467 :     # tran_peg is now a hash from subsystem_peg->local_peg
1468 :     #
1469 :    
1470 :     #
1471 :     # Write the translations out to a file in the queue directory
1472 :     # for use during installation.
1473 :     #
1474 :    
1475 : olson 1.50 if ($self->{use_cache} and open(my $fh, ">$self->{qdir}/peg_translation"))
1476 : olson 1.39 {
1477 :     for my $p (keys(%$tran_peg))
1478 :     {
1479 :     my $tp = $tran_peg->{$p};
1480 :     print $fh "$p\t$tp\n";
1481 :     }
1482 :     close($fh);
1483 :     }
1484 :     }
1485 :     $self->{tran_peg} = $tran_peg;
1486 :     return $tran_peg;
1487 :     }
1488 :    
1489 :     #
1490 : olson 1.33 # Analyze this subsystem for compatibility with this SEED install.
1491 :     #
1492 :     # Returns three lists:
1493 :     #
1494 :     # A major conflict list, consisting of tuples
1495 :     # [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs] where $ss_peg
1496 :     # is the peg in the subsystem being analyzied, and $ss_func is
1497 :     # its assigned function in that subsystem. $loc_peg is the peg
1498 :     # in the local SEED, and $loc_func its local assignment. $subs is
1499 :     # the list of pairs [$subsystem_name, $role] denoting the subsystem(s)
1500 :     # that $loc_peg particpates in.
1501 :     #
1502 :     # A conflict is flagged if the local function is different than
1503 :     # the one being imported, and if the local peg is in a subsystem.
1504 :     #
1505 :     # A minor conflict list, consisting of tuples [$ss_peg, $ss_func, $loc_peg, $loc_func].
1506 :     #
1507 :     #
1508 :     # The second list is a list of subsystem pegs that do not have
1509 :     # a local equivalent. Each entry is a triple
1510 :     # [peg, orgname, function].
1511 :     #
1512 :    
1513 :     sub analyze
1514 :     {
1515 :     my($self) = @_;
1516 :     my $fig = $self->{fig};
1517 :    
1518 : olson 1.39 my $tran_peg = $self->ensure_peg_translations();
1519 :    
1520 :     #
1521 :     # Now we walk the PEGs, determining a) which are missing
1522 :     # in the local SEED, and b) which have a conflicting assignment.
1523 :     #
1524 : olson 1.33 #
1525 : olson 1.39 # We also need to determine if this assignment will cause
1526 :     # pegs to be filled into subsystem roles that were not
1527 :     # otherwise going to be added.
1528 : olson 1.33 #
1529 : olson 1.39 # To enable this, we determine from the subsystem index
1530 :     # the list all roles that are present in subsystems on
1531 :     # this SEED.
1532 : olson 1.33 #
1533 :    
1534 : olson 1.39 my $sub_name = $self->name();
1535 :     my $subsystem_roles = $fig->subsystem_roles();
1536 :    
1537 :     my(@conflict, $minor_conflict, $missing);
1538 :    
1539 : olson 1.40 #
1540 :     # Hashes for accumulating aggregate counts of conflicts.
1541 :     #
1542 :    
1543 :     my(%subs_in, %subs_out, %roles_in, %roles_out);
1544 :    
1545 : olson 1.39 $missing = [];
1546 :    
1547 :     print "Determining conflicts...<p>\n";
1548 : olson 1.33
1549 :     for my $pegent (@{$self->{pegs}})
1550 :     {
1551 : olson 1.39 my($ss_peg, undef, $ss_org, $ss_func) = @$pegent;
1552 :    
1553 :     #
1554 :     # If this peg has a local translation, determine if
1555 :     # the associated assignment conflicts with a local assignment.
1556 :     #
1557 :     # One type of conflict occurs when the new assignment would cause
1558 :     # the peg to be removed from a subsystem. This occurs when the
1559 :     # new functional assignment is different from the current
1560 :     # assignment, and the peg is already in a subsystem.
1561 :     #
1562 :     # Another type of conflict occurs when the new assignment
1563 :     # for a peg matches a role that exists in a locally-installed
1564 :     # subsystem. This will cause the peg to be added to a
1565 :     # subsystem upon refill of that subsystem.
1566 :     #
1567 :     # It is possible for both the above conditions to hold,
1568 :     # in which case a peg would be moved out of one
1569 :     # subsystem into another.
1570 :     #
1571 :     # We denote these cases in the generated conflict list by
1572 :     # annotating the entry with the list of subsystems from which
1573 :     # the peg would be removed if the assignment were to be
1574 :     # accepted, and the list of subsystems to which the
1575 :     # peg would be added.
1576 :     #
1577 :    
1578 :     if (my $loc_peg = $tran_peg->{$ss_peg})
1579 :     {
1580 :     my $subs_removed = [];
1581 :     my $subs_added = [];
1582 :    
1583 :     #
1584 :     # Determine what our locally-assigned function is, and what
1585 :     # subsystem this peg appears in.
1586 :     #
1587 :    
1588 :     my $loc_func = $fig->function_of($loc_peg);
1589 :    
1590 :     #
1591 :     # If the functions don't match, it's a conflict.
1592 :     # If the local function is in a subsystem, it's a major
1593 :     # conflict. If it's not, it's a minor conflict.
1594 :     #
1595 :     # We actually let the major/minor determination be done by
1596 :     # the analysis display code, since the difference is only in whether
1597 :     # there are subsystems.
1598 :     #
1599 :    
1600 :     if ($loc_func ne $ss_func)
1601 :     {
1602 :    
1603 :     #
1604 :     # If the function defined in the new subsystem is different than
1605 :     # the current function, we mark a conflict. Along with the conflict
1606 :     # we include a list of the subsystems in which the local peg
1607 :     # is included.
1608 :     #
1609 :     # We use the subsystems_for_peg method to determine local subsystems
1610 :     # associated with a peg. It returns a list of pairs [subsystem, rolename].
1611 :     #
1612 :    
1613 :     #
1614 :     # What if we are loading a new version of an existing subsystem, and
1615 :     # a role name has changed?
1616 :     #
1617 :     # In this case, $loc_func ne $ss_func, $loc_peg will appear in the local copy of
1618 :     # the subsystem we are loading, and hence as a candidate for removal from that subsystem.
1619 :     # This may be thought of as a spurious message, and leads me to want to remove
1620 :     # such warnings (if I'm updating a subsystem, I can expect that the pegs in that
1621 :     # subsystem will change).
1622 :     #
1623 : olson 1.40 # subsystems_for_peg returns a list of pairs [subsystem, role].
1624 :     #
1625 :     # There might be somethign of a discrepancy here. This only
1626 :     # measures the subsystems the peg is actually currently part of, not
1627 :     # the number of subsystems that have a role corresponding to the peg's
1628 :     # current assignment.
1629 :     #
1630 : olson 1.39
1631 :     my @removed = $fig->subsystems_for_peg($loc_peg);
1632 : olson 1.40
1633 :     for my $r (@removed)
1634 :     {
1635 :     my($rsub, $rrole) = @$r;
1636 :    
1637 :     #
1638 :     # Skip the numbers for an existing subsystem.
1639 :     #
1640 :     next if $rsub eq $sub_name;
1641 :    
1642 :     $roles_out{$rrole}++;
1643 :     $subs_out{$rsub}++;
1644 :    
1645 :     push(@$subs_removed, $r);
1646 :     }
1647 : olson 1.39
1648 :     #
1649 :     # We also check to see if the new function is present
1650 :     # as a role in any local subsystem. If it is, then when that subsystem
1651 :     # is refilled, this peg will appear in it.
1652 :     #
1653 : olson 1.41 # $subsystem_roles is a hash keyed on role name with each value
1654 : olson 1.40 # a list of subsystem names.
1655 : olson 1.39 #
1656 :    
1657 :     if (my $loc_ss = $subsystem_roles->{$ss_func})
1658 :     {
1659 : olson 1.40 #
1660 :     # $loc_ss is the set of subsystems that have the new
1661 :     # function assignment as a role name.
1662 :     #
1663 : olson 1.41 my @subs = grep { $_ ne $sub_name} @$loc_ss;
1664 : olson 1.40
1665 : olson 1.41 if (@subs)
1666 :     {
1667 :     push(@$subs_added, @subs);
1668 :    
1669 :     map { $subs_in{$_}++ } @subs;
1670 :     $roles_in{$ss_func}++;
1671 :     }
1672 : olson 1.39 }
1673 :    
1674 :     push(@conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs_removed, $subs_added]);
1675 :     }
1676 :    
1677 :     }
1678 :     else
1679 :     {
1680 :     push(@$missing, [$ss_peg, $ss_org, $ss_func]);
1681 :     }
1682 :     }
1683 :    
1684 :     my $conflict = [sort { @{$b->[4]} + @{$b->[5]} <=> @{$a->[4]} + @{$a->[5]} } @conflict];
1685 :    
1686 : olson 1.40 my $aggreg = {
1687 :     roles_in => [keys(%roles_in)],
1688 :     roles_out => [keys(%roles_out)],
1689 :     subs_in => [keys(%subs_in)],
1690 :     subs_out => [keys(%subs_out)],
1691 :     };
1692 :    
1693 :     return ($conflict, $missing, $aggreg);
1694 : olson 1.39 }
1695 :    
1696 :     sub read_cached_analysis
1697 :     {
1698 :     my($self) = @_;
1699 :    
1700 :     my $cfile = "$self->{qdir}/conflicts";
1701 :     my $mfile = "$self->{qdir}/missing";
1702 :    
1703 :     my($conflict, $missing);
1704 :     $conflict = [];
1705 :     $missing = [];
1706 :    
1707 :     if (open(my $fh, "<$cfile"))
1708 :     {
1709 :    
1710 :     while (<$fh>)
1711 :     {
1712 :     chomp;
1713 :    
1714 :     my($ss_peg, $ss_func, $loc_peg, $loc_func) = split(/\t/);
1715 :    
1716 :     my $subs_removed = <$fh>;
1717 :     my $subs_added = <$fh>;
1718 :    
1719 :     chomp($subs_removed);
1720 :     chomp($subs_added);
1721 :    
1722 :     my @subs_removed_raw = split(/\t/, $subs_removed);
1723 :     my $subs_added_list = [split(/\t/, $subs_added)];
1724 :    
1725 :     my $subs_removed_list = [];
1726 :    
1727 :     while (@subs_removed_raw)
1728 :     {
1729 :     my($v1, $v2, @rest) = @subs_removed_raw;
1730 :     @subs_removed_raw = @rest;
1731 :     push(@$subs_removed_list, [$v1, $v2]);
1732 :     }
1733 :    
1734 :     push(@$conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs_removed_list, $subs_added_list]);
1735 :     }
1736 :     }
1737 :    
1738 :     if (open(my $fh, "<$mfile"))
1739 :     {
1740 :    
1741 :     while (<$fh>)
1742 :     {
1743 :     chomp;
1744 :    
1745 :     my(@a) = split(/\t/);
1746 :     push(@$missing, [@a]);
1747 :     }
1748 :     }
1749 :    
1750 :     return($conflict, $missing);
1751 :     }
1752 :    
1753 :     #
1754 :     # Install this subsystem.
1755 :     #
1756 : olson 1.42 # $dont_assign is a list of subsytem PEGs that should not have their assignments overwritten.
1757 : olson 1.39 #
1758 :     # We return a list of for-the-installer messages that will be presented when the install completes.
1759 :     #
1760 : olson 1.50 # If $assignments_file is set, assignments will be written to that file
1761 :     # instead of being installed.
1762 :     #
1763 : olson 1.39 sub install
1764 :     {
1765 : olson 1.50 my($self, $dont_assign, $imported_from, $assignments_file) = @_;
1766 : olson 1.39
1767 :     my @messages;
1768 :    
1769 :     my $fig = $self->{fig};
1770 :     my $subsystems_dir = "$FIG_Config::data/Subsystems";
1771 :    
1772 :     my $sub_name = $self->name();
1773 :     $sub_name =~ s/ /_/g;
1774 :     my $sub_dir = "$subsystems_dir/$sub_name";
1775 :     my $ver = $self->version();
1776 :    
1777 :     #
1778 :     # First check to see if we already have this subsystem installed.
1779 :     #
1780 :    
1781 :     if (-d $sub_dir and (my $cur_ver = $fig->subsystem_version($sub_name)) >= $ver)
1782 :     {
1783 :     warn "Not importing $sub_name: current version $cur_ver >= imported version $ver";
1784 : olson 1.50 push(@messages, "Not importing $sub_name: current version $cur_ver >= imported version $ver\n");
1785 :     return @messages;
1786 : olson 1.39 }
1787 :    
1788 :     warn "Importing $sub_name version $ver\n";
1789 :     push(@messages, "Importing $sub_name version $ver\n");
1790 :    
1791 :     if (! -d $sub_dir)
1792 :     {
1793 :     mkdir($sub_dir, 0777) or die "Cannot mkdir $sub_dir: $!";
1794 : olson 1.33 }
1795 :    
1796 : olson 1.39 #
1797 :     # Write the header/meta information.
1798 :     #
1799 :    
1800 :     my $fh;
1801 : olson 1.50 $imported_from = "???" unless $imported_from ne '';
1802 : olson 1.39
1803 :     open($fh, ">$sub_dir/VERSION") or die "Cannot open $sub_dir/VERSION for writing: $!";
1804 :     print $fh "$ver\n";
1805 :     close($fh);
1806 :     chmod(0666, "$sub_dir/VERSION");
1807 :    
1808 :     open($fh, ">$sub_dir/EXCHANGABLE") or die "Cannot open $sub_dir/EXCHANGABLE for writing: $!";
1809 :     print $fh $self->exchangable() . "\n";
1810 :     close($fh);
1811 :     chmod(0666, "$sub_dir/EXCHANGABLE");
1812 :    
1813 :     open($fh, ">$sub_dir/curation.log") or die "Cannot open $sub_dir/curation.logt for writing: $!";
1814 :     print $fh "$self->{curation_log}\n";
1815 :     my $time = time;
1816 :     print $fh "$time\t$imported_from\timported_from\n";
1817 :     close($fh);
1818 :     chmod(0666, "$sub_dir/curation.log");
1819 :    
1820 :     open($fh, ">$sub_dir/notes") or die "Cannot open $sub_dir/notes for writing: $!";
1821 :     print $fh $self->{notes_txt} . "\n";
1822 :     close($fh);
1823 :     chmod(0666, "$sub_dir/notes");
1824 : olson 1.49
1825 :     if ($self->{reactions})
1826 :     {
1827 :     open($fh, ">$sub_dir/reactions") or die "Cannot open $sub_dir/reactions for writing: $!";
1828 :     print $fh $self->{reactions} . "\n";
1829 :     close($fh);
1830 :     chmod(0666, "$sub_dir/reactions");
1831 :     }
1832 : olson 1.39
1833 :     my $tran_peg = $self->ensure_peg_translations();
1834 : olson 1.33
1835 :     #
1836 : olson 1.39 # We can start writing the spreadsheet.
1837 : olson 1.33 #
1838 :    
1839 : olson 1.39 my $ssa_fh;
1840 :     open($ssa_fh, ">$sub_dir/spreadsheet") or die "Cannot open $sub_dir/spreadsheet for writing: $!";
1841 :    
1842 : olson 1.33 #
1843 : olson 1.39 # Start with the roles and subsets.
1844 : olson 1.33 #
1845 : olson 1.39
1846 :     print $ssa_fh $self->{role_text};
1847 :     print $ssa_fh "//\n";
1848 :    
1849 :     print $ssa_fh $self->{subsets_text};
1850 :     print $ssa_fh "//\n";
1851 : olson 1.33
1852 : olson 1.39 for my $g (@{$self->{genomes}})
1853 : olson 1.33 {
1854 : olson 1.39 my $gobj = $self->{genome_objs}->{$g};
1855 :     my ($trans_genome, @row) = $gobj->translate($tran_peg);
1856 :    
1857 :     if ($trans_genome)
1858 : olson 1.33 {
1859 : olson 1.39 print $ssa_fh join("\t", $trans_genome, $gobj->{variant}, @row), "\n";
1860 : olson 1.33 }
1861 :     }
1862 : olson 1.39
1863 :     close($ssa_fh);
1864 :    
1865 :     #
1866 : olson 1.50 # The subsystem itself is now in place. Depending on how we were
1867 :     # invoked, write the assignments to a file, or install them on
1868 :     # the system.
1869 :     #
1870 :    
1871 :     if (defined($assignments_file))
1872 :     {
1873 :     $self->write_assignments_to_file(\@messages, $tran_peg, $assignments_file);
1874 :     }
1875 :     else
1876 :     {
1877 :     $self->install_assignments(\@messages, $tran_peg, $dont_assign);
1878 :     }
1879 :    
1880 :     $self->install_blocks(\@messages, $sub_dir);
1881 :    
1882 :     return @messages;
1883 :     }
1884 :    
1885 :     #
1886 :     # Install any other block-data code that's in the package. Right now this is just
1887 :     # the diagrams.
1888 :     #
1889 :     sub install_blocks
1890 :     {
1891 :     my($self, $messages, $sub_dir) = @_;
1892 :     my $fig = $self->{fig};
1893 :    
1894 :     #
1895 :     # At this point, the rest of the subsystem is written to disk. We can
1896 :     # use the Subsys.pm mechanism to write this stuff out.
1897 :     #
1898 :    
1899 :     my $sub = $fig->get_subsystem($self->name());
1900 :    
1901 :     for my $block (@{$self->{blocks}})
1902 :     {
1903 :     my($block_hdr, $block_data) = @$block;
1904 :    
1905 :     if ($block_hdr =~ /^diagram:([^:]+):name\t(\S+)/)
1906 :     {
1907 :     #
1908 :     # The diagram output format ensures this is the first block, so just
1909 :     # create the diagram.
1910 :     #
1911 :    
1912 :     my $diagram_id = $1;
1913 :     my $diagram_name = $2;
1914 :    
1915 : olson 1.51 $sub->create_new_diagram(undef, undef, $diagram_name, $diagram_id);
1916 : olson 1.50 }
1917 :     elsif ($block_hdr =~ m,^diagram:([^:]+):diagram=([^\s/]+)\t(\d+),)
1918 :     {
1919 :     my $diagram_id = $1;
1920 :     my $img_file = $2;
1921 :     my $size = $3;
1922 :    
1923 :     my $ddir = "$sub_dir/diagrams/$diagram_id";
1924 :    
1925 :     if (! -d $ddir)
1926 :     {
1927 :     push(@$messages, "Invalid diagrams: diagram directory for $diagram_id did not exist while parsing diagram file\n");
1928 :     next;
1929 :     }
1930 :    
1931 :     if (!open(FH, ">$ddir/$img_file"))
1932 :     {
1933 :     push(@$messages, "Cannot open image file $ddir/$img_file for writing: $!\n");
1934 :     next;
1935 :     }
1936 :    
1937 :     for my $line (@$block_data)
1938 :     {
1939 :     $line =~ s/^B://;
1940 :     my $dec = decode_base64($line);
1941 :     print FH $dec;
1942 :     }
1943 :     close(FH);
1944 :    
1945 :     my $fsize = -s "$ddir/$img_file";
1946 :     if ($fsize != $size)
1947 :     {
1948 :     push(@$messages, "Diagram image $img_file size $fsize does not match declared size $size\n");
1949 :     warn "Diagram image $img_file size $fsize does not match declared size $size";
1950 :     }
1951 :     }
1952 :     }
1953 :     }
1954 :    
1955 :    
1956 :    
1957 :     sub write_assignments_to_file
1958 :     {
1959 :     my($self, $messages, $tran_peg, $filename) = @_;
1960 :     my $fig = $self->{fig};
1961 :    
1962 :     my $fh;
1963 :    
1964 :     for my $pegent (@{$self->{pegs}})
1965 :     {
1966 :     my($peg, $aliases, $org, $func) = @$pegent;
1967 :     my $tpeg = $tran_peg->{$peg};
1968 :    
1969 :     if (!$tpeg)
1970 :     {
1971 :     warn "Couldn't translate $peg (from $org)\n";
1972 :     push(@$messages, "Couldn't translate $peg (from $org)");
1973 :     next;
1974 :     }
1975 :    
1976 :     my $old = $fig->function_of($tpeg);
1977 :    
1978 :     if ($old ne $func)
1979 :     {
1980 :     #
1981 :     # Only open the file if we have assignments to write.
1982 :     #
1983 :    
1984 :     if (!defined($fh))
1985 :     {
1986 :     open($fh, ">$filename") or confess "Error opening $filename for writing: $!";
1987 :     }
1988 :     print $fh "$tpeg\t$func\n";
1989 :     }
1990 :     }
1991 :     if (defined($fh))
1992 :     {
1993 :     close($fh);
1994 :     }
1995 :     }
1996 :    
1997 :    
1998 :     sub install_assignments
1999 :     {
2000 :     my($self, $messages, $tran_peg, $dont_assign) = @_;
2001 :     my $fig = $self->{fig};
2002 :    
2003 : olson 1.33 #
2004 : olson 1.39 # Enter the new assignments, saving the old assignments in the spool dir.
2005 : olson 1.33 #
2006 :    
2007 : olson 1.39 my $now = time;
2008 :    
2009 :     my $old_funcs_fh;
2010 :     open($old_funcs_fh, ">>$self->{qdir}/old_assignments.$now");
2011 :    
2012 :     my $curator = $self->curator();
2013 : olson 1.33
2014 : olson 1.42 my %dont_assign;
2015 :    
2016 :     map { $dont_assign{$_}++ } @$dont_assign;
2017 :    
2018 : olson 1.33 for my $pegent (@{$self->{pegs}})
2019 :     {
2020 : olson 1.39 my($peg, $aliases, $org, $func) = @$pegent;
2021 :     my $tpeg = $tran_peg->{$peg};
2022 :    
2023 :     if (!$tpeg)
2024 :     {
2025 :     warn "Couldn't translate $peg (from $org)\n";
2026 : olson 1.50 push(@$messages, "Couldn't translate $peg (from $org)");
2027 : olson 1.39 next;
2028 :     }
2029 :    
2030 : olson 1.42 if ($dont_assign{$peg})
2031 :     {
2032 :     warn "Skipping assignment of $peg ($tpeg locally)\n";
2033 :     next;
2034 :     }
2035 :    
2036 : olson 1.39 my $old = $fig->function_of($tpeg);
2037 :    
2038 :     if ($old ne $func)
2039 : olson 1.33 {
2040 : olson 1.39 print $old_funcs_fh "$tpeg\t$old\t$curator\t$func\n";
2041 :     $fig->add_annotation($tpeg, $curator,
2042 :     "Assigning function $func based on installation of subsystem $self->{name}");
2043 : olson 1.33
2044 : olson 1.39 if ($curator =~ /master:(.*)/)
2045 :     {
2046 :     my $user = $1;
2047 :     $fig->assign_function($tpeg, "master", $func, "");
2048 : olson 1.48 $fig->add_annotation($tpeg, $user, "Set master function to\n$func\n");
2049 : olson 1.39 }
2050 :     else
2051 : olson 1.33 {
2052 : olson 1.39 $fig->assign_function($tpeg, $curator, $func, "");
2053 : olson 1.48 $fig->add_annotation($tpeg, $curator, "Set function to\n$func\n");
2054 : olson 1.33 }
2055 :     }
2056 :     else
2057 :     {
2058 : olson 1.39 # print "$tpeg already has assignment $func\n";
2059 : olson 1.33 }
2060 :     }
2061 : olson 1.39 close($old_funcs_fh);
2062 : olson 1.33 }
2063 :    
2064 : olson 1.41 #
2065 :     # Read the aggregate analysis results.
2066 :     #
2067 :    
2068 :     sub aggregate_analysis
2069 :     {
2070 :     my($self) = @_;
2071 :    
2072 :     if (open(my $fh, "<$self->{qdir}/aggregate"))
2073 :     {
2074 :     local($/);
2075 :     my $txt = <$fh>;
2076 :     close($fh);
2077 :    
2078 :     my $VAR1; # For the Dumper'd data.
2079 :    
2080 :     my $compartment = new Safe;
2081 :     my $aggr = $compartment->reval($txt);
2082 :     if ($@)
2083 :     {
2084 :     warn "aggregate_analysis: error parsing saved data: $@";
2085 :     return undef;
2086 :     }
2087 :     return $aggr;
2088 :     }
2089 :     else
2090 :     {
2091 :     return undef;
2092 :     }
2093 :     }
2094 :    
2095 : olson 1.33 sub name
2096 :     {
2097 :     my($self) = @_;
2098 :     return $self->{name};
2099 :     }
2100 :    
2101 :    
2102 :     sub version
2103 :     {
2104 :     my($self) = @_;
2105 :     return $self->{version};
2106 :     }
2107 :    
2108 :     sub exchangable
2109 :     {
2110 :     my($self) = @_;
2111 :     return $self->{exchangable};
2112 :     }
2113 :    
2114 :     sub curator
2115 :     {
2116 :     my($self) = @_;
2117 :     return $self->{curator};
2118 :     }
2119 :    
2120 : olson 1.39 sub analysis_complete
2121 :     {
2122 :     my($self) = @_;
2123 :    
2124 :     return -f "$self->{qdir}/analysis_complete";
2125 :     }
2126 :    
2127 :     sub analysis_jobid
2128 :     {
2129 :     my($self) = @_;
2130 :    
2131 :     my $jid_file = "$self->{qdir}/analysis_jobid";
2132 :    
2133 :     return &FIG::file_head($jid_file, 1);
2134 :     }
2135 :    
2136 : olson 1.33 package GenomeObj;
2137 :    
2138 : olson 1.39 use strict;
2139 :     use Data::Dumper;
2140 :    
2141 :     #
2142 :     # A genomeobj is a small datatype that holds the data in a row of a
2143 :     # spreadsheet file.
2144 :     #
2145 :    
2146 : olson 1.33 sub new
2147 :     {
2148 :     my($class, $subfile, $fig, $genome, $variant, $items) = @_;
2149 :    
2150 :     my $self = {
2151 :     fig => $fig,
2152 :     subfile => $subfile,
2153 :     genome => $genome,
2154 :     variant => $variant,
2155 :     items => $items,
2156 :     };
2157 :     return bless($self, $class);
2158 :    
2159 :     }
2160 :    
2161 : olson 1.39 #
2162 :     # Translate this row to a new context.
2163 :     #
2164 :     # $trans_peg is a hash mapping from spreadsheet PEG to local PEG
2165 :     #
2166 :     sub translate
2167 :     {
2168 :     my($self, $trans_peg) = @_;
2169 :     my $fig = $self->{fig};
2170 :    
2171 :     my $genome = $self->{genome};
2172 :    
2173 :     my $parsed_items = [];
2174 :     $self->{parsed_items} = $parsed_items;
2175 :     my $trans_items = [];
2176 :     $self->{trans_items} = $trans_items;
2177 :    
2178 :     #
2179 :     # Hash of genomes seen in this row.
2180 :     my %genomes;
2181 :    
2182 :     for my $item (@{$self->{items}})
2183 :     {
2184 :     my $l = [ map { $_ eq '' ? undef : "fig|$genome.peg.$_" } split(/,/, $item) ];
2185 :     my $t = [ map { $trans_peg->{$_} } @$l ];
2186 :    
2187 :     push(@$parsed_items, $l);
2188 :     push(@$trans_items, $t);
2189 :    
2190 :     #
2191 :     # Count the genomes that are seen in the translated pegs.
2192 :     #
2193 :    
2194 :     for my $tpeg (@$t)
2195 :     {
2196 :     my $tg = $fig->genome_of($tpeg);
2197 :     $genomes{$tg}++ if $tg ne "";
2198 :     }
2199 :    
2200 :     }
2201 :    
2202 :     #
2203 :     # Now determine the dominant organism for this translated row.
2204 :     #
2205 :    
2206 :     my @orgs = sort { $genomes{$b} <=> $genomes{$a} } keys(%genomes);
2207 :    
2208 :     # print "@{$self->{items}}\n";
2209 :     # print join(" ", map { "$_: $genomes{$_} " } @orgs ), "\n";
2210 :    
2211 :     unless (@orgs == 1 # Single organism
2212 :     or
2213 :     (@orgs > 1 and $genomes{$orgs[0]} > (2 * $genomes{$orgs[1]})) # First org has > 2x the second org
2214 :     )
2215 :     {
2216 :     warn "Could not determine translation for $genome\n";
2217 :     return undef;
2218 :     }
2219 :    
2220 :     #
2221 :     # The dominant organism is the first in the list.
2222 :     #
2223 :    
2224 :     my $dom = $orgs[0];
2225 :    
2226 :     #
2227 :     # Run through the translated pegs, and remove the ones that are
2228 :     # not in the dominant organism.
2229 :     #
2230 :    
2231 :     my @res;
2232 :     for my $i (0..@$trans_items - 1)
2233 :     {
2234 :     my $t = $trans_items->[$i];
2235 :    
2236 :     my @nt;
2237 :     for my $peg (@$t)
2238 :     {
2239 :     if ($peg =~ /^fig\|(\d+\.\d+)\.peg\.(\d+)$/)
2240 :     {
2241 :     if ($1 eq $dom)
2242 :     {
2243 :     push(@nt, $2);
2244 :     }
2245 :     }
2246 :     }
2247 :     push(@res, join(",", @nt));
2248 :     }
2249 :     return $dom, @res;
2250 :     }
2251 : efrank 1.1 1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3