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

Annotation of /Sprout/GenomeSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (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 :     # Is this a new set?
129 :     if ($newSetID != $setID) {
130 :     # Yes. Output the old set.
131 : parrello 1.6 $self->OutputGenomeSet(\%names, $setID, \@genomes);
132 : parrello 1.2 # Clear the set data.
133 :     %names = ();
134 :     @genomes = ();
135 :     # Save the new set ID.
136 :     $setID = $newSetID;
137 :     }
138 :     # Only proceed if this is one of our genomes.
139 :     if ($genomeHash->{$genomeID}) {
140 :     $self->Add("set-genomes" => 1);
141 :     # Save the genome ID.
142 :     push @genomes, $genomeID;
143 :     # Remember it as the representative if it's the first in the set.
144 :     # Count the genus.
145 :     my ($genus) = split /\s/, $name, 2;
146 :     $names{$genus}++;
147 :     }
148 :     }
149 :     # Close the input file.
150 :     close $ih;
151 :     # Output the last set.
152 : parrello 1.6 $self->OutputGenomeSet(\%names, $setID, \@genomes);
153 : parrello 1.2 }
154 :    
155 :     =head3 OutputGenomeSet
156 :    
157 : parrello 1.6 $sl->OutputGenomeSet(\%names, $setID, \@genomes);
158 : parrello 1.2
159 : parrello 1.6 Output the data for a genome set. The appropriate GenomeSet and IsCollectionOf
160 :     records will be generated for the genomes in the set.
161 : parrello 1.2
162 :     =over 4
163 :    
164 :     =item names
165 :    
166 :     Reference to a hash of the genus names used in the set. The hash maps each name
167 :     to the number of times it appeared.
168 :    
169 : parrello 1.6 =item setID
170 : parrello 1.2
171 : parrello 1.6 The ID to use for this set.
172 : parrello 1.2
173 :     =item genomes
174 :    
175 :     Reference to a list of the IDs for the genomes in the set.
176 :    
177 :     =back
178 :    
179 :     =cut
180 :    
181 :     sub OutputGenomeSet {
182 :     # Get the parameters.
183 : parrello 1.6 my ($self, $names, $setID, $genomes) = @_;
184 : parrello 1.2 # Only proceed if there is at least one genome.
185 :     my $count = scalar @$genomes;
186 :     if ($count) {
187 :     # Create the set record.
188 : parrello 1.6 $self->PutE(GenomeSet => $setID);
189 : parrello 1.2 # This will be TRUE for the first genome and FALSE thereafter, insuring that
190 :     # the first genome is used for the representative.
191 :     my $repFlag = 1;
192 :     # Connect all the genomes to it.
193 :     for my $genome (@$genomes) {
194 : parrello 1.6 $self->PutR(IsCollectionOf => $setID, $genome, representative => $repFlag);
195 : parrello 1.2 $repFlag = 0;
196 :     }
197 :     }
198 :     }
199 :    
200 :    
201 : parrello 1.1 =head3 CreateTaxonomies
202 :    
203 :     $sl->CreateTaxonomies();
204 :    
205 :     Generate the taxonomy hierarchy. This includes the TaxonomicGrouping,
206 : parrello 1.2 IsGroupFor, TaxonomicGroupingAlias, and IsTaxonomyOf relationships. The
207 :     taxonomy hierarchy is computed from the NCBI taxonomy dump.
208 : parrello 1.1
209 :     =cut
210 :    
211 :     sub CreateTaxonomies {
212 :     # Get the parameters.
213 :     my ($self) = @_;
214 :     # Get the Sapling object.
215 :     my $sapling = $self->db();
216 : parrello 1.2 # Get the name of the taxonomy dump directory.
217 :     my $taxDir = "$FIG_Config::global/Taxonomy";
218 :     # The first step is to read in all the names. We will build a hash that maps
219 :     # each taxonomy ID to a list of its names. The first scientific name encountered
220 :     # will be saved as the primary name. Only scientific names, synonoyms, and
221 :     # equivalent names will be kept.
222 :     my (%nameLists, %primaryNames);
223 :     my $ih = Open(undef, "<$taxDir/names.dmp");
224 :     while (! eof $ih) {
225 :     # Get the next name.
226 :     my ($taxID, $name, undef, $type) = GetTaxData($ih);
227 :     $self->Add('taxnames-in' => 1);
228 :     # Is this a scientific name?
229 :     if ($type =~ /scientific/i) {
230 :     # Yes. Save it if it is the first for this ID.
231 :     if (! exists $primaryNames{$taxID}) {
232 :     $primaryNames{$taxID} = $name;
233 :     }
234 :     # Add it to the name list.
235 :     push @{$nameLists{$taxID}}, $name;
236 :     $self->Add('taxnames-scientific' => 1);
237 :     } elsif ($type =~ /synonym|equivalent/i) {
238 :     # Here it's not scientific, but it's generally useful, so we keep it.
239 :     push @{$nameLists{$taxID}}, $name;
240 :     $self->Add('taxnames-other' => 1);
241 :     }
242 :     }
243 :     # Now we read in the taxonomy nodes. For each node, we generate a TaxonomicGrouping
244 :     # record, and we connect it to its parent using IsGroupFor.
245 :     close $ih;
246 :     $ih = Open(undef, "<$taxDir/nodes.dmp");
247 :     while (! eof $ih) {
248 :     # Get the data for this group.
249 :     my ($taxID, $parent, $type, undef, undef,
250 :     undef, undef, undef, undef, undef, $hidden) = GetTaxData($ih);
251 : parrello 1.4 # Determine whether or not this is a domain group. A domain group is
252 :     # terminal when doing taxonomy searches. The NCBI indicates a top-level
253 :     # node by making it a child of the root node 1. We also include
254 :     # super-kingdoms (archaea, eukaryota, bacteria), which are below cellular
255 :     # organisms but are still terminal in our book.
256 :     my $domain = ($type eq 'superkingdom') || ($parent == 1);
257 : parrello 1.2 # Get the node's name.
258 :     my $name = $primaryNames{$taxID};
259 :     # It's an error if there's no name.
260 :     Confess("No name found for tax ID $taxID.") if ! $name;
261 :     # Create the taxonomy group record.
262 :     $self->PutE(TaxonomicGrouping => $taxID, domain => $domain, hidden => $hidden,
263 :     scientific_name => $name);
264 :     # Create the alias records.
265 :     for my $alias (@{$nameLists{$taxID}}) {
266 :     $self->PutE(TaxonomicGroupingAlias => $taxID, alias => $alias);
267 : parrello 1.1 }
268 : parrello 1.2 # Connect the group to its parent.
269 :     $self->PutR(IsGroupFor => $parent, $taxID);
270 :     }
271 :     # Now we need to connect each genome to its taxonomic grouping.
272 :     # Get the genome hash. This gives us our list of genome IDs.
273 :     my $genomeHash = $sapling->GenomeHash();
274 :     # Loop through the genomes.
275 :     for my $genomeID (keys %$genomeHash) {
276 :     # Get this genome's taxonomic group.
277 :     my ($taxID) = split /\./, $genomeID, 2;
278 :     # Connect the genome and the group.
279 :     $self->PutR(IsTaxonomyOf => $taxID, $genomeID);
280 : parrello 1.1 }
281 :     }
282 :    
283 :    
284 :     =head3 PlaceGenome
285 :    
286 :     $sl->PlaceGenome($genomeID);
287 :    
288 :     Generate the data for a specific genome. This method generates data for
289 : parrello 1.2 the Genome, IsMadeUpOf, Contig, HasSection, and DNASequence tables.
290 : parrello 1.1
291 :     =over 4
292 :    
293 :     =item genomeID
294 :    
295 :     ID of the genome whose data is to be generated.
296 :    
297 :     =back
298 :    
299 :     =cut
300 :    
301 :     sub PlaceGenome {
302 :     # Get the parameters.
303 :     my ($self, $genomeID) = @_;
304 :     # Get the Sapling object.
305 :     my $sapling = $self->db();
306 :     # Get the source object.
307 :     my $fig = $sapling->GetSourceObject();
308 :     # We start with the genome record itself, asking the FIG object
309 :     # for its various properties.
310 : parrello 1.2 my $scientific_name = $fig->genus_species($genomeID);
311 : parrello 1.1 my $complete = $fig->is_complete($genomeID);
312 :     my $dna_size = $fig->genome_szdna($genomeID);
313 :     my $pegs = $fig->genome_pegs($genomeID);
314 :     my $rnas = $fig->genome_rnas($genomeID);
315 : parrello 1.5 my $domain = $fig->genome_domain($genomeID);
316 :     my $prokaryotic = ($domain =~ /bacter|archae/i);
317 : parrello 1.1 # We need to compute the number of contigs from the list of contig IDs.
318 :     my @contigIDs = $fig->contigs_of($genomeID);
319 :     my $contigs = scalar(@contigIDs);
320 :     # Write the genome record.
321 :     $self->PutE(Genome => $genomeID, complete => $complete, contigs => $contigs,
322 : parrello 1.2 dna_size => $dna_size, scientific_name => $scientific_name,
323 : parrello 1.5 pegs => $pegs, rnas => $rnas, prokaryotic => $prokaryotic,
324 :     domain => $domain);
325 : parrello 1.2 # Now we create the Contigs. Each one needs to be split into DNA sequences.
326 : parrello 1.1 for my $contigID (@contigIDs) {
327 :     $self->Track(Contigs => $contigID, 100);
328 :     # Get the contig length.
329 :     my $length = $fig->contig_ln($genomeID, $contigID);
330 :     # Generate the contig record. Note that the contig ID includes
331 :     # the genome ID as a prefix. Otherwise, it would be non-unique.
332 :     my $realContigID = "$genomeID:$contigID";
333 : parrello 1.2 $self->PutE(Contig => $realContigID, length => $length);
334 : parrello 1.1 $self->PutR(IsMadeUpOf => $genomeID, $realContigID);
335 : parrello 1.2 # Now we loop through the DNA chunks.
336 :     my $loc = 1;
337 :     my $ordinal = 0;
338 :     my $segmentLength = $sapling->TuningParameter('maxSequenceLength');
339 :     while ($loc < $length) {
340 :     # Get this segment's true length.
341 :     my $trueLength = Tracer::Min($length - $loc, $segmentLength);
342 :     # Compute the index of this segment's last base pair.
343 :     my $endPoint = $loc + $trueLength - 1;
344 :     # Get the DNA.
345 :     my $chunkDNA = $fig->get_dna($genomeID, $contigID, $loc, $endPoint);
346 :     # Create its sequence record.
347 :     my $paddedOrdinal = Tracer::Pad($ordinal, 7, 1, '0');
348 :     my $seqID = "$realContigID:$paddedOrdinal";
349 :     $self->PutE(DNASequence => $seqID, sequence => $chunkDNA);
350 :     $self->Add('dna-letters' => $trueLength);
351 :     # Connect it to the contig.
352 :     $self->PutR(HasSection => $realContigID, $seqID);
353 :     # Move to the next section of the contig.
354 :     $loc = $endPoint + 1;
355 :     $ordinal++;
356 :     }
357 :     }
358 :     }
359 :    
360 :     =head3 GetTaxData
361 :    
362 :     my @fields = GenomeSaplingLoader::GetTaxData($ih);
363 :    
364 :     Read a taxonomy dump record and return its fields in a list. Taxonomy
365 :     dump records end in a tab-bar-newline sequence, and fields are separated
366 :     by a tab-bar-tab sequence, a more complex arrangement than is used in
367 :     standard tab-delimited files.
368 :    
369 :     =over 4
370 :    
371 :     =item ih
372 :    
373 :     Open input handle for the taxonomy dump file.
374 :    
375 :     =item RETURN
376 :    
377 :     Returns a list of the fields in the record read.
378 :    
379 :     =back
380 :    
381 :     =cut
382 :    
383 :     sub GetTaxData {
384 :     # Get the parameters.
385 :     my ($ih) = @_;
386 :     # Temporarily change the end-of-record character.
387 :     local $/ = "\t|\n";
388 :     # Read the next record.
389 :     my $line = <$ih>;
390 :     # Chop off the end, if any.
391 :     if ($line =~ /(.+)\t\|\n$/) {
392 :     $line = $1;
393 : parrello 1.1 }
394 : parrello 1.2 # Split the line into fields.
395 :     my @retVal = split /\t\|\t/, $line;
396 :     # Return the result.
397 :     return @retVal;
398 : parrello 1.1 }
399 :    
400 :    
401 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3