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

Annotation of /FigKernelPackages/P2Pupdate.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : efrank 1.1 package P2Pupdate;
2 :    
3 : olson 1.33 use strict;
4 :    
5 : efrank 1.1 use FIG_Config;
6 :     use FIG;
7 :     use Carp;
8 :     use Data::Dumper;
9 :     use Cwd;
10 :    
11 :     =pod
12 :    
13 :     =head1 updating code
14 :    
15 :     This routine packages what is needed to upgrade an older system to the
16 :     current code. Code releases are numered
17 :    
18 :     p1n1.p2n2.p3n3...
19 :    
20 :     where "." is added at the point the code moved to another branch of
21 :     the tree. FIG, who provided the initial release of the SEED, will
22 :     number all of their code releases as
23 :    
24 :     FIGn
25 :    
26 :     where n is an integer. Suppose that between releases 13 and 14 a
27 :     second group (which we will term "Idiots" for convenience) took
28 :     release 13 and wished to branch the code tree. At that point, they
29 :     would name their first release as
30 :    
31 :     FIG13.Idiots1
32 :    
33 :     We are, of course, being both cavalier and nasty when we make such a
34 :     reference. We do, however, wish to express the view that it will
35 :     benefit everyone to attempt to reconcile differences and maintain a
36 :     single code progression as long as possible. There are often good
37 :     reasons to part ways, but we urge people to think carefully before
38 :     taking such a step.
39 :    
40 :     Two code releases
41 :    
42 :     i1.i2.i3...in
43 :     and j1.j2.j3...jm with m <= n
44 :    
45 :     are compatible iff
46 :    
47 :     ip == jp for p < m, and
48 :     jm and im have the same "source" and
49 :     jm <= im
50 :    
51 :     A new code release must have the property that it can bring any
52 :     "older" compatible release up to its release.
53 :    
54 :     Note that there is an issue relating to the code to build/install packages.
55 :     Since a system may be radically restructured between releases of code, the
56 :     code to build a "package" and the code to "install" a package are radically
57 :     separated. For example, the code in P2Pupdate.pm for building an assignment
58 :     package and the code for installing an assignment package both apply to the
59 :     release of code current on the system containing P2Pupdate.pm. In fact, the
60 :     code releases may be quite different on two synchronizing systems.
61 :    
62 :     To make things work the following rules must be observed:
63 :    
64 :     1. a code release is a tar file containing VERSION, Packages,
65 :     bin/ToolTemplates, and CGI/ToolTemplates. The installing system needs
66 :     to place these at the appropriate spots, and then run bring_system_up_to_date,
67 :     which is supposed to do any required restructuring.
68 :    
69 :     2. an assignments package is a tar file containing a single directory. The directory
70 :     contains subdirectories -- one per genome. Each genome subdirectory contains zero
71 :     or more files. The name of the file is the "user" and the contents will be the
72 :     assignments made by that user.
73 :    
74 :     3. an annotations package is a tar file containing a single directory. The files in
75 :     the directory are named by genome. They contain the annotations for the genome.
76 :    
77 :     =cut
78 :    
79 :     =pod
80 :    
81 :     =head1 what_code_do_I_have
82 :    
83 :     usage: &what_code_do_I_have($fig_base)
84 :    
85 :     This just returns the current version of the code.
86 :    
87 :     =cut
88 :    
89 :     sub what_code_do_I_have {
90 :     my($fig_base) = @_;
91 :    
92 : olson 1.34 my $version = &FIG::file_read("$fig_base/VERSION");
93 :     chomp $version;
94 : efrank 1.1 return $version;
95 :     }
96 :    
97 :     =pod
98 :    
99 :     =head1 updatable_code
100 :    
101 :     usage: &updatable_code_code($v1,$v2)
102 :    
103 :     This just returns true iff the two versions of code are compatible and $v1
104 :     is "more recent".
105 :    
106 :     =cut
107 :    
108 :     sub updatable_code {
109 :     my($v1,$v2) = @_;
110 :     my($i,$v1p,$v1n,$v2p,$v2n);
111 :    
112 :     my @v1 = split(/\./,$v1);
113 :     my @v2 = split(/\./,$v2);
114 :     if (@v1 < @v2) { return 0 }
115 :    
116 :     for ($i=0; ($i < $#v2) && ($v1[$i] eq $v2[$i]); $i++) {}
117 :     if ($i == $#v2)
118 :     {
119 :     $v1[$i] =~ /^(.*[^\d])(\d+)$/;
120 :     $v1p = $1;
121 :     $v1n = $2;
122 :    
123 :     $v2[$i] =~ /^(.*[^\d])(\d+)$/;
124 :     $v2p = $1;
125 :     $v2n = $2;
126 :    
127 :     return (($v2p eq $v1p) && ($v2n < $v1n));
128 :     }
129 :     return 0;
130 :     }
131 :    
132 :     =pod
133 :    
134 :     =head1 package_code
135 :    
136 :     usage: &package_code($fig_disk,$file)
137 :    
138 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
139 :     the updated code release will be taken.
140 :    
141 :     $file must be an absolute filename where the "code package" will be built.
142 :    
143 :     =cut
144 :    
145 :     sub package_code {
146 :     my($fig_disk,$file) = @_;
147 :    
148 :     &force_absolute($fig_disk);
149 :     &force_absolute($file);
150 : olson 1.34 my @tmp = &FIG::file_head("$fig_disk/CURRENT_RELEASE", 1);
151 : overbeek 1.2 my $current_release = $tmp[0];
152 : olson 1.34 chomp $current_release;
153 : overbeek 1.2
154 :     &FIG::run("cd $fig_disk/dist/releases; tar czf $file $current_release");
155 : efrank 1.1 }
156 :    
157 :     sub force_absolute {
158 :     my($file) = @_;
159 :    
160 :     if (substr($file,0,1) ne "/")
161 :     {
162 : overbeek 1.2 print "Error: Please use absolute file names (i.e., /Users/fig/... or /home/fig/...)\n";
163 :     exit;
164 : efrank 1.1 }
165 :     }
166 :    
167 :     =pod
168 :    
169 :     =head1 install_code
170 :    
171 :     usage: &install_code($fig_disk,$package)
172 :    
173 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
174 :    
175 :     $package must be an absolute filename where the "code package" from which to make
176 :     the update exists.
177 :    
178 :     Note that this routine does not check that the updated code is compatible, or even less
179 :     current. It is assumed that upper level logic is doing that.
180 :    
181 :     =cut
182 :    
183 :     sub install_code {
184 :     my($fig_disk,$package) = @_;
185 :     my $fig_base = "$fig_disk/FIG";
186 :     &force_absolute($fig_base);
187 :     &force_absolute($package);
188 :    
189 :     if (getcwd() !~ /FIGdisk$/) { print die "Sorry, you must run this while in $FIG_Config::fig_disk" }
190 :    
191 :    
192 :     (! -d "$fig_disk/BackupCode") || &FIG::run("rm -rf $fig_disk/BackupCode");
193 :     mkdir("$fig_disk/BackupCode",0777) || die "Could not make the BackupCode directory";
194 :     (! -d "$fig_disk/BackupEnv") || &FIG::run("rm -rf $fig_disk/BackupEnv");
195 :     mkdir("$fig_disk/BackupEnv",0777) || die "Could not make the BackupEnv directory";
196 :    
197 :     my $version = &what_code_do_I_have($fig_base);
198 :     &FIG::run("cd $fig_disk; mv README install lib man env src $fig_disk/BackupEnv");
199 :     &FIG::run("cd $fig_base; mv VERSION Packages CGI $fig_disk/BackupCode");
200 :     print STDERR "made backups\n";
201 :    
202 :     &FIG::run("cd $fig_disk; tar xzf $package");
203 :     print STDERR "untarred new code\n";
204 :    
205 :     &fix_config("$fig_base/Packages/FIG_Config.pm","$fig_disk/BackupCode/Packages/FIG_Config.pm");
206 :     &FIG::run("cd $fig_base/bin; touch ToolTemplates/*/*; make all");
207 :     &FIG::run("cd $fig_base/CGI; touch ToolTemplates/*/*; make all");
208 :     print STDERR "installed new bin and CGI\n";
209 :    
210 :     &FIG::run("bring_system_up_to_date $version");
211 :     }
212 :    
213 :     =pod
214 :    
215 :     =head1 package_lightweight_code
216 :    
217 :     usage: &package_lightweight_code($fig_disk,$file)
218 :    
219 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
220 :     the updated code release will be taken.
221 :    
222 :     $file must be an absolute filename where the "code package" will be built.
223 :    
224 :     =cut
225 :    
226 :     sub package_lightweight_code {
227 :     my($fig_disk,$file) = @_;
228 :    
229 :     &force_absolute($fig_disk);
230 :     &force_absolute($file);
231 : olson 1.34 my @tmp = &FIG::file_head("$fig_disk/CURRENT_RELEASE", 1);
232 : overbeek 1.2 my $current_release = $tmp[0];
233 : olson 1.34 chomp $current_release;
234 : overbeek 1.2
235 :     &FIG::run("cd $fig_disk/dist/releases; tar czf $file $current_release");
236 : efrank 1.1 }
237 :    
238 :     =pod
239 :    
240 :     =head1 install_lightweight_code
241 :    
242 :     usage: &install_lightweight_code($fig_disk,$package)
243 :    
244 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
245 :    
246 :     $package must be an absolute filename where the "code package" from which to make
247 :     the update exists.
248 :    
249 :     Note that this routine does not check that the updated code is compatible, or even less
250 :     current. It is assumed that upper level logic is doing that.
251 :    
252 :     =cut
253 :    
254 :     sub install_lightweight_code {
255 :     my($fig_disk,$package) = @_;
256 :     my $fig_base = "$fig_disk/FIG";
257 :     &force_absolute($fig_base);
258 :     &force_absolute($package);
259 :    
260 : overbeek 1.2 if (! mkdir("$fig_disk/Tmp$$",0777))
261 :     {
262 :     print "Error: could not make $fig_disk/Tmp$$\n";
263 :     exit;
264 :     }
265 : efrank 1.1
266 : overbeek 1.2 &FIG::run("cd $fig_disk/Tmp$$; tar xzf $package");
267 :     if (! opendir(TMP,"$fig_disk/Tmp$$"))
268 :     {
269 :     print "Error: could not open $fig_disk/Tmp$$\n";
270 :     exit;
271 :     }
272 : efrank 1.1
273 : overbeek 1.2 my @rels = grep { $_ !~ /^\./ } readdir(TMP);
274 :     closedir(TMP);
275 :     if (@rels != 1)
276 :     {
277 :     print "Error: Bad code package: $package\n";
278 :     exit;
279 :     }
280 : efrank 1.1
281 : overbeek 1.2 my $new_release = $rels[0];
282 :     if (-d "$fig_disk/dist/releases/$new_release")
283 :     {
284 :     print "Error: $new_release already exists; we are doing nothing\n";
285 :     exit;
286 :     }
287 : efrank 1.1
288 : efrank 1.3 &FIG::run("mv $fig_disk/Tmp$$/$new_release $fig_disk/dist/releases");
289 :     &FIG::run("rm -rf $fig_disk/Tmp$$");
290 : olson 1.22
291 :     #
292 :     # Ugh. For now, find the arch in the fig config file $fig_disk/config/fig-user-env.sh"
293 :     #
294 :    
295 :     my $arch;
296 :     open(FH, "<$fig_disk/config/fig-user-env.sh");
297 :     while (<FH>)
298 :     {
299 :     if (/RTARCH="(.*)"/)
300 :     {
301 :     $arch = $1;
302 :     last;
303 :     }
304 :     }
305 :     close(FH);
306 :    
307 :     if ($arch eq "")
308 :     {
309 :     die "Couldn't determine SEED install architecture, not switching to release.";
310 :     }
311 :    
312 :     $ENV{RTARCH} = $arch;
313 : olson 1.32
314 :     #
315 :     # Need to put the ext_bin in the path.
316 :     #
317 :    
318 :     $ENV{PATH} .= ":$FIG_Config::ext_bin";
319 : olson 1.22
320 : efrank 1.9 &FIG::run("$FIG_Config::bin/switch_to_release $new_release");
321 : efrank 1.1 }
322 :    
323 :    
324 :     sub fix_config {
325 :     my($new,$old) = @_;
326 :     my($line,$i);
327 :    
328 : olson 1.34 my @new = &FIG::file_read($new);
329 :     foreach $line (&FIG::file_read($old))
330 : efrank 1.1 {
331 :     if ($line =~ /^(\S+)\s+\=/)
332 :     {
333 : olson 1.33 my $var = $1;
334 :     my $varQ = quotemeta $var;
335 : efrank 1.1
336 :     for ($i=0; ($i < $#new) && ($new[$i] !~ /^$varQ\s+\=/); $i++) {}
337 :     if ($i == $#new)
338 :     {
339 :     splice(@new,$i,0,$line);
340 :     }
341 :     else
342 :     {
343 :     splice(@new,$i,1,$line);
344 :     }
345 :     }
346 :     }
347 :     open(NEW,">$new") || confess "could not overwrite $new";
348 :     foreach $line (@new)
349 :     {
350 :     print NEW $line;
351 :     }
352 :     close(NEW);
353 :     }
354 :    
355 :     =pod
356 :    
357 :     =head1 what_genomes_will_I_sync
358 :    
359 :     usage: &what_genomes_will_I_sync($fig_base,$who)
360 :    
361 :     This routine returns the list of genome IDs that you are willing to sync with $who.
362 :    
363 :     =cut
364 :    
365 :     sub what_genomes_will_I_sync {
366 :     my($fig_base,$who) = @_;
367 :    
368 :     # This is the promiscuous version - it will sync all genomes with anyone.
369 :    
370 :     opendir(GENOMES,"$fig_base/Data/Organisms") || die "could not open $fig_base/Data/Organisms";
371 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(GENOMES);
372 :     closedir(GENOMES);
373 :     return @genomes;
374 :     }
375 :    
376 :     =pod
377 :    
378 :     =head1 package_annotations
379 :    
380 : overbeek 1.29 usage: &package_annotations($fig,$genomes,$file)
381 : efrank 1.1
382 :     $genomes is a pointer to a list of genome IDs that will be exchanged.
383 :    
384 :     $file must be an absolute filename where the "annotation package" will be built.
385 :    
386 :     =cut
387 :    
388 :     sub package_annotations {
389 : overbeek 1.29 my($fig,$who,$date,$genomes,$file) = @_;
390 :     my $fig_base = "$FIG_Config::fig_disk/FIG";
391 : efrank 1.1
392 : overbeek 1.29 if (open(ANNOTATIONS,">$file"))
393 : efrank 1.1 {
394 : overbeek 1.29 my @annotations = sort { $a->[0] cmp $b->[0] } $fig->annotations_made($genomes,$who,$date);
395 : olson 1.33 foreach my $x (@annotations)
396 : efrank 1.1 {
397 : overbeek 1.29 print ANNOTATIONS join("\n",@$x),"\n///\n";
398 : efrank 1.1 }
399 : overbeek 1.29 print ANNOTATIONS "//\n";
400 : efrank 1.15
401 : olson 1.33 foreach my $x (@annotations)
402 : efrank 1.15 {
403 : olson 1.33 my $peg = $x->[0];
404 : overbeek 1.30 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
405 :     print ANNOTATIONS join("\t",($peg,join(",",@aliases),$fig->genus_species($fig->genome_of($peg)),scalar $fig->function_of($peg))) . "\n";
406 : efrank 1.15 }
407 : overbeek 1.29 print ANNOTATIONS "//\n";
408 :    
409 : olson 1.33 foreach my $x (@annotations)
410 : efrank 1.15 {
411 : olson 1.33 my $peg;
412 : overbeek 1.29 ($peg,undef) = @$x;
413 : overbeek 1.30 my $seq = $fig->get_translation($peg);
414 : overbeek 1.29 &FIG::display_id_and_seq($peg,\$seq,\*ANNOTATIONS);
415 : efrank 1.15 }
416 : overbeek 1.29 close(ANNOTATIONS);
417 : efrank 1.15 }
418 : efrank 1.1 }
419 :    
420 : overbeek 1.29
421 : efrank 1.1 =pod
422 :    
423 :     =head1 install_annotations
424 :    
425 :     usage: &install_annotations($fig_disk,$package)
426 :    
427 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
428 :    
429 :     $package must be an absolute filename where the "annotations package" from which to make
430 :     the update exists.
431 :    
432 :     =cut
433 :    
434 :     sub install_annotations {
435 : overbeek 1.29 my($fig,$package) = @_;
436 :     my($user,$who,$date,$userR,@assignments,$peg,$aliases,$org,$func);
437 :     my(%pegs,%seq_of,@seq,$peg_to,$trans_pegs,$seq,$line,@ann,$ann);
438 : efrank 1.1 my($genome);
439 :    
440 : overbeek 1.29 my $fig_disk = $FIG_Config::fig_disk;
441 :     open(IN,"<$package") || die "could not open $package";
442 :     $/ = "\n//\n";
443 :     if (defined($line = <IN>))
444 :     {
445 : olson 1.33 my(@annotations);
446 :    
447 : overbeek 1.29 $line =~ s/\n\/\/\n/\n/s;
448 :     $line =~ s/\n\/\/\/\n//s;
449 :     @ann = split(/\n\/\/\/\n/,$line);
450 :     foreach $ann (@ann)
451 :     {
452 :     if ($ann =~ /^(fig\|\d+\.\d+\.peg\.\d+)\n(\d+)\n(\S+)\n(.*)/s)
453 :     {
454 :     push(@annotations,[$1,$2,$3,$4]);
455 :     }
456 :     }
457 :     $/ = "\n";
458 :     while ($line && defined($line = <IN>) && ($line !~ /^\/\//))
459 :     {
460 : olson 1.34 chomp $line;
461 : overbeek 1.29 ($peg,$aliases,$org,$func) = split(/\t/,$line);
462 :     $pegs{$peg} = [$aliases,$org,$func];
463 :     }
464 :    
465 :     if ($line) { $line = <IN> }
466 :     while (defined($line) && ($line !~ /^\/\//))
467 :     {
468 :     if ($line =~ /^>(\S+)/)
469 :     {
470 :     $peg = $1;
471 :     @seq = ();
472 :     while (defined($line = <IN>) && ($line !~ /^[>\/]/) && ($line !~ /^\/\//))
473 :     {
474 :     push(@seq,$line);
475 :     $line = <IN>;
476 :     }
477 :     $seq = join("",@seq);
478 :     $seq =~ s/[ \n\t]//gs;
479 :     $seq_of{$peg} = uc $seq;
480 :     }
481 :     }
482 :     close(IN);
483 :     $trans_pegs = $fig->translate_pegs(\%pegs,\%seq_of);
484 :     @annotations = sort { ($a->[0] cmp $b->[0]) or ($a->[1] <=> $b->[1]) }
485 :     map { ($peg = $trans_pegs->{$_->[0]}) ? [$peg,$_->[1],$_->[2],$_->[3]] : () }
486 :     @annotations;
487 :    
488 :     if (-d "$fig_disk/BackupAnnotations") { system "rm -rf $fig_disk/BackupAnnotations" }
489 :     mkdir("$fig_disk/BackupAnnotations",0777);
490 :     mkdir("$fig_disk/BackupAnnotations/New",0777);
491 : olson 1.33 my $i;
492 :     for ($i=0; ($i < @annotations); $i++)
493 : overbeek 1.29 {
494 :     if (($i == 0) || ($fig->genome_of($annotations[$i]->[0]) ne $fig->genome_of($annotations[$i-1]->[0])))
495 :     {
496 :     if ($i != 0)
497 :     {
498 :     close(OUT);
499 :     }
500 :     $genome = $fig->genome_of($annotations[$i]->[0]);
501 :     open(OUT,">$fig_disk/BackupAnnotations/New/$genome")
502 :     || die "could not open $fig_disk/BackupAnnotations/New/$genome";
503 :     }
504 :     print OUT join("\n",@{$annotations[$i]}),"\n//\n";
505 :     }
506 :     if ($i > 0) { close(OUT) }
507 :     }
508 : efrank 1.15
509 : efrank 1.1 opendir(TMP,"$fig_disk/BackupAnnotations/New") || die "could not open $fig_disk/BackupAnnotations/New";
510 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(TMP);
511 :     closedir(TMP);
512 :     foreach $genome (@genomes)
513 :     {
514 : efrank 1.14 next if (! -d "$fig_disk/FIG/Data/organisms/$genome");
515 : efrank 1.13
516 : efrank 1.1 print STDERR "installing $fig_disk/FIG/Data/Organisms/$genome/annotations\n";
517 :     if (-s "$fig_disk/FIG/Data/Organisms/$genome/annotations")
518 :     {
519 :     &FIG::run("cp -p $fig_disk/FIG/Data/Organisms/$genome/annotations $fig_disk/BackupAnnotations/$genome");
520 : 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");
521 : efrank 1.1 }
522 :     else
523 :     {
524 :     &FIG::run("cp $fig_disk/BackupAnnotations/New/$genome $fig_disk/FIG/Data/Organisms/$genome/annotations");
525 :     }
526 : disz 1.31 chmod 02777,"$fig_disk/FIG/Data/Organisms/$genome/annotations";
527 : efrank 1.1 }
528 : efrank 1.9 &FIG::run("$FIG_Config::bin/index_annotations");
529 : efrank 1.1 }
530 :    
531 :     =pod
532 :    
533 :     =head1 restore_annotations
534 :    
535 :     usage: &restore_annotations($fig_disk);
536 :    
537 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
538 :    
539 :     =cut
540 :    
541 :     sub restore_annotations {
542 :     my($fig_disk) = @_;
543 :    
544 :     &force_absolute($fig_disk);
545 :     (-d "$fig_disk/BackupAnnotations") || die "could not find an active backup";
546 :     opendir(TMP,"$fig_disk/BackupAnnotations") || die "could not open $fig_disk/BackupAnnotations";
547 :     my @genomes = grep { $_ =~ /^\d+\.\d+$/ } readdir(TMP);
548 :     closedir(TMP);
549 : olson 1.33 foreach my $genome (@genomes)
550 : efrank 1.1 {
551 :     unlink("$fig_disk/FIG/Data/Organisms/$genome/annotations");
552 :     &FIG::run("cp $fig_disk/BackupAnnotations/$genome $fig_disk/FIG/Data/Organisms/$genome/annotations");
553 : disz 1.31 system "chmod 2777 $fig_disk/FIG/Data/Organisms/$genome/annotations";
554 : efrank 1.1 }
555 : efrank 1.9 &FIG::run("$FIG_Config::bin/index_annotations");
556 : efrank 1.1 }
557 :    
558 :     =pod
559 :    
560 :     =head1 package_aassignments
561 :    
562 : overbeek 1.20 usage: package_assignments($fig,$user,$who,$date,$genomes,$file)
563 : efrank 1.1
564 : overbeek 1.20 $user designates the user wishing to get the assignments
565 : efrank 1.1
566 : overbeek 1.5 $who designates whose assignments you want (defaults to "master")
567 :    
568 :     $date if given indicates a point in time (get assignments after that point)
569 :    
570 : efrank 1.1 $genomes is a pointer to a list of genome IDs that will be exchanged.
571 :    
572 :     $file must be an absolute filename where the "assignment package" will be built.
573 :    
574 :     =cut
575 :    
576 :     sub package_assignments {
577 : overbeek 1.20 my($fig,$user,$who,$date,$genomes,$file) = @_;
578 : overbeek 1.27 my($genome,$x,$org,$curr,$peg);
579 : overbeek 1.4 $who = $who ? $who : "master";
580 : overbeek 1.7 $date = $date ? $date : 0;
581 : overbeek 1.27
582 : overbeek 1.20 if (open(ASSIGNMENTS,">$file"))
583 : efrank 1.1 {
584 : overbeek 1.20 print ASSIGNMENTS "$user\t$who\t$date\n";
585 : olson 1.33 my @assignments = sort { $a->[0] cmp $b->[0] } $fig->assignments_made_full($genomes,$who,$date);
586 : overbeek 1.27 foreach $x (@assignments)
587 : efrank 1.1 {
588 : olson 1.33 my($peg, $function, $adate, $awho) = @$x;
589 :     print ASSIGNMENTS join("\t", $peg, $function),"\n";
590 : overbeek 1.4 }
591 : overbeek 1.27 print ASSIGNMENTS "//\n";
592 :    
593 :     foreach $x (@assignments)
594 :     {
595 :     ($peg,undef) = @$x;
596 : overbeek 1.30 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
597 : olson 1.33
598 :     my $alias_txt = join(",",@aliases);
599 :     my $gs_txt = $fig->genus_species($fig->genome_of($peg));
600 :     my $func_txt = scalar $fig->function_of($peg);
601 :    
602 :     print ASSIGNMENTS join("\t",($peg,
603 :     $alias_txt,
604 :     $gs_txt,
605 :     $func_txt)) . "\n";
606 : overbeek 1.28 }
607 :     print ASSIGNMENTS "//\n";
608 :    
609 :     foreach $x (@assignments)
610 :     {
611 :     ($peg,undef) = @$x;
612 : overbeek 1.30 my $seq = $fig->get_translation($peg);
613 : overbeek 1.27 &FIG::display_id_and_seq($peg,\$seq,\*ASSIGNMENTS);
614 :     }
615 :     close(ASSIGNMENTS);
616 : efrank 1.1 }
617 :     }
618 :    
619 :     =pod
620 :    
621 :     =head1 install_assignments
622 :    
623 : overbeek 1.20 usage: &install_assignments($package)
624 : efrank 1.1
625 : overbeek 1.20 $package must be a filename where the "assignments package" from which to make
626 :     the assignment set exists
627 : efrank 1.1
628 :     =cut
629 :    
630 :     sub install_assignments {
631 : overbeek 1.29 my($fig,$package) = @_;
632 :     my($user,$who,$date,$userR,@assignments,$peg,$aliases,$org,$func);
633 :     my(%pegs,%seq_of,@seq,$peg_to,$trans_pegs,$seq);
634 : efrank 1.1
635 : overbeek 1.20 open(IN,"<$package") || die "could not open $package";
636 :     my $line = <IN>;
637 : olson 1.34 chomp $line;
638 : overbeek 1.20 ($user,$who,$date) = split(/\t/,$line);
639 : olson 1.26 $userR = $user;
640 :     $userR =~ s/^master://;
641 : overbeek 1.29
642 : overbeek 1.30 while (defined($line = <IN>) && ($line !~ /^\/\//))
643 : overbeek 1.29 {
644 :     if ($line =~ /^(fig\|\d+\.\d+\.peg\.\d+)\t(\S.*\S)/)
645 :     {
646 :     push(@assignments,[$1,$2]);
647 :     }
648 :     }
649 :     while ($line && defined($line = <IN>) && ($line !~ /^\/\//))
650 :     {
651 : olson 1.34 chomp $line;
652 : overbeek 1.29 ($peg,$aliases,$org,$func) = split(/\t/,$line);
653 :     $pegs{$peg} = [$aliases,$org,$func];
654 :     }
655 :    
656 :     if ($line) { $line = <IN> }
657 :     while (defined($line) && ($line !~ /^\/\//))
658 :     {
659 :     if ($line =~ /^>(\S+)/)
660 :     {
661 :     $peg = $1;
662 :     @seq = ();
663 :     while (defined($line = <IN>) && ($line !~ /^[>\/]/) && ($line !~ /^\/\//))
664 :     {
665 :     push(@seq,$line);
666 :     $line = <IN>;
667 :     }
668 :     $seq = join("",@seq);
669 :     $seq =~ s/[ \n\t]//gs;
670 :     $seq_of{$peg} = uc $seq;
671 :     }
672 :     }
673 :     close(IN);
674 :     $trans_pegs = $fig->translate_pegs(\%pegs,\%seq_of);
675 :    
676 : olson 1.26 &FIG::verify_dir("$FIG_Config::data/Assignments/$userR");
677 : overbeek 1.29 my $file = &FIG::epoch_to_readable($date) . ":$who:imported";
678 : overbeek 1.20 $file =~ s/\//-/g;
679 : olson 1.26 open(OUT,">$FIG_Config::data/Assignments/$userR/$file")
680 :     || die "could not open $FIG_Config::data/Assignments/$userR/$file";
681 : overbeek 1.29
682 :     foreach $peg (keys(%$trans_pegs))
683 : overbeek 1.20 {
684 : overbeek 1.29 $peg_to = $trans_pegs->{$peg};
685 :     $func = $pegs{$peg}->[2];
686 :     if ($fig->function_of($peg_to) ne $func)
687 :     {
688 :     print OUT "$peg_to\t$func\n";
689 :     }
690 : overbeek 1.20 }
691 : overbeek 1.29
692 : overbeek 1.20 close(OUT);
693 : olson 1.26 if (! -s "$FIG_Config::data/Assignments/$userR/$file") { unlink("$FIG_Config::data/Assignments/$userR/$file") }
694 : efrank 1.1 }
695 :    
696 :     =pod
697 :    
698 :     =head1 package_translation_rules
699 :    
700 :     usage: &package_translation_rules($fig_base,$file)
701 :    
702 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
703 :     the updated code release will be taken.
704 :    
705 :     $file must be an absolute filename where the "translation_rules package" will be built.
706 :    
707 :     =cut
708 :    
709 :     sub package_translation_rules {
710 :     my($fig_base,$file) = @_;
711 :    
712 :     &FIG::run("cp $fig_base/Data/Global/function.synonyms $file");
713 :     }
714 :    
715 :     =pod
716 :    
717 :     =head1 install_translation_rules
718 :    
719 : efrank 1.10 usage: &install_translation_rules($fig_disk,$from,$package)
720 : efrank 1.1
721 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
722 :    
723 :     $package must be an absolute filename where the "translation_rules package" from which to make
724 :     the update exists.
725 :    
726 :     =cut
727 :    
728 :     sub install_translation_rules {
729 : efrank 1.10 my($fig_disk,$from,$package) = @_;
730 : efrank 1.1
731 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
732 :     &force_absolute($fig_disk);
733 :     if (-d "$fig_disk/BackupTranslation_Rules") { system "rm -rf $fig_disk/BackupTranslation_Rules" }
734 :     mkdir("$fig_disk/BackupTranslation_Rules",0777);
735 : disz 1.31 chmod 02777,"$fig_disk/BackupTranslation_Rules";
736 : efrank 1.1 if (-s $file)
737 :     {
738 :     &FIG::run("cp $file $fig_disk/BackupTranslation_Rules");
739 :     }
740 : efrank 1.10 &FIG::run("$FIG_Config::bin/merge_translation_rules $fig_disk/BackupTranslation_Rules/function.synonyms $package $from > $file");
741 : disz 1.31 chmod 02777,$file;
742 : efrank 1.1 }
743 :    
744 :     =pod
745 :    
746 :     =head1 restore_translation_rules
747 :    
748 :     usage: &restore_translation_rules($fig_disk);
749 :    
750 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
751 :    
752 :     =cut
753 :    
754 :     sub restore_translation_rules {
755 :     my($fig_disk) = @_;
756 :    
757 :     &force_absolute($fig_disk);
758 :    
759 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
760 :     (-s "$fig_disk/BackupTranslation_Rules/function.synonyms") || die "could not find an active backup";
761 :     if (-s "$fig_disk/BackupTranslation_Rules/function.synonyms")
762 :     {
763 :     &FIG::run("cp $fig_disk/BackupTranslation_Rules/function.synonyms $file");
764 : disz 1.31 chmod 02777, $file;
765 : efrank 1.1 }
766 :     }
767 :    
768 : overbeek 1.23 sub package_subsystems {
769 : overbeek 1.30 my($fig,$file,$just_exchangable) = @_;
770 : overbeek 1.23 my($ssa);
771 :    
772 : overbeek 1.27 $just_exchangable = defined($just_exchangable) ? $just_exchangable : 1;
773 :     my @exchangable = grep { (! $just_exchangable) || $fig->is_exchangable_subsystem($_) }
774 :     $fig->all_subsystems;
775 :    
776 : overbeek 1.23 my $fig = new FIG;
777 : overbeek 1.24 if ((@exchangable > 0) && open(SUB,">$file"))
778 : overbeek 1.23 {
779 : overbeek 1.24 foreach $ssa (@exchangable)
780 : overbeek 1.23 {
781 : overbeek 1.25 # print STDERR "writing $ssa to $file\n";
782 :     my($spreadsheet,$notes) = $fig->exportable_subsystem($ssa);
783 : overbeek 1.23 print SUB join("",@$spreadsheet),join("",@$notes),"########################\n";
784 :     }
785 :     close(SUB);
786 :     }
787 : overbeek 1.25 else
788 :     {
789 :     # print STDERR &Dumper(\@exchangable,$file);
790 :     }
791 : overbeek 1.23 }
792 :    
793 : overbeek 1.30 sub install_subsystems {
794 :     my($fig,$package) = @_;
795 :    
796 : disz 1.31 &FIG::run("$FIG_Config::bin/import_subsystems master last_release < $package");
797 : overbeek 1.30 }
798 :    
799 : olson 1.33 package SubsystemFile;
800 :    
801 :     use Data::Dumper;
802 :     use strict;
803 :    
804 :     sub new
805 :     {
806 :     my($class, $qdir, $file, $fig) = @_;
807 :     my(@info);
808 :    
809 :     @info = FIG::file_head($file, 4);
810 :     if (!@info)
811 :     {
812 :     warn "Cannot open $file\n";
813 :     return undef;
814 :     }
815 :    
816 :     chomp(@info);
817 :    
818 :     my $name = $info[0];
819 :     my $version = $info[1];
820 :     my $exc = $info[2];
821 :    
822 :     my @c = split(/\t/, $info[3]);
823 :    
824 :     my $curator = $c[1];
825 :    
826 :     my $self = {
827 :     qdir => $qdir,
828 :     file => $file,
829 :     name => $name,
830 :     version => $version,
831 :     exchangable => $exc,
832 :     curator => $curator,
833 :     fig => $fig,
834 :     };
835 :    
836 :     return bless($self, $class);
837 :    
838 :     }
839 :    
840 :     #
841 :     # Load the export file into internal data structures.
842 :     #
843 :     # It's structured as
844 :     #
845 :     # name
846 :     # version
847 :     # exchangable
848 :     # creation date <tab> curator <tab> "started"
849 :     # //
850 :     # roles
851 :     # //
852 :     # subsets
853 :     # //
854 :     # spreadsheet
855 :     # //
856 :     # assignments
857 :     # //
858 :     # sequences
859 :     # //
860 :     # notes
861 :     # //
862 :     #
863 :     # Subsections:
864 :     #
865 :     # roles:
866 :     #
867 :     # abbr <tab> role-name
868 :     #
869 :     # subsets has meaning to the acutal subsystems, but we'll use it as a string.
870 :     #
871 :     # spreadsheet:
872 :     #
873 :     # genome <tab> variant <tab> items
874 :     #
875 :     # Where items is tab-separated columns, each of which is comma-separated peg number in the genome
876 :     #
877 :     # assignments:
878 :     #
879 :     # fid <tab> aliases <tab> organism <tab> function
880 :     #
881 :     # sequences:
882 :     #
883 :     # list of fasta's
884 :     #
885 :     # notes:
886 :     #
887 :     # plain text
888 :     #
889 :     sub load
890 :     {
891 :     my($self) = @_;
892 :    
893 :     my $fig = $self->{fig};
894 :    
895 :     my($fh);
896 :    
897 :     open($fh, "<$self->{file}") or die "Cannot open $self->{file}: $!\n";
898 :    
899 :     #
900 :     # Skip intro section
901 :     #
902 :    
903 :     while (<$fh>)
904 :     {
905 :     chomp;
906 :     last if m,^//,;
907 :     }
908 :    
909 :     #
910 :     # Read the roles.
911 :     #
912 :    
913 :    
914 :     my $nroles;
915 :    
916 :     while (<$fh>)
917 :     {
918 :     last if m,^//,;
919 :    
920 :     $self->{role_text} .= $_;
921 :     chomp $_;
922 :    
923 :     my($abbr, $role) = split(/\t/);
924 :    
925 :     warn "Have role $role\n";
926 :    
927 :     push(@{$self->{roles}}, $role);
928 :     push(@{$self->{abbrs}}, $abbr);
929 :    
930 :     $nroles++;
931 :     }
932 :    
933 :     #
934 :     # Read in subsets as a string.
935 :     #
936 :    
937 :     while (<$fh>)
938 :     {
939 :     last if m,^//,;
940 :    
941 :     $self->{subsets_text} .= $_;
942 :     }
943 :    
944 :     #
945 :     # Read the spreadsheet.
946 :     #
947 :    
948 :     while (<$fh>)
949 :     {
950 :     last if m,^//,;
951 :    
952 :     $self->{spreadsheet_text} .= $_;
953 :    
954 :     chomp;
955 :    
956 :     my($genome, $variant, @items) = split(/\t/, $_, $nroles + 2);
957 :    
958 :     push(@{$self->{genomes}}, $genome);
959 :    
960 :     my $gobj = GenomeObj->new($self, $fig, $genome, $variant, [@items]);
961 :    
962 :     $self->{genome_objs}->{$genome} = $gobj;
963 :     }
964 :    
965 :     #
966 :     # Read PEG info
967 :     #
968 :    
969 :     while (<$fh>)
970 :     {
971 :     last if m,^//,;
972 :    
973 :     chomp;
974 :    
975 :     my ($peg, $aliases, $org, $func) = split(/\t/);
976 :    
977 :     push(@{$self->{pegs}}, [$peg, $aliases, $org, $func]);
978 :     }
979 :    
980 :     #
981 :     # Read sequence info
982 :     #
983 :    
984 :     my($cur, $cur_peg);
985 :    
986 :     while (<$fh>)
987 :     {
988 :     if (/^>(fig\|\d+\.\d+\.peg\.\d+)/)
989 :     {
990 :     if ($cur)
991 :     {
992 :     $cur =~ s/\s+//gs;
993 :     $self->{peg_seq}->{$cur_peg} = $cur;
994 :     }
995 :     $cur_peg = $1;
996 :     $cur = '';
997 :     }
998 :     elsif (m,^//,)
999 :     {
1000 :     $cur =~ s/\s+//gs;
1001 :     $self->{peg_seq}->{$cur_peg} = $cur;
1002 :     last;
1003 :     }
1004 :     else
1005 :     {
1006 :     $cur .= $_;
1007 :     }
1008 :     }
1009 :    
1010 :     #
1011 :     # Read notes as a string
1012 :     #
1013 :    
1014 :     while (<$fh>)
1015 :     {
1016 :     last if m,^//,;
1017 :    
1018 :     $self->{notes_txt} .= $_;
1019 :     }
1020 :    
1021 :     }
1022 :    
1023 :     #
1024 :     # Analyze this subsystem for compatibility with this SEED install.
1025 :     #
1026 :     # Returns three lists:
1027 :     #
1028 :     # A major conflict list, consisting of tuples
1029 :     # [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs] where $ss_peg
1030 :     # is the peg in the subsystem being analyzied, and $ss_func is
1031 :     # its assigned function in that subsystem. $loc_peg is the peg
1032 :     # in the local SEED, and $loc_func its local assignment. $subs is
1033 :     # the list of pairs [$subsystem_name, $role] denoting the subsystem(s)
1034 :     # that $loc_peg particpates in.
1035 :     #
1036 :     # A conflict is flagged if the local function is different than
1037 :     # the one being imported, and if the local peg is in a subsystem.
1038 :     #
1039 :     # A minor conflict list, consisting of tuples [$ss_peg, $ss_func, $loc_peg, $loc_func].
1040 :     #
1041 :     #
1042 :     # The second list is a list of subsystem pegs that do not have
1043 :     # a local equivalent. Each entry is a triple
1044 :     # [peg, orgname, function].
1045 :     #
1046 :    
1047 :     sub analyze
1048 :     {
1049 :     my($self) = @_;
1050 :     my $fig = $self->{fig};
1051 :    
1052 :     #
1053 :     # First we map the PEGs in this subsystem to PEGs in the
1054 :     # local SEED.
1055 :     #
1056 :     # translate_pegs requires a hash of peg->[aliases] as the first argument,
1057 :     # and a hash of peg->sequence as the second argument.
1058 :     #
1059 :    
1060 :     my %pegs;
1061 :     my %seqs_of;
1062 :    
1063 :     for my $pegent (@{$self->{pegs}})
1064 :     {
1065 :     my($peg, $aliases, $org, $func) = @$pegent;
1066 :     $pegs{$peg} = [$aliases, $org, $func];
1067 :     $seqs_of{$peg} = $self->{peg_seq}->{$peg};
1068 :     }
1069 :    
1070 :     my $tran_peg = $fig->translate_pegs(\%pegs, \%seqs_of);
1071 :    
1072 :     #
1073 :     # tran_peg is now a hash from subsystem_peg->local_peg
1074 :     #
1075 :    
1076 :     #
1077 :     # Write the translations out to a file in the queue directory
1078 :     # for use during installation.
1079 :     #
1080 :    
1081 :     {
1082 :     open(my $fh, ">$self->{qdir}/peg_translation");
1083 :     for my $p (keys(%$tran_peg))
1084 :     {
1085 :     my $tp = $tran_peg->{$p};
1086 :     print $fh "$p\t$tp\n";
1087 :     }
1088 :     close($fh);
1089 :     }
1090 :    
1091 :     #
1092 :     # Now we walk the PEGs, determining a) which are missing
1093 :     # in the local SEED, and b) which have a conflicting assignment.
1094 :     #
1095 :    
1096 :     my($conflict, $minor_conflict, $missing);
1097 :     $conflict = [];
1098 :     $missing = [];
1099 :    
1100 :     for my $pegent (@{$self->{pegs}})
1101 :     {
1102 :     my($ss_peg, undef, $ss_org, $ss_func) = @$pegent;
1103 :    
1104 :     if (my $loc_peg = $tran_peg->{$ss_peg})
1105 :     {
1106 :     my $loc_func = $fig->function_of($loc_peg);
1107 :     my @subs = $fig->subsystems_for_peg($loc_peg);
1108 :    
1109 :     #
1110 :     # If the functions don't match, it's a conflict.
1111 :     # If the local function is in a subsystem, it's a major
1112 :     # conflict. If it's not, it's a minor conflict.
1113 :     #
1114 :    
1115 :     if ($loc_func ne $ss_func)
1116 :     {
1117 :     push(@$conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, [@subs]]);
1118 :     }
1119 :     }
1120 :     else
1121 :     {
1122 :     push(@$missing, [$ss_peg, $ss_org, $ss_func]);
1123 :     }
1124 :     }
1125 :    
1126 :     return ($conflict, $missing);
1127 :     }
1128 :    
1129 :    
1130 :     sub name
1131 :     {
1132 :     my($self) = @_;
1133 :     return $self->{name};
1134 :     }
1135 :    
1136 :    
1137 :     sub version
1138 :     {
1139 :     my($self) = @_;
1140 :     return $self->{version};
1141 :     }
1142 :    
1143 :     sub exchangable
1144 :     {
1145 :     my($self) = @_;
1146 :     return $self->{exchangable};
1147 :     }
1148 :    
1149 :     sub curator
1150 :     {
1151 :     my($self) = @_;
1152 :     return $self->{curator};
1153 :     }
1154 :    
1155 :     package GenomeObj;
1156 :    
1157 :     sub new
1158 :     {
1159 :     my($class, $subfile, $fig, $genome, $variant, $items) = @_;
1160 :    
1161 :     my $self = {
1162 :     fig => $fig,
1163 :     subfile => $subfile,
1164 :     genome => $genome,
1165 :     variant => $variant,
1166 :     items => $items,
1167 :     };
1168 :     return bless($self, $class);
1169 :    
1170 :     }
1171 :    
1172 :    
1173 : efrank 1.1 1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3