[Bio] / FortyEight / rp_write_exports.pl Repository:
ViewVC logotype

Annotation of /FortyEight/rp_write_exports.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.1
2 :     #
3 :     # Write the export files for this completed job.
4 :     #
5 :    
6 :     use Data::Dumper;
7 :     use Carp;
8 :     use strict;
9 :     use FIG;
10 :     use FIG_Config;
11 : olson 1.2 use FileHandle;
12 : olson 1.1 use File::Basename;
13 :     use GenomeMeta;
14 :     use SeedExport;
15 :     use Job48;
16 :    
17 :     @ARGV == 1 or die "Usage: $0 job-dir\n";
18 :    
19 :     my $jobdir = shift;
20 :    
21 :     -d $jobdir or die "$0: job dir $jobdir does not exist\n";
22 :    
23 :     my $job = new Job48($jobdir);
24 :     $job or die "cannot create job for $jobdir";
25 :    
26 :     my $hostname = `hostname`;
27 :     chomp $hostname;
28 :    
29 :     my $genome = &FIG::file_head("$jobdir/GENOME_ID");
30 :     chomp $genome;
31 :     $genome =~ /^\d+\.\d+/ or die "$0: Cannnot find genome ID for jobdir $jobdir\n";
32 :    
33 :     my $meta = new GenomeMeta($genome, "$jobdir/meta.xml");
34 :    
35 :     my $genome_dir = "$jobdir/rp/$genome";
36 :    
37 : olson 1.2 my $export_dir = "$jobdir/download";
38 : olson 1.1 &FIG::verify_dir($export_dir);
39 :    
40 :     $meta->set_metadata("export.hostname", $hostname);
41 :     $meta->set_metadata("export.running", "yes");
42 :     $meta->set_metadata("status.export", "in_progress");
43 :    
44 : olson 1.4 #
45 :     # Before writing the export, perform a final check on the genome directory.
46 :     # If this is not intended for SEED use, we pass the -no_fatal_stops
47 :     # option in order to have jobs not fail in the face of some of the
48 :     # genes with bad stops that are currently being generated.
49 :     #
50 :    
51 :     my @verify_cmd = ("$FIG_Config::bin/verify_genome_directory");
52 :    
53 :     if (!($meta->get_metadata("import.candidate")))
54 :     {
55 :     push(@verify_cmd, "-no_fatal_stops");
56 :     }
57 :     if ((my $code = $meta->get_metadata("genome.genetic_code")) ne '')
58 :     {
59 :     push(@verify_cmd, "-code=$code");
60 :     }
61 :     push(@verify_cmd, $genome_dir);
62 :    
63 :     my $verify_cmd = "@verify_cmd > $jobdir/rp.errors/verify_genome_directory.report 2>&1";
64 :    
65 :     $meta->add_log_entry($0, "Verifying with command: $verify_cmd");
66 :     my $rc = system($verify_cmd);
67 :    
68 :     if ($rc != 0)
69 :     {
70 :     $meta->set_metadata("genome.directory_verification_status", "failed_$rc");
71 :     &fatal("verify_genome_directory failed with rc=$rc");
72 :     }
73 :    
74 :     $meta->set_metadata("genome.directory_verification_status", "success");
75 :     $meta->add_log_entry($0, "Verification succeeded");
76 :    
77 : olson 1.1 $meta->add_log_entry($0, "Writing exports to $export_dir");
78 :    
79 : olson 1.4
80 : olson 1.1 my @export_types = qw(genbank GTF embl gff);
81 : olson 1.2 my @strip_ec_flag = (0, 1);
82 :     my %export_names = (genbank => "Genbank",
83 :     GTF => "GTF",
84 :     embl => "EMBL",
85 :     gff => "GFF3",
86 :     );
87 :     my %export_suffix = (genbank => "gbk",
88 :     GTF => "gtf",
89 :     embl => "embl",
90 :     gff => "gff",
91 :     );
92 : olson 1.1
93 : olson 1.3 #
94 :     # If we have not yet indexed the contigs, do that here. Speeds a lot
95 :     # of stuff up.
96 :     #
97 :    
98 :     if (! -f "$genome_dir/contigs.btree")
99 :     {
100 :     my $rc = system("$FIG_Config::bin/make_fasta_btree",
101 :     "$genome_dir/contigs",
102 :     "$genome_dir/contigs.btree",
103 :     "$genome_dir/contig_len.btree");
104 :     if ($rc != 0)
105 :     {
106 :     warn "make_fasta_btree failed with rc $rc\n";
107 :     }
108 :     }
109 :    
110 : olson 1.2 open(I, ">$export_dir/index");
111 :     for my $strip_ec (@strip_ec_flag)
112 : olson 1.1 {
113 : olson 1.2 my $strip_fn_part = $strip_ec ? ".ec-stripped" : "";
114 :     my $strip_msg = $strip_ec ? " (EC numbers stripped)" : "";
115 :     for my $type (@export_types)
116 : olson 1.1 {
117 : olson 1.2 $meta->add_log_entry($0, "Exporting type $type");
118 :    
119 :     my $filename = "$genome${strip_fn_part}.$export_suffix{$type}";
120 :    
121 :     my $p = {
122 :     virtual_genome_directory => $genome_dir,
123 :     genome => $genome,
124 :     directory => "$export_dir/",
125 :     filename => "$export_dir/$filename",
126 :     export_format => $type,
127 :     strip_ec => $strip_ec,
128 :     };
129 :     eval {
130 :     SeedExport::export($p);
131 :     print I "$filename\t$export_names{$type}$strip_msg\n";
132 :     };
133 :     if ($@)
134 :     {
135 :     &fatal("Error exporting type $type of genome $genome to $export_dir: $@");
136 :     }
137 : olson 1.1 }
138 :     }
139 : olson 1.2 I->autoflush(1);
140 : olson 1.1
141 :     #
142 :     # Also export the entire genome directory as a tgz file.
143 :     #
144 :    
145 :     my @cmd = ("tar", "-C", "$jobdir/rp", "-z", "-c", "-f", "$export_dir/$genome.tgz", $genome);
146 :     my $rc = system(@cmd);
147 :     if ($rc != 0)
148 :     {
149 :     &fatal("error $rc creating tarfile with: @cmd");
150 :     }
151 :    
152 : olson 1.2 print I "$genome.tgz\tGenome directory\n";
153 :     close(I);
154 :    
155 : olson 1.1 $meta->set_metadata("export.running", "no");
156 :     $meta->set_metadata("status.export", "complete");
157 :    
158 :     exit(0);
159 :    
160 :     sub fatal
161 :     {
162 :     my($msg) = @_;
163 :    
164 :     if ($meta)
165 :     {
166 :     $meta->add_log_entry($0, ['fatal error', $msg]);
167 :     $meta->set_metadata("export.running", "no");
168 :     $meta->set_metadata("status.export", "error");
169 :     }
170 :     croak "$0: $msg";
171 :     }
172 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3