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

Diff of /Sprout/GenomeStats.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6, Sun Jun 18 07:33:37 2006 UTC revision 1.29, Mon Aug 20 23:14:33 2007 UTC
# Line 6  Line 6 
6  the genomes in each of the genome groups. Genomes that are new to this version  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  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.  current and previous Sprout databases must be available on this machine.
 This is one positional parameter: the name of a directory in which to place  
 the include files.  
9    
10  The currently-supported command-line options are as follows.  The currently-supported command-line options are as follows.
11    
# Line 61  Line 59 
59    
60  Style to use for small-text markers (e.g. NEW!)  Style to use for small-text markers (e.g. NEW!)
61    
62    =item numStyle
63    
64    Style to use for numeric cells.
65    
66    =item counterStyle
67    
68    Style to use for counter cells.
69    
70  =item linkCGI  =item linkCGI
71    
72  Path to the CGI script for displaying detailed statistics.  Path to the CGI script for displaying detailed statistics.
73    
74    =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  =back  =back
82    
83  =cut  =cut
# Line 80  Line 93 
93  use SFXlate;  use SFXlate;
94  use CGI qw(:standard);  use CGI qw(:standard);
95  use FIG;  use FIG;
 no warnings 'once'; # only when coding  
96    
97  # Get the command-line options and parameters.  # Get the command-line options and parameters.
98  my ($options, @parameters) = StandardSetup([qw(Sprout ERDB) ],  my ($options, @parameters) = StandardSetup([qw(Sprout ERDB) ],
99                                             {                                             {
100                                              strict => [0, 'keep related groups separate'],                                              strict => [0, 'keep related groups separate'],
101                                              oddStyle => ['odd', 'style for odd rows'],                                              oddStyle => ['odd', 'style for odd rows'],
102                                                trace => [2, 'tracing level'],
103                                              evenStyle => ['even', 'style for even rows'],                                              evenStyle => ['even', 'style for even rows'],
104                                              tableStyle => ['genomestats', 'style for whole table'],                                              tableStyle => ['genomestats', 'style for whole table'],
105                                              markerStyle => ['tinytext', 'style for markers'],                                              markerStyle => ['tinytext', 'style for markers'],
106                                                numStyle => ['numcell', 'style for cells with numeric values'],
107                                                counterStyle => ['countercell', 'style for cells with counter values'],
108                                              linkCGI => ['../FIG/genome_statistics.cgi',                                              linkCGI => ['../FIG/genome_statistics.cgi',
109                                                          'path to CGI script for detailed statistics'],                                                          'path to CGI script for detailed statistics'],
110                                                groupFile => ["$FIG_Config::sproutData/groups.tbl",
111                                                              'location of the NMPDR group description file'],
112                                                noNewCheck => [0, 'if specified, skips the check for new genomes'],
113                                                targetDir => ["$FIG_Config::nmpdr_base/next/html/includes",
114                                                              'target directory'],
115                                             },                                             },
116                                             "<targetDir>",                                             "",
117                                             @ARGV);                                             @ARGV);
118    # This table controls the special attribute columns. For each we need to know the attribute name and the
119    # column title. If any genomes in a group have a value for one of the special columns, that column is
120    # displayed along with the attribute values.
121    my %specialCols = (Serotype => 'Serotype_code',
122                       Phenotype => 'Phenotype');
123  # Verify the directory name.  # Verify the directory name.
124  my $targetDir = $parameters[0];  my $targetDir = $options->{targetDir};
125  if (! $targetDir) {  if (! $targetDir) {
126      Confess("No target directory specified.");      Confess("No target directory specified.");
127  } elsif (! -d $targetDir) {  } elsif (! -d $targetDir) {
128      Confess("Target directory $targetDir not found.");      Confess("Target directory $targetDir not found.");
129  } else {  } else {
     # *Get the old Sprout.  
     my $oldSprout = SFXlate->new_sprout_only($FIG_Config::oldSproutDB);  
     # Extract the genome group data from the old Sprout.  
     my %oldGroupHash = $oldSprout->GetGroups();  
     if (! $options->{strict}) {  
         %oldGroupHash = Fix(%oldGroupHash);  
     }  
130      # Get the new Sprout.      # Get the new Sprout.
131      my $sprout = SFXlate->new_sprout_only();      my $sprout = SFXlate->new_sprout_only();
132      my %newGroupHash = $sprout->GetGroups();      my %newGroupHash = $sprout->GetGroups();
133        # Extract the genome group data from the new Sprout.
134      if (! $options->{strict}) {      if (! $options->{strict}) {
135          %newGroupHash = Fix(%newGroupHash);          %newGroupHash = Sprout::Fix(%newGroupHash);
136      }      }
137        # This hash will be used to determine which genomes are new.
138        my %oldGroupHash = ();
139        if ($options->{noNewCheck}) {
140            # Here we can't look at the old Sprout. Set up the hash
141            # so it looks like the old Sprout's data is the same as ours.
142            %oldGroupHash = map { $_ => $newGroupHash{$_} } keys %newGroupHash;
143        } else {
144            # Get the old Sprout.
145            my $oldSprout = SFXlate->old_sprout_only();
146            # Extract the genome group data from the old Sprout.
147            %oldGroupHash = $oldSprout->GetGroups();
148            if (! $options->{strict}) {
149                %oldGroupHash = Sprout::Fix(%oldGroupHash);
150            }
151        }
152        # Get a FIG object for computing attributes.
153        my $fig = FIG->new();
154        # Read the group file.
155        my %groupData = Sprout::ReadGroupFile($options->{groupFile});
156        # Set up some useful stuff for the four count columns.
157        my %linkParms = ( s0 => "nothypo_sub", n0 => "nothypo_nosub",
158                          s1 => "hypo_sub", n1 => "hypo_nosub" );
159        my @columnTypes = ('s0', 'n0', 's1', 'n1');
160        # Get the styles.
161        my ($tableStyle, $markerStyle, @rowStyle) = ($options->{tableStyle}, $options->{markerStyle},
162                                                     $options->{evenStyle}, $options->{oddStyle});
163        my ($numStyle, $counterStyle) = ($options->{numStyle}, $options->{counterStyle});
164        # Prepare a hash for the summary counters. These will be used on the organism summary page.
165        my %summaries = ();
166      # Loop through the groups.      # Loop through the groups.
167      for my $groupID (keys %newGroupHash) {      for my $groupID (keys %newGroupHash) {
168          Trace("Processing group $groupID.") if T(2);          Trace("Processing group $groupID.") if T(2);
169            # Create a hash for summarizing the counters.
170            my %groupTotals = ( genomes => 0, pegs => 0, RNAs => 0,
171                               map { $_ => } @columnTypes, features => 0 );
172          # Get the genomes from the new hash.          # Get the genomes from the new hash.
173          my @newGenomes = @{$newGroupHash{$groupID}};          my @newGenomes = @{$newGroupHash{$groupID}};
174          # Create a hash for finding if a genome is in the old group. If the entire group is          # Create a hash for finding if a genome is in the old group. If the entire group is
# Line 127  Line 178 
178              %oldGenomes = map { $_ => 1 } @{$oldGroupHash{$groupID}};              %oldGenomes = map { $_ => 1 } @{$oldGroupHash{$groupID}};
179          }          }
180          # Create the output file.          # Create the output file.
181          Open(\*GROUPFILE, ">$targetDir/$groupID.inc");          my $outFileName = "stats-" . lc($groupID) . ".inc";
182          # Get the styles.          Open(\*GROUPFILE, ">$targetDir/$outFileName");
183          my ($tableStyle, $markerStyle, @rowStyle) = ($options->{tableStyle}, $options->{markerStyle},          # Get the special columns. We'll stuff them in a hash keyed by column name. Each column name will contain
184                                                       $options->{evenStyle}, $options->{oddStyle});          # a sub-hash that translates each genome ID to its applicable attribute value (if any).
185          # Start the table.          my %specialData = ();
186          print GROUPFILE "<table class=\"$tableStyle\">\n";          for my $specialColumn (keys %specialCols) {
187          # Create the header row.              # Get the attribute mapping.
188          print GROUPFILE Tr( { class => 'odd' }, th("Strain annotated in NMPDR",              my %specialDataList = map { $_->[0] => $_->[2] } $fig->get_attributes(\@newGenomes, $specialCols{$specialColumn});
189                                                   "Genome size, bp",              # We only proceed if some attributes were found. As a result, the keys in %specialData will only be keys
190                # for columns that exist in the output.
191                if (scalar(keys %specialDataList)) {
192                    $specialData{$specialColumn} = \%specialDataList;
193                }
194            }
195            # Set up the column names.
196            my @columnNames = "Strain annotated in NMPDR";
197            push @columnNames, sort keys %specialData;
198            push @columnNames,  "Genome size, bp",
199                                                   "Protein Encoding Genes (PEGs)",                                                   "Protein Encoding Genes (PEGs)",
200                                                   "Named genes in subsystems",            # s0                                                   "Named genes in subsystems",            # s0
201                                                   "Named genes not in subsystems",        # n0                                                   "Named genes not in subsystems",        # n0
202                                                   "Hypothetical genes in subsystems",     # s1                                                   "Hypothetical genes in subsystems",     # s1
203                                                   "Hypothetical genes not in subsystems", # n1                                                   "Hypothetical genes not in subsystems", # n1
204                                                   "RNAs")) . "\n";                              "Subsystems",
205          # Set up some useful stuff for the four count columns.                              "RNAs";
206          my %linkParms = ( s0 => "nohypo_sub", n0 => "nohypo_nosub",          # Start the table.
207                            s1 => "hypo_sub", n1 => "hypo_nosub" );          print GROUPFILE "<table class=\"$tableStyle\">\n";
208          my @columnTypes = ('s0', 'n0', 's1', 'n1');          # Create the header row.
209            print GROUPFILE Tr( { class => 'odd' }, th(\@columnNames)) . "\n";
210          # The data rows will be built next. We'll be putting them into a hash keyed by          # The data rows will be built next. We'll be putting them into a hash keyed by
211          # organism name. The hash enables us to spit them out sorted by name.          # organism name. The hash enables us to spit them out sorted by name.
212          my %rows = ();          my %rows = ();
213            # This variable is used to hold the counts.
214            my $num;
215          # Loop through the genomes in the new group.          # Loop through the genomes in the new group.
216          for my $genomeID (@newGenomes) {          for my $genomeID (@newGenomes) {
217                # Count this genome.
218                $groupTotals{genomes}++;
219              # Check to see if this genome is new.              # Check to see if this genome is new.
220              my $new = (! exists $oldGenomes{$genomeID} ? "new " : "");              my $new = (! exists $oldGenomes{$genomeID} ? "new " : "");
221              Trace("Processing ${new}genome $genomeID for $groupID.") if T(3);              Trace("Processing ${new}genome $genomeID for $groupID.") if T(3);
222              # Get the strain name.              # Get the strain name.
223              my $genomeName = $sprout->GenusSpecies($genomeID);              my $genomeName = $sprout->GenusSpecies($genomeID);
224                # Apply a link.
225                my $genomeText = CGI::a({ href => "../FIG/genome_statistics.cgi?genome=$genomeID;SPROUT=1" }, $genomeName);
226              # If this is a new strain, build the HTML for the NEW! mark.              # If this is a new strain, build the HTML for the NEW! mark.
227              if ($new) {              if ($new) {
228                  $new = " <span class=\"$markerStyle\">NEW!</span>";                  $new = " <span class=\"$markerStyle\">NEW!</span>";
229              }              }
230              # Get the genome length.              # Get the genome length.
231              my $genomeLen = $sprout->GenomeLength($genomeID);              $num = $sprout->GenomeLength($genomeID);
232                my $genomeLen = Tracer::CommaFormat($num);
233              # Get the number of PEGs.              # Get the number of PEGs.
234              my $pegCount = $sprout->FeatureCount($genomeID, 'peg');              $num = $sprout->FeatureCount($genomeID, 'peg');
235                my $pegCount = Tracer::CommaFormat($num);
236                $groupTotals{pegs} += $num;
237              # Get the number of RNAs.              # Get the number of RNAs.
238              my $rnaCount = $sprout->FeatureCount($genomeID, 'rna');              $num = $sprout->FeatureCount($genomeID, 'rna');
239                my $rnaCount = Tracer::CommaFormat($num);
240                $groupTotals{RNAs} += $num;
241                # If there are no RNAs, we say we don't know the number, since we know there
242                # must be RNAs somewhere.
243                if (! $rnaCount) {
244                    $rnaCount = "n/d";
245                }
246              # Now we have four categories of features to work with, for each              # Now we have four categories of features to work with, for each
247              # combination of named or hypothetical vs. in-subsystem or              # combination of named or hypothetical vs. in-subsystem or
248              # not-in-subsystem. First, we get all of the feature assignments for              # not-in-subsystem. First, we get all of the feature assignments for
249              # the genome.              # the genome.
250              my $assignHash = $sprout->GenomeAssignments($genomeID);              my $assignHash = $sprout->GenomeAssignments($genomeID);
251              # Next, we get all of the features in the genome that belong to a              # Next, we get all of the features in the genome that belong to a
252              # subsystem. This involves a query via the subsystem spreadsheet.              # subsystem.
253              my %ssHash = map { $_ => 1 } $sprout->GetFlat(['IsGenomeOf', 'ContainsFeature'],              my %ssHash = $sprout->GenomeSubsystemData($genomeID);
                                                     "IsGenomeOf(from-link) = ?",  
                                                     [$genomeID], 'ContainsFeature(to-link)');  
254              # Create a hash to track the four categories. "s" or "n" indicates              # Create a hash to track the four categories. "s" or "n" indicates
255              # in or out of a subsystem. "1" or "0" indicates hypothetical or              # in or out of a subsystem. "1" or "0" indicates hypothetical or
256              # real.              # real.
# Line 191  Line 266 
266                  $totalFeatures++;                  $totalFeatures++;
267              }              }
268              Trace("$totalFeatures total features found for $genomeID.") if T(3);              Trace("$totalFeatures total features found for $genomeID.") if T(3);
269                for my $counterKey (@columnTypes) {
270                    $groupTotals{$counterKey} += $counters{$counterKey};
271                }
272                $groupTotals{features} += $totalFeatures;
273              # We have all our data. Next we need to compute the percentages and the links.              # We have all our data. Next we need to compute the percentages and the links.
274              # First, the link stuff.              # First, the link stuff.
275              my $linkPrefix = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&request=";              my $linkPrefix = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&request=";
# Line 198  Line 277 
277              for my $type (keys %linkParms) {              for my $type (keys %linkParms) {
278                  $counters{$type} = a( { href => "$linkPrefix$linkParms{$type}" },                  $counters{$type} = a( { href => "$linkPrefix$linkParms{$type}" },
279                                       sprintf("%d(%.1f%%)", $counters{$type},                                       sprintf("%d(%.1f%%)", $counters{$type},
280                                               $counters{$type} * 100 / $totalFeatures));                                               Tracer::Percent($counters{$type}, $totalFeatures)));
281              }              }
282              # Create the row text.              my @counterValues = map { $counters{$_} } @columnTypes;
283              my $rowHtml = td( "$genomeName$new", $genomeLen, $pegCount,              # The last link is a button to look at the subsystem summaries.
284                                map { $counters{$_} } @columnTypes,              my $ssCount = $sprout->GetCount(['ParticipatesIn'], 'ParticipatesIn(from-link) = ?',
285                                $rnaCount );                                                 [$genomeID]);
286                my $ssLink = "$options->{linkCGI}?user=\&genome=$genomeID&SPROUT=1&show_subsystems=1";
287                my $ssCol = "<a href=\"$ssLink\">$ssCount</a>";
288                # Start creating the table cells.
289                my $rowHtml = td("$genomeText$new");
290                # Add any special columns.
291                for my $specialCol (keys %specialData) {
292                    # Here we get the attribute value. If there is none, we leave the column blank.
293                    my $attribute = $specialData{$specialCol}->{$genomeID} || "&nbsp;";
294                    $rowHtml .= td($attribute);
295                }
296                # Now add the data columns.
297                $rowHtml .= join("",
298                                td({ class => $numStyle }, $genomeLen),
299                                td({ class => $numStyle }, $pegCount),
300                                td({ class => $counterStyle }, \@counterValues),
301                                td({ class => $numStyle }, $ssCol),
302                                td({ class => $numStyle }, $rnaCount),
303                                );
304              # Put it in the row hash.              # Put it in the row hash.
305              $rows{$genomeName} = $rowHtml;              $rows{$genomeName} = $rowHtml;
306          }          }
# Line 222  Line 319 
319              # Count the row.              # Count the row.
320              $rowCount++;              $rowCount++;
321          }          }
322          # All done, close the file.          # All done, terminate the table and close the file.
323            print GROUPFILE "</table>\n";
324          close GROUPFILE;          close GROUPFILE;
325          Trace("$rowCount genomes processed.") if T(2);          Trace("$rowCount genomes processed.") if T(2);
326            # Now save the group totals.
327            $summaries{$groupID} = \%groupTotals;
328      }      }
329        # Now produce the summary table.
330        my $sumFileName = "stats-groups.inc";
331        Open(\*SUMFILE, ">$targetDir/$sumFileName");
332        # Start the table.
333        print SUMFILE "<table class=\"$tableStyle\">\n";
334        # Create the header row.
335        print SUMFILE Tr( { class => 'odd' }, th(["Group name",
336                                                 "Genomes",
337                                                 "Protein Encoding Genes (PEGs)",
338                                                 "Named genes in subsystems",            # s0
339                                                 "Named genes not in subsystems",        # n0
340                                                 "Hypothetical genes in subsystems",     # s1
341                                                 "Hypothetical genes not in subsystems", # n1
342                                                 "RNAs",
343                                                   ])) . "\n";
344        # Set up a flag for the odd-even styling.
345        my $rowFlag = 0;
346        # Put in the data rows.
347        for my $groupName (sort keys %summaries) {
348            my $group = $summaries{$groupName};
349            # Compute the link for the current group.
350            my $groupLink = a({ href => $groupData{$groupName}->[0] }, $groupName);
351            # Create the table row.
352            my $rowHtml = join("",
353                               td($groupLink),
354                               td({ class => $numStyle }, Tracer::CommaFormat($group->{genomes})),
355                               td({ class => $numStyle }, Tracer::CommaFormat($group->{pegs})),
356                               td({ class => $counterStyle }, [ map { Tracer::CommaFormat($group->{$_}) } @columnTypes ]),
357                               td({ class => $numStyle }, Tracer::CommaFormat($group->{RNAs})),
358                              );
359            print SUMFILE Tr( { class => $rowStyle[$rowFlag] }, $rowHtml ) . "\n";
360            # Flip the row style.
361            $rowFlag = 1 - $rowFlag;
362        }
363        # Terminate the table and close the file.
364        print SUMFILE "</table>\n";
365        close SUMFILE;
366      # We're all done.      # We're all done.
367      Trace("Processing complete.") if T(2);      Trace("Processing complete.") if T(2);
368  }  }
369    
 =head3 Fix  
   
 C<< my %fixedHash = Fix(%groupHash); >>  
   
 Prepare a genome group hash for processing. Groups with the same primary name will be combined.  
 The primary name is the first capitalized word in the group name.  
   
 =over 4  
   
 =item groupHash  
   
 Hash to be fixed up.  
   
 =item RETURN  
   
 Returns a fixed-up version of the hash.  
   
 =back  
   
 =cut  
   
 sub Fix {  
     # Get the parameters.  
     my (%groupHash) = @_;  
     # Create the result hash.  
     my %retVal = ();  
     # Copy over the genomes.  
     for my $groupID (keys %groupHash) {  
         # Make a safety copy of the group ID.  
         my $realGroupID = $groupID;  
         # Yank the primary name.  
         if ($groupID =~ /([A-Z]\w+)/) {  
             $realGroupID = $1;  
         }  
         # Append this group's genomes into the result hash.  
         Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});  
     }  
     # Return the result hash.  
     return %retVal;  
 }  
   
   
370  1;  1;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.29

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3