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

Annotation of /Sprout/GenomeStats.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (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 :     "location of the NMPDR group description file"],
115 : parrello 1.25 noNewCheck => [0, 'if specified, skips the check for new genomes'],
116 : parrello 1.22 },
117 : parrello 1.1 "<targetDir>",
118 :     @ARGV);
119 :     # Verify the directory name.
120 :     my $targetDir = $parameters[0];
121 :     if (! $targetDir) {
122 :     Confess("No target directory specified.");
123 :     } elsif (! -d $targetDir) {
124 :     Confess("Target directory $targetDir not found.");
125 :     } else {
126 :     # Get the new Sprout.
127 :     my $sprout = SFXlate->new_sprout_only();
128 :     my %newGroupHash = $sprout->GetGroups();
129 : parrello 1.25 # Extract the genome group data from the new Sprout.
130 : parrello 1.1 if (! $options->{strict}) {
131 : parrello 1.22 %newGroupHash = Sprout::Fix(%newGroupHash);
132 : parrello 1.1 }
133 : parrello 1.25 # This hash will be used to determine which genomes are new.
134 :     my %oldGroupHash = ();
135 :     if ($options->{noNewCheck}) {
136 :     # Here we can't look at the old Sprout. Set up the hash
137 :     # so it looks like the old Sprout's data is the same as ours.
138 :     %oldGroupHash = map { $_ => $newGroupHash{$_} } keys %newGroupHash;
139 :     } else {
140 :     # Get the old Sprout.
141 :     my $oldSprout = SFXlate->new_sprout_only($FIG_Config::oldSproutDB);
142 :     # Extract the genome group data from the old Sprout.
143 :     my %oldGroupHash = $oldSprout->GetGroups();
144 :     if (! $options->{strict}) {
145 :     %oldGroupHash = Sprout::Fix(%oldGroupHash);
146 :     }
147 :     }
148 : parrello 1.22 # Read the group file.
149 :     my %groupData = Sprout::ReadGroupFile($options->{groupFile});
150 :     # Set up some useful stuff for the four count columns.
151 :     my %linkParms = ( s0 => "nothypo_sub", n0 => "nothypo_nosub",
152 :     s1 => "hypo_sub", n1 => "hypo_nosub" );
153 :     my @columnTypes = ('s0', 'n0', 's1', 'n1');
154 :     # Get the styles.
155 :     my ($tableStyle, $markerStyle, @rowStyle) = ($options->{tableStyle}, $options->{markerStyle},
156 :     $options->{evenStyle}, $options->{oddStyle});
157 :     my ($numStyle, $counterStyle) = ($options->{numStyle}, $options->{counterStyle});
158 :     # Prepare a hash for the summary counters. These will be used on the organism summary page.
159 :     my %summaries = ();
160 : parrello 1.1 # Loop through the groups.
161 :     for my $groupID (keys %newGroupHash) {
162 :     Trace("Processing group $groupID.") if T(2);
163 : parrello 1.22 # Create a hash for summarizing the counters.
164 :     my %groupTotals = ( genomes => 0, pegs => 0, RNAs => 0,
165 :     map { $_ => } @columnTypes, features => 0 );
166 : parrello 1.1 # Get the genomes from the new hash.
167 :     my @newGenomes = @{$newGroupHash{$groupID}};
168 :     # Create a hash for finding if a genome is in the old group. If the entire group is
169 :     # new, we just use an empty hash.
170 :     my %oldGenomes = ();
171 :     if (exists $oldGroupHash{$groupID}) {
172 :     %oldGenomes = map { $_ => 1 } @{$oldGroupHash{$groupID}};
173 :     }
174 :     # Create the output file.
175 : parrello 1.14 my $outFileName = "stats-" . lc($groupID) . ".inc";
176 : parrello 1.15 Open(\*GROUPFILE, ">$targetDir/$outFileName");
177 : parrello 1.1 # Start the table.
178 :     print GROUPFILE "<table class=\"$tableStyle\">\n";
179 :     # Create the header row.
180 : parrello 1.9 print GROUPFILE Tr( { class => 'odd' }, th(["Strain annotated in NMPDR",
181 : parrello 1.1 "Genome size, bp",
182 :     "Protein Encoding Genes (PEGs)",
183 :     "Named genes in subsystems", # s0
184 :     "Named genes not in subsystems", # n0
185 :     "Hypothetical genes in subsystems", # s1
186 :     "Hypothetical genes not in subsystems", # n1
187 : parrello 1.21 "Subsystems",
188 : parrello 1.9 "RNAs",
189 :     ])) . "\n";
190 : parrello 1.1 # The data rows will be built next. We'll be putting them into a hash keyed by
191 :     # organism name. The hash enables us to spit them out sorted by name.
192 :     my %rows = ();
193 : parrello 1.22 # This variable is used to hold the counts.
194 :     my $num;
195 : parrello 1.1 # Loop through the genomes in the new group.
196 :     for my $genomeID (@newGenomes) {
197 : parrello 1.22 # Count this genome.
198 :     $groupTotals{genomes}++;
199 : parrello 1.1 # Check to see if this genome is new.
200 :     my $new = (! exists $oldGenomes{$genomeID} ? "new " : "");
201 :     Trace("Processing ${new}genome $genomeID for $groupID.") if T(3);
202 :     # Get the strain name.
203 :     my $genomeName = $sprout->GenusSpecies($genomeID);
204 :     # If this is a new strain, build the HTML for the NEW! mark.
205 :     if ($new) {
206 :     $new = " <span class=\"$markerStyle\">NEW!</span>";
207 :     }
208 :     # Get the genome length.
209 : parrello 1.22 $num = $sprout->GenomeLength($genomeID);
210 :     my $genomeLen = Tracer::CommaFormat($num);
211 : parrello 1.1 # Get the number of PEGs.
212 : parrello 1.22 $num = $sprout->FeatureCount($genomeID, 'peg');
213 :     my $pegCount = Tracer::CommaFormat($num);
214 :     $groupTotals{pegs} += $num;
215 : parrello 1.1 # Get the number of RNAs.
216 : parrello 1.22 $num = $sprout->FeatureCount($genomeID, 'rna');
217 :     my $rnaCount = Tracer::CommaFormat($num);
218 :     $groupTotals{RNAs} += $num;
219 : parrello 1.17 # If there are no RNAs, we say we don't know the number, since we know there
220 :     # must be RNAs somewhere.
221 :     if (! $rnaCount) {
222 :     $rnaCount = "n/d";
223 :     }
224 : parrello 1.1 # Now we have four categories of features to work with, for each
225 :     # combination of named or hypothetical vs. in-subsystem or
226 :     # not-in-subsystem. First, we get all of the feature assignments for
227 :     # the genome.
228 :     my $assignHash = $sprout->GenomeAssignments($genomeID);
229 :     # Next, we get all of the features in the genome that belong to a
230 : parrello 1.18 # subsystem.
231 :     my %ssHash = $sprout->GenomeSubsystemData($genomeID);
232 : parrello 1.1 # Create a hash to track the four categories. "s" or "n" indicates
233 :     # in or out of a subsystem. "1" or "0" indicates hypothetical or
234 :     # real.
235 :     my %counters = ( s0 => 0, n0 => 0, s1 => 0, n1 => 0 );
236 :     # Also keep a count of all the features.
237 :     my $totalFeatures = 0;
238 :     # Loop through the assignments.
239 :     for my $fid (keys %{$assignHash}) {
240 :     # Form the counter key.
241 :     my $ss = ( exists $ssHash{$fid} ? "s" : "n" ) . FIG::hypo($assignHash->{$fid} );
242 :     # Record this feature.
243 :     $counters{$ss} += 1;
244 :     $totalFeatures++;
245 :     }
246 : parrello 1.6 Trace("$totalFeatures total features found for $genomeID.") if T(3);
247 : parrello 1.22 for my $counterKey (@columnTypes) {
248 :     $groupTotals{$counterKey} += $counters{$counterKey};
249 :     }
250 :     $groupTotals{features} += $totalFeatures;
251 : parrello 1.1 # We have all our data. Next we need to compute the percentages and the links.
252 :     # First, the link stuff.
253 :     my $linkPrefix = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&request=";
254 :     # Now format the counters and percentages.
255 :     for my $type (keys %linkParms) {
256 :     $counters{$type} = a( { href => "$linkPrefix$linkParms{$type}" },
257 :     sprintf("%d(%.1f%%)", $counters{$type},
258 : parrello 1.7 Tracer::Percent($counters{$type}, $totalFeatures)));
259 : parrello 1.1 }
260 : parrello 1.9 my @counterValues = map { $counters{$_} } @columnTypes;
261 : parrello 1.19 # The last link is a button to look at the subsystem summaries.
262 : parrello 1.21 my $ssCount = $sprout->GetCount(['ParticipatesIn'], 'ParticipatesIn(from-link) = ?',
263 :     [$genomeID]);
264 : parrello 1.19 my $ssLink = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&show_subsystems=1";
265 : parrello 1.21 my $ssCol = "<a href=\"$ssLink\">$ssCount</a>";
266 : parrello 1.10 # Create the row text. Note that we use the distributive capability of the TD
267 :     # function to apply the same style to each one.
268 :     my $rowHtml = join("",
269 :     td("$genomeName$new"),
270 :     td({ class => $numStyle }, $genomeLen),
271 :     td({ class => $numStyle }, $pegCount),
272 :     td({ class => $counterStyle }, \@counterValues),
273 : parrello 1.21 td({ class => $numStyle }, $ssCol),
274 : parrello 1.10 td({ class => $numStyle }, $rnaCount),
275 :     );
276 : parrello 1.1 # Put it in the row hash.
277 :     $rows{$genomeName} = $rowHtml;
278 :     }
279 :     # Set up a counter.
280 :     my $rowCount = 0;
281 :     # Now we have all the rows set up. We want to sort them and then
282 :     # write them to the output file. First, we create a variable
283 :     # we can use to select the even or odd style.
284 :     my $rowType = 0;
285 :     # Loop through the rows.
286 :     for my $rowID (sort keys %rows) {
287 :     # Format the row.
288 :     print GROUPFILE Tr( { class => $rowStyle[$rowType] }, $rows{$rowID} ) . "\n";
289 :     # Flip the row type.
290 :     $rowType = 1 - $rowType;
291 :     # Count the row.
292 :     $rowCount++;
293 :     }
294 : parrello 1.8 # All done, terminate the table and close the file.
295 :     print GROUPFILE "</table>\n";
296 : parrello 1.1 close GROUPFILE;
297 :     Trace("$rowCount genomes processed.") if T(2);
298 : parrello 1.22 # Now save the group totals.
299 :     $summaries{$groupID} = \%groupTotals;
300 :     }
301 :     # Now produce the summary table.
302 : parrello 1.23 my $sumFileName = "stats-groups.inc";
303 : parrello 1.22 Open(\*SUMFILE, ">$targetDir/$sumFileName");
304 :     # Start the table.
305 :     print SUMFILE "<table class=\"$tableStyle\">\n";
306 :     # Create the header row.
307 :     print SUMFILE Tr( { class => 'odd' }, th(["Group name",
308 :     "Genomes",
309 :     "Protein Encoding Genes (PEGs)",
310 :     "Named genes in subsystems", # s0
311 :     "Named genes not in subsystems", # n0
312 :     "Hypothetical genes in subsystems", # s1
313 :     "Hypothetical genes not in subsystems", # n1
314 :     "RNAs",
315 :     ])) . "\n";
316 :     # Set up a flag for the odd-even styling.
317 :     my $rowFlag = 0;
318 :     # Put in the data rows.
319 :     for my $groupName (sort keys %summaries) {
320 :     my $group = $summaries{$groupName};
321 :     # Compute the link for the current group.
322 :     my $groupLink = a({ href => $groupData{$groupName}->[0] }, $groupName);
323 :     # Create the table row.
324 :     my $rowHtml = join("",
325 :     td($groupLink),
326 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{genomes})),
327 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{pegs})),
328 :     td({ class => $counterStyle }, [ map { Tracer::CommaFormat($group->{$_}) } @columnTypes ]),
329 :     td({ class => $numStyle }, Tracer::CommaFormat($group->{RNAs})),
330 :     );
331 :     print SUMFILE Tr( { class => $rowStyle[$rowFlag] }, $rowHtml ) . "\n";
332 :     # Flip the row style.
333 :     $rowFlag = 1 - $rowFlag;
334 : parrello 1.1 }
335 : parrello 1.22 # Terminate the table and close the file.
336 :     print SUMFILE "</table>\n";
337 :     close SUMFILE;
338 : parrello 1.1 # We're all done.
339 :     Trace("Processing complete.") if T(2);
340 :     }
341 :    
342 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3