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

Annotation of /Sprout/GenomeStats.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3