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

Annotation of /Sprout/GenomeSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (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 : parrello 1.12 my $taxDir = "/vol/biodb/ncbi/taxonomy";
221 : parrello 1.2 # 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 : parrello 1.10 # record, and we connect it to its parent using IsGroupFor. We also keep the node ID
248 :     # for later so we know what's available.
249 : parrello 1.2 close $ih;
250 :     $ih = Open(undef, "<$taxDir/nodes.dmp");
251 :     while (! eof $ih) {
252 :     # Get the data for this group.
253 :     my ($taxID, $parent, $type, undef, undef,
254 :     undef, undef, undef, undef, undef, $hidden) = GetTaxData($ih);
255 : parrello 1.4 # Determine whether or not this is a domain group. A domain group is
256 :     # terminal when doing taxonomy searches. The NCBI indicates a top-level
257 :     # node by making it a child of the root node 1. We also include
258 :     # super-kingdoms (archaea, eukaryota, bacteria), which are below cellular
259 :     # organisms but are still terminal in our book.
260 :     my $domain = ($type eq 'superkingdom') || ($parent == 1);
261 : parrello 1.2 # Get the node's name.
262 :     my $name = $primaryNames{$taxID};
263 :     # It's an error if there's no name.
264 :     Confess("No name found for tax ID $taxID.") if ! $name;
265 :     # Create the taxonomy group record.
266 :     $self->PutE(TaxonomicGrouping => $taxID, domain => $domain, hidden => $hidden,
267 :     scientific_name => $name);
268 :     # Create the alias records.
269 :     for my $alias (@{$nameLists{$taxID}}) {
270 :     $self->PutE(TaxonomicGroupingAlias => $taxID, alias => $alias);
271 : parrello 1.1 }
272 : parrello 1.2 # Connect the group to its parent.
273 :     $self->PutR(IsGroupFor => $parent, $taxID);
274 :     }
275 : parrello 1.10 # Read in the merge file. The merge file tells us which old IDs are mapped to
276 :     # new IDs. We need this to connect genomes with old IDs to the correct group.
277 :     my %merges;
278 :     $ih = Open(undef, "<$taxDir/merged.dmp");
279 :     while (! eof $ih) {
280 :     # Get this merge record.
281 :     my ($oldID, $newID) = GetTaxData($ih);
282 :     # Store it in the hash.
283 :     $merges{$oldID} = $newID;
284 :     }
285 : parrello 1.2 # Now we need to connect each genome to its taxonomic grouping.
286 :     # Get the genome hash. This gives us our list of genome IDs.
287 :     my $genomeHash = $sapling->GenomeHash();
288 :     # Loop through the genomes.
289 :     for my $genomeID (keys %$genomeHash) {
290 :     # Get this genome's taxonomic group.
291 :     my ($taxID) = split /\./, $genomeID, 2;
292 : parrello 1.10 # Check to see if we have this tax ID. If we don't, we check for a merge.
293 :     if (! $primaryNames{$taxID}) {
294 :     if ($merges{$taxID}) {
295 :     $taxID = $merges{$taxID};
296 :     $self->Add('merged-names' => 1);
297 : parrello 1.11 Trace("$genomeID has alternate taxonomy ID $taxID.") if T(ERDBLoadGroup => 2);
298 : parrello 1.10 } else {
299 :     $taxID = undef;
300 :     $self->Add('missing-groups' => 1);
301 : parrello 1.11 Trace("$genomeID has no taxonomy group.") if T(ERDBLoadGroup => 1);
302 : parrello 1.10 }
303 :     }
304 :     # Connect the genome and the group if the group is real.
305 :     if (defined $taxID) {
306 :     $self->PutR(IsTaxonomyOf => $taxID, $genomeID);
307 :     }
308 : parrello 1.1 }
309 :     }
310 :    
311 :    
312 :     =head3 PlaceGenome
313 :    
314 :     $sl->PlaceGenome($genomeID);
315 :    
316 :     Generate the data for a specific genome. This method generates data for
317 : parrello 1.2 the Genome, IsMadeUpOf, Contig, HasSection, and DNASequence tables.
318 : parrello 1.1
319 :     =over 4
320 :    
321 :     =item genomeID
322 :    
323 :     ID of the genome whose data is to be generated.
324 :    
325 :     =back
326 :    
327 :     =cut
328 :    
329 :     sub PlaceGenome {
330 :     # Get the parameters.
331 :     my ($self, $genomeID) = @_;
332 :     # Get the Sapling object.
333 :     my $sapling = $self->db();
334 :     # Get the source object.
335 :     my $fig = $sapling->GetSourceObject();
336 : parrello 1.8 # Get the DNA chunk size.
337 :     my $segmentLength = $sapling->TuningParameter('maxSequenceLength');
338 :     Trace("DNA chunk size is $segmentLength.") if T(ERDBLoadGroup => 3);
339 : parrello 1.1 # We start with the genome record itself, asking the FIG object
340 :     # for its various properties.
341 : parrello 1.2 my $scientific_name = $fig->genus_species($genomeID);
342 : parrello 1.1 my $complete = $fig->is_complete($genomeID);
343 :     my $dna_size = $fig->genome_szdna($genomeID);
344 :     my $pegs = $fig->genome_pegs($genomeID);
345 :     my $rnas = $fig->genome_rnas($genomeID);
346 : parrello 1.5 my $domain = $fig->genome_domain($genomeID);
347 :     my $prokaryotic = ($domain =~ /bacter|archae/i);
348 : parrello 1.1 # We need to compute the number of contigs from the list of contig IDs.
349 :     my @contigIDs = $fig->contigs_of($genomeID);
350 :     my $contigs = scalar(@contigIDs);
351 : parrello 1.8 # Compute the genetic code. Normally, it's 11, but it may be overridden
352 :     # by a GENETIC_CODE file.
353 :     my $gcFile = "$FIG_Config::organisms/$genomeID/GENETIC_CODE";
354 :     my $genetic_code = 11;
355 :     if (-f $gcFile) {
356 :     $genetic_code = Tracer::GetFile($gcFile);
357 :     chomp $genetic_code;
358 :     }
359 : parrello 1.1 # Write the genome record.
360 :     $self->PutE(Genome => $genomeID, complete => $complete, contigs => $contigs,
361 : parrello 1.2 dna_size => $dna_size, scientific_name => $scientific_name,
362 : parrello 1.5 pegs => $pegs, rnas => $rnas, prokaryotic => $prokaryotic,
363 : parrello 1.8 domain => $domain, genetic_code => $genetic_code);
364 : parrello 1.2 # Now we create the Contigs. Each one needs to be split into DNA sequences.
365 : parrello 1.1 for my $contigID (@contigIDs) {
366 :     $self->Track(Contigs => $contigID, 100);
367 :     # Get the contig length.
368 :     my $length = $fig->contig_ln($genomeID, $contigID);
369 :     # Generate the contig record. Note that the contig ID includes
370 :     # the genome ID as a prefix. Otherwise, it would be non-unique.
371 :     my $realContigID = "$genomeID:$contigID";
372 : parrello 1.2 $self->PutE(Contig => $realContigID, length => $length);
373 : parrello 1.1 $self->PutR(IsMadeUpOf => $genomeID, $realContigID);
374 : parrello 1.2 # Now we loop through the DNA chunks.
375 :     my $loc = 1;
376 :     my $ordinal = 0;
377 : parrello 1.9 while ($loc <= $length) {
378 : parrello 1.2 # Get this segment's true length.
379 : parrello 1.9 my $trueLength = Tracer::Min($length + 1 - $loc, $segmentLength);
380 : parrello 1.2 # Compute the index of this segment's last base pair.
381 :     my $endPoint = $loc + $trueLength - 1;
382 :     # Get the DNA.
383 :     my $chunkDNA = $fig->get_dna($genomeID, $contigID, $loc, $endPoint);
384 :     # Create its sequence record.
385 :     my $paddedOrdinal = Tracer::Pad($ordinal, 7, 1, '0');
386 :     my $seqID = "$realContigID:$paddedOrdinal";
387 :     $self->PutE(DNASequence => $seqID, sequence => $chunkDNA);
388 :     $self->Add('dna-letters' => $trueLength);
389 :     # Connect it to the contig.
390 :     $self->PutR(HasSection => $realContigID, $seqID);
391 :     # Move to the next section of the contig.
392 :     $loc = $endPoint + 1;
393 :     $ordinal++;
394 :     }
395 :     }
396 :     }
397 :    
398 :     =head3 GetTaxData
399 :    
400 :     my @fields = GenomeSaplingLoader::GetTaxData($ih);
401 :    
402 :     Read a taxonomy dump record and return its fields in a list. Taxonomy
403 :     dump records end in a tab-bar-newline sequence, and fields are separated
404 :     by a tab-bar-tab sequence, a more complex arrangement than is used in
405 :     standard tab-delimited files.
406 :    
407 :     =over 4
408 :    
409 :     =item ih
410 :    
411 :     Open input handle for the taxonomy dump file.
412 :    
413 :     =item RETURN
414 :    
415 :     Returns a list of the fields in the record read.
416 :    
417 :     =back
418 :    
419 :     =cut
420 :    
421 :     sub GetTaxData {
422 :     # Get the parameters.
423 :     my ($ih) = @_;
424 :     # Temporarily change the end-of-record character.
425 :     local $/ = "\t|\n";
426 :     # Read the next record.
427 :     my $line = <$ih>;
428 :     # Chop off the end, if any.
429 :     if ($line =~ /(.+)\t\|\n$/) {
430 :     $line = $1;
431 : parrello 1.1 }
432 : parrello 1.2 # Split the line into fields.
433 :     my @retVal = split /\t\|\t/, $line;
434 :     # Return the result.
435 :     return @retVal;
436 : parrello 1.1 }
437 :    
438 :    
439 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3