[Bio] / Sprout / GenomeSaplingLoader.pm Repository:
ViewVC logotype

Annotation of /Sprout/GenomeSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     package GenomeSaplingLoader;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use base 'BaseSaplingLoader';
26 :    
27 :     =head1 Sapling Genome Load Group Class
28 :    
29 :     =head2 Introduction
30 :    
31 :     The Load Group includes all of the major genome-related tables.
32 :    
33 :     =head3 new
34 :    
35 :     my $sl = GenomeSaplingLoader->new($erdb, $source, $options, @tables);
36 :    
37 :     Construct a new GenomeSaplingLoader object.
38 :    
39 :     =over 4
40 :    
41 :     =item erdb
42 :    
43 : parrello 1.3 L<Sapling> object for the database being loaded.
44 : parrello 1.1
45 :     =item options
46 :    
47 :     Reference to a hash of command-line options.
48 :    
49 :     =item tables
50 :    
51 :     List of tables in this load group.
52 :    
53 :     =back
54 :    
55 :     =cut
56 :    
57 :     sub new {
58 :     # Get the parameters.
59 :     my ($class, $erdb, $options) = @_;
60 :     # Create the table list.
61 : parrello 1.2 my @tables = sort qw(GenomeSet IsMadeUpOf IsCollectionOf Genome IsTaxonomyOf TaxonomicGrouping
62 :     TaxonomicGroupingAlias IsGroupFor Contig HasSection DNASequence);
63 : parrello 1.1 # Create the BaseSaplingLoader object.
64 :     my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
65 :     # Return it.
66 :     return $retVal;
67 :     }
68 :    
69 :     =head2 Public Methods
70 :    
71 :     =head3 Generate
72 :    
73 :     $sl->Generate();
74 :    
75 :     Generate the data for the genome-related files.
76 :    
77 :     =cut
78 :    
79 :     sub Generate {
80 :     # Get the parameters.
81 :     my ($self) = @_;
82 :     # Process according to the type of section.
83 :     if ($self->global()) {
84 :     # This is the global section. Create the taxonomic hierarchy.
85 :     $self->CreateTaxonomies();
86 : parrello 1.2 # Create the genome sets.
87 :     $self->CreateGenomeSets();
88 : parrello 1.1 } else {
89 :     # Get the section ID.
90 :     my $genomeID = $self->section();
91 :     # This is a genome section. Create the data for the genome.
92 :     $self->PlaceGenome($genomeID);
93 :     }
94 :     }
95 :    
96 : parrello 1.2 =head3 CreateGenomeSets
97 :    
98 :     $sl->CreateGenomeSets();
99 :    
100 :     Generate the genome sets. This includes the GenomeSet and IsCollectionOf
101 :     tables.
102 :    
103 :     =cut
104 :    
105 :     sub CreateGenomeSets {
106 :     # Get the parameters.
107 :     my ($self) = @_;
108 :     # Get the genome hash. Only genomes in this hash will be put into a set.
109 :     my $sapling = $self->db();
110 :     my $genomeHash = $sapling->GenomeHash();
111 :     # We'll track genome set names in here. The set name is the most common
112 :     # genus in the set with an optional number for uniqueness.
113 :     my %setNames;
114 :     # Get the genome set file.
115 :     my $ih = Open(undef, "<$FIG_Config::global/genome.sets");
116 :     # We will accumulate set data and output a set at the end of each set group.
117 :     # This will be a list of genome IDs for the set.
118 :     my @genomes;
119 :     # This will contain the genus counts.
120 :     my %names;
121 :     # This will be the set ID number.
122 :     my $setID;
123 :     # Loop through the set file.
124 :     while (! eof $ih) {
125 :     # Get the next record.
126 :     $self->Add("set-records" => 1);
127 :     my ($newSetID, $genomeID, $name) = Tracer::GetLine($ih);
128 : parrello 1.7 # Only accept the genome if it's one of ours.
129 : parrello 1.2 if ($genomeHash->{$genomeID}) {
130 : parrello 1.7 # Is this a new set?
131 :     if ($newSetID != $setID) {
132 :     # Yes. Output the old set.
133 :     $self->OutputGenomeSet(\%names, $setID, \@genomes);
134 :     # Clear the set data.
135 :     %names = ();
136 :     @genomes = ();
137 :     # Save the new set ID.
138 :     $setID = $newSetID;
139 :     }
140 :     # Only proceed if this is one of our genomes.
141 :     if ($genomeHash->{$genomeID}) {
142 :     $self->Add("set-genomes" => 1);
143 :     # Save the genome ID.
144 :     push @genomes, $genomeID;
145 :     # Remember it as the representative if it's the first in the set.
146 :     # Count the genus.
147 :     my ($genus) = split /\s/, $name, 2;
148 :     $names{$genus}++;
149 :     }
150 : parrello 1.2 }
151 :     }
152 :     # Close the input file.
153 :     close $ih;
154 :     # Output the last set.
155 : parrello 1.6 $self->OutputGenomeSet(\%names, $setID, \@genomes);
156 : parrello 1.2 }
157 :    
158 :     =head3 OutputGenomeSet
159 :    
160 : parrello 1.6 $sl->OutputGenomeSet(\%names, $setID, \@genomes);
161 : parrello 1.2
162 : parrello 1.6 Output the data for a genome set. The appropriate GenomeSet and IsCollectionOf
163 :     records will be generated for the genomes in the set.
164 : parrello 1.2
165 :     =over 4
166 :    
167 :     =item names
168 :    
169 :     Reference to a hash of the genus names used in the set. The hash maps each name
170 :     to the number of times it appeared.
171 :    
172 : parrello 1.6 =item setID
173 : parrello 1.2
174 : parrello 1.6 The ID to use for this set.
175 : parrello 1.2
176 :     =item genomes
177 :    
178 :     Reference to a list of the IDs for the genomes in the set.
179 :    
180 :     =back
181 :    
182 :     =cut
183 :    
184 :     sub OutputGenomeSet {
185 :     # Get the parameters.
186 : parrello 1.6 my ($self, $names, $setID, $genomes) = @_;
187 : parrello 1.2 # Only proceed if there is at least one genome.
188 :     my $count = scalar @$genomes;
189 :     if ($count) {
190 :     # Create the set record.
191 : parrello 1.6 $self->PutE(GenomeSet => $setID);
192 : parrello 1.2 # This will be TRUE for the first genome and FALSE thereafter, insuring that
193 :     # the first genome is used for the representative.
194 :     my $repFlag = 1;
195 :     # Connect all the genomes to it.
196 :     for my $genome (@$genomes) {
197 : parrello 1.6 $self->PutR(IsCollectionOf => $setID, $genome, representative => $repFlag);
198 : parrello 1.2 $repFlag = 0;
199 :     }
200 :     }
201 :     }
202 :    
203 :    
204 : parrello 1.1 =head3 CreateTaxonomies
205 :    
206 :     $sl->CreateTaxonomies();
207 :    
208 :     Generate the taxonomy hierarchy. This includes the TaxonomicGrouping,
209 : parrello 1.2 IsGroupFor, TaxonomicGroupingAlias, and IsTaxonomyOf relationships. The
210 :     taxonomy hierarchy is computed from the NCBI taxonomy dump.
211 : parrello 1.1
212 :     =cut
213 :    
214 :     sub CreateTaxonomies {
215 :     # Get the parameters.
216 :     my ($self) = @_;
217 :     # Get the Sapling object.
218 :     my $sapling = $self->db();
219 : parrello 1.2 # Get the name of the taxonomy dump directory.
220 :     my $taxDir = "$FIG_Config::global/Taxonomy";
221 :     # The first step is to read in all the names. We will build a hash that maps
222 :     # each taxonomy ID to a list of its names. The first scientific name encountered
223 :     # will be saved as the primary name. Only scientific names, synonoyms, and
224 :     # equivalent names will be kept.
225 :     my (%nameLists, %primaryNames);
226 :     my $ih = Open(undef, "<$taxDir/names.dmp");
227 :     while (! eof $ih) {
228 :     # Get the next name.
229 :     my ($taxID, $name, undef, $type) = GetTaxData($ih);
230 :     $self->Add('taxnames-in' => 1);
231 :     # Is this a scientific name?
232 :     if ($type =~ /scientific/i) {
233 :     # Yes. Save it if it is the first for this ID.
234 :     if (! exists $primaryNames{$taxID}) {
235 :     $primaryNames{$taxID} = $name;
236 :     }
237 :     # Add it to the name list.
238 :     push @{$nameLists{$taxID}}, $name;
239 :     $self->Add('taxnames-scientific' => 1);
240 :     } elsif ($type =~ /synonym|equivalent/i) {
241 :     # Here it's not scientific, but it's generally useful, so we keep it.
242 :     push @{$nameLists{$taxID}}, $name;
243 :     $self->Add('taxnames-other' => 1);
244 :     }
245 :     }
246 :     # Now we read in the taxonomy nodes. For each node, we generate a TaxonomicGrouping
247 :     # record, and we connect it to its parent using IsGroupFor.
248 :     close $ih;
249 :     $ih = Open(undef, "<$taxDir/nodes.dmp");
250 :     while (! eof $ih) {
251 :     # Get the data for this group.
252 :     my ($taxID, $parent, $type, undef, undef,
253 :     undef, undef, undef, undef, undef, $hidden) = GetTaxData($ih);
254 : parrello 1.4 # Determine whether or not this is a domain group. A domain group is
255 :     # terminal when doing taxonomy searches. The NCBI indicates a top-level
256 :     # node by making it a child of the root node 1. We also include
257 :     # super-kingdoms (archaea, eukaryota, bacteria), which are below cellular
258 :     # organisms but are still terminal in our book.
259 :     my $domain = ($type eq 'superkingdom') || ($parent == 1);
260 : parrello 1.2 # Get the node's name.
261 :     my $name = $primaryNames{$taxID};
262 :     # It's an error if there's no name.
263 :     Confess("No name found for tax ID $taxID.") if ! $name;
264 :     # Create the taxonomy group record.
265 :     $self->PutE(TaxonomicGrouping => $taxID, domain => $domain, hidden => $hidden,
266 :     scientific_name => $name);
267 :     # Create the alias records.
268 :     for my $alias (@{$nameLists{$taxID}}) {
269 :     $self->PutE(TaxonomicGroupingAlias => $taxID, alias => $alias);
270 : parrello 1.1 }
271 : parrello 1.2 # Connect the group to its parent.
272 :     $self->PutR(IsGroupFor => $parent, $taxID);
273 :     }
274 :     # Now we need to connect each genome to its taxonomic grouping.
275 :     # Get the genome hash. This gives us our list of genome IDs.
276 :     my $genomeHash = $sapling->GenomeHash();
277 :     # Loop through the genomes.
278 :     for my $genomeID (keys %$genomeHash) {
279 :     # Get this genome's taxonomic group.
280 :     my ($taxID) = split /\./, $genomeID, 2;
281 :     # Connect the genome and the group.
282 :     $self->PutR(IsTaxonomyOf => $taxID, $genomeID);
283 : parrello 1.1 }
284 :     }
285 :    
286 :    
287 :     =head3 PlaceGenome
288 :    
289 :     $sl->PlaceGenome($genomeID);
290 :    
291 :     Generate the data for a specific genome. This method generates data for
292 : parrello 1.2 the Genome, IsMadeUpOf, Contig, HasSection, and DNASequence tables.
293 : parrello 1.1
294 :     =over 4
295 :    
296 :     =item genomeID
297 :    
298 :     ID of the genome whose data is to be generated.
299 :    
300 :     =back
301 :    
302 :     =cut
303 :    
304 :     sub PlaceGenome {
305 :     # Get the parameters.
306 :     my ($self, $genomeID) = @_;
307 :     # Get the Sapling object.
308 :     my $sapling = $self->db();
309 :     # Get the source object.
310 :     my $fig = $sapling->GetSourceObject();
311 :     # We start with the genome record itself, asking the FIG object
312 :     # for its various properties.
313 : parrello 1.2 my $scientific_name = $fig->genus_species($genomeID);
314 : parrello 1.1 my $complete = $fig->is_complete($genomeID);
315 :     my $dna_size = $fig->genome_szdna($genomeID);
316 :     my $pegs = $fig->genome_pegs($genomeID);
317 :     my $rnas = $fig->genome_rnas($genomeID);
318 : parrello 1.5 my $domain = $fig->genome_domain($genomeID);
319 :     my $prokaryotic = ($domain =~ /bacter|archae/i);
320 : parrello 1.1 # We need to compute the number of contigs from the list of contig IDs.
321 :     my @contigIDs = $fig->contigs_of($genomeID);
322 :     my $contigs = scalar(@contigIDs);
323 :     # Write the genome record.
324 :     $self->PutE(Genome => $genomeID, complete => $complete, contigs => $contigs,
325 : parrello 1.2 dna_size => $dna_size, scientific_name => $scientific_name,
326 : parrello 1.5 pegs => $pegs, rnas => $rnas, prokaryotic => $prokaryotic,
327 :     domain => $domain);
328 : parrello 1.2 # Now we create the Contigs. Each one needs to be split into DNA sequences.
329 : parrello 1.1 for my $contigID (@contigIDs) {
330 :     $self->Track(Contigs => $contigID, 100);
331 :     # Get the contig length.
332 :     my $length = $fig->contig_ln($genomeID, $contigID);
333 :     # Generate the contig record. Note that the contig ID includes
334 :     # the genome ID as a prefix. Otherwise, it would be non-unique.
335 :     my $realContigID = "$genomeID:$contigID";
336 : parrello 1.2 $self->PutE(Contig => $realContigID, length => $length);
337 : parrello 1.1 $self->PutR(IsMadeUpOf => $genomeID, $realContigID);
338 : parrello 1.2 # Now we loop through the DNA chunks.
339 :     my $loc = 1;
340 :     my $ordinal = 0;
341 :     my $segmentLength = $sapling->TuningParameter('maxSequenceLength');
342 :     while ($loc < $length) {
343 :     # Get this segment's true length.
344 :     my $trueLength = Tracer::Min($length - $loc, $segmentLength);
345 :     # Compute the index of this segment's last base pair.
346 :     my $endPoint = $loc + $trueLength - 1;
347 :     # Get the DNA.
348 :     my $chunkDNA = $fig->get_dna($genomeID, $contigID, $loc, $endPoint);
349 :     # Create its sequence record.
350 :     my $paddedOrdinal = Tracer::Pad($ordinal, 7, 1, '0');
351 :     my $seqID = "$realContigID:$paddedOrdinal";
352 :     $self->PutE(DNASequence => $seqID, sequence => $chunkDNA);
353 :     $self->Add('dna-letters' => $trueLength);
354 :     # Connect it to the contig.
355 :     $self->PutR(HasSection => $realContigID, $seqID);
356 :     # Move to the next section of the contig.
357 :     $loc = $endPoint + 1;
358 :     $ordinal++;
359 :     }
360 :     }
361 :     }
362 :    
363 :     =head3 GetTaxData
364 :    
365 :     my @fields = GenomeSaplingLoader::GetTaxData($ih);
366 :    
367 :     Read a taxonomy dump record and return its fields in a list. Taxonomy
368 :     dump records end in a tab-bar-newline sequence, and fields are separated
369 :     by a tab-bar-tab sequence, a more complex arrangement than is used in
370 :     standard tab-delimited files.
371 :    
372 :     =over 4
373 :    
374 :     =item ih
375 :    
376 :     Open input handle for the taxonomy dump file.
377 :    
378 :     =item RETURN
379 :    
380 :     Returns a list of the fields in the record read.
381 :    
382 :     =back
383 :    
384 :     =cut
385 :    
386 :     sub GetTaxData {
387 :     # Get the parameters.
388 :     my ($ih) = @_;
389 :     # Temporarily change the end-of-record character.
390 :     local $/ = "\t|\n";
391 :     # Read the next record.
392 :     my $line = <$ih>;
393 :     # Chop off the end, if any.
394 :     if ($line =~ /(.+)\t\|\n$/) {
395 :     $line = $1;
396 : parrello 1.1 }
397 : parrello 1.2 # Split the line into fields.
398 :     my @retVal = split /\t\|\t/, $line;
399 :     # Return the result.
400 :     return @retVal;
401 : parrello 1.1 }
402 :    
403 :    
404 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3