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

Annotation of /Sprout/SproutLoad.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 :     package SproutLoad;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use PageBuilder;
8 :     use ERDBLoad;
9 :     use FIG;
10 :     use Sprout;
11 :     use Stats;
12 :     use BasicLocation;
13 :    
14 :     =head1 Sprout Load Methods
15 :    
16 :     =head2 Introduction
17 :    
18 :     This object contains the methods needed to copy data from the FIG data store to the
19 :     Sprout database. It makes heavy use of the ERDBLoad object to manage the load into
20 :     individual tables. The client can create an instance of this object and then
21 :     call methods for each group of tables to load. For example, the following code will
22 :     load the Genome- and Feature-related tables. (It is presumed the first command line
23 :     parameter contains the name of a file specifying the genomes.)
24 :    
25 :     my $fig = FIG->new();
26 :     my $sprout = SFXlate->new_sprout_only();
27 :     my $spl = SproutLoad->new($sprout, $fig, $ARGV[0]);
28 :     my $stats = $spl->LoadGenomeData();
29 :     $stats->Accumulate($spl->LoadFeatureData());
30 :     print $stats->Show();
31 :    
32 :     This module makes use of the internal Sprout property C<_erdb>.
33 :    
34 :     It is worth noting that the FIG object does not need to be a real one. Any object
35 :     that implements the FIG methods for data retrieval could be used. So, for example,
36 :     this object could be used to copy data from one Sprout database to another, or
37 :     from any FIG-compliant data story implemented in the future.
38 :    
39 :     To insure that this is possible, each time the FIG object is used, it will be via
40 :     a variable called C<$fig>. This makes it fairly straightforward to determine which
41 :     FIG methods are required to load the Sprout database.
42 :    
43 : parrello 1.5 This object creates the load files; however, the tables are not created until it
44 :     is time to actually do the load from the files into the target database.
45 :    
46 : parrello 1.1 =cut
47 :    
48 :     #: Constructor SproutLoad->new();
49 :    
50 :     =head2 Public Methods
51 :    
52 :     =head3 new
53 :    
54 :     C<< my $spl = SproutLoad->new($sprout, $fig, $genomeFile, $subsysFile); >>
55 :    
56 :     Construct a new Sprout Loader object, specifying the two participating databases and
57 :     the name of the files containing the list of genomes and subsystems to use.
58 :    
59 :     =over 4
60 :    
61 :     =item sprout
62 :    
63 :     Sprout object representing the target database. This also specifies the directory to
64 :     be used for creating the load files.
65 :    
66 :     =item fig
67 :    
68 :     FIG object representing the source data store from which the data is to be taken.
69 :    
70 :     =item genomeFile
71 :    
72 :     Either the name of the file containing the list of genomes to load or a reference to
73 :     a hash of genome IDs to access codes. If nothing is specified, all complete genomes
74 :     will be loaded and the access code will default to 1. The genome list is presumed
75 :     to be all-inclusive. In other words, all existing data in the target database will
76 :     be deleted and replaced with the data on the specified genes. If a file is specified,
77 :     it should contain one genome ID and access code per line, tab-separated.
78 :    
79 :     =item subsysFile
80 :    
81 :     Either the name of the file containing the list of trusted subsystems or a reference
82 :     to a list of subsystem names. If nothing is specified, all known subsystems will be
83 :     considered trusted. Only subsystem data related to the trusted subsystems is loaded.
84 :    
85 :     =back
86 :    
87 :     =cut
88 :    
89 :     sub new {
90 :     # Get the parameters.
91 :     my ($class, $sprout, $fig, $genomeFile, $subsysFile) = @_;
92 :     # Load the list of genomes into a hash.
93 :     my %genomes;
94 :     if (! defined($genomeFile) || $genomeFile eq '') {
95 :     # Here we want all the complete genomes and an access code of 1.
96 :     my @genomeList = $fig->genomes(1);
97 :     %genomes = map { $_ => 1 } @genomeList;
98 : parrello 1.3 } else {
99 :     my $type = ref $genomeFile;
100 :     Trace("Genome file parameter type is \"$type\".") if T(3);
101 :     if ($type eq 'HASH') {
102 :     # Here the user specified a hash of genome IDs to access codes, which is
103 :     # exactly what we want.
104 :     %genomes = %{$genomeFile};
105 :     } elsif (! $type || $type eq 'SCALAR' ) {
106 :     # The caller specified a file, so read the genomes from the file. (Note
107 :     # that some PERLs return an empty string rather than SCALAR.)
108 :     my @genomeList = Tracer::GetFile($genomeFile);
109 :     if (! @genomeList) {
110 :     # It's an error if the genome file is empty or not found.
111 :     Confess("No genomes found in file \"$genomeFile\".");
112 :     } else {
113 :     # We build the genome Hash using a loop rather than "map" so that
114 :     # an omitted access code can be defaulted to 1.
115 :     for my $genomeLine (@genomeList) {
116 :     my ($genomeID, $accessCode) = split("\t", $genomeLine);
117 :     if (undef $accessCode) {
118 :     $accessCode = 1;
119 :     }
120 :     $genomes{$genomeID} = $accessCode;
121 : parrello 1.1 }
122 :     }
123 : parrello 1.3 } else {
124 :     Confess("Invalid genome parameter ($type) in SproutLoad constructor.");
125 : parrello 1.1 }
126 :     }
127 :     # Load the list of trusted subsystems.
128 :     my %subsystems = ();
129 :     if (! defined $subsysFile || $subsysFile eq '') {
130 :     # Here we want all the subsystems.
131 :     %subsystems = map { $_ => 1 } $fig->all_subsystems();
132 : parrello 1.4 } else {
133 :     my $type = ref $subsysFile;
134 :     if ($type eq 'ARRAY') {
135 :     # Here the user passed in a list of subsystems.
136 :     %subsystems = map { $_ => 1 } @{$subsysFile};
137 :     } elsif (! $type || $type eq 'SCALAR') {
138 :     # Here the list of subsystems is in a file.
139 :     if (! -e $subsysFile) {
140 :     # It's an error if the file does not exist.
141 :     Confess("Trusted subsystem file not found.");
142 :     } else {
143 :     # GetFile automatically chomps end-of-line characters, so this
144 :     # is an easy task.
145 :     %subsystems = map { $_ => 1 } Tracer::GetFile($subsysFile);
146 :     }
147 : parrello 1.1 } else {
148 : parrello 1.4 Confess("Invalid subsystem parameter in SproutLoad constructor.");
149 : parrello 1.1 }
150 :     }
151 :     # Get the data directory from the Sprout object.
152 :     my ($directory) = $sprout->LoadInfo();
153 :     # Create the Sprout load object.
154 :     my $retVal = {
155 :     fig => $fig,
156 :     genomes => \%genomes,
157 :     subsystems => \%subsystems,
158 :     sprout => $sprout,
159 :     loadDirectory => $directory,
160 :     erdb => $sprout->{_erdb},
161 :     loaders => []
162 :     };
163 :     # Bless and return it.
164 :     bless $retVal, $class;
165 :     return $retVal;
166 :     }
167 :    
168 :     =head3 LoadGenomeData
169 :    
170 :     C<< my $stats = $spl->LoadGenomeData(); >>
171 :    
172 :     Load the Genome, Contig, and Sequence data from FIG into Sprout.
173 :    
174 :     The Sequence table is the largest single relation in the Sprout database, so this
175 :     method is expected to be slow and clumsy. At some point we will need to make it
176 :     restartable, since an error 10 gigabytes through a 20-gigabyte load is bound to be
177 :     very annoying otherwise.
178 :    
179 :     The following relations are loaded by this method.
180 :    
181 :     Genome
182 :     HasContig
183 :     Contig
184 :     IsMadeUpOf
185 :     Sequence
186 :    
187 :     =over 4
188 :    
189 :     =item RETURNS
190 :    
191 :     Returns a statistics object for the loads.
192 :    
193 :     =back
194 :    
195 :     B<TO DO>
196 :    
197 :     Real quality vectors instead of C<unknown> for everything.
198 :    
199 :     GenomeGroup relation. (The original script took group information from the C<NMPDR> file
200 :     in each genome's main directory, but no such file exists anywhere in my version of the
201 :     data store.)
202 :    
203 :     =cut
204 :     #: Return Type $%;
205 :     sub LoadGenomeData {
206 :     # Get this object instance.
207 :     my ($self) = @_;
208 :     # Get the FIG object.
209 :     my $fig = $self->{fig};
210 :     # Get the genome count.
211 :     my $genomeHash = $self->{genomes};
212 :     my $genomeCount = (keys %{$genomeHash});
213 :     Trace("Beginning genome data load.") if T(2);
214 :     # Create load objects for each of the tables we're loading.
215 :     my $loadGenome = $self->_TableLoader('Genome', $genomeCount);
216 :     my $loadHasContig = $self->_TableLoader('HasContig', $genomeCount * 300);
217 :     my $loadContig = $self->_TableLoader('Contig', $genomeCount * 300);
218 :     my $loadIsMadeUpOf = $self->_TableLoader('IsMadeUpOf', $genomeCount * 60000);
219 :     my $loadSequence = $self->_TableLoader('Sequence', $genomeCount * 60000);
220 :     # Now we loop through the genomes, generating the data for each one.
221 :     for my $genomeID (sort keys %{$genomeHash}) {
222 :     Trace("Loading data for genome $genomeID.") if T(3);
223 : parrello 1.6 $loadGenome->Add("genomeIn");
224 : parrello 1.1 # The access code comes in via the genome hash.
225 :     my $accessCode = $genomeHash->{$genomeID};
226 :     # Get the genus, species, and strain from the scientific name. Note that we append
227 :     # the genome ID to the strain. In some cases this is the totality of the strain name.
228 :     my ($genus, $species, @extraData) = split / /, $self->{fig}->genus_species($genomeID);
229 : parrello 1.4 my $extra = join " ", @extraData, "[$genomeID]";
230 : parrello 1.1 # Get the full taxonomy.
231 :     my $taxonomy = $fig->taxonomy_of($genomeID);
232 :     # Output the genome record.
233 :     $loadGenome->Put($genomeID, $accessCode, $fig->is_complete($genomeID), $genus,
234 :     $species, $extra, $taxonomy);
235 :     # Now we loop through each of the genome's contigs.
236 :     my @contigs = $fig->all_contigs($genomeID);
237 :     for my $contigID (@contigs) {
238 :     Trace("Processing contig $contigID for $genomeID.") if T(4);
239 : parrello 1.6 $loadContig->Add("contigIn");
240 :     $loadSequence->Add("contigIn");
241 : parrello 1.1 # Create the contig ID.
242 :     my $sproutContigID = "$genomeID:$contigID";
243 :     # Create the contig record and relate it to the genome.
244 :     $loadContig->Put($sproutContigID);
245 :     $loadHasContig->Put($genomeID, $sproutContigID);
246 :     # Now we need to split the contig into sequences. The maximum sequence size is
247 :     # a property of the Sprout object.
248 :     my $chunkSize = $self->{sprout}->MaxSequence();
249 :     # Now we get the sequence a chunk at a time.
250 :     my $contigLen = $fig->contig_ln($genomeID, $contigID);
251 :     for (my $i = 1; $i <= $contigLen; $i += $chunkSize) {
252 : parrello 1.6 $loadSequence->Add("chunkIn");
253 : parrello 1.1 # Compute the endpoint of this chunk.
254 :     my $end = FIG::min($i + $chunkSize - 1, $contigLen);
255 :     # Get the actual DNA.
256 :     my $dna = $fig->get_dna($genomeID, $contigID, $i, $end);
257 :     # Compute the sequenceID.
258 :     my $seqID = "$sproutContigID.$i";
259 :     # Write out the data. For now, the quality vector is always "unknown".
260 :     $loadIsMadeUpOf->Put($sproutContigID, $seqID, $end + 1 - $i, $i);
261 :     $loadSequence->Put($seqID, "unknown", $dna);
262 :     }
263 :     }
264 :     }
265 :     # Finish the loads.
266 :     my $retVal = $self->_FinishAll();
267 :     # Return the result.
268 :     return $retVal;
269 :     }
270 :    
271 :     =head3 LoadCouplingData
272 :    
273 :     C<< my $stats = $spl->LoadCouplingData(); >>
274 :    
275 :     Load the coupling and evidence data from FIG into Sprout.
276 :    
277 :     The coupling data specifies which genome features are functionally coupled. The
278 :     evidence data explains why the coupling is functional.
279 :    
280 :     The following relations are loaded by this method.
281 :    
282 :     Coupling
283 :     IsEvidencedBy
284 :     PCH
285 :     ParticipatesInCoupling
286 :     UsesAsEvidence
287 :    
288 :     =over 4
289 :    
290 :     =item RETURNS
291 :    
292 :     Returns a statistics object for the loads.
293 :    
294 :     =back
295 :    
296 :     =cut
297 :     #: Return Type $%;
298 :     sub LoadCouplingData {
299 :     # Get this object instance.
300 :     my ($self) = @_;
301 :     # Get the FIG object.
302 :     my $fig = $self->{fig};
303 :     # Get the genome hash.
304 :     my $genomeFilter = $self->{genomes};
305 :     my $genomeCount = (keys %{$genomeFilter});
306 :     my $featureCount = $genomeCount * 4000;
307 :     # Start the loads.
308 :     my $loadCoupling = $self->_TableLoader('Coupling', $featureCount * $genomeCount);
309 :     my $loadIsEvidencedBy = $self->_TableLoader('IsEvidencedBy', $featureCount * 8000);
310 :     my $loadPCH = $self->_TableLoader('PCH', $featureCount * 2000);
311 :     my $loadParticipatesInCoupling = $self->_TableLoader('ParticipatesInCoupling', $featureCount * 2000);
312 :     my $loadUsesAsEvidence = $self->_TableLoader('UsesAsEvidence', $featureCount * 8000);
313 :     Trace("Beginning coupling data load.") if T(2);
314 :     # Loop through the genomes found.
315 :     for my $genome (sort keys %{$genomeFilter}) {
316 :     Trace("Generating coupling data for $genome.") if T(3);
317 : parrello 1.6 $loadCoupling->Add("genomeIn");
318 : parrello 1.1 # Create a hash table for holding coupled pairs. We use this to prevent
319 :     # duplicates. For example, if A is coupled to B, we don't want to also
320 :     # assert that B is coupled to A, because we already know it. Fortunately,
321 :     # all couplings occur within a genome, so we can keep the hash table
322 :     # size reasonably small.
323 :     my %dupHash = ();
324 :     # Get all of the genome's PEGs.
325 :     my @pegs = $fig->pegs_of($genome);
326 :     # Loop through the PEGs.
327 :     for my $peg1 (@pegs) {
328 : parrello 1.6 $loadCoupling->Add("pegIn");
329 : parrello 1.1 Trace("Processing PEG $peg1 for $genome.") if T(4);
330 :     # Get a list of the coupled PEGs.
331 :     my @couplings = $fig->coupled_to($peg1);
332 :     # For each coupled PEG, we need to verify that a coupling already
333 :     # exists. If not, we have to create one.
334 :     for my $coupleData (@couplings) {
335 :     my ($peg2, $score) = @{$coupleData};
336 :     # Compute the coupling ID.
337 :     my $coupleID = Sprout::CouplingID($peg1, $peg2);
338 :     if (! exists $dupHash{$coupleID}) {
339 : parrello 1.6 $loadCoupling->Add("couplingIn");
340 : parrello 1.1 # Here we have a new coupling to store in the load files.
341 :     Trace("Storing coupling ($coupleID) with score $score.") if T(4);
342 :     # Ensure we don't do this again.
343 :     $dupHash{$coupleID} = $score;
344 :     # Write the coupling record.
345 :     $loadCoupling->Put($coupleID, $score);
346 :     # Connect it to the coupled PEGs.
347 :     $loadParticipatesInCoupling->Put($peg1, $coupleID, 1);
348 :     $loadParticipatesInCoupling->Put($peg2, $coupleID, 2);
349 :     # Get the evidence for this coupling.
350 :     my @evidence = $fig->coupling_evidence($peg1, $peg2);
351 :     # Organize the evidence into a hash table.
352 :     my %evidenceMap = ();
353 :     # Process each evidence item.
354 :     for my $evidenceData (@evidence) {
355 : parrello 1.6 $loadPCH->Add("evidenceIn");
356 : parrello 1.1 my ($peg3, $peg4, $usage) = @{$evidenceData};
357 :     # Only proceed if the evidence is from a Sprout
358 :     # genome.
359 :     if ($genomeFilter->{$fig->genome_of($peg3)}) {
360 : parrello 1.6 $loadUsesAsEvidence->Add("evidenceChosen");
361 : parrello 1.1 my $evidenceKey = "$coupleID $peg3 $peg4";
362 :     # We store this evidence in the hash if the usage
363 :     # is nonzero or no prior evidence has been found. This
364 :     # insures that if there is duplicate evidence, we
365 :     # at least keep the meaningful ones. Only evidence is
366 :     # the hash makes it to the output.
367 :     if ($usage || ! exists $evidenceMap{$evidenceKey}) {
368 :     $evidenceMap{$evidenceKey} = $evidenceData;
369 :     }
370 :     }
371 :     }
372 :     for my $evidenceID (keys %evidenceMap) {
373 :     # Create the evidence record.
374 :     my ($peg3, $peg4, $usage) = @{$evidenceMap{$evidenceID}};
375 :     $loadPCH->Put($evidenceID, $usage);
376 :     # Connect it to the coupling.
377 :     $loadIsEvidencedBy->Put($coupleID, $evidenceID);
378 :     # Connect it to the features.
379 :     $loadUsesAsEvidence->Put($evidenceID, $peg3, 1);
380 :     $loadUsesAsEvidence->Put($evidenceID, $peg4, 1);
381 :     }
382 :     }
383 :     }
384 :     }
385 :     }
386 :     # All done. Finish the load.
387 :     my $retVal = $self->_FinishAll();
388 :     return $retVal;
389 :     }
390 :    
391 :     =head3 LoadFeatureData
392 :    
393 :     C<< my $stats = $spl->LoadFeatureData(); >>
394 :    
395 :     Load the feature data from FIG into Sprout.
396 :    
397 :     Features represent annotated genes, and are therefore the heart of the data store.
398 :    
399 :     The following relations are loaded by this method.
400 :    
401 :     Feature
402 :     FeatureAlias
403 :     FeatureLink
404 :     FeatureTranslation
405 :     FeatureUpstream
406 :     IsLocatedIn
407 :    
408 :     =over 4
409 :    
410 :     =item RETURNS
411 :    
412 :     Returns a statistics object for the loads.
413 :    
414 :     =back
415 :    
416 :     =cut
417 :     #: Return Type $%;
418 :     sub LoadFeatureData {
419 :     # Get this object instance.
420 :     my ($self) = @_;
421 :     # Get the FIG object.
422 :     my $fig = $self->{fig};
423 :     # Get the table of genome IDs.
424 :     my $genomeHash = $self->{genomes};
425 :     my $genomeCount = (keys %{$genomeHash});
426 :     my $featureCount = $genomeCount * 4000;
427 :     # Create load objects for each of the tables we're loading.
428 :     my $loadFeature = $self->_TableLoader('Feature', $featureCount);
429 :     my $loadFeatureAlias = $self->_TableLoader('FeatureAlias', $featureCount * 6);
430 :     my $loadFeatureLink = $self->_TableLoader('FeatureLink', $featureCount * 10);
431 :     my $loadFeatureTranslation = $self->_TableLoader('FeatureTranslation', $featureCount);
432 :     my $loadFeatureUpstream = $self->_TableLoader('FeatureUpstream', $featureCount);
433 :     my $loadIsLocatedIn = $self->_TableLoader('IsLocatedIn', $featureCount);
434 :     # Get the maximum sequence size. We need this later for splitting up the
435 :     # locations.
436 :     my $chunkSize = $self->{sprout}->MaxSegment();
437 :     Trace("Beginning feature data load.") if T(2);
438 :     # Now we loop through the genomes, generating the data for each one.
439 :     for my $genomeID (sort keys %{$genomeHash}) {
440 :     Trace("Loading features for genome $genomeID.") if T(3);
441 : parrello 1.6 $loadFeature->Add("genomeIn");
442 : parrello 1.1 # Get the feature list for this genome.
443 :     my $features = $fig->all_features_detailed($genomeID);
444 :     # Loop through the features.
445 :     for my $featureData (@{$features}) {
446 : parrello 1.6 $loadFeature->Add("featureIn");
447 : parrello 1.1 # Split the tuple.
448 :     my ($featureID, $locations, $aliases, $type) = @{$featureData};
449 :     # Create the feature record.
450 : parrello 1.7 $loadFeature->Put($featureID, 1, $type);
451 : parrello 1.1 # Create the aliases.
452 :     for my $alias (split /\s*,\s*/, $aliases) {
453 :     $loadFeatureAlias->Put($featureID, $alias);
454 :     }
455 :     # Get the links.
456 :     my @links = $fig->fid_links($featureID);
457 :     for my $link (@links) {
458 :     $loadFeatureLink->Put($featureID, $link);
459 :     }
460 :     # If this is a peg, generate the translation and the upstream.
461 :     if ($type eq 'peg') {
462 : parrello 1.6 $loadFeatureTranslation->Add("pegIn");
463 : parrello 1.1 my $translation = $fig->get_translation($featureID);
464 :     if ($translation) {
465 :     $loadFeatureTranslation->Put($featureID, $translation);
466 :     }
467 :     # We use the default upstream values of u=200 and c=100.
468 :     my $upstream = $fig->upstream_of($featureID, 200, 100);
469 :     if ($upstream) {
470 :     $loadFeatureUpstream->Put($featureID, $upstream);
471 :     }
472 :     }
473 :     # This part is the roughest. We need to relate the features to contig
474 :     # locations, and the locations must be split so that none of them exceed
475 :     # the maximum segment size. This simplifies the genes_in_region processing
476 :     # for Sprout.
477 :     my @locationList = split /\s*,\s*/, $locations;
478 :     # Loop through the locations.
479 :     for my $location (@locationList) {
480 :     # Parse the location.
481 :     my $locObject = BasicLocation->new($location);
482 :     # Split it into a list of chunks.
483 :     my @locOList = ();
484 :     while (my $peeling = $locObject->Peel($chunkSize)) {
485 : parrello 1.6 $loadIsLocatedIn->Add("peeling");
486 : parrello 1.1 push @locOList, $peeling;
487 :     }
488 :     push @locOList, $locObject;
489 :     # Loop through the chunks, creating IsLocatedIn records. The variable
490 :     # "$i" will be used to keep the location index.
491 :     my $i = 1;
492 :     for my $locChunk (@locOList) {
493 :     $loadIsLocatedIn->Put($featureID, $locChunk->Contig, $locChunk->Left,
494 :     $locChunk->Dir, $locChunk->Length, $i);
495 :     $i++;
496 :     }
497 :     }
498 :     }
499 :     }
500 :     # Finish the loads.
501 :     my $retVal = $self->_FinishAll();
502 :     return $retVal;
503 :     }
504 :    
505 :     =head3 LoadBBHData
506 :    
507 :     C<< my $stats = $spl->LoadBBHData(); >>
508 :    
509 :     Load the bidirectional best hit data from FIG into Sprout.
510 :    
511 :     Sprout does not store information on similarities. Instead, it has only the
512 :     bi-directional best hits. Even so, the BBH table is one of the largest in
513 :     the database.
514 :    
515 :     The following relations are loaded by this method.
516 :    
517 :     IsBidirectionalBestHitOf
518 :    
519 :     =over 4
520 :    
521 :     =item RETURNS
522 :    
523 :     Returns a statistics object for the loads.
524 :    
525 :     =back
526 :    
527 :     =cut
528 :     #: Return Type $%;
529 : parrello 1.2 sub LoadBBHData {
530 : parrello 1.1 # Get this object instance.
531 :     my ($self) = @_;
532 :     # Get the FIG object.
533 :     my $fig = $self->{fig};
534 :     # Get the table of genome IDs.
535 :     my $genomeHash = $self->{genomes};
536 :     my $genomeCount = (keys %{$genomeHash});
537 :     my $featureCount = $genomeCount * 4000;
538 :     # Create load objects for each of the tables we're loading.
539 :     my $loadIsBidirectionalBestHitOf = $self->_TableLoader('IsBidirectionalBestHitOf',
540 :     $featureCount * $genomeCount);
541 :     Trace("Beginning BBH load.") if T(2);
542 :     # Now we loop through the genomes, generating the data for each one.
543 :     for my $genomeID (sort keys %{$genomeHash}) {
544 : parrello 1.6 $loadIsBidirectionalBestHitOf->Add("genomeIn");
545 : parrello 1.1 Trace("Processing features for genome $genomeID.") if T(3);
546 :     # Get the feature list for this genome.
547 :     my $features = $fig->all_features_detailed($genomeID);
548 :     # Loop through the features.
549 :     for my $featureData (@{$features}) {
550 :     # Split the tuple.
551 :     my ($featureID, $locations, $aliases, $type) = @{$featureData};
552 :     # Get the bi-directional best hits.
553 :     my @bbhList = $fig->bbhs($featureID);
554 :     for my $bbhEntry (@bbhList) {
555 :     # Get the target feature ID and the score.
556 :     my ($targetID, $score) = @{$bbhEntry};
557 :     # Check the target feature's genome.
558 :     my $targetGenomeID = $fig->genome_of($targetID);
559 :     # Only proceed if it's one of our genomes.
560 :     if ($genomeHash->{$targetGenomeID}) {
561 :     $loadIsBidirectionalBestHitOf->Put($featureID, $targetID, $targetGenomeID,
562 :     $score);
563 :     }
564 :     }
565 :     }
566 :     }
567 :     # Finish the loads.
568 :     my $retVal = $self->_FinishAll();
569 :     return $retVal;
570 :     }
571 :    
572 :     =head3 LoadSubsystemData
573 :    
574 :     C<< my $stats = $spl->LoadSubsystemData(); >>
575 :    
576 :     Load the subsystem data from FIG into Sprout.
577 :    
578 :     Subsystems are groupings of genetic roles that work together to effect a specific
579 :     chemical reaction. Similar organisms require similar subsystems. To curate a subsystem,
580 :     a spreadsheet is created with genomes on one axis and subsystem roles on the other
581 :     axis. Similar features are then mapped into the cells, allowing the annotation of one
582 :     genome's roles to be used to assist in the annotation of others.
583 :    
584 :     The following relations are loaded by this method.
585 :    
586 :     Subsystem
587 :     Role
588 :     SSCell
589 :     ContainsFeature
590 :     IsGenomeOf
591 :     IsRoleOf
592 :     OccursInSubsystem
593 :     ParticipatesIn
594 :     HasSSCell
595 :    
596 :     =over 4
597 :    
598 :     =item RETURNS
599 :    
600 :     Returns a statistics object for the loads.
601 :    
602 :     =back
603 :    
604 :     B<TO DO>
605 :    
606 :     Generate RoleName table?
607 :    
608 :     =cut
609 :     #: Return Type $%;
610 :     sub LoadSubsystemData {
611 :     # Get this object instance.
612 :     my ($self) = @_;
613 :     # Get the FIG object.
614 :     my $fig = $self->{fig};
615 :     # Get the genome hash. We'll use it to filter the genomes in each
616 :     # spreadsheet.
617 :     my $genomeHash = $self->{genomes};
618 :     # Get the subsystem hash. This lists the subsystems we'll process.
619 :     my $subsysHash = $self->{subsystems};
620 :     my @subsysIDs = sort keys %{$subsysHash};
621 :     my $subsysCount = @subsysIDs;
622 :     my $genomeCount = (keys %{$genomeHash});
623 :     my $featureCount = $genomeCount * 4000;
624 :     # Create load objects for each of the tables we're loading.
625 :     my $loadSubsystem = $self->_TableLoader('Subsystem', $subsysCount);
626 :     my $loadRole = $self->_TableLoader('Role', $featureCount * 6);
627 :     my $loadSSCell = $self->_TableLoader('SSCell', $featureCount * $genomeCount);
628 :     my $loadContainsFeature = $self->_TableLoader('ContainsFeature', $featureCount * $subsysCount);
629 :     my $loadIsGenomeOf = $self->_TableLoader('IsGenomeOf', $featureCount * $genomeCount);
630 :     my $loadIsRoleOf = $self->_TableLoader('IsRoleOf', $featureCount * $genomeCount);
631 :     my $loadOccursInSubsystem = $self->_TableLoader('OccursInSubsystem', $featureCount * 6);
632 :     my $loadParticipatesIn = $self->_TableLoader('ParticipatesIn', $subsysCount * $genomeCount);
633 :     my $loadHasSSCell = $self->_TableLoader('HasSSCell', $featureCount * $genomeCount);
634 :     Trace("Beginning subsystem data load.") if T(2);
635 :     # Loop through the subsystems. Our first task will be to create the
636 :     # roles. We do this by looping through the subsystems and creating a
637 :     # role hash. The hash tracks each role ID so that we don't create
638 :     # duplicates. As we move along, we'll connect the roles and subsystems.
639 :     my %roleData = ();
640 :     for my $subsysID (@subsysIDs) {
641 :     Trace("Creating subsystem $subsysID.") if T(3);
642 : parrello 1.6 $loadSubsystem->Add("subsystemIn");
643 : parrello 1.1 # Create the subsystem record.
644 :     $loadSubsystem->Put($subsysID);
645 :     # Get the subsystem's roles.
646 : parrello 1.6 my @roles = $fig->subsystem_to_roles($subsysID);
647 : parrello 1.1 # Connect the roles to the subsystem. If a role is new, we create
648 :     # a role record for it.
649 :     for my $roleID (@roles) {
650 : parrello 1.6 $loadOccursInSubsystem->Add("roleIn");
651 : parrello 1.1 $loadOccursInSubsystem->Put($roleID, $subsysID);
652 :     if (! exists $roleData{$roleID}) {
653 :     $loadRole->Put($roleID);
654 :     $roleData{$roleID} = 1;
655 :     }
656 :     }
657 :     # Now all roles for this subsystem have been filled in. We create the
658 :     # spreadsheet by matches roles to genomes. To do this, we need to
659 :     # get the genomes on the sheet.
660 :     Trace("Creating subsystem $subsysID spreadsheet.") if T(3);
661 :     my @genomes = map { $_->[0] } @{$fig->subsystem_genomes($subsysID)};
662 :     for my $genomeID (@genomes) {
663 :     # Only process this genome if it's one of ours.
664 :     if (exists $genomeHash->{$genomeID}) {
665 :     # Connect the genome to the subsystem.
666 :     $loadParticipatesIn->Put($genomeID, $subsysID);
667 :     # Loop through the subsystem's roles. We use an index because it is
668 :     # part of the spreadsheet cell ID.
669 :     for (my $i = 0; $i <= $#roles; $i++) {
670 :     my $role = $roles[$i];
671 :     # Get the features in the spreadsheet cell for this genome and role.
672 : parrello 1.6 my @pegs = $fig->pegs_in_subsystem_cell($subsysID, $genomeID, $i);
673 : parrello 1.1 # Only proceed if features exist.
674 :     if (@pegs > 0) {
675 :     # Create the spreadsheet cell.
676 :     my $cellID = "$subsysID:$genomeID:$i";
677 :     $loadSSCell->Put($cellID);
678 :     $loadIsGenomeOf->Put($genomeID, $cellID);
679 :     $loadIsRoleOf->Put($role, $cellID);
680 :     $loadHasSSCell->Put($subsysID, $cellID);
681 :     # Attach the features to it.
682 :     for my $pegID (@pegs) {
683 :     $loadContainsFeature->Put($cellID, $pegID);
684 :     }
685 :     }
686 :     }
687 :     }
688 :     }
689 :     }
690 :     # Finish the load.
691 :     my $retVal = $self->_FinishAll();
692 :     return $retVal;
693 :     }
694 :    
695 :     =head3 LoadDiagramData
696 :    
697 :     C<< my $stats = $spl->LoadDiagramData(); >>
698 :    
699 :     Load the diagram data from FIG into Sprout.
700 :    
701 :     Diagrams are used to organize functional roles. The diagram shows the
702 :     connections between chemicals that interact with a subsystem.
703 :    
704 :     The following relations are loaded by this method.
705 :    
706 :     Diagram
707 :     RoleOccursIn
708 :    
709 :     =over 4
710 :    
711 :     =item RETURNS
712 :    
713 :     Returns a statistics object for the loads.
714 :    
715 :     =back
716 :    
717 :     =cut
718 :     #: Return Type $%;
719 :     sub LoadDiagramData {
720 :     # Get this object instance.
721 :     my ($self) = @_;
722 :     # Get the FIG object.
723 :     my $fig = $self->{fig};
724 :     # Get the map list.
725 :     my @maps = $fig->all_maps;
726 :     my $mapCount = @maps;
727 :     my $genomeCount = (keys %{$self->{genomes}});
728 :     my $featureCount = $genomeCount * 4000;
729 :     # Create load objects for each of the tables we're loading.
730 :     my $loadDiagram = $self->_TableLoader('Diagram', $mapCount);
731 :     my $loadRoleOccursIn = $self->_TableLoader('RoleOccursIn', $featureCount * 6);
732 :     Trace("Beginning diagram data load.") if T(2);
733 :     # Loop through the diagrams.
734 :     for my $map ($fig->all_maps) {
735 :     Trace("Loading diagram $map.") if T(3);
736 :     # Get the diagram's descriptive name.
737 :     my $name = $fig->map_name($map);
738 :     $loadDiagram->Put($map, $name);
739 :     # Now we need to link all the map's roles to it.
740 :     # A hash is used to prevent duplicates.
741 :     my %roleHash = ();
742 :     for my $role ($fig->map_to_ecs($map)) {
743 :     if (! $roleHash{$role}) {
744 :     $loadRoleOccursIn->Put($role, $map);
745 :     $roleHash{$role} = 1;
746 :     }
747 :     }
748 :     }
749 :     # Finish the load.
750 :     my $retVal = $self->_FinishAll();
751 :     return $retVal;
752 :     }
753 :    
754 :     =head3 LoadPropertyData
755 :    
756 :     C<< my $stats = $spl->LoadPropertyData(); >>
757 :    
758 :     Load the attribute data from FIG into Sprout.
759 :    
760 :     Attribute data in FIG corresponds to the Sprout concept of Property. As currently
761 :     implemented, each key-value attribute combination in the SEED corresponds to a
762 :     record in the B<Property> table. The B<HasProperty> relationship links the
763 :     features to the properties.
764 :    
765 :     The SEED also allows attributes to be assigned to genomes, but this is not yet
766 :     supported by Sprout.
767 :    
768 :     The following relations are loaded by this method.
769 :    
770 :     HasProperty
771 :     Property
772 :    
773 :     =over 4
774 :    
775 :     =item RETURNS
776 :    
777 :     Returns a statistics object for the loads.
778 :    
779 :     =back
780 :    
781 :     =cut
782 :     #: Return Type $%;
783 :     sub LoadPropertyData {
784 :     # Get this object instance.
785 :     my ($self) = @_;
786 :     # Get the FIG object.
787 :     my $fig = $self->{fig};
788 :     # Get the genome hash.
789 :     my $genomeHash = $self->{genomes};
790 :     my $genomeCount = (keys %{$genomeHash});
791 :     # Create load objects for each of the tables we're loading.
792 :     my $loadProperty = $self->_TableLoader('Property', $genomeCount * 1500);
793 :     my $loadHasProperty = $self->_TableLoader('HasProperty', $genomeCount * 1500);
794 :     Trace("Beginning property data load.") if T(2);
795 :     # Create a hash for storing property IDs.
796 :     my %propertyKeys = ();
797 :     my $nextID = 1;
798 :     # Loop through the genomes.
799 :     for my $genomeID (keys %{$genomeHash}) {
800 : parrello 1.6 $loadProperty->Add("genomeIn");
801 : parrello 1.1 # Get the genome's features. The feature ID is the first field in the
802 :     # tuples returned by "all_features_detailed". We use "all_features_detailed"
803 :     # rather than "all_features" because we want all features regardless of type.
804 :     my @features = map { $_->[0] } @{$fig->all_features_detailed($genomeID)};
805 :     # Loop through the features, creating HasProperty records.
806 :     for my $fid (@features) {
807 : parrello 1.6 $loadProperty->Add("featureIn");
808 : parrello 1.1 # Get all attributes for this feature. We do this one feature at a time
809 :     # to insure we do not get any genome attributes.
810 :     my @attributeList = $fig->get_attributes($fid, '', '', '');
811 :     # Loop through the attributes.
812 :     for my $tuple (@attributeList) {
813 :     # Get this attribute value's data. Note that we throw away the FID,
814 :     # since it will always be the same as the value if "$fid".
815 :     my (undef, $key, $value, $url) = @{$tuple};
816 :     # Concatenate the key and value and check the "propertyKeys" hash to
817 :     # see if we already have an ID for it. We use a tab for the separator
818 :     # character.
819 :     my $propertyKey = "$key\t$value";
820 :     # Use the concatenated value to check for an ID. If no ID exists, we
821 :     # create one.
822 :     my $propertyID = $propertyKeys{$propertyKey};
823 :     if (! $propertyID) {
824 :     # Here we need to create a new property ID for this key/value pair.
825 :     $propertyKeys{$propertyKey} = $nextID;
826 :     $propertyID = $nextID;
827 :     $nextID++;
828 :     $loadProperty->Put($propertyID, $key, $value);
829 :     }
830 :     # Create the HasProperty entry for this feature/property association.
831 :     $loadHasProperty->Put($fid, $propertyID, $url);
832 :     }
833 :     }
834 :     }
835 :     # Finish the load.
836 :     my $retVal = $self->_FinishAll();
837 :     return $retVal;
838 :     }
839 :    
840 :     =head3 LoadAnnotationData
841 :    
842 :     C<< my $stats = $spl->LoadAnnotationData(); >>
843 :    
844 :     Load the annotation data from FIG into Sprout.
845 :    
846 :     Sprout annotations encompass both the assignments and the annotations in SEED.
847 :     These describe the function performed by a PEG as well as any other useful
848 :     information that may aid in identifying its purpose.
849 :    
850 :     The following relations are loaded by this method.
851 :    
852 :     Annotation
853 :     IsTargetOfAnnotation
854 :     SproutUser
855 :     MadeAnnotation
856 :    
857 :     =over 4
858 :    
859 :     =item RETURNS
860 :    
861 :     Returns a statistics object for the loads.
862 :    
863 :     =back
864 :    
865 :     =cut
866 :     #: Return Type $%;
867 :     sub LoadAnnotationData {
868 :     # Get this object instance.
869 :     my ($self) = @_;
870 :     # Get the FIG object.
871 :     my $fig = $self->{fig};
872 :     # Get the genome hash.
873 :     my $genomeHash = $self->{genomes};
874 :     my $genomeCount = (keys %{$genomeHash});
875 :     # Create load objects for each of the tables we're loading.
876 :     my $loadAnnotation = $self->_TableLoader('Annotation', $genomeCount * 4000);
877 :     my $loadIsTargetOfAnnotation = $self->_TableLoader('IsTargetOfAnnotation', $genomeCount * 4000);
878 :     my $loadSproutUser = $self->_TableLoader('SproutUser', 100);
879 :     my $loadUserAccess = $self->_TableLoader('UserAccess', 1000);
880 :     my $loadMadeAnnotation = $self->_TableLoader('MadeAnnotation', $genomeCount * 4000);
881 :     Trace("Beginning annotation data load.") if T(2);
882 :     # Create a hash of user names. We'll use this to prevent us from generating duplicate
883 :     # user records.
884 :     my %users = ( FIG => 1, master => 1 );
885 :     # Put in FIG and "master".
886 :     $loadSproutUser->Put("FIG", "Fellowship for Interpretation of Genomes");
887 :     $loadUserAccess->Put("FIG", 1);
888 :     $loadSproutUser->Put("master", "Master User");
889 :     $loadUserAccess->Put("master", 1);
890 :     # Get the current time.
891 :     my $time = time();
892 :     # Loop through the genomes.
893 : parrello 1.6 for my $genomeID (sort keys %{$genomeHash}) {
894 : parrello 1.1 Trace("Processing $genomeID.") if T(3);
895 :     # Get the genome's PEGs.
896 :     my @pegs = $fig->pegs_of($genomeID);
897 :     for my $peg (@pegs) {
898 :     Trace("Processing $peg.") if T(4);
899 :     # Create a hash of timestamps. We use this to prevent duplicate time stamps
900 :     # from showing up for a single PEG's annotations.
901 :     my %seenTimestamps = ();
902 :     # Check for a functional assignment.
903 :     my $func = $fig->function_of($peg);
904 :     if ($func) {
905 :     # If this is NOT a hypothetical assignment, we create an
906 :     # assignment annotation for it.
907 :     if (! FIG::hypo($peg)) {
908 :     # Note that we double the slashes so that what goes into the database is
909 :     # a new-line escape sequence rather than an actual new-line.
910 :     $loadAnnotation->Put("$peg:$time", $time, "FIG\\nSet function to\\n$func");
911 :     $loadIsTargetOfAnnotation->Put($peg, "$peg:$time");
912 :     $loadMadeAnnotation->Put("FIG", "$peg:$time");
913 :     # Denote we've seen this timestamp.
914 :     $seenTimestamps{$time} = 1;
915 :     }
916 :     # Now loop through the real annotations.
917 :     for my $tuple ($fig->feature_annotations($peg, "raw")) {
918 : parrello 1.6 my ($fid, $timestamp, $user, $text) = @{$tuple};
919 : parrello 1.1 # Here we fix up the annotation text. "\r" is removed,
920 :     # and "\t" and "\n" are escaped. Note we use the "s"
921 :     # modifier so that new-lines inside the text do not
922 :     # stop the substitution search.
923 :     $text =~ s/\r//gs;
924 :     $text =~ s/\t/\\t/gs;
925 :     $text =~ s/\n/\\n/gs;
926 :     # Change assignments by the master user to FIG assignments.
927 :     $text =~ s/Set master function/Set FIG function/s;
928 :     # Insure the time stamp is valid.
929 :     if ($timestamp =~ /^\d+$/) {
930 :     # Here it's a number. We need to insure it's unique.
931 :     while ($seenTimestamps{$timestamp}) {
932 :     $timestamp++;
933 :     }
934 :     $seenTimestamps{$timestamp} = 1;
935 :     my $annotationID = "$peg:$timestamp";
936 :     # Insure the user exists.
937 :     if (! $users{$user}) {
938 :     $loadSproutUser->Put($user, "SEED user");
939 :     $loadUserAccess->Put($user, 1);
940 :     $users{$user} = 1;
941 :     }
942 :     # Generate the annotation.
943 :     $loadAnnotation->Put($annotationID, $timestamp, "$user\\n$text");
944 :     $loadIsTargetOfAnnotation->Put($peg, $annotationID);
945 :     $loadMadeAnnotation->Put($user, $annotationID);
946 :     } else {
947 :     # Here we have an invalid time stamp.
948 :     Trace("Invalid time stamp \"$timestamp\" in annotations for $peg.") if T(1);
949 :     }
950 :     }
951 :     }
952 :     }
953 :     }
954 :     # Finish the load.
955 :     my $retVal = $self->_FinishAll();
956 :     return $retVal;
957 :     }
958 :    
959 : parrello 1.5 =head3 LoadSourceData
960 :    
961 :     C<< my $stats = $spl->LoadSourceData(); >>
962 :    
963 :     Load the source data from FIG into Sprout.
964 :    
965 :     Source data links genomes to information about the organizations that
966 :     mapped it.
967 :    
968 :     The following relations are loaded by this method.
969 :    
970 :     ComesFrom
971 :     Source
972 :     SourceURL
973 :    
974 :     There is no direct support for source attribution in FIG, so we access the SEED
975 :     files directly.
976 :    
977 :     =over 4
978 :    
979 :     =item RETURNS
980 :    
981 :     Returns a statistics object for the loads.
982 :    
983 :     =back
984 :    
985 :     =cut
986 :     #: Return Type $%;
987 :     sub LoadSourceData {
988 :     # Get this object instance.
989 :     my ($self) = @_;
990 :     # Get the FIG object.
991 :     my $fig = $self->{fig};
992 :     # Get the genome hash.
993 :     my $genomeHash = $self->{genomes};
994 :     my $genomeCount = (keys %{$genomeHash});
995 :     # Create load objects for each of the tables we're loading.
996 :     my $loadComesFrom = $self->_TableLoader('ComesFrom', $genomeCount * 4);
997 :     my $loadSource = $self->_TableLoader('Source', $genomeCount * 4);
998 :     my $loadSourceURL = $self->_TableLoader('SourceURL', $genomeCount * 8);
999 :     Trace("Beginning source data load.") if T(2);
1000 :     # Create hashes to collect the Source information.
1001 :     my %sourceURL = ();
1002 :     my %sourceDesc = ();
1003 :     # Loop through the genomes.
1004 :     my $line;
1005 : parrello 1.6 for my $genomeID (sort keys %{$genomeHash}) {
1006 : parrello 1.5 Trace("Processing $genomeID.") if T(3);
1007 :     # Open the project file.
1008 :     if ((open(TMP, "<$FIG_Config::organisms/$genomeID/PROJECT")) &&
1009 :     defined($line = <TMP>)) {
1010 :     chomp $line;
1011 : parrello 1.6 my($sourceID, $desc, $url) = split(/\t/,$line);
1012 : parrello 1.5 $loadComesFrom->Put($genomeID, $sourceID);
1013 :     if ($url && ! exists $sourceURL{$genomeID}) {
1014 :     $loadSourceURL->Put($sourceID, $url);
1015 :     $sourceURL{$sourceID} = 1;
1016 :     }
1017 :     if ($desc && ! exists $sourceDesc{$sourceID}) {
1018 :     $loadSource->Put($sourceID, $desc);
1019 :     $sourceDesc{$sourceID} = 1;
1020 :     }
1021 :     }
1022 :     close TMP;
1023 :     }
1024 :     # Finish the load.
1025 :     my $retVal = $self->_FinishAll();
1026 :     return $retVal;
1027 :     }
1028 :    
1029 : parrello 1.6 =head3 LoadExternalData
1030 :    
1031 :     C<< my $stats = $spl->LoadExternalData(); >>
1032 :    
1033 :     Load the external data from FIG into Sprout.
1034 :    
1035 :     External data contains information about external feature IDs.
1036 :    
1037 :     The following relations are loaded by this method.
1038 :    
1039 :     ExternalAliasFunc
1040 :     ExternalAliasOrg
1041 :    
1042 :     The support for external IDs in FIG is hidden beneath layers of other data, so
1043 :     we access the SEED files directly to create these tables. This is also one of
1044 :     the few load methods that does not proceed genome by genome.
1045 :    
1046 :     =over 4
1047 :    
1048 :     =item RETURNS
1049 :    
1050 :     Returns a statistics object for the loads.
1051 :    
1052 :     =back
1053 :    
1054 :     =cut
1055 :     #: Return Type $%;
1056 :     sub LoadExternalData {
1057 :     # Get this object instance.
1058 :     my ($self) = @_;
1059 :     # Get the FIG object.
1060 :     my $fig = $self->{fig};
1061 :     # Get the genome hash.
1062 :     my $genomeHash = $self->{genomes};
1063 :     my $genomeCount = (keys %{$genomeHash});
1064 :     # Convert the genome hash. We'll get the genus and species for each genome and make
1065 :     # it the key.
1066 :     my %speciesHash = map { $fig->genus_species($_) => $_ } (keys %{$genomeHash});
1067 :     # Create load objects for each of the tables we're loading.
1068 :     my $loadExternalAliasFunc = $self->_TableLoader('ExternalAliasFunc', $genomeCount * 4000);
1069 :     my $loadExternalAliasOrg = $self->_TableLoader('ExternalAliasOrg', $genomeCount * 4000);
1070 :     Trace("Beginning external data load.") if T(2);
1071 :     # We loop through the files one at a time. First, the organism file.
1072 :     Open(\*ORGS, "<$FIG_Config::global/ext_org.table");
1073 :     my $orgLine;
1074 :     while (defined($orgLine = <ORGS>)) {
1075 :     # Clean the input line.
1076 :     chomp $orgLine;
1077 :     # Parse the organism name.
1078 :     my ($protID, $name) = split /\s*\t\s*/, $orgLine;
1079 :     $loadExternalAliasOrg->Put($protID, $name);
1080 :     }
1081 :     close ORGS;
1082 :     # Now the function file.
1083 :     my $funcLine;
1084 :     Open(\*FUNCS, "<$FIG_Config::global/ext_func.table");
1085 :     while (defined($funcLine = <FUNCS>)) {
1086 :     # Clean the line ending.
1087 :     chomp $funcLine;
1088 :     # Only proceed if the line is non-blank.
1089 :     if ($funcLine) {
1090 :     # Split it into fields.
1091 :     my @funcFields = split /\s*\t\s*/, $funcLine;
1092 :     # If there's an EC number, append it to the description.
1093 :     if ($#funcFields >= 2 && $funcFields[2] =~ /^(EC .*\S)/) {
1094 :     $funcFields[1] .= " $1";
1095 :     }
1096 :     # Output the function line.
1097 :     $loadExternalAliasFunc->Put(@funcFields[0,1]);
1098 :     }
1099 :     }
1100 :     # Finish the load.
1101 :     my $retVal = $self->_FinishAll();
1102 :     return $retVal;
1103 :     }
1104 : parrello 1.5
1105 :     =head3 LoadGroupData
1106 :    
1107 :     C<< my $stats = $spl->LoadGroupData(); >>
1108 :    
1109 :     Load the genome Groups into Sprout.
1110 :    
1111 :     The following relations are loaded by this method.
1112 :    
1113 :     GenomeGroups
1114 :    
1115 :     There is no direct support for genome groups in FIG, so we access the SEED
1116 :     files directly.
1117 :    
1118 :     =over 4
1119 :    
1120 :     =item RETURNS
1121 :    
1122 :     Returns a statistics object for the loads.
1123 :    
1124 :     =back
1125 :    
1126 :     =cut
1127 :     #: Return Type $%;
1128 :     sub LoadGroupData {
1129 :     # Get this object instance.
1130 :     my ($self) = @_;
1131 :     # Get the FIG object.
1132 :     my $fig = $self->{fig};
1133 :     # Get the genome hash.
1134 :     my $genomeHash = $self->{genomes};
1135 :     my $genomeCount = (keys %{$genomeHash});
1136 :     # Create a load object for the table we're loading.
1137 :     my $loadGenomeGroups = $self->_TableLoader('GenomeGroups', $genomeCount * 4);
1138 :     Trace("Beginning group data load.") if T(2);
1139 :     # Loop through the genomes.
1140 :     my $line;
1141 : parrello 1.6 for my $genomeID (keys %{$genomeHash}) {
1142 : parrello 1.5 Trace("Processing $genomeID.") if T(3);
1143 :     # Open the NMPDR group file for this genome.
1144 :     if (open(TMP, "<$FIG_Config::organisms/$genomeID/NMPDR") &&
1145 :     defined($line = <TMP>)) {
1146 :     # Clean the line ending.
1147 : parrello 1.6 chomp $line;
1148 : parrello 1.5 # Add the group to the table. Note that there can only be one group
1149 :     # per genome.
1150 :     $loadGenomeGroups->Put($genomeID, $line);
1151 :     }
1152 :     close TMP;
1153 :     }
1154 :     # Finish the load.
1155 :     my $retVal = $self->_FinishAll();
1156 :     return $retVal;
1157 :     }
1158 :    
1159 : parrello 1.1 =head2 Internal Utility Methods
1160 :    
1161 :     =head3 TableLoader
1162 :    
1163 :     Create an ERDBLoad object for the specified table. The object is also added to
1164 :     the internal list in the C<loaders> property of this object. That enables the
1165 :     L</FinishAll> method to terminate all the active loads.
1166 :    
1167 :     This is an instance method.
1168 :    
1169 :     =over 4
1170 :    
1171 :     =item tableName
1172 :    
1173 :     Name of the table (relation) being loaded.
1174 :    
1175 :     =item rowCount (optional)
1176 :    
1177 :     Estimated maximum number of rows in the table.
1178 :    
1179 :     =item RETURN
1180 :    
1181 :     Returns an ERDBLoad object for loading the specified table.
1182 :    
1183 :     =back
1184 :    
1185 :     =cut
1186 :    
1187 :     sub _TableLoader {
1188 :     # Get the parameters.
1189 :     my ($self, $tableName, $rowCount) = @_;
1190 :     # Create the load object.
1191 :     my $retVal = ERDBLoad->new($self->{erdb}, $tableName, $self->{loadDirectory}, $rowCount);
1192 :     # Cache it in the loader list.
1193 :     push @{$self->{loaders}}, $retVal;
1194 :     # Return it to the caller.
1195 :     return $retVal;
1196 :     }
1197 :    
1198 :     =head3 FinishAll
1199 :    
1200 :     Finish all the active loads on this object.
1201 :    
1202 :     When a load is started by L</TableLoader>, the controlling B<ERDBLoad> object is cached in
1203 :     the list pointed to be the C<loaders> property of this object. This method pops the loaders
1204 :     off the list and finishes them to flush out any accumulated residue.
1205 :    
1206 :     This is an instance method.
1207 :    
1208 :     =over 4
1209 :    
1210 :     =item RETURN
1211 :    
1212 :     Returns a statistics object containing the accumulated statistics for the load.
1213 :    
1214 :     =back
1215 :    
1216 :     =cut
1217 :    
1218 :     sub _FinishAll {
1219 :     # Get this object instance.
1220 :     my ($self) = @_;
1221 :     # Create the statistics object.
1222 :     my $retVal = Stats->new();
1223 :     # Get the loader list.
1224 :     my $loadList = $self->{loaders};
1225 :     # Loop through the list, finishing the loads. Note that if the finish fails, we die
1226 :     # ignominiously. At some future point, we want to make the loads restartable.
1227 :     while (my $loader = pop @{$loadList}) {
1228 :     my $stats = $loader->Finish();
1229 :     $retVal->Accumulate($stats);
1230 :     my $relName = $loader->RelName;
1231 :     Trace("Statistics for $relName:\n" . $stats->Show()) if T(2);
1232 :     }
1233 :     # Return the load statistics.
1234 :     return $retVal;
1235 :     }
1236 :    
1237 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3