[Bio] / Sprout / GenomeStats.pl Repository:
ViewVC logotype

Annotation of /Sprout/GenomeStats.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     =head1 Genome Data Generator
4 :    
5 :     This script creates a set of HTML include files that list the statistics for
6 :     the genomes in each of the genome groups. Genomes that are new to this version
7 :     of the Sprout will be specially marked. In order for this to work, both the
8 :     current and previous Sprout databases must be available on this machine.
9 :    
10 :     The currently-supported command-line options are as follows.
11 :    
12 :     =over 4
13 :    
14 :     =item user
15 :    
16 :     Name suffix to be used for log files. If omitted, the PID is used.
17 :    
18 :     =item trace
19 :    
20 :     Numeric trace level. A higher trace level causes more messages to appear. The
21 :     default trace level is 2. Tracing will be directly to the standard output
22 :     as well as to a C<trace>I<User>C<.log> file in the FIG temporary directory,
23 :     where I<User> is the value of the B<user> option above.
24 :    
25 :     =item sql
26 :    
27 :     If specified, turns on tracing of SQL activity.
28 :    
29 :     =item background
30 :    
31 :     Save the standard and error output to files. The files will be created
32 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
33 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
34 :     B<user> option above.
35 :    
36 :     =item h
37 :    
38 :     Display this command's parameters and options.
39 :    
40 :     =item strict
41 :    
42 :     If specified, strict groups will be used; otherwise, groups with a common primary name
43 :     will be combined into a single group. (The primary name of the group is the first
44 :     capitalized word.)
45 :    
46 :     =item oddStyle
47 :    
48 :     Style to use for odd rows of the table.
49 :    
50 :     =item evenStyle
51 :    
52 :     Style to use for even rows of the table.
53 :    
54 :     =item tableStyle
55 :    
56 :     Style to use for the table itself.
57 :    
58 :     =item markerStyle
59 :    
60 :     Style to use for small-text markers (e.g. NEW!)
61 :    
62 : parrello 1.10 =item numStyle
63 :    
64 :     Style to use for numeric cells.
65 :    
66 :     =item counterStyle
67 :    
68 :     Style to use for counter cells.
69 :    
70 : parrello 1.1 =item linkCGI
71 :    
72 :     Path to the CGI script for displaying detailed statistics.
73 :    
74 : parrello 1.25 =item noNewCheck
75 :    
76 :     If specified, the check for new genomes in the group is suppressed. This
77 :     may need to be done if there's been a change in the database definition. Note
78 :     that all this really does is keep the B<NEW!> symbol from showing. It does
79 :     not affect which genomes show up in the table.
80 :    
81 : parrello 1.1 =back
82 :    
83 :     =cut
84 :    
85 :     use strict;
86 :     use Tracer;
87 :     use Cwd;
88 :     use File::Copy;
89 :     use File::Path;
90 :     use Sprout;
91 :     use SFXlate;
92 :     use CGI qw(:standard);
93 :     use FIG;
94 :    
95 :     # Get the command-line options and parameters.
96 :     my ($options, @parameters) = StandardSetup([qw(Sprout ERDB) ],
97 :     {
98 :     strict => [0, 'keep related groups separate'],
99 :     oddStyle => ['odd', 'style for odd rows'],
100 : parrello 1.24 trace => [2, 'tracing level'],
101 : parrello 1.1 evenStyle => ['even', 'style for even rows'],
102 :     tableStyle => ['genomestats', 'style for whole table'],
103 :     markerStyle => ['tinytext', 'style for markers'],
104 : parrello 1.10 numStyle => ['numcell', 'style for cells with numeric values'],
105 :     counterStyle => ['countercell', 'style for cells with counter values'],
106 : parrello 1.1 linkCGI => ['../FIG/genome_statistics.cgi',
107 :     'path to CGI script for detailed statistics'],
108 : parrello 1.25 noNewCheck => [0, 'if specified, skips the check for new genomes'],
109 : parrello 1.26 targetDir => ["$FIG_Config::nmpdr_base/next/html/includes",
110 :     'target directory'],
111 : parrello 1.22 },
112 : parrello 1.26 "",
113 : parrello 1.1 @ARGV);
114 : parrello 1.30 # The return type (error/no error) goes in here.
115 :     my $rtype;
116 :     eval {
117 :     # This table controls the special attribute columns. For each we need to know the attribute name and the
118 :     # column title. If any genomes in a group have a value for one of the special columns, that column is
119 :     # displayed along with the attribute values.
120 :     my %specialCols = (Serotype => 'Serotype_code',
121 :     Phenotype => 'Phenotype');
122 :     # Verify the directory name.
123 :     my $targetDir = $options->{targetDir};
124 :     if (! $targetDir) {
125 :     Confess("No target directory specified.");
126 :     } elsif (! -d $targetDir) {
127 :     Confess("Target directory $targetDir not found.");
128 : parrello 1.25 } else {
129 : parrello 1.30 # Get the new Sprout.
130 :     my $sprout = SFXlate->new_sprout_only();
131 :     my %newGroupHash = $sprout->GetGroups();
132 :     # Extract the genome group data from the new Sprout.
133 : parrello 1.25 if (! $options->{strict}) {
134 : parrello 1.30 %newGroupHash = $sprout->Fix(%newGroupHash);
135 : parrello 1.1 }
136 : parrello 1.30 # This hash will be used to determine which genomes are new.
137 :     my %oldGroupHash = ();
138 :     if ($options->{noNewCheck}) {
139 :     # Here we can't look at the old Sprout. Set up the hash
140 :     # so it looks like the old Sprout's data is the same as ours.
141 :     %oldGroupHash = map { $_ => $newGroupHash{$_} } keys %newGroupHash;
142 :     } else {
143 :     # Get the old Sprout.
144 :     my $oldSprout = SFXlate->old_sprout_only();
145 :     # Extract the genome group data from the old Sprout.
146 :     %oldGroupHash = $oldSprout->GetGroups();
147 :     if (! $options->{strict}) {
148 :     %oldGroupHash = $oldSprout->Fix(%oldGroupHash);
149 : parrello 1.29 }
150 :     }
151 : parrello 1.30 # Get a FIG object for computing attributes.
152 :     my $fig = FIG->new();
153 :     # Get the super-group list.
154 :     my @superGroups = sort keys %newGroupHash;
155 :     # Set up some useful stuff for the four count columns.
156 :     my %linkParms = ( s0 => "nothypo_sub", n0 => "nothypo_nosub",
157 :     s1 => "hypo_sub", n1 => "hypo_nosub" );
158 :     my @columnTypes = ('s0', 'n0', 's1', 'n1');
159 :     # Get the styles.
160 :     my ($tableStyle, $markerStyle, @rowStyle) = ($options->{tableStyle}, $options->{markerStyle},
161 :     $options->{evenStyle}, $options->{oddStyle});
162 :     my ($numStyle, $counterStyle) = ($options->{numStyle}, $options->{counterStyle});
163 :     # Prepare a hash for the summary counters. These will be used on the organism summary page.
164 :     my %summaries = ();
165 :     # Loop through the groups.
166 :     for my $groupID (@superGroups) {
167 :     Trace("Processing group $groupID.") if T(2);
168 :     # Create a hash for summarizing the counters.
169 :     my %groupTotals = ( genomes => 0, pegs => 0, RNAs => 0,
170 :     map { $_ => } @columnTypes, features => 0 );
171 :     # Get the genomes from the new hash.
172 :     my @newGenomes = @{$newGroupHash{$groupID}};
173 :     # Create a hash for finding if a genome is in the old group. If the entire group is
174 :     # new, we just use an empty hash.
175 :     my %oldGenomes = ();
176 :     if (exists $oldGroupHash{$groupID}) {
177 :     %oldGenomes = map { $_ => 1 } @{$oldGroupHash{$groupID}};
178 : parrello 1.17 }
179 : parrello 1.30 # Create the output file.
180 :     my $outFileName = "stats-" . lc($groupID) . ".inc";
181 :     Open(\*GROUPFILE, ">$targetDir/$outFileName");
182 :     # Get the special columns. We'll stuff them in a hash keyed by column name. Each column name will contain
183 :     # a sub-hash that translates each genome ID to its applicable attribute value (if any).
184 :     my %specialData = ();
185 :     for my $specialColumn (keys %specialCols) {
186 :     # Get the attribute mapping.
187 :     my %specialDataList = map { $_->[0] => $_->[2] } $fig->get_attributes(\@newGenomes, $specialCols{$specialColumn});
188 :     # We only proceed if some attributes were found. As a result, the keys in %specialData will only be keys
189 :     # for columns that exist in the output.
190 :     if (scalar(keys %specialDataList)) {
191 :     $specialData{$specialColumn} = \%specialDataList;
192 :     }
193 : parrello 1.1 }
194 : parrello 1.30 # Set up the column names.
195 :     my @columnNames = "Strain annotated in NMPDR";
196 :     push @columnNames, sort keys %specialData;
197 :     push @columnNames, "Genome size, bp",
198 :     "Protein Encoding Genes (PEGs)",
199 :     "Named genes in subsystems", # s0
200 :     "Named genes not in subsystems", # n0
201 :     "Hypothetical genes in subsystems", # s1
202 :     "Hypothetical genes not in subsystems", # n1
203 :     "Subsystems",
204 :     "RNAs";
205 :     # Start the table.
206 :     print GROUPFILE "<table class=\"$tableStyle\">\n";
207 :     # Create the header row.
208 :     print GROUPFILE Tr( { class => 'odd' }, th(\@columnNames)) . "\n";
209 :     # The data rows will be built next. We'll be putting them into a hash keyed by
210 :     # organism name. The hash enables us to spit them out sorted by name.
211 :     my %rows = ();
212 :     # This variable is used to hold the counts.
213 :     my $num;
214 :     # Loop through the genomes in the new group.
215 :     for my $genomeID (@newGenomes) {
216 :     # Count this genome.
217 :     $groupTotals{genomes}++;
218 :     # Check to see if this genome is new.
219 :     my $new = (! exists $oldGenomes{$genomeID} ? "new " : "");
220 :     Trace("Processing ${new}genome $genomeID for $groupID.") if T(3);
221 :     # Get the strain name.
222 :     my $genomeName = $sprout->GenusSpecies($genomeID);
223 :     # Apply a link.
224 :     my $genomeText = CGI::a({ href => "../FIG/genome_statistics.cgi?genome=$genomeID;SPROUT=1" }, $genomeName);
225 :     # If this is a new strain, build the HTML for the NEW! mark.
226 :     if ($new) {
227 :     $new = " <span class=\"$markerStyle\">NEW!</span>";
228 :     }
229 :     # Get the genome length.
230 :     $num = $sprout->GenomeLength($genomeID);
231 :     my $genomeLen = Tracer::CommaFormat($num);
232 :     # Get the number of PEGs.
233 :     $num = $sprout->FeatureCount($genomeID, 'peg');
234 :     my $pegCount = Tracer::CommaFormat($num);
235 :     $groupTotals{pegs} += $num;
236 :     # Get the number of RNAs.
237 :     $num = $sprout->FeatureCount($genomeID, 'rna');
238 :     my $rnaCount = Tracer::CommaFormat($num);
239 :     $groupTotals{RNAs} += $num;
240 :     # If there are no RNAs, we say we don't know the number, since we know there
241 :     # must be RNAs somewhere.
242 :     if (! $rnaCount) {
243 :     $rnaCount = "n/d";
244 :     }
245 :     # Now we have four categories of features to work with, for each
246 :     # combination of named or hypothetical vs. in-subsystem or
247 :     # not-in-subsystem. First, we get all of the feature assignments for
248 :     # the genome.
249 :     my $assignHash = $sprout->GenomeAssignments($genomeID);
250 :     # Next, we get all of the features in the genome that belong to a
251 :     # subsystem.
252 :     my %ssHash = $sprout->GenomeSubsystemData($genomeID);
253 :     # Create a hash to track the four categories. "s" or "n" indicates
254 :     # in or out of a subsystem. "1" or "0" indicates hypothetical or
255 :     # real.
256 :     my %counters = ( s0 => 0, n0 => 0, s1 => 0, n1 => 0 );
257 :     # Also keep a count of all the features.
258 :     my $totalFeatures = 0;
259 :     # Loop through the assignments.
260 :     for my $fid (keys %{$assignHash}) {
261 :     # Form the counter key.
262 :     my $ss = ( exists $ssHash{$fid} ? "s" : "n" ) . FIG::hypo($assignHash->{$fid} );
263 :     # Record this feature.
264 :     $counters{$ss} += 1;
265 :     $totalFeatures++;
266 :     }
267 :     Trace("$totalFeatures total features found for $genomeID.") if T(3);
268 :     for my $counterKey (@columnTypes) {
269 :     $groupTotals{$counterKey} += $counters{$counterKey};
270 :     }
271 :     $groupTotals{features} += $totalFeatures;
272 :     # We have all our data. Next we need to compute the percentages and the links.
273 :     # First, the link stuff.
274 :     my $linkPrefix = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&request=";
275 :     # Now format the counters and percentages.
276 :     for my $type (keys %linkParms) {
277 :     $counters{$type} = a( { href => "$linkPrefix$linkParms{$type}" },
278 :     sprintf("%d(%.1f%%)", $counters{$type},
279 :     Tracer::Percent($counters{$type}, $totalFeatures)));
280 :     }
281 :     my @counterValues = map { $counters{$_} } @columnTypes;
282 :     # The last link is a button to look at the subsystem summaries.
283 :     my $ssCount = $sprout->GetCount(['ParticipatesIn'], 'ParticipatesIn(from-link) = ?',
284 :     [$genomeID]);
285 :     my $ssLink = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&show_subsystems=1";
286 :     my $ssCol = "<a href=\"$ssLink\">$ssCount</a>";
287 :     # Start creating the table cells.
288 :     my $rowHtml = td("$genomeText$new");
289 :     # Add any special columns.
290 :     for my $specialCol (keys %specialData) {
291 :     # Here we get the attribute value. If there is none, we leave the column blank.
292 :     my $attribute = $specialData{$specialCol}->{$genomeID} || "&nbsp;";
293 :     $rowHtml .= td($attribute);
294 :     }
295 :     # Now add the data columns.
296 :     $rowHtml .= join("",
297 :     td({ class => $numStyle }, $genomeLen),
298 :     td({ class => $numStyle }, $pegCount),
299 :     td({ class => $counterStyle }, \@counterValues),
300 :     td({ class => $numStyle }, $ssCol),
301 :     td({ class => $numStyle }, $rnaCount),
302 :     );
303 :     # Put it in the row hash.
304 :     $rows{$genomeName} = $rowHtml;
305 : parrello 1.22 }
306 : parrello 1.30 # Set up a counter.
307 :     my $rowCount = 0;
308 :     # Now we have all the rows set up. We want to sort them and then
309 :     # write them to the output file. First, we create a variable
310 :     # we can use to select the even or odd style.
311 :     my $rowType = 0;
312 :     # Loop through the rows.
313 :     for my $rowID (sort keys %rows) {
314 :     # Format the row.
315 :     print GROUPFILE Tr( { class => $rowStyle[$rowType] }, $rows{$rowID} ) . "\n";
316 :     # Flip the row type.
317 :     $rowType = 1 - $rowType;
318 :     # Count the row.
319 :     $rowCount++;
320 : parrello 1.1 }
321 : parrello 1.30 # All done, terminate the table and close the file.
322 :     print GROUPFILE "</table>\n";
323 :     close GROUPFILE;
324 :     Trace("$rowCount genomes processed.") if T(2);
325 :     # Now save the group totals.
326 :     $summaries{$groupID} = \%groupTotals;
327 : parrello 1.1 }
328 : parrello 1.30 # Now produce the summary table.
329 :     my $sumFileName = "stats-groups.inc";
330 :     Open(\*SUMFILE, ">$targetDir/$sumFileName");
331 :     # Start the table.
332 :     print SUMFILE "<table class=\"$tableStyle\">\n";
333 :     # Create the header row.
334 :     print SUMFILE Tr( { class => 'odd' }, th(["Group name",
335 :     "Genomes",
336 :     "Protein Encoding Genes (PEGs)",
337 :     "Named genes in subsystems", # s0
338 :     "Named genes not in subsystems", # n0
339 :     "Hypothetical genes in subsystems", # s1
340 :     "Hypothetical genes not in subsystems", # n1
341 :     "RNAs",
342 :     ])) . "\n";
343 :     # Set up a flag for the odd-even styling.
344 :     my $rowFlag = 0;
345 :     # Put in the data rows.
346 :     for my $groupName (sort keys %summaries) {
347 :     my $group = $summaries{$groupName};
348 :     # Compute the link for the current group.
349 :     my $groupLink = a({ href => $sprout->GroupPageName($groupName) }, $groupName);
350 :     # Create the table row.
351 :     my $rowHtml = join("",
352 :     td($groupLink),
353 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{genomes})),
354 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{pegs})),
355 :     td({ class => $counterStyle }, [ map { Tracer::CommaFormat($group->{$_}) } @columnTypes ]),
356 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{RNAs})),
357 :     );
358 :     print SUMFILE Tr( { class => $rowStyle[$rowFlag] }, $rowHtml ) . "\n";
359 :     # Flip the row style.
360 :     $rowFlag = 1 - $rowFlag;
361 : parrello 1.1 }
362 : parrello 1.30 # Terminate the table and close the file.
363 :     print SUMFILE "</table>\n";
364 :     close SUMFILE;
365 :     # We're all done.
366 :     Trace("Processing complete.") if T(2);
367 : parrello 1.22 }
368 : parrello 1.30 };
369 :     if ($@) {
370 :     Trace("Stats failed with error: $@") if T(0);
371 :     $rtype = "error";
372 :     } else {
373 :     Trace("Stats complete.") if T(2);
374 :     $rtype = "no error";
375 :     }
376 :     if ($options->{phone}) {
377 :     my $msgID = Tracer::SendSMS($options->{phone}, "GenomeStats terminated with $rtype.");
378 :     if ($msgID) {
379 :     Trace("Phone message sent with ID $msgID.") if T(2);
380 :     } else {
381 :     Trace("Phone message not sent.") if T(2);
382 : parrello 1.1 }
383 :     }
384 :    
385 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3