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

Annotation of /Sprout/GenomeStats.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (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 :     =back
77 :    
78 :     =cut
79 :    
80 :     use strict;
81 :     use Tracer;
82 :     use DocUtils;
83 :     use TestUtils;
84 :     use Cwd;
85 :     use File::Copy;
86 :     use File::Path;
87 :     use Sprout;
88 :     use SFXlate;
89 :     use CGI qw(:standard);
90 :     use FIG;
91 :     no warnings 'once'; # only when coding
92 :    
93 :     # Get the command-line options and parameters.
94 :     my ($options, @parameters) = StandardSetup([qw(Sprout ERDB) ],
95 :     {
96 :     strict => [0, 'keep related groups separate'],
97 :     oddStyle => ['odd', 'style for odd rows'],
98 :     evenStyle => ['even', 'style for even rows'],
99 :     tableStyle => ['genomestats', 'style for whole table'],
100 :     markerStyle => ['tinytext', 'style for markers'],
101 : parrello 1.10 numStyle => ['numcell', 'style for cells with numeric values'],
102 :     counterStyle => ['countercell', 'style for cells with counter values'],
103 : parrello 1.1 linkCGI => ['../FIG/genome_statistics.cgi',
104 :     'path to CGI script for detailed statistics'],
105 :     },
106 :     "<targetDir>",
107 :     @ARGV);
108 :     # Verify the directory name.
109 :     my $targetDir = $parameters[0];
110 :     if (! $targetDir) {
111 :     Confess("No target directory specified.");
112 :     } elsif (! -d $targetDir) {
113 :     Confess("Target directory $targetDir not found.");
114 :     } else {
115 : parrello 1.16 # Get the old Sprout.
116 : parrello 1.1 my $oldSprout = SFXlate->new_sprout_only($FIG_Config::oldSproutDB);
117 :     # Extract the genome group data from the old Sprout.
118 :     my %oldGroupHash = $oldSprout->GetGroups();
119 :     if (! $options->{strict}) {
120 : parrello 1.2 %oldGroupHash = Fix(%oldGroupHash);
121 : parrello 1.1 }
122 :     # Get the new Sprout.
123 :     my $sprout = SFXlate->new_sprout_only();
124 :     my %newGroupHash = $sprout->GetGroups();
125 :     if (! $options->{strict}) {
126 : parrello 1.3 %newGroupHash = Fix(%newGroupHash);
127 : parrello 1.1 }
128 :     # Loop through the groups.
129 :     for my $groupID (keys %newGroupHash) {
130 :     Trace("Processing group $groupID.") if T(2);
131 :     # Get the genomes from the new hash.
132 :     my @newGenomes = @{$newGroupHash{$groupID}};
133 :     # Create a hash for finding if a genome is in the old group. If the entire group is
134 :     # new, we just use an empty hash.
135 :     my %oldGenomes = ();
136 :     if (exists $oldGroupHash{$groupID}) {
137 :     %oldGenomes = map { $_ => 1 } @{$oldGroupHash{$groupID}};
138 :     }
139 :     # Create the output file.
140 : parrello 1.14 my $outFileName = "stats-" . lc($groupID) . ".inc";
141 : parrello 1.15 Open(\*GROUPFILE, ">$targetDir/$outFileName");
142 : parrello 1.1 # Get the styles.
143 :     my ($tableStyle, $markerStyle, @rowStyle) = ($options->{tableStyle}, $options->{markerStyle},
144 :     $options->{evenStyle}, $options->{oddStyle});
145 : parrello 1.10 my ($numStyle, $counterStyle) = ($options->{numStyle}, $options->{counterStyle});
146 : parrello 1.1 # Start the table.
147 :     print GROUPFILE "<table class=\"$tableStyle\">\n";
148 :     # Create the header row.
149 : parrello 1.9 print GROUPFILE Tr( { class => 'odd' }, th(["Strain annotated in NMPDR",
150 : parrello 1.1 "Genome size, bp",
151 :     "Protein Encoding Genes (PEGs)",
152 :     "Named genes in subsystems", # s0
153 :     "Named genes not in subsystems", # n0
154 :     "Hypothetical genes in subsystems", # s1
155 :     "Hypothetical genes not in subsystems", # n1
156 : parrello 1.21 "Subsystems",
157 : parrello 1.9 "RNAs",
158 :     ])) . "\n";
159 : parrello 1.1 # Set up some useful stuff for the four count columns.
160 : parrello 1.11 my %linkParms = ( s0 => "nothypo_sub", n0 => "nothypo_nosub",
161 : parrello 1.1 s1 => "hypo_sub", n1 => "hypo_nosub" );
162 :     my @columnTypes = ('s0', 'n0', 's1', 'n1');
163 :     # The data rows will be built next. We'll be putting them into a hash keyed by
164 :     # organism name. The hash enables us to spit them out sorted by name.
165 :     my %rows = ();
166 :     # Loop through the genomes in the new group.
167 :     for my $genomeID (@newGenomes) {
168 :     # Check to see if this genome is new.
169 :     my $new = (! exists $oldGenomes{$genomeID} ? "new " : "");
170 :     Trace("Processing ${new}genome $genomeID for $groupID.") if T(3);
171 :     # Get the strain name.
172 :     my $genomeName = $sprout->GenusSpecies($genomeID);
173 :     # If this is a new strain, build the HTML for the NEW! mark.
174 :     if ($new) {
175 :     $new = " <span class=\"$markerStyle\">NEW!</span>";
176 :     }
177 :     # Get the genome length.
178 : parrello 1.12 my $genomeLen = Tracer::CommaFormat($sprout->GenomeLength($genomeID));
179 : parrello 1.1 # Get the number of PEGs.
180 : parrello 1.12 my $pegCount = Tracer::CommaFormat($sprout->FeatureCount($genomeID, 'peg'));
181 : parrello 1.1 # Get the number of RNAs.
182 : parrello 1.13 my $rnaCount = Tracer::CommaFormat($sprout->FeatureCount($genomeID, 'rna'));
183 : parrello 1.17 # If there are no RNAs, we say we don't know the number, since we know there
184 :     # must be RNAs somewhere.
185 :     if (! $rnaCount) {
186 :     $rnaCount = "n/d";
187 :     }
188 : parrello 1.1 # Now we have four categories of features to work with, for each
189 :     # combination of named or hypothetical vs. in-subsystem or
190 :     # not-in-subsystem. First, we get all of the feature assignments for
191 :     # the genome.
192 :     my $assignHash = $sprout->GenomeAssignments($genomeID);
193 :     # Next, we get all of the features in the genome that belong to a
194 : parrello 1.18 # subsystem.
195 :     my %ssHash = $sprout->GenomeSubsystemData($genomeID);
196 : parrello 1.1 # Create a hash to track the four categories. "s" or "n" indicates
197 :     # in or out of a subsystem. "1" or "0" indicates hypothetical or
198 :     # real.
199 :     my %counters = ( s0 => 0, n0 => 0, s1 => 0, n1 => 0 );
200 :     # Also keep a count of all the features.
201 :     my $totalFeatures = 0;
202 :     # Loop through the assignments.
203 :     for my $fid (keys %{$assignHash}) {
204 :     # Form the counter key.
205 :     my $ss = ( exists $ssHash{$fid} ? "s" : "n" ) . FIG::hypo($assignHash->{$fid} );
206 :     # Record this feature.
207 :     $counters{$ss} += 1;
208 :     $totalFeatures++;
209 :     }
210 : parrello 1.6 Trace("$totalFeatures total features found for $genomeID.") if T(3);
211 : parrello 1.1 # We have all our data. Next we need to compute the percentages and the links.
212 :     # First, the link stuff.
213 :     my $linkPrefix = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&request=";
214 :     # Now format the counters and percentages.
215 :     for my $type (keys %linkParms) {
216 :     $counters{$type} = a( { href => "$linkPrefix$linkParms{$type}" },
217 :     sprintf("%d(%.1f%%)", $counters{$type},
218 : parrello 1.7 Tracer::Percent($counters{$type}, $totalFeatures)));
219 : parrello 1.1 }
220 : parrello 1.9 my @counterValues = map { $counters{$_} } @columnTypes;
221 : parrello 1.19 # The last link is a button to look at the subsystem summaries.
222 : parrello 1.21 my $ssCount = $sprout->GetCount(['ParticipatesIn'], 'ParticipatesIn(from-link) = ?',
223 :     [$genomeID]);
224 : parrello 1.19 my $ssLink = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&show_subsystems=1";
225 : parrello 1.21 my $ssCol = "<a href=\"$ssLink\">$ssCount</a>";
226 : parrello 1.10 # Create the row text. Note that we use the distributive capability of the TD
227 :     # function to apply the same style to each one.
228 :     my $rowHtml = join("",
229 :     td("$genomeName$new"),
230 :     td({ class => $numStyle }, $genomeLen),
231 :     td({ class => $numStyle }, $pegCount),
232 :     td({ class => $counterStyle }, \@counterValues),
233 : parrello 1.21 td({ class => $numStyle }, $ssCol),
234 : parrello 1.10 td({ class => $numStyle }, $rnaCount),
235 :     );
236 : parrello 1.1 # Put it in the row hash.
237 :     $rows{$genomeName} = $rowHtml;
238 :     }
239 :     # Set up a counter.
240 :     my $rowCount = 0;
241 :     # Now we have all the rows set up. We want to sort them and then
242 :     # write them to the output file. First, we create a variable
243 :     # we can use to select the even or odd style.
244 :     my $rowType = 0;
245 :     # Loop through the rows.
246 :     for my $rowID (sort keys %rows) {
247 :     # Format the row.
248 :     print GROUPFILE Tr( { class => $rowStyle[$rowType] }, $rows{$rowID} ) . "\n";
249 :     # Flip the row type.
250 :     $rowType = 1 - $rowType;
251 :     # Count the row.
252 :     $rowCount++;
253 :     }
254 : parrello 1.8 # All done, terminate the table and close the file.
255 :     print GROUPFILE "</table>\n";
256 : parrello 1.1 close GROUPFILE;
257 :     Trace("$rowCount genomes processed.") if T(2);
258 :     }
259 :     # We're all done.
260 :     Trace("Processing complete.") if T(2);
261 :     }
262 :    
263 :     =head3 Fix
264 :    
265 :     C<< my %fixedHash = Fix(%groupHash); >>
266 :    
267 :     Prepare a genome group hash for processing. Groups with the same primary name will be combined.
268 :     The primary name is the first capitalized word in the group name.
269 :    
270 :     =over 4
271 :    
272 :     =item groupHash
273 :    
274 :     Hash to be fixed up.
275 :    
276 :     =item RETURN
277 :    
278 :     Returns a fixed-up version of the hash.
279 :    
280 :     =back
281 :    
282 :     =cut
283 :    
284 :     sub Fix {
285 :     # Get the parameters.
286 :     my (%groupHash) = @_;
287 :     # Create the result hash.
288 :     my %retVal = ();
289 :     # Copy over the genomes.
290 :     for my $groupID (keys %groupHash) {
291 :     # Make a safety copy of the group ID.
292 :     my $realGroupID = $groupID;
293 :     # Yank the primary name.
294 :     if ($groupID =~ /([A-Z]\w+)/) {
295 :     $realGroupID = $1;
296 :     }
297 :     # Append this group's genomes into the result hash.
298 : parrello 1.5 Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
299 : parrello 1.1 }
300 :     # Return the result hash.
301 :     return %retVal;
302 :     }
303 :    
304 :    
305 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3