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

Annotation of /FigKernelPackages/P2Pupdate.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (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.36 my @curr_assignments = ();
587 : overbeek 1.27 foreach $x (@assignments)
588 : efrank 1.1 {
589 : overbeek 1.36 my($peg, $function, undef, undef) = @$x;
590 :     if ($function eq $fig->function_of($peg,$who))
591 :     {
592 :     print ASSIGNMENTS join("\t", $peg, $function),"\n";
593 :     push(@curr_assignments,$x);
594 :     }
595 : overbeek 1.4 }
596 : overbeek 1.27 print ASSIGNMENTS "//\n";
597 :    
598 : overbeek 1.36 foreach $x (@curr_assignments)
599 : overbeek 1.27 {
600 :     ($peg,undef) = @$x;
601 : overbeek 1.30 my @aliases = grep { $_ =~ /^(sp\||gi\||pirnr\||kegg\||N[PGZ]_)/ } $fig->feature_aliases($peg);
602 : olson 1.33
603 :     my $alias_txt = join(",",@aliases);
604 :     my $gs_txt = $fig->genus_species($fig->genome_of($peg));
605 :     my $func_txt = scalar $fig->function_of($peg);
606 :    
607 :     print ASSIGNMENTS join("\t",($peg,
608 :     $alias_txt,
609 :     $gs_txt,
610 :     $func_txt)) . "\n";
611 : overbeek 1.28 }
612 :     print ASSIGNMENTS "//\n";
613 :    
614 : overbeek 1.36 foreach $x (@curr_assignments)
615 : overbeek 1.28 {
616 :     ($peg,undef) = @$x;
617 : overbeek 1.30 my $seq = $fig->get_translation($peg);
618 : overbeek 1.27 &FIG::display_id_and_seq($peg,\$seq,\*ASSIGNMENTS);
619 :     }
620 :     close(ASSIGNMENTS);
621 : efrank 1.1 }
622 :     }
623 :    
624 :     =pod
625 :    
626 :     =head1 install_assignments
627 :    
628 : overbeek 1.20 usage: &install_assignments($package)
629 : efrank 1.1
630 : overbeek 1.20 $package must be a filename where the "assignments package" from which to make
631 :     the assignment set exists
632 : efrank 1.1
633 :     =cut
634 :    
635 :     sub install_assignments {
636 : overbeek 1.35 my($fig,$package,$make_assignments) = @_;
637 : overbeek 1.29 my($user,$who,$date,$userR,@assignments,$peg,$aliases,$org,$func);
638 :     my(%pegs,%seq_of,@seq,$peg_to,$trans_pegs,$seq);
639 : efrank 1.1
640 : overbeek 1.20 open(IN,"<$package") || die "could not open $package";
641 :     my $line = <IN>;
642 : olson 1.34 chomp $line;
643 : overbeek 1.20 ($user,$who,$date) = split(/\t/,$line);
644 : olson 1.26 $userR = $user;
645 :     $userR =~ s/^master://;
646 : overbeek 1.29
647 : overbeek 1.30 while (defined($line = <IN>) && ($line !~ /^\/\//))
648 : overbeek 1.29 {
649 :     if ($line =~ /^(fig\|\d+\.\d+\.peg\.\d+)\t(\S.*\S)/)
650 :     {
651 :     push(@assignments,[$1,$2]);
652 :     }
653 :     }
654 :     while ($line && defined($line = <IN>) && ($line !~ /^\/\//))
655 :     {
656 : olson 1.34 chomp $line;
657 : overbeek 1.29 ($peg,$aliases,$org,$func) = split(/\t/,$line);
658 :     $pegs{$peg} = [$aliases,$org,$func];
659 :     }
660 :    
661 :     if ($line) { $line = <IN> }
662 :     while (defined($line) && ($line !~ /^\/\//))
663 :     {
664 :     if ($line =~ /^>(\S+)/)
665 :     {
666 :     $peg = $1;
667 :     @seq = ();
668 :     while (defined($line = <IN>) && ($line !~ /^[>\/]/) && ($line !~ /^\/\//))
669 :     {
670 :     push(@seq,$line);
671 :     $line = <IN>;
672 :     }
673 :     $seq = join("",@seq);
674 :     $seq =~ s/[ \n\t]//gs;
675 :     $seq_of{$peg} = uc $seq;
676 :     }
677 :     }
678 :     close(IN);
679 :     $trans_pegs = $fig->translate_pegs(\%pegs,\%seq_of);
680 :    
681 : olson 1.26 &FIG::verify_dir("$FIG_Config::data/Assignments/$userR");
682 : overbeek 1.29 my $file = &FIG::epoch_to_readable($date) . ":$who:imported";
683 : overbeek 1.20 $file =~ s/\//-/g;
684 : overbeek 1.35
685 :     if (! $make_assignments)
686 :     {
687 :     open(OUT,">$FIG_Config::data/Assignments/$userR/$file")
688 :     || die "could not open $FIG_Config::data/Assignments/$userR/$file";
689 :     }
690 : overbeek 1.29
691 :     foreach $peg (keys(%$trans_pegs))
692 : overbeek 1.20 {
693 : overbeek 1.29 $peg_to = $trans_pegs->{$peg};
694 :     $func = $pegs{$peg}->[2];
695 :     if ($fig->function_of($peg_to) ne $func)
696 :     {
697 : overbeek 1.35 if ($make_assignments)
698 :     {
699 :     if ($user =~ /master:(.*)/)
700 :     {
701 :     $userR = $1;
702 :     $fig->assign_function($peg_to,"master",$func,"");
703 :     if ($userR ne "none")
704 :     {
705 :     $fig->add_annotation($peg_to,$userR,"Set master function to\n$func\n");
706 :     }
707 :     }
708 :     else
709 :     {
710 :     $fig->assign_function($peg_to,$user,$func,"");
711 :     if ($user ne "none")
712 :     {
713 :     $fig->add_annotation($peg_to,$user,"Set function to\n$func\n");
714 :     }
715 :     }
716 :     }
717 :     else
718 :     {
719 :     print OUT "$peg_to\t$func\n";
720 :     }
721 : overbeek 1.29 }
722 : overbeek 1.20 }
723 : overbeek 1.35 if (! $make_assignments)
724 :     {
725 :     close(OUT);
726 :     if (! -s "$FIG_Config::data/Assignments/$userR/$file") { unlink("$FIG_Config::data/Assignments/$userR/$file") }
727 :     }
728 : efrank 1.1 }
729 :    
730 :     =pod
731 :    
732 :     =head1 package_translation_rules
733 :    
734 :     usage: &package_translation_rules($fig_base,$file)
735 :    
736 :     $fig_base must be an absolute filename (begins with "/") giving the FIG from which
737 :     the updated code release will be taken.
738 :    
739 :     $file must be an absolute filename where the "translation_rules package" will be built.
740 :    
741 :     =cut
742 :    
743 :     sub package_translation_rules {
744 :     my($fig_base,$file) = @_;
745 :    
746 :     &FIG::run("cp $fig_base/Data/Global/function.synonyms $file");
747 :     }
748 :    
749 :     =pod
750 :    
751 :     =head1 install_translation_rules
752 :    
753 : efrank 1.10 usage: &install_translation_rules($fig_disk,$from,$package)
754 : efrank 1.1
755 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
756 :    
757 :     $package must be an absolute filename where the "translation_rules package" from which to make
758 :     the update exists.
759 :    
760 :     =cut
761 :    
762 :     sub install_translation_rules {
763 : efrank 1.10 my($fig_disk,$from,$package) = @_;
764 : efrank 1.1
765 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
766 :     &force_absolute($fig_disk);
767 :     if (-d "$fig_disk/BackupTranslation_Rules") { system "rm -rf $fig_disk/BackupTranslation_Rules" }
768 :     mkdir("$fig_disk/BackupTranslation_Rules",0777);
769 : disz 1.31 chmod 02777,"$fig_disk/BackupTranslation_Rules";
770 : efrank 1.1 if (-s $file)
771 :     {
772 :     &FIG::run("cp $file $fig_disk/BackupTranslation_Rules");
773 :     }
774 : efrank 1.10 &FIG::run("$FIG_Config::bin/merge_translation_rules $fig_disk/BackupTranslation_Rules/function.synonyms $package $from > $file");
775 : disz 1.31 chmod 02777,$file;
776 : efrank 1.1 }
777 :    
778 :     =pod
779 :    
780 :     =head1 restore_translation_rules
781 :    
782 :     usage: &restore_translation_rules($fig_disk);
783 :    
784 :     $fig_disk must be an absolute filename (begins with "/") giving the FIG to be updated.
785 :    
786 :     =cut
787 :    
788 :     sub restore_translation_rules {
789 :     my($fig_disk) = @_;
790 :    
791 :     &force_absolute($fig_disk);
792 :    
793 :     my $file = "$fig_disk/FIG/Data/Global/function.synonyms";
794 :     (-s "$fig_disk/BackupTranslation_Rules/function.synonyms") || die "could not find an active backup";
795 :     if (-s "$fig_disk/BackupTranslation_Rules/function.synonyms")
796 :     {
797 :     &FIG::run("cp $fig_disk/BackupTranslation_Rules/function.synonyms $file");
798 : disz 1.31 chmod 02777, $file;
799 : efrank 1.1 }
800 :     }
801 :    
802 : overbeek 1.23 sub package_subsystems {
803 : overbeek 1.30 my($fig,$file,$just_exchangable) = @_;
804 : overbeek 1.23 my($ssa);
805 :    
806 : overbeek 1.27 $just_exchangable = defined($just_exchangable) ? $just_exchangable : 1;
807 :     my @exchangable = grep { (! $just_exchangable) || $fig->is_exchangable_subsystem($_) }
808 :     $fig->all_subsystems;
809 :    
810 : overbeek 1.23 my $fig = new FIG;
811 : overbeek 1.24 if ((@exchangable > 0) && open(SUB,">$file"))
812 : overbeek 1.23 {
813 : overbeek 1.24 foreach $ssa (@exchangable)
814 : overbeek 1.23 {
815 : overbeek 1.25 # print STDERR "writing $ssa to $file\n";
816 :     my($spreadsheet,$notes) = $fig->exportable_subsystem($ssa);
817 : overbeek 1.23 print SUB join("",@$spreadsheet),join("",@$notes),"########################\n";
818 :     }
819 :     close(SUB);
820 :     }
821 : overbeek 1.25 else
822 :     {
823 :     # print STDERR &Dumper(\@exchangable,$file);
824 :     }
825 : overbeek 1.23 }
826 :    
827 : overbeek 1.30 sub install_subsystems {
828 :     my($fig,$package) = @_;
829 :    
830 : disz 1.31 &FIG::run("$FIG_Config::bin/import_subsystems master last_release < $package");
831 : overbeek 1.30 }
832 :    
833 : olson 1.33 package SubsystemFile;
834 :    
835 :     use Data::Dumper;
836 :     use strict;
837 :    
838 :     sub new
839 :     {
840 :     my($class, $qdir, $file, $fig) = @_;
841 :     my(@info);
842 :    
843 :     @info = FIG::file_head($file, 4);
844 :     if (!@info)
845 :     {
846 :     warn "Cannot open $file\n";
847 :     return undef;
848 :     }
849 :    
850 :     chomp(@info);
851 :    
852 :     my $name = $info[0];
853 :     my $version = $info[1];
854 :     my $exc = $info[2];
855 :    
856 :     my @c = split(/\t/, $info[3]);
857 :    
858 :     my $curator = $c[1];
859 :    
860 :     my $self = {
861 :     qdir => $qdir,
862 :     file => $file,
863 :     name => $name,
864 :     version => $version,
865 :     exchangable => $exc,
866 :     curator => $curator,
867 :     fig => $fig,
868 :     };
869 :    
870 :     return bless($self, $class);
871 :    
872 :     }
873 :    
874 :     #
875 :     # Load the export file into internal data structures.
876 :     #
877 :     # It's structured as
878 :     #
879 :     # name
880 :     # version
881 :     # exchangable
882 :     # creation date <tab> curator <tab> "started"
883 :     # //
884 :     # roles
885 :     # //
886 :     # subsets
887 :     # //
888 :     # spreadsheet
889 :     # //
890 :     # assignments
891 :     # //
892 :     # sequences
893 :     # //
894 :     # notes
895 :     # //
896 :     #
897 :     # Subsections:
898 :     #
899 :     # roles:
900 :     #
901 :     # abbr <tab> role-name
902 :     #
903 :     # subsets has meaning to the acutal subsystems, but we'll use it as a string.
904 :     #
905 :     # spreadsheet:
906 :     #
907 :     # genome <tab> variant <tab> items
908 :     #
909 :     # Where items is tab-separated columns, each of which is comma-separated peg number in the genome
910 :     #
911 :     # assignments:
912 :     #
913 :     # fid <tab> aliases <tab> organism <tab> function
914 :     #
915 :     # sequences:
916 :     #
917 :     # list of fasta's
918 :     #
919 :     # notes:
920 :     #
921 :     # plain text
922 :     #
923 :     sub load
924 :     {
925 :     my($self) = @_;
926 :    
927 :     my $fig = $self->{fig};
928 :    
929 :     my($fh);
930 :    
931 :     open($fh, "<$self->{file}") or die "Cannot open $self->{file}: $!\n";
932 :    
933 :     #
934 :     # Skip intro section
935 :     #
936 :    
937 :     while (<$fh>)
938 :     {
939 :     chomp;
940 :     last if m,^//,;
941 :     }
942 :    
943 :     #
944 :     # Read the roles.
945 :     #
946 :    
947 :    
948 :     my $nroles;
949 :    
950 :     while (<$fh>)
951 :     {
952 :     last if m,^//,;
953 :    
954 :     $self->{role_text} .= $_;
955 :     chomp $_;
956 :    
957 :     my($abbr, $role) = split(/\t/);
958 :    
959 :     warn "Have role $role\n";
960 :    
961 :     push(@{$self->{roles}}, $role);
962 :     push(@{$self->{abbrs}}, $abbr);
963 :    
964 :     $nroles++;
965 :     }
966 :    
967 :     #
968 :     # Read in subsets as a string.
969 :     #
970 :    
971 :     while (<$fh>)
972 :     {
973 :     last if m,^//,;
974 :    
975 :     $self->{subsets_text} .= $_;
976 :     }
977 :    
978 :     #
979 :     # Read the spreadsheet.
980 :     #
981 :    
982 :     while (<$fh>)
983 :     {
984 :     last if m,^//,;
985 :    
986 :     $self->{spreadsheet_text} .= $_;
987 :    
988 :     chomp;
989 :    
990 :     my($genome, $variant, @items) = split(/\t/, $_, $nroles + 2);
991 :    
992 :     push(@{$self->{genomes}}, $genome);
993 :    
994 :     my $gobj = GenomeObj->new($self, $fig, $genome, $variant, [@items]);
995 :    
996 :     $self->{genome_objs}->{$genome} = $gobj;
997 :     }
998 :    
999 :     #
1000 :     # Read PEG info
1001 :     #
1002 :    
1003 :     while (<$fh>)
1004 :     {
1005 :     last if m,^//,;
1006 :    
1007 :     chomp;
1008 :    
1009 :     my ($peg, $aliases, $org, $func) = split(/\t/);
1010 :    
1011 :     push(@{$self->{pegs}}, [$peg, $aliases, $org, $func]);
1012 :     }
1013 :    
1014 :     #
1015 :     # Read sequence info
1016 :     #
1017 :    
1018 :     my($cur, $cur_peg);
1019 :    
1020 :     while (<$fh>)
1021 :     {
1022 :     if (/^>(fig\|\d+\.\d+\.peg\.\d+)/)
1023 :     {
1024 :     if ($cur)
1025 :     {
1026 :     $cur =~ s/\s+//gs;
1027 :     $self->{peg_seq}->{$cur_peg} = $cur;
1028 :     }
1029 :     $cur_peg = $1;
1030 :     $cur = '';
1031 :     }
1032 :     elsif (m,^//,)
1033 :     {
1034 :     $cur =~ s/\s+//gs;
1035 :     $self->{peg_seq}->{$cur_peg} = $cur;
1036 :     last;
1037 :     }
1038 :     else
1039 :     {
1040 :     $cur .= $_;
1041 :     }
1042 :     }
1043 :    
1044 :     #
1045 :     # Read notes as a string
1046 :     #
1047 :    
1048 :     while (<$fh>)
1049 :     {
1050 :     last if m,^//,;
1051 :    
1052 :     $self->{notes_txt} .= $_;
1053 :     }
1054 :    
1055 :     }
1056 :    
1057 :     #
1058 :     # Analyze this subsystem for compatibility with this SEED install.
1059 :     #
1060 :     # Returns three lists:
1061 :     #
1062 :     # A major conflict list, consisting of tuples
1063 :     # [$ss_peg, $ss_func, $loc_peg, $loc_func, $subs] where $ss_peg
1064 :     # is the peg in the subsystem being analyzied, and $ss_func is
1065 :     # its assigned function in that subsystem. $loc_peg is the peg
1066 :     # in the local SEED, and $loc_func its local assignment. $subs is
1067 :     # the list of pairs [$subsystem_name, $role] denoting the subsystem(s)
1068 :     # that $loc_peg particpates in.
1069 :     #
1070 :     # A conflict is flagged if the local function is different than
1071 :     # the one being imported, and if the local peg is in a subsystem.
1072 :     #
1073 :     # A minor conflict list, consisting of tuples [$ss_peg, $ss_func, $loc_peg, $loc_func].
1074 :     #
1075 :     #
1076 :     # The second list is a list of subsystem pegs that do not have
1077 :     # a local equivalent. Each entry is a triple
1078 :     # [peg, orgname, function].
1079 :     #
1080 :    
1081 :     sub analyze
1082 :     {
1083 :     my($self) = @_;
1084 :     my $fig = $self->{fig};
1085 :    
1086 :     #
1087 :     # First we map the PEGs in this subsystem to PEGs in the
1088 :     # local SEED.
1089 :     #
1090 :     # translate_pegs requires a hash of peg->[aliases] as the first argument,
1091 :     # and a hash of peg->sequence as the second argument.
1092 :     #
1093 :    
1094 :     my %pegs;
1095 :     my %seqs_of;
1096 :    
1097 :     for my $pegent (@{$self->{pegs}})
1098 :     {
1099 :     my($peg, $aliases, $org, $func) = @$pegent;
1100 :     $pegs{$peg} = [$aliases, $org, $func];
1101 :     $seqs_of{$peg} = $self->{peg_seq}->{$peg};
1102 :     }
1103 :    
1104 :     my $tran_peg = $fig->translate_pegs(\%pegs, \%seqs_of);
1105 :    
1106 :     #
1107 :     # tran_peg is now a hash from subsystem_peg->local_peg
1108 :     #
1109 :    
1110 :     #
1111 :     # Write the translations out to a file in the queue directory
1112 :     # for use during installation.
1113 :     #
1114 :    
1115 :     {
1116 :     open(my $fh, ">$self->{qdir}/peg_translation");
1117 :     for my $p (keys(%$tran_peg))
1118 :     {
1119 :     my $tp = $tran_peg->{$p};
1120 :     print $fh "$p\t$tp\n";
1121 :     }
1122 :     close($fh);
1123 :     }
1124 :    
1125 :     #
1126 :     # Now we walk the PEGs, determining a) which are missing
1127 :     # in the local SEED, and b) which have a conflicting assignment.
1128 :     #
1129 :    
1130 :     my($conflict, $minor_conflict, $missing);
1131 :     $conflict = [];
1132 :     $missing = [];
1133 :    
1134 :     for my $pegent (@{$self->{pegs}})
1135 :     {
1136 :     my($ss_peg, undef, $ss_org, $ss_func) = @$pegent;
1137 :    
1138 :     if (my $loc_peg = $tran_peg->{$ss_peg})
1139 :     {
1140 :     my $loc_func = $fig->function_of($loc_peg);
1141 :     my @subs = $fig->subsystems_for_peg($loc_peg);
1142 :    
1143 :     #
1144 :     # If the functions don't match, it's a conflict.
1145 :     # If the local function is in a subsystem, it's a major
1146 :     # conflict. If it's not, it's a minor conflict.
1147 :     #
1148 :    
1149 :     if ($loc_func ne $ss_func)
1150 :     {
1151 :     push(@$conflict, [$ss_peg, $ss_func, $loc_peg, $loc_func, [@subs]]);
1152 :     }
1153 :     }
1154 :     else
1155 :     {
1156 :     push(@$missing, [$ss_peg, $ss_org, $ss_func]);
1157 :     }
1158 :     }
1159 :    
1160 :     return ($conflict, $missing);
1161 :     }
1162 :    
1163 :    
1164 :     sub name
1165 :     {
1166 :     my($self) = @_;
1167 :     return $self->{name};
1168 :     }
1169 :    
1170 :    
1171 :     sub version
1172 :     {
1173 :     my($self) = @_;
1174 :     return $self->{version};
1175 :     }
1176 :    
1177 :     sub exchangable
1178 :     {
1179 :     my($self) = @_;
1180 :     return $self->{exchangable};
1181 :     }
1182 :    
1183 :     sub curator
1184 :     {
1185 :     my($self) = @_;
1186 :     return $self->{curator};
1187 :     }
1188 :    
1189 :     package GenomeObj;
1190 :    
1191 :     sub new
1192 :     {
1193 :     my($class, $subfile, $fig, $genome, $variant, $items) = @_;
1194 :    
1195 :     my $self = {
1196 :     fig => $fig,
1197 :     subfile => $subfile,
1198 :     genome => $genome,
1199 :     variant => $variant,
1200 :     items => $items,
1201 :     };
1202 :     return bless($self, $class);
1203 :    
1204 :     }
1205 :    
1206 :    
1207 : efrank 1.1 1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3