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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.69 - (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 : parrello 1.18 use HTML;
14 : parrello 1.1
15 :     =head1 Sprout Load Methods
16 :    
17 :     =head2 Introduction
18 :    
19 :     This object contains the methods needed to copy data from the FIG data store to the
20 :     Sprout database. It makes heavy use of the ERDBLoad object to manage the load into
21 :     individual tables. The client can create an instance of this object and then
22 :     call methods for each group of tables to load. For example, the following code will
23 :     load the Genome- and Feature-related tables. (It is presumed the first command line
24 :     parameter contains the name of a file specifying the genomes.)
25 :    
26 :     my $fig = FIG->new();
27 :     my $sprout = SFXlate->new_sprout_only();
28 :     my $spl = SproutLoad->new($sprout, $fig, $ARGV[0]);
29 :     my $stats = $spl->LoadGenomeData();
30 :     $stats->Accumulate($spl->LoadFeatureData());
31 :     print $stats->Show();
32 :    
33 :     It is worth noting that the FIG object does not need to be a real one. Any object
34 :     that implements the FIG methods for data retrieval could be used. So, for example,
35 :     this object could be used to copy data from one Sprout database to another, or
36 :     from any FIG-compliant data story implemented in the future.
37 :    
38 :     To insure that this is possible, each time the FIG object is used, it will be via
39 :     a variable called C<$fig>. This makes it fairly straightforward to determine which
40 :     FIG methods are required to load the Sprout database.
41 :    
42 : parrello 1.5 This object creates the load files; however, the tables are not created until it
43 :     is time to actually do the load from the files into the target database.
44 :    
45 : parrello 1.1 =cut
46 :    
47 :     #: Constructor SproutLoad->new();
48 :    
49 :     =head2 Public Methods
50 :    
51 :     =head3 new
52 :    
53 : parrello 1.8 C<< my $spl = SproutLoad->new($sprout, $fig, $genomeFile, $subsysFile, $options); >>
54 : parrello 1.1
55 :     Construct a new Sprout Loader object, specifying the two participating databases and
56 :     the name of the files containing the list of genomes and subsystems to use.
57 :    
58 :     =over 4
59 :    
60 :     =item sprout
61 :    
62 :     Sprout object representing the target database. This also specifies the directory to
63 :     be used for creating the load files.
64 :    
65 :     =item fig
66 :    
67 :     FIG object representing the source data store from which the data is to be taken.
68 :    
69 :     =item genomeFile
70 :    
71 :     Either the name of the file containing the list of genomes to load or a reference to
72 :     a hash of genome IDs to access codes. If nothing is specified, all complete genomes
73 :     will be loaded and the access code will default to 1. The genome list is presumed
74 :     to be all-inclusive. In other words, all existing data in the target database will
75 :     be deleted and replaced with the data on the specified genes. If a file is specified,
76 :     it should contain one genome ID and access code per line, tab-separated.
77 :    
78 :     =item subsysFile
79 :    
80 :     Either the name of the file containing the list of trusted subsystems or a reference
81 : parrello 1.34 to a list of subsystem names. If nothing is specified, all NMPDR subsystems will be
82 :     considered trusted. (A subsystem is considered NMPDR if it has a file named C<NMPDR>
83 :     in its data directory.) Only subsystem data related to the trusted subsystems is loaded.
84 : parrello 1.1
85 : parrello 1.8 =item options
86 :    
87 :     Reference to a hash of command-line options.
88 :    
89 : parrello 1.1 =back
90 :    
91 :     =cut
92 :    
93 :     sub new {
94 :     # Get the parameters.
95 : parrello 1.8 my ($class, $sprout, $fig, $genomeFile, $subsysFile, $options) = @_;
96 : parrello 1.35 # Create the genome hash.
97 :     my %genomes = ();
98 :     # We only need it if load-only is NOT specified.
99 :     if (! $options->{loadOnly}) {
100 :     if (! defined($genomeFile) || $genomeFile eq '') {
101 :     # Here we want all the complete genomes and an access code of 1.
102 :     my @genomeList = $fig->genomes(1);
103 :     %genomes = map { $_ => 1 } @genomeList;
104 :     } else {
105 :     my $type = ref $genomeFile;
106 :     Trace("Genome file parameter type is \"$type\".") if T(3);
107 :     if ($type eq 'HASH') {
108 :     # Here the user specified a hash of genome IDs to access codes, which is
109 :     # exactly what we want.
110 :     %genomes = %{$genomeFile};
111 :     } elsif (! $type || $type eq 'SCALAR' ) {
112 :     # The caller specified a file, so read the genomes from the file. (Note
113 :     # that some PERLs return an empty string rather than SCALAR.)
114 :     my @genomeList = Tracer::GetFile($genomeFile);
115 :     if (! @genomeList) {
116 :     # It's an error if the genome file is empty or not found.
117 :     Confess("No genomes found in file \"$genomeFile\".");
118 :     } else {
119 :     # We build the genome Hash using a loop rather than "map" so that
120 :     # an omitted access code can be defaulted to 1.
121 :     for my $genomeLine (@genomeList) {
122 :     my ($genomeID, $accessCode) = split("\t", $genomeLine);
123 : parrello 1.65 if (! defined($accessCode)) {
124 : parrello 1.35 $accessCode = 1;
125 :     }
126 :     $genomes{$genomeID} = $accessCode;
127 : parrello 1.3 }
128 : parrello 1.1 }
129 : parrello 1.35 } else {
130 :     Confess("Invalid genome parameter ($type) in SproutLoad constructor.");
131 : parrello 1.1 }
132 :     }
133 :     }
134 :     # Load the list of trusted subsystems.
135 :     my %subsystems = ();
136 : parrello 1.35 # We only need it if load-only is NOT specified.
137 :     if (! $options->{loadOnly}) {
138 :     if (! defined $subsysFile || $subsysFile eq '') {
139 : parrello 1.55 # Here we want all the usable subsystems. First we get the whole list.
140 : parrello 1.35 my @subs = $fig->all_subsystems();
141 : parrello 1.55 # Loop through, checking for usability.
142 : parrello 1.35 for my $sub (@subs) {
143 : parrello 1.55 if ($fig->usable_subsystem($sub)) {
144 : parrello 1.35 $subsystems{$sub} = 1;
145 :     }
146 : parrello 1.33 }
147 : parrello 1.35 } else {
148 :     my $type = ref $subsysFile;
149 :     if ($type eq 'ARRAY') {
150 :     # Here the user passed in a list of subsystems.
151 :     %subsystems = map { $_ => 1 } @{$subsysFile};
152 :     } elsif (! $type || $type eq 'SCALAR') {
153 :     # Here the list of subsystems is in a file.
154 :     if (! -e $subsysFile) {
155 :     # It's an error if the file does not exist.
156 :     Confess("Trusted subsystem file not found.");
157 :     } else {
158 :     # GetFile automatically chomps end-of-line characters, so this
159 :     # is an easy task.
160 :     %subsystems = map { $_ => 1 } Tracer::GetFile($subsysFile);
161 :     }
162 : parrello 1.4 } else {
163 : parrello 1.35 Confess("Invalid subsystem parameter in SproutLoad constructor.");
164 : parrello 1.4 }
165 : parrello 1.1 }
166 :     }
167 :     # Get the data directory from the Sprout object.
168 :     my ($directory) = $sprout->LoadInfo();
169 :     # Create the Sprout load object.
170 :     my $retVal = {
171 :     fig => $fig,
172 :     genomes => \%genomes,
173 :     subsystems => \%subsystems,
174 :     sprout => $sprout,
175 :     loadDirectory => $directory,
176 : parrello 1.39 erdb => $sprout,
177 : parrello 1.8 loaders => [],
178 :     options => $options
179 : parrello 1.1 };
180 :     # Bless and return it.
181 :     bless $retVal, $class;
182 :     return $retVal;
183 :     }
184 :    
185 : parrello 1.23 =head3 LoadOnly
186 :    
187 :     C<< my $flag = $spl->LoadOnly; >>
188 :    
189 :     Return TRUE if we are in load-only mode, else FALSE.
190 :    
191 :     =cut
192 :    
193 :     sub LoadOnly {
194 :     my ($self) = @_;
195 :     return $self->{options}->{loadOnly};
196 :     }
197 :    
198 : parrello 1.25 =head3 PrimaryOnly
199 :    
200 :     C<< my $flag = $spl->PrimaryOnly; >>
201 :    
202 :     Return TRUE if only the main entity is to be loaded, else FALSE.
203 :    
204 :     =cut
205 :    
206 :     sub PrimaryOnly {
207 :     my ($self) = @_;
208 :     return $self->{options}->{primaryOnly};
209 :     }
210 :    
211 : parrello 1.1 =head3 LoadGenomeData
212 :    
213 :     C<< my $stats = $spl->LoadGenomeData(); >>
214 :    
215 :     Load the Genome, Contig, and Sequence data from FIG into Sprout.
216 :    
217 :     The Sequence table is the largest single relation in the Sprout database, so this
218 :     method is expected to be slow and clumsy. At some point we will need to make it
219 :     restartable, since an error 10 gigabytes through a 20-gigabyte load is bound to be
220 :     very annoying otherwise.
221 :    
222 :     The following relations are loaded by this method.
223 :    
224 :     Genome
225 :     HasContig
226 :     Contig
227 :     IsMadeUpOf
228 :     Sequence
229 :    
230 :     =over 4
231 :    
232 :     =item RETURNS
233 :    
234 :     Returns a statistics object for the loads.
235 :    
236 :     =back
237 :    
238 :     =cut
239 :     #: Return Type $%;
240 :     sub LoadGenomeData {
241 :     # Get this object instance.
242 :     my ($self) = @_;
243 :     # Get the FIG object.
244 :     my $fig = $self->{fig};
245 :     # Get the genome count.
246 :     my $genomeHash = $self->{genomes};
247 :     my $genomeCount = (keys %{$genomeHash});
248 :     # Create load objects for each of the tables we're loading.
249 : parrello 1.23 my $loadGenome = $self->_TableLoader('Genome');
250 : parrello 1.25 my $loadHasContig = $self->_TableLoader('HasContig', $self->PrimaryOnly);
251 :     my $loadContig = $self->_TableLoader('Contig', $self->PrimaryOnly);
252 :     my $loadIsMadeUpOf = $self->_TableLoader('IsMadeUpOf', $self->PrimaryOnly);
253 :     my $loadSequence = $self->_TableLoader('Sequence', $self->PrimaryOnly);
254 : parrello 1.23 if ($self->{options}->{loadOnly}) {
255 :     Trace("Loading from existing files.") if T(2);
256 :     } else {
257 :     Trace("Generating genome data.") if T(2);
258 :     # Now we loop through the genomes, generating the data for each one.
259 :     for my $genomeID (sort keys %{$genomeHash}) {
260 :     Trace("Generating data for genome $genomeID.") if T(3);
261 :     $loadGenome->Add("genomeIn");
262 :     # The access code comes in via the genome hash.
263 :     my $accessCode = $genomeHash->{$genomeID};
264 : parrello 1.28 # Get the genus, species, and strain from the scientific name.
265 : parrello 1.23 my ($genus, $species, @extraData) = split / /, $self->{fig}->genus_species($genomeID);
266 : parrello 1.28 my $extra = join " ", @extraData;
267 : parrello 1.23 # Get the full taxonomy.
268 :     my $taxonomy = $fig->taxonomy_of($genomeID);
269 : parrello 1.68 # Open the NMPDR group file for this genome.
270 :     my $group;
271 :     if (open(TMP, "<$FIG_Config::organisms/$genomeID/NMPDR") &&
272 :     defined($group = <TMP>)) {
273 :     # Clean the line ending.
274 :     chomp $group;
275 :     } else {
276 :     # No group, so use the default.
277 :     $group = $FIG_Config::otherGroup;
278 :     }
279 :     close TMP;
280 : parrello 1.23 # Output the genome record.
281 :     $loadGenome->Put($genomeID, $accessCode, $fig->is_complete($genomeID), $genus,
282 : parrello 1.68 $group, $species, $extra, $taxonomy);
283 : parrello 1.23 # Now we loop through each of the genome's contigs.
284 :     my @contigs = $fig->all_contigs($genomeID);
285 :     for my $contigID (@contigs) {
286 :     Trace("Processing contig $contigID for $genomeID.") if T(4);
287 :     $loadContig->Add("contigIn");
288 :     $loadSequence->Add("contigIn");
289 :     # Create the contig ID.
290 :     my $sproutContigID = "$genomeID:$contigID";
291 :     # Create the contig record and relate it to the genome.
292 :     $loadContig->Put($sproutContigID);
293 :     $loadHasContig->Put($genomeID, $sproutContigID);
294 :     # Now we need to split the contig into sequences. The maximum sequence size is
295 :     # a property of the Sprout object.
296 :     my $chunkSize = $self->{sprout}->MaxSequence();
297 :     # Now we get the sequence a chunk at a time.
298 :     my $contigLen = $fig->contig_ln($genomeID, $contigID);
299 :     for (my $i = 1; $i <= $contigLen; $i += $chunkSize) {
300 :     $loadSequence->Add("chunkIn");
301 :     # Compute the endpoint of this chunk.
302 :     my $end = FIG::min($i + $chunkSize - 1, $contigLen);
303 :     # Get the actual DNA.
304 :     my $dna = $fig->get_dna($genomeID, $contigID, $i, $end);
305 :     # Compute the sequenceID.
306 :     my $seqID = "$sproutContigID.$i";
307 :     # Write out the data. For now, the quality vector is always "unknown".
308 :     $loadIsMadeUpOf->Put($sproutContigID, $seqID, $end + 1 - $i, $i);
309 :     $loadSequence->Put($seqID, "unknown", $dna);
310 :     }
311 : parrello 1.1 }
312 :     }
313 :     }
314 :     # Finish the loads.
315 :     my $retVal = $self->_FinishAll();
316 :     # Return the result.
317 :     return $retVal;
318 :     }
319 :    
320 :     =head3 LoadCouplingData
321 :    
322 :     C<< my $stats = $spl->LoadCouplingData(); >>
323 :    
324 :     Load the coupling and evidence data from FIG into Sprout.
325 :    
326 :     The coupling data specifies which genome features are functionally coupled. The
327 :     evidence data explains why the coupling is functional.
328 :    
329 :     The following relations are loaded by this method.
330 :    
331 :     Coupling
332 :     IsEvidencedBy
333 :     PCH
334 :     ParticipatesInCoupling
335 :     UsesAsEvidence
336 :    
337 :     =over 4
338 :    
339 :     =item RETURNS
340 :    
341 :     Returns a statistics object for the loads.
342 :    
343 :     =back
344 :    
345 :     =cut
346 :     #: Return Type $%;
347 :     sub LoadCouplingData {
348 :     # Get this object instance.
349 :     my ($self) = @_;
350 :     # Get the FIG object.
351 :     my $fig = $self->{fig};
352 :     # Get the genome hash.
353 :     my $genomeFilter = $self->{genomes};
354 : parrello 1.50 # Set up an ID counter for the PCHs.
355 :     my $pchID = 0;
356 : parrello 1.1 # Start the loads.
357 : parrello 1.23 my $loadCoupling = $self->_TableLoader('Coupling');
358 : parrello 1.25 my $loadIsEvidencedBy = $self->_TableLoader('IsEvidencedBy', $self->PrimaryOnly);
359 :     my $loadPCH = $self->_TableLoader('PCH', $self->PrimaryOnly);
360 :     my $loadParticipatesInCoupling = $self->_TableLoader('ParticipatesInCoupling', $self->PrimaryOnly);
361 :     my $loadUsesAsEvidence = $self->_TableLoader('UsesAsEvidence', $self->PrimaryOnly);
362 : parrello 1.23 if ($self->{options}->{loadOnly}) {
363 :     Trace("Loading from existing files.") if T(2);
364 :     } else {
365 :     Trace("Generating coupling data.") if T(2);
366 :     # Loop through the genomes found.
367 :     for my $genome (sort keys %{$genomeFilter}) {
368 :     Trace("Generating coupling data for $genome.") if T(3);
369 :     $loadCoupling->Add("genomeIn");
370 :     # Create a hash table for holding coupled pairs. We use this to prevent
371 :     # duplicates. For example, if A is coupled to B, we don't want to also
372 :     # assert that B is coupled to A, because we already know it. Fortunately,
373 :     # all couplings occur within a genome, so we can keep the hash table
374 :     # size reasonably small.
375 :     my %dupHash = ();
376 :     # Get all of the genome's PEGs.
377 :     my @pegs = $fig->pegs_of($genome);
378 :     # Loop through the PEGs.
379 :     for my $peg1 (@pegs) {
380 :     $loadCoupling->Add("pegIn");
381 :     Trace("Processing PEG $peg1 for $genome.") if T(4);
382 :     # Get a list of the coupled PEGs.
383 :     my @couplings = $fig->coupled_to($peg1);
384 :     # For each coupled PEG, we need to verify that a coupling already
385 :     # exists. If not, we have to create one.
386 :     for my $coupleData (@couplings) {
387 :     my ($peg2, $score) = @{$coupleData};
388 :     # Compute the coupling ID.
389 : parrello 1.47 my $coupleID = $self->{erdb}->CouplingID($peg1, $peg2);
390 : parrello 1.23 if (! exists $dupHash{$coupleID}) {
391 :     $loadCoupling->Add("couplingIn");
392 :     # Here we have a new coupling to store in the load files.
393 :     Trace("Storing coupling ($coupleID) with score $score.") if T(4);
394 :     # Ensure we don't do this again.
395 :     $dupHash{$coupleID} = $score;
396 :     # Write the coupling record.
397 :     $loadCoupling->Put($coupleID, $score);
398 :     # Connect it to the coupled PEGs.
399 :     $loadParticipatesInCoupling->Put($peg1, $coupleID, 1);
400 :     $loadParticipatesInCoupling->Put($peg2, $coupleID, 2);
401 :     # Get the evidence for this coupling.
402 :     my @evidence = $fig->coupling_evidence($peg1, $peg2);
403 :     # Organize the evidence into a hash table.
404 :     my %evidenceMap = ();
405 :     # Process each evidence item.
406 :     for my $evidenceData (@evidence) {
407 :     $loadPCH->Add("evidenceIn");
408 :     my ($peg3, $peg4, $usage) = @{$evidenceData};
409 :     # Only proceed if the evidence is from a Sprout
410 :     # genome.
411 :     if ($genomeFilter->{$fig->genome_of($peg3)}) {
412 :     $loadUsesAsEvidence->Add("evidenceChosen");
413 :     my $evidenceKey = "$coupleID $peg3 $peg4";
414 :     # We store this evidence in the hash if the usage
415 :     # is nonzero or no prior evidence has been found. This
416 :     # insures that if there is duplicate evidence, we
417 :     # at least keep the meaningful ones. Only evidence in
418 :     # the hash makes it to the output.
419 :     if ($usage || ! exists $evidenceMap{$evidenceKey}) {
420 :     $evidenceMap{$evidenceKey} = $evidenceData;
421 :     }
422 : parrello 1.1 }
423 :     }
424 : parrello 1.23 for my $evidenceID (keys %evidenceMap) {
425 : parrello 1.50 # Get the ID for this evidence.
426 :     $pchID++;
427 : parrello 1.23 # Create the evidence record.
428 :     my ($peg3, $peg4, $usage) = @{$evidenceMap{$evidenceID}};
429 : parrello 1.50 $loadPCH->Put($pchID, $usage);
430 : parrello 1.23 # Connect it to the coupling.
431 : parrello 1.50 $loadIsEvidencedBy->Put($coupleID, $pchID);
432 : parrello 1.23 # Connect it to the features.
433 : parrello 1.50 $loadUsesAsEvidence->Put($pchID, $peg3, 1);
434 :     $loadUsesAsEvidence->Put($pchID, $peg4, 2);
435 : parrello 1.23 }
436 : parrello 1.1 }
437 :     }
438 :     }
439 :     }
440 :     }
441 :     # All done. Finish the load.
442 :     my $retVal = $self->_FinishAll();
443 :     return $retVal;
444 :     }
445 :    
446 :     =head3 LoadFeatureData
447 :    
448 :     C<< my $stats = $spl->LoadFeatureData(); >>
449 :    
450 :     Load the feature data from FIG into Sprout.
451 :    
452 :     Features represent annotated genes, and are therefore the heart of the data store.
453 :    
454 :     The following relations are loaded by this method.
455 :    
456 :     Feature
457 :     FeatureAlias
458 :     FeatureLink
459 :     FeatureTranslation
460 :     FeatureUpstream
461 :     IsLocatedIn
462 : parrello 1.30 HasFeature
463 : parrello 1.69 HasRoleInSubsystem
464 : parrello 1.1
465 :     =over 4
466 :    
467 :     =item RETURNS
468 :    
469 :     Returns a statistics object for the loads.
470 :    
471 :     =back
472 :    
473 :     =cut
474 :     #: Return Type $%;
475 :     sub LoadFeatureData {
476 :     # Get this object instance.
477 :     my ($self) = @_;
478 :     # Get the FIG object.
479 :     my $fig = $self->{fig};
480 :     # Get the table of genome IDs.
481 :     my $genomeHash = $self->{genomes};
482 :     # Create load objects for each of the tables we're loading.
483 : parrello 1.23 my $loadFeature = $self->_TableLoader('Feature');
484 : parrello 1.25 my $loadIsLocatedIn = $self->_TableLoader('IsLocatedIn', $self->PrimaryOnly);
485 : parrello 1.23 my $loadFeatureAlias = $self->_TableLoader('FeatureAlias');
486 :     my $loadFeatureLink = $self->_TableLoader('FeatureLink');
487 :     my $loadFeatureTranslation = $self->_TableLoader('FeatureTranslation');
488 :     my $loadFeatureUpstream = $self->_TableLoader('FeatureUpstream');
489 : parrello 1.69 my $loadHasFeature = $self->_TableLoader('HasFeature', $self->PrimaryOnly);
490 :     my $loadHasRoleInSubsystem = $self->_TableLoader('HasRoleInSubsystem', $self->PrimaryOnly);
491 : parrello 1.1 # Get the maximum sequence size. We need this later for splitting up the
492 :     # locations.
493 :     my $chunkSize = $self->{sprout}->MaxSegment();
494 : parrello 1.23 if ($self->{options}->{loadOnly}) {
495 :     Trace("Loading from existing files.") if T(2);
496 :     } else {
497 :     Trace("Generating feature data.") if T(2);
498 :     # Now we loop through the genomes, generating the data for each one.
499 :     for my $genomeID (sort keys %{$genomeHash}) {
500 :     Trace("Loading features for genome $genomeID.") if T(3);
501 :     $loadFeature->Add("genomeIn");
502 :     # Get the feature list for this genome.
503 :     my $features = $fig->all_features_detailed($genomeID);
504 : parrello 1.56 # Sort and count the list.
505 : parrello 1.57 my @featureTuples = sort { $a->[0] cmp $b->[0] } @{$features};
506 :     my $count = scalar @featureTuples;
507 : parrello 1.54 Trace("$count features found for genome $genomeID.") if T(3);
508 : parrello 1.56 # Set up for our duplicate-feature check.
509 :     my $oldFeatureID = "";
510 : parrello 1.23 # Loop through the features.
511 : parrello 1.57 for my $featureTuple (@featureTuples) {
512 : parrello 1.23 # Split the tuple.
513 : parrello 1.57 my ($featureID, $locations, undef, $type) = @{$featureTuple};
514 : parrello 1.56 # Check for duplicates.
515 :     if ($featureID eq $oldFeatureID) {
516 :     Trace("Duplicate feature $featureID found.") if T(1);
517 :     } else {
518 :     $oldFeatureID = $featureID;
519 :     # Count this feature.
520 :     $loadFeature->Add("featureIn");
521 : parrello 1.67 # Get the functional assignment.
522 :     my $assignment = $fig->function_of($featureID);
523 : parrello 1.56 # Create the feature record.
524 : parrello 1.67 $loadFeature->Put($featureID, 1, $type, $assignment);
525 : parrello 1.56 # Link it to the parent genome.
526 :     $loadHasFeature->Put($genomeID, $featureID, $type);
527 :     # Create the aliases.
528 :     for my $alias ($fig->feature_aliases($featureID)) {
529 :     $loadFeatureAlias->Put($featureID, $alias);
530 : parrello 1.8 }
531 : parrello 1.56 # Get the links.
532 :     my @links = $fig->fid_links($featureID);
533 :     for my $link (@links) {
534 :     $loadFeatureLink->Put($featureID, $link);
535 : parrello 1.8 }
536 : parrello 1.56 # If this is a peg, generate the translation and the upstream.
537 :     if ($type eq 'peg') {
538 :     $loadFeatureTranslation->Add("pegIn");
539 :     my $translation = $fig->get_translation($featureID);
540 :     if ($translation) {
541 :     $loadFeatureTranslation->Put($featureID, $translation);
542 :     }
543 :     # We use the default upstream values of u=200 and c=100.
544 :     my $upstream = $fig->upstream_of($featureID, 200, 100);
545 :     if ($upstream) {
546 :     $loadFeatureUpstream->Put($featureID, $upstream);
547 :     }
548 : parrello 1.23 }
549 : parrello 1.69 # Now we need to find the subsystems this feature participates in.
550 :     my @subsystems = $fig->peg_to_subsystems($featureID);
551 :     for my $subsystem (@subsystems) {
552 :     $loadHasRoleInSubsystem->Put($featureID, $subsystem);
553 :     }
554 : parrello 1.56 # This part is the roughest. We need to relate the features to contig
555 :     # locations, and the locations must be split so that none of them exceed
556 :     # the maximum segment size. This simplifies the genes_in_region processing
557 :     # for Sprout.
558 :     my @locationList = split /\s*,\s*/, $locations;
559 :     # Create the location position indicator.
560 :     my $i = 1;
561 :     # Loop through the locations.
562 :     for my $location (@locationList) {
563 :     # Parse the location.
564 :     my $locObject = BasicLocation->new("$genomeID:$location");
565 :     # Split it into a list of chunks.
566 :     my @locOList = ();
567 :     while (my $peeling = $locObject->Peel($chunkSize)) {
568 :     $loadIsLocatedIn->Add("peeling");
569 :     push @locOList, $peeling;
570 :     }
571 :     push @locOList, $locObject;
572 :     # Loop through the chunks, creating IsLocatedIn records. The variable
573 :     # "$i" will be used to keep the location index.
574 :     for my $locChunk (@locOList) {
575 :     $loadIsLocatedIn->Put($featureID, $locChunk->Contig, $locChunk->Left,
576 :     $locChunk->Dir, $locChunk->Length, $i);
577 :     $i++;
578 :     }
579 : parrello 1.23 }
580 : parrello 1.1 }
581 :     }
582 :     }
583 :     }
584 :     # Finish the loads.
585 :     my $retVal = $self->_FinishAll();
586 :     return $retVal;
587 :     }
588 :    
589 :     =head3 LoadBBHData
590 :    
591 :     C<< my $stats = $spl->LoadBBHData(); >>
592 :    
593 :     Load the bidirectional best hit data from FIG into Sprout.
594 :    
595 :     Sprout does not store information on similarities. Instead, it has only the
596 :     bi-directional best hits. Even so, the BBH table is one of the largest in
597 :     the database.
598 :    
599 :     The following relations are loaded by this method.
600 :    
601 :     IsBidirectionalBestHitOf
602 :    
603 :     =over 4
604 :    
605 :     =item RETURNS
606 :    
607 :     Returns a statistics object for the loads.
608 :    
609 :     =back
610 :    
611 :     =cut
612 :     #: Return Type $%;
613 : parrello 1.2 sub LoadBBHData {
614 : parrello 1.1 # Get this object instance.
615 :     my ($self) = @_;
616 :     # Get the FIG object.
617 :     my $fig = $self->{fig};
618 :     # Get the table of genome IDs.
619 :     my $genomeHash = $self->{genomes};
620 :     # Create load objects for each of the tables we're loading.
621 : parrello 1.23 my $loadIsBidirectionalBestHitOf = $self->_TableLoader('IsBidirectionalBestHitOf');
622 :     if ($self->{options}->{loadOnly}) {
623 :     Trace("Loading from existing files.") if T(2);
624 :     } else {
625 :     Trace("Generating BBH data.") if T(2);
626 :     # Now we loop through the genomes, generating the data for each one.
627 :     for my $genomeID (sort keys %{$genomeHash}) {
628 :     $loadIsBidirectionalBestHitOf->Add("genomeIn");
629 :     Trace("Processing features for genome $genomeID.") if T(3);
630 :     # Get the feature list for this genome.
631 :     my $features = $fig->all_features_detailed($genomeID);
632 : parrello 1.65 # Count the BBHs we find.
633 :     my $bbhCount = 0;
634 : parrello 1.23 # Loop through the features.
635 :     for my $featureData (@{$features}) {
636 :     # Split the tuple.
637 :     my ($featureID, $locations, $aliases, $type) = @{$featureData};
638 :     # Get the bi-directional best hits.
639 :     my @bbhList = $fig->bbhs($featureID);
640 :     for my $bbhEntry (@bbhList) {
641 :     # Get the target feature ID and the score.
642 :     my ($targetID, $score) = @{$bbhEntry};
643 :     # Check the target feature's genome.
644 :     my $targetGenomeID = $fig->genome_of($targetID);
645 :     # Only proceed if it's one of our genomes.
646 :     if ($genomeHash->{$targetGenomeID}) {
647 :     $loadIsBidirectionalBestHitOf->Put($featureID, $targetID, $targetGenomeID,
648 :     $score);
649 : parrello 1.65 $bbhCount++;
650 : parrello 1.23 }
651 : parrello 1.1 }
652 :     }
653 : parrello 1.65 Trace("$bbhCount BBHs found for $genomeID.") if T(3);
654 : parrello 1.1 }
655 :     }
656 :     # Finish the loads.
657 :     my $retVal = $self->_FinishAll();
658 :     return $retVal;
659 :     }
660 :    
661 :     =head3 LoadSubsystemData
662 :    
663 :     C<< my $stats = $spl->LoadSubsystemData(); >>
664 :    
665 :     Load the subsystem data from FIG into Sprout.
666 :    
667 :     Subsystems are groupings of genetic roles that work together to effect a specific
668 :     chemical reaction. Similar organisms require similar subsystems. To curate a subsystem,
669 :     a spreadsheet is created with genomes on one axis and subsystem roles on the other
670 :     axis. Similar features are then mapped into the cells, allowing the annotation of one
671 :     genome's roles to be used to assist in the annotation of others.
672 :    
673 :     The following relations are loaded by this method.
674 :    
675 :     Subsystem
676 : parrello 1.46 SubsystemClass
677 : parrello 1.1 Role
678 : parrello 1.19 RoleEC
679 : parrello 1.1 SSCell
680 :     ContainsFeature
681 :     IsGenomeOf
682 :     IsRoleOf
683 :     OccursInSubsystem
684 :     ParticipatesIn
685 :     HasSSCell
686 : parrello 1.18 ConsistsOfRoles
687 :     RoleSubset
688 :     HasRoleSubset
689 :     ConsistsOfGenomes
690 :     GenomeSubset
691 :     HasGenomeSubset
692 : parrello 1.20 Catalyzes
693 : parrello 1.21 Diagram
694 :     RoleOccursIn
695 : parrello 1.1
696 :     =over 4
697 :    
698 :     =item RETURNS
699 :    
700 :     Returns a statistics object for the loads.
701 :    
702 :     =back
703 :    
704 :     =cut
705 :     #: Return Type $%;
706 :     sub LoadSubsystemData {
707 :     # Get this object instance.
708 :     my ($self) = @_;
709 :     # Get the FIG object.
710 :     my $fig = $self->{fig};
711 :     # Get the genome hash. We'll use it to filter the genomes in each
712 :     # spreadsheet.
713 :     my $genomeHash = $self->{genomes};
714 :     # Get the subsystem hash. This lists the subsystems we'll process.
715 :     my $subsysHash = $self->{subsystems};
716 :     my @subsysIDs = sort keys %{$subsysHash};
717 : parrello 1.21 # Get the map list.
718 :     my @maps = $fig->all_maps;
719 : parrello 1.1 # Create load objects for each of the tables we're loading.
720 : parrello 1.25 my $loadDiagram = $self->_TableLoader('Diagram', $self->PrimaryOnly);
721 :     my $loadRoleOccursIn = $self->_TableLoader('RoleOccursIn', $self->PrimaryOnly);
722 : parrello 1.23 my $loadSubsystem = $self->_TableLoader('Subsystem');
723 : parrello 1.25 my $loadRole = $self->_TableLoader('Role', $self->PrimaryOnly);
724 :     my $loadRoleEC = $self->_TableLoader('RoleEC', $self->PrimaryOnly);
725 :     my $loadCatalyzes = $self->_TableLoader('Catalyzes', $self->PrimaryOnly);
726 :     my $loadSSCell = $self->_TableLoader('SSCell', $self->PrimaryOnly);
727 :     my $loadContainsFeature = $self->_TableLoader('ContainsFeature', $self->PrimaryOnly);
728 :     my $loadIsGenomeOf = $self->_TableLoader('IsGenomeOf', $self->PrimaryOnly);
729 :     my $loadIsRoleOf = $self->_TableLoader('IsRoleOf', $self->PrimaryOnly);
730 :     my $loadOccursInSubsystem = $self->_TableLoader('OccursInSubsystem', $self->PrimaryOnly);
731 :     my $loadParticipatesIn = $self->_TableLoader('ParticipatesIn', $self->PrimaryOnly);
732 :     my $loadHasSSCell = $self->_TableLoader('HasSSCell', $self->PrimaryOnly);
733 :     my $loadRoleSubset = $self->_TableLoader('RoleSubset', $self->PrimaryOnly);
734 :     my $loadGenomeSubset = $self->_TableLoader('GenomeSubset', $self->PrimaryOnly);
735 :     my $loadConsistsOfRoles = $self->_TableLoader('ConsistsOfRoles', $self->PrimaryOnly);
736 :     my $loadConsistsOfGenomes = $self->_TableLoader('ConsistsOfGenomes', $self->PrimaryOnly);
737 :     my $loadHasRoleSubset = $self->_TableLoader('HasRoleSubset', $self->PrimaryOnly);
738 :     my $loadHasGenomeSubset = $self->_TableLoader('HasGenomeSubset', $self->PrimaryOnly);
739 : parrello 1.46 my $loadSubsystemClass = $self->_TableLoader('SubsystemClass', $self->PrimaryOnly);
740 : parrello 1.23 if ($self->{options}->{loadOnly}) {
741 :     Trace("Loading from existing files.") if T(2);
742 :     } else {
743 :     Trace("Generating subsystem data.") if T(2);
744 :     # This hash will contain the role for each EC. When we're done, this
745 :     # information will be used to generate the Catalyzes table.
746 :     my %ecToRoles = ();
747 :     # Loop through the subsystems. Our first task will be to create the
748 :     # roles. We do this by looping through the subsystems and creating a
749 :     # role hash. The hash tracks each role ID so that we don't create
750 :     # duplicates. As we move along, we'll connect the roles and subsystems
751 :     # and memorize up the reactions.
752 :     my ($genomeID, $roleID);
753 :     my %roleData = ();
754 :     for my $subsysID (@subsysIDs) {
755 :     # Get the subsystem object.
756 :     my $sub = $fig->get_subsystem($subsysID);
757 : parrello 1.32 # Only proceed if the subsystem has a spreadsheet.
758 :     if (! $sub->{empty_ss}) {
759 : parrello 1.31 Trace("Creating subsystem $subsysID.") if T(3);
760 :     $loadSubsystem->Add("subsystemIn");
761 :     # Create the subsystem record.
762 :     my $curator = $sub->get_curator();
763 :     my $notes = $sub->get_notes();
764 :     $loadSubsystem->Put($subsysID, $curator, $notes);
765 : parrello 1.64 my $classList = $fig->subsystem_classification($subsysID);
766 :     my @classes = @$classList;
767 :     if (@classes) {
768 :     for my $class (@classes) {
769 :     $loadSubsystemClass->Put($subsysID, $class);
770 :     }
771 : parrello 1.46 }
772 : parrello 1.31 # Connect it to its roles. Each role is a column in the subsystem spreadsheet.
773 :     for (my $col = 0; defined($roleID = $sub->get_role($col)); $col++) {
774 :     # Connect to this role.
775 :     $loadOccursInSubsystem->Add("roleIn");
776 :     $loadOccursInSubsystem->Put($roleID, $subsysID, $col);
777 :     # If it's a new role, add it to the role table.
778 :     if (! exists $roleData{$roleID}) {
779 :     # Get the role's abbreviation.
780 :     my $abbr = $sub->get_role_abbr($col);
781 :     # Add the role.
782 :     $loadRole->Put($roleID, $abbr);
783 :     $roleData{$roleID} = 1;
784 :     # Check for an EC number.
785 :     if ($roleID =~ /\(EC ([^.]+\.[^.]+\.[^.]+\.[^)]+)\)\s*$/) {
786 :     my $ec = $1;
787 :     $loadRoleEC->Put($roleID, $ec);
788 :     $ecToRoles{$ec} = $roleID;
789 :     }
790 : parrello 1.23 }
791 : parrello 1.18 }
792 : parrello 1.31 # Now we create the spreadsheet for the subsystem by matching roles to
793 :     # genomes. Each genome is a row and each role is a column. We may need
794 :     # to actually create the roles as we find them.
795 :     Trace("Creating subsystem $subsysID spreadsheet.") if T(3);
796 :     for (my $row = 0; defined($genomeID = $sub->get_genome($row)); $row++) {
797 :     # Only proceed if this is one of our genomes.
798 :     if (exists $genomeHash->{$genomeID}) {
799 :     # Count the PEGs and cells found for verification purposes.
800 :     my $pegCount = 0;
801 :     my $cellCount = 0;
802 :     # Create a list for the PEGs we find. This list will be used
803 :     # to generate cluster numbers.
804 :     my @pegsFound = ();
805 :     # Create a hash that maps spreadsheet IDs to PEGs. We will
806 :     # use this to generate the ContainsFeature data after we have
807 :     # the cluster numbers.
808 :     my %cellPegs = ();
809 :     # Get the genome's variant code for this subsystem.
810 :     my $variantCode = $sub->get_variant_code($row);
811 :     # Loop through the subsystem's roles. We use an index because it is
812 :     # part of the spreadsheet cell ID.
813 :     for (my $col = 0; defined($roleID = $sub->get_role($col)); $col++) {
814 :     # Get the features in the spreadsheet cell for this genome and role.
815 : parrello 1.37 my @pegs = grep { !$fig->is_deleted_fid($_) } $sub->get_pegs_from_cell($row, $col);
816 : parrello 1.31 # Only proceed if features exist.
817 :     if (@pegs > 0) {
818 :     # Create the spreadsheet cell.
819 :     $cellCount++;
820 :     my $cellID = "$subsysID:$genomeID:$col";
821 :     $loadSSCell->Put($cellID);
822 :     $loadIsGenomeOf->Put($genomeID, $cellID);
823 :     $loadIsRoleOf->Put($roleID, $cellID);
824 :     $loadHasSSCell->Put($subsysID, $cellID);
825 :     # Remember its features.
826 :     push @pegsFound, @pegs;
827 :     $cellPegs{$cellID} = \@pegs;
828 :     $pegCount += @pegs;
829 :     }
830 : parrello 1.23 }
831 : parrello 1.31 # If we found some cells for this genome, we need to compute clusters and
832 :     # denote it participates in the subsystem.
833 :     if ($pegCount > 0) {
834 :     Trace("$pegCount PEGs in $cellCount cells for $genomeID.") if T(3);
835 :     $loadParticipatesIn->Put($genomeID, $subsysID, $variantCode);
836 :     # Create a hash mapping PEG IDs to cluster numbers.
837 :     # We default to -1 for all of them.
838 :     my %clusterOf = map { $_ => -1 } @pegsFound;
839 : parrello 1.41 # Partition the PEGs found into clusters.
840 :     my @clusters = $fig->compute_clusters([keys %clusterOf], $sub);
841 : parrello 1.31 for (my $i = 0; $i <= $#clusters; $i++) {
842 :     my $subList = $clusters[$i];
843 :     for my $peg (@{$subList}) {
844 :     $clusterOf{$peg} = $i;
845 :     }
846 : parrello 1.23 }
847 : parrello 1.31 # Create the ContainsFeature data.
848 :     for my $cellID (keys %cellPegs) {
849 :     my $cellList = $cellPegs{$cellID};
850 :     for my $cellPeg (@$cellList) {
851 :     $loadContainsFeature->Put($cellID, $cellPeg, $clusterOf{$cellPeg});
852 :     }
853 : parrello 1.23 }
854 : parrello 1.18 }
855 :     }
856 : parrello 1.15 }
857 : parrello 1.31 # Now we need to generate the subsets. The subset names must be concatenated to
858 :     # the subsystem name to make them unique keys. There are two types of subsets:
859 :     # genome subsets and role subsets. We do the role subsets first.
860 :     my @subsetNames = $sub->get_subset_names();
861 :     for my $subsetID (@subsetNames) {
862 :     # Create the subset record.
863 :     my $actualID = "$subsysID:$subsetID";
864 :     $loadRoleSubset->Put($actualID);
865 :     # Connect the subset to the subsystem.
866 :     $loadHasRoleSubset->Put($subsysID, $actualID);
867 :     # Connect the subset to its roles.
868 :     my @roles = $sub->get_subsetC_roles($subsetID);
869 :     for my $roleID (@roles) {
870 :     $loadConsistsOfRoles->Put($actualID, $roleID);
871 :     }
872 :     }
873 :     # Next the genome subsets.
874 :     @subsetNames = $sub->get_subset_namesR();
875 :     for my $subsetID (@subsetNames) {
876 :     # Create the subset record.
877 :     my $actualID = "$subsysID:$subsetID";
878 :     $loadGenomeSubset->Put($actualID);
879 :     # Connect the subset to the subsystem.
880 :     $loadHasGenomeSubset->Put($subsysID, $actualID);
881 :     # Connect the subset to its genomes.
882 :     my @genomes = $sub->get_subsetR($subsetID);
883 :     for my $genomeID (@genomes) {
884 :     $loadConsistsOfGenomes->Put($actualID, $genomeID);
885 :     }
886 : parrello 1.23 }
887 : parrello 1.18 }
888 : parrello 1.57 }
889 :     # Now we loop through the diagrams. We need to create the diagram records
890 :     # and link each diagram to its roles. Note that only roles which occur
891 :     # in subsystems (and therefore appear in the %ecToRoles hash) are
892 :     # included.
893 :     for my $map (@maps) {
894 :     Trace("Loading diagram $map.") if T(3);
895 :     # Get the diagram's descriptive name.
896 :     my $name = $fig->map_name($map);
897 :     $loadDiagram->Put($map, $name);
898 :     # Now we need to link all the map's roles to it.
899 :     # A hash is used to prevent duplicates.
900 :     my %roleHash = ();
901 :     for my $role ($fig->map_to_ecs($map)) {
902 :     if (exists $ecToRoles{$role} && ! $roleHash{$role}) {
903 :     $loadRoleOccursIn->Put($ecToRoles{$role}, $map);
904 :     $roleHash{$role} = 1;
905 : parrello 1.23 }
906 : parrello 1.21 }
907 : parrello 1.57 }
908 :     # Before we leave, we must create the Catalyzes table. We start with the reactions,
909 :     # then use the "ecToRoles" table to convert EC numbers to role IDs.
910 :     my @reactions = $fig->all_reactions();
911 :     for my $reactionID (@reactions) {
912 :     # Get this reaction's list of roles. The results will be EC numbers.
913 :     my @roles = $fig->catalyzed_by($reactionID);
914 :     # Loop through the roles, creating catalyzation records.
915 :     for my $thisRole (@roles) {
916 :     if (exists $ecToRoles{$thisRole}) {
917 :     $loadCatalyzes->Put($ecToRoles{$thisRole}, $reactionID);
918 : parrello 1.23 }
919 : parrello 1.18 }
920 :     }
921 : parrello 1.1 }
922 :     # Finish the load.
923 :     my $retVal = $self->_FinishAll();
924 :     return $retVal;
925 :     }
926 :    
927 :     =head3 LoadPropertyData
928 :    
929 :     C<< my $stats = $spl->LoadPropertyData(); >>
930 :    
931 :     Load the attribute data from FIG into Sprout.
932 :    
933 :     Attribute data in FIG corresponds to the Sprout concept of Property. As currently
934 :     implemented, each key-value attribute combination in the SEED corresponds to a
935 :     record in the B<Property> table. The B<HasProperty> relationship links the
936 :     features to the properties.
937 :    
938 :     The SEED also allows attributes to be assigned to genomes, but this is not yet
939 :     supported by Sprout.
940 :    
941 :     The following relations are loaded by this method.
942 :    
943 :     HasProperty
944 :     Property
945 :    
946 :     =over 4
947 :    
948 :     =item RETURNS
949 :    
950 :     Returns a statistics object for the loads.
951 :    
952 :     =back
953 :    
954 :     =cut
955 :     #: Return Type $%;
956 :     sub LoadPropertyData {
957 :     # Get this object instance.
958 :     my ($self) = @_;
959 :     # Get the FIG object.
960 :     my $fig = $self->{fig};
961 :     # Get the genome hash.
962 :     my $genomeHash = $self->{genomes};
963 :     # Create load objects for each of the tables we're loading.
964 : parrello 1.23 my $loadProperty = $self->_TableLoader('Property');
965 : parrello 1.25 my $loadHasProperty = $self->_TableLoader('HasProperty', $self->PrimaryOnly);
966 : parrello 1.23 if ($self->{options}->{loadOnly}) {
967 :     Trace("Loading from existing files.") if T(2);
968 :     } else {
969 :     Trace("Generating property data.") if T(2);
970 :     # Create a hash for storing property IDs.
971 :     my %propertyKeys = ();
972 :     my $nextID = 1;
973 :     # Loop through the genomes.
974 : parrello 1.66 for my $genomeID (sort keys %{$genomeHash}) {
975 : parrello 1.23 $loadProperty->Add("genomeIn");
976 : parrello 1.24 Trace("Generating properties for $genomeID.") if T(3);
977 : parrello 1.23 # Get the genome's features. The feature ID is the first field in the
978 :     # tuples returned by "all_features_detailed". We use "all_features_detailed"
979 :     # rather than "all_features" because we want all features regardless of type.
980 :     my @features = map { $_->[0] } @{$fig->all_features_detailed($genomeID)};
981 : parrello 1.24 my $featureCount = 0;
982 :     my $propertyCount = 0;
983 : parrello 1.23 # Loop through the features, creating HasProperty records.
984 :     for my $fid (@features) {
985 :     # Get all attributes for this feature. We do this one feature at a time
986 :     # to insure we do not get any genome attributes.
987 :     my @attributeList = $fig->get_attributes($fid, '', '', '');
988 : parrello 1.24 if (scalar @attributeList) {
989 :     $featureCount++;
990 :     }
991 : parrello 1.23 # Loop through the attributes.
992 :     for my $tuple (@attributeList) {
993 : parrello 1.24 $propertyCount++;
994 : parrello 1.23 # Get this attribute value's data. Note that we throw away the FID,
995 :     # since it will always be the same as the value if "$fid".
996 :     my (undef, $key, $value, $url) = @{$tuple};
997 :     # Concatenate the key and value and check the "propertyKeys" hash to
998 :     # see if we already have an ID for it. We use a tab for the separator
999 :     # character.
1000 :     my $propertyKey = "$key\t$value";
1001 :     # Use the concatenated value to check for an ID. If no ID exists, we
1002 :     # create one.
1003 :     my $propertyID = $propertyKeys{$propertyKey};
1004 :     if (! $propertyID) {
1005 :     # Here we need to create a new property ID for this key/value pair.
1006 :     $propertyKeys{$propertyKey} = $nextID;
1007 :     $propertyID = $nextID;
1008 :     $nextID++;
1009 :     $loadProperty->Put($propertyID, $key, $value);
1010 :     }
1011 :     # Create the HasProperty entry for this feature/property association.
1012 :     $loadHasProperty->Put($fid, $propertyID, $url);
1013 : parrello 1.1 }
1014 :     }
1015 : parrello 1.24 # Update the statistics.
1016 :     Trace("$propertyCount attributes processed for $featureCount features.") if T(3);
1017 :     $loadHasProperty->Add("featuresIn", $featureCount);
1018 :     $loadHasProperty->Add("propertiesIn", $propertyCount);
1019 : parrello 1.1 }
1020 :     }
1021 :     # Finish the load.
1022 :     my $retVal = $self->_FinishAll();
1023 :     return $retVal;
1024 :     }
1025 :    
1026 :     =head3 LoadAnnotationData
1027 :    
1028 :     C<< my $stats = $spl->LoadAnnotationData(); >>
1029 :    
1030 :     Load the annotation data from FIG into Sprout.
1031 :    
1032 :     Sprout annotations encompass both the assignments and the annotations in SEED.
1033 :     These describe the function performed by a PEG as well as any other useful
1034 :     information that may aid in identifying its purpose.
1035 :    
1036 :     The following relations are loaded by this method.
1037 :    
1038 :     Annotation
1039 :     IsTargetOfAnnotation
1040 :     SproutUser
1041 :     MadeAnnotation
1042 :    
1043 :     =over 4
1044 :    
1045 :     =item RETURNS
1046 :    
1047 :     Returns a statistics object for the loads.
1048 :    
1049 :     =back
1050 :    
1051 :     =cut
1052 :     #: Return Type $%;
1053 :     sub LoadAnnotationData {
1054 :     # Get this object instance.
1055 :     my ($self) = @_;
1056 :     # Get the FIG object.
1057 :     my $fig = $self->{fig};
1058 :     # Get the genome hash.
1059 :     my $genomeHash = $self->{genomes};
1060 :     # Create load objects for each of the tables we're loading.
1061 : parrello 1.23 my $loadAnnotation = $self->_TableLoader('Annotation');
1062 : parrello 1.25 my $loadIsTargetOfAnnotation = $self->_TableLoader('IsTargetOfAnnotation', $self->PrimaryOnly);
1063 :     my $loadSproutUser = $self->_TableLoader('SproutUser', $self->PrimaryOnly);
1064 :     my $loadUserAccess = $self->_TableLoader('UserAccess', $self->PrimaryOnly);
1065 :     my $loadMadeAnnotation = $self->_TableLoader('MadeAnnotation', $self->PrimaryOnly);
1066 : parrello 1.23 if ($self->{options}->{loadOnly}) {
1067 :     Trace("Loading from existing files.") if T(2);
1068 :     } else {
1069 :     Trace("Generating annotation data.") if T(2);
1070 :     # Create a hash of user names. We'll use this to prevent us from generating duplicate
1071 :     # user records.
1072 :     my %users = ( FIG => 1, master => 1 );
1073 :     # Put in FIG and "master".
1074 :     $loadSproutUser->Put("FIG", "Fellowship for Interpretation of Genomes");
1075 :     $loadUserAccess->Put("FIG", 1);
1076 :     $loadSproutUser->Put("master", "Master User");
1077 :     $loadUserAccess->Put("master", 1);
1078 :     # Get the current time.
1079 :     my $time = time();
1080 :     # Loop through the genomes.
1081 :     for my $genomeID (sort keys %{$genomeHash}) {
1082 :     Trace("Processing $genomeID.") if T(3);
1083 : parrello 1.38 # Create a hash of timestamps. We use this to prevent duplicate time stamps
1084 :     # from showing up for a single PEG's annotations.
1085 :     my %seenTimestamps = ();
1086 : parrello 1.36 # Get the genome's annotations.
1087 :     my @annotations = $fig->read_all_annotations($genomeID);
1088 :     Trace("Processing annotations.") if T(2);
1089 :     for my $tuple (@annotations) {
1090 :     # Get the annotation tuple.
1091 :     my ($peg, $timestamp, $user, $text) = @{$tuple};
1092 :     # Here we fix up the annotation text. "\r" is removed,
1093 : parrello 1.42 # and "\t" and "\n" are escaped. Note we use the "gs"
1094 : parrello 1.36 # modifier so that new-lines inside the text do not
1095 :     # stop the substitution search.
1096 :     $text =~ s/\r//gs;
1097 :     $text =~ s/\t/\\t/gs;
1098 :     $text =~ s/\n/\\n/gs;
1099 :     # Change assignments by the master user to FIG assignments.
1100 :     $text =~ s/Set master function/Set FIG function/s;
1101 :     # Insure the time stamp is valid.
1102 :     if ($timestamp =~ /^\d+$/) {
1103 :     # Here it's a number. We need to insure the one we use to form
1104 :     # the key is unique.
1105 :     my $keyStamp = $timestamp;
1106 :     while ($seenTimestamps{"$peg:$keyStamp"}) {
1107 :     $keyStamp++;
1108 : parrello 1.1 }
1109 : parrello 1.36 my $annotationID = "$peg:$keyStamp";
1110 :     $seenTimestamps{$annotationID} = 1;
1111 :     # Insure the user exists.
1112 :     if (! $users{$user}) {
1113 :     $loadSproutUser->Put($user, "SEED user");
1114 :     $loadUserAccess->Put($user, 1);
1115 :     $users{$user} = 1;
1116 :     }
1117 :     # Generate the annotation.
1118 :     $loadAnnotation->Put($annotationID, $timestamp, $text);
1119 :     $loadIsTargetOfAnnotation->Put($peg, $annotationID);
1120 :     $loadMadeAnnotation->Put($user, $annotationID);
1121 :     } else {
1122 :     # Here we have an invalid time stamp.
1123 :     Trace("Invalid time stamp \"$timestamp\" in annotations for $peg.") if T(1);
1124 : parrello 1.1 }
1125 :     }
1126 :     }
1127 :     }
1128 :     # Finish the load.
1129 :     my $retVal = $self->_FinishAll();
1130 :     return $retVal;
1131 :     }
1132 :    
1133 : parrello 1.5 =head3 LoadSourceData
1134 :    
1135 :     C<< my $stats = $spl->LoadSourceData(); >>
1136 :    
1137 :     Load the source data from FIG into Sprout.
1138 :    
1139 :     Source data links genomes to information about the organizations that
1140 :     mapped it.
1141 :    
1142 :     The following relations are loaded by this method.
1143 :    
1144 :     ComesFrom
1145 :     Source
1146 :     SourceURL
1147 :    
1148 :     There is no direct support for source attribution in FIG, so we access the SEED
1149 :     files directly.
1150 :    
1151 :     =over 4
1152 :    
1153 :     =item RETURNS
1154 :    
1155 :     Returns a statistics object for the loads.
1156 :    
1157 :     =back
1158 :    
1159 :     =cut
1160 :     #: Return Type $%;
1161 :     sub LoadSourceData {
1162 :     # Get this object instance.
1163 :     my ($self) = @_;
1164 :     # Get the FIG object.
1165 :     my $fig = $self->{fig};
1166 :     # Get the genome hash.
1167 :     my $genomeHash = $self->{genomes};
1168 :     # Create load objects for each of the tables we're loading.
1169 : parrello 1.25 my $loadComesFrom = $self->_TableLoader('ComesFrom', $self->PrimaryOnly);
1170 : parrello 1.23 my $loadSource = $self->_TableLoader('Source');
1171 :     my $loadSourceURL = $self->_TableLoader('SourceURL');
1172 :     if ($self->{options}->{loadOnly}) {
1173 :     Trace("Loading from existing files.") if T(2);
1174 :     } else {
1175 :     Trace("Generating annotation data.") if T(2);
1176 :     # Create hashes to collect the Source information.
1177 :     my %sourceURL = ();
1178 :     my %sourceDesc = ();
1179 :     # Loop through the genomes.
1180 :     my $line;
1181 :     for my $genomeID (sort keys %{$genomeHash}) {
1182 :     Trace("Processing $genomeID.") if T(3);
1183 :     # Open the project file.
1184 :     if ((open(TMP, "<$FIG_Config::organisms/$genomeID/PROJECT")) &&
1185 :     defined($line = <TMP>)) {
1186 :     chomp $line;
1187 :     my($sourceID, $desc, $url) = split(/\t/,$line);
1188 :     $loadComesFrom->Put($genomeID, $sourceID);
1189 :     if ($url && ! exists $sourceURL{$sourceID}) {
1190 :     $loadSourceURL->Put($sourceID, $url);
1191 :     $sourceURL{$sourceID} = 1;
1192 :     }
1193 :     if ($desc) {
1194 :     $sourceDesc{$sourceID} = $desc;
1195 :     } elsif (! exists $sourceDesc{$sourceID}) {
1196 :     $sourceDesc{$sourceID} = $sourceID;
1197 :     }
1198 : parrello 1.5 }
1199 : parrello 1.23 close TMP;
1200 :     }
1201 :     # Write the source descriptions.
1202 :     for my $sourceID (keys %sourceDesc) {
1203 :     $loadSource->Put($sourceID, $sourceDesc{$sourceID});
1204 : parrello 1.5 }
1205 : parrello 1.16 }
1206 : parrello 1.5 # Finish the load.
1207 :     my $retVal = $self->_FinishAll();
1208 :     return $retVal;
1209 :     }
1210 :    
1211 : parrello 1.6 =head3 LoadExternalData
1212 :    
1213 :     C<< my $stats = $spl->LoadExternalData(); >>
1214 :    
1215 :     Load the external data from FIG into Sprout.
1216 :    
1217 :     External data contains information about external feature IDs.
1218 :    
1219 :     The following relations are loaded by this method.
1220 :    
1221 :     ExternalAliasFunc
1222 :     ExternalAliasOrg
1223 :    
1224 :     The support for external IDs in FIG is hidden beneath layers of other data, so
1225 :     we access the SEED files directly to create these tables. This is also one of
1226 :     the few load methods that does not proceed genome by genome.
1227 :    
1228 :     =over 4
1229 :    
1230 :     =item RETURNS
1231 :    
1232 :     Returns a statistics object for the loads.
1233 :    
1234 :     =back
1235 :    
1236 :     =cut
1237 :     #: Return Type $%;
1238 :     sub LoadExternalData {
1239 :     # Get this object instance.
1240 :     my ($self) = @_;
1241 :     # Get the FIG object.
1242 :     my $fig = $self->{fig};
1243 :     # Get the genome hash.
1244 :     my $genomeHash = $self->{genomes};
1245 :     # Convert the genome hash. We'll get the genus and species for each genome and make
1246 :     # it the key.
1247 :     my %speciesHash = map { $fig->genus_species($_) => $_ } (keys %{$genomeHash});
1248 :     # Create load objects for each of the tables we're loading.
1249 : parrello 1.23 my $loadExternalAliasFunc = $self->_TableLoader('ExternalAliasFunc');
1250 :     my $loadExternalAliasOrg = $self->_TableLoader('ExternalAliasOrg');
1251 :     if ($self->{options}->{loadOnly}) {
1252 :     Trace("Loading from existing files.") if T(2);
1253 :     } else {
1254 :     Trace("Generating external data.") if T(2);
1255 :     # We loop through the files one at a time. First, the organism file.
1256 : parrello 1.58 Open(\*ORGS, "sort +0 -1 -u -t\"\t\" $FIG_Config::global/ext_org.table |");
1257 : parrello 1.23 my $orgLine;
1258 :     while (defined($orgLine = <ORGS>)) {
1259 :     # Clean the input line.
1260 :     chomp $orgLine;
1261 :     # Parse the organism name.
1262 :     my ($protID, $name) = split /\s*\t\s*/, $orgLine;
1263 :     $loadExternalAliasOrg->Put($protID, $name);
1264 :     }
1265 :     close ORGS;
1266 :     # Now the function file.
1267 :     my $funcLine;
1268 : parrello 1.58 Open(\*FUNCS, "sort +0 -1 -u -t\"\t\" $FIG_Config::global/ext_func.table |");
1269 : parrello 1.23 while (defined($funcLine = <FUNCS>)) {
1270 :     # Clean the line ending.
1271 :     chomp $funcLine;
1272 :     # Only proceed if the line is non-blank.
1273 :     if ($funcLine) {
1274 :     # Split it into fields.
1275 :     my @funcFields = split /\s*\t\s*/, $funcLine;
1276 :     # If there's an EC number, append it to the description.
1277 :     if ($#funcFields >= 2 && $funcFields[2] =~ /^(EC .*\S)/) {
1278 :     $funcFields[1] .= " $1";
1279 :     }
1280 :     # Output the function line.
1281 :     $loadExternalAliasFunc->Put(@funcFields[0,1]);
1282 : parrello 1.6 }
1283 :     }
1284 :     }
1285 :     # Finish the load.
1286 :     my $retVal = $self->_FinishAll();
1287 :     return $retVal;
1288 :     }
1289 : parrello 1.5
1290 : parrello 1.18
1291 :     =head3 LoadReactionData
1292 :    
1293 :     C<< my $stats = $spl->LoadReactionData(); >>
1294 :    
1295 :     Load the reaction data from FIG into Sprout.
1296 :    
1297 :     Reaction data connects reactions to the compounds that participate in them.
1298 :    
1299 :     The following relations are loaded by this method.
1300 :    
1301 : parrello 1.20 Reaction
1302 : parrello 1.18 ReactionURL
1303 :     Compound
1304 :     CompoundName
1305 :     CompoundCAS
1306 :     IsAComponentOf
1307 :    
1308 :     This method proceeds reaction by reaction rather than genome by genome.
1309 :    
1310 :     =over 4
1311 :    
1312 :     =item RETURNS
1313 :    
1314 :     Returns a statistics object for the loads.
1315 :    
1316 :     =back
1317 :    
1318 :     =cut
1319 :     #: Return Type $%;
1320 :     sub LoadReactionData {
1321 :     # Get this object instance.
1322 :     my ($self) = @_;
1323 :     # Get the FIG object.
1324 :     my $fig = $self->{fig};
1325 :     # Create load objects for each of the tables we're loading.
1326 : parrello 1.23 my $loadReaction = $self->_TableLoader('Reaction');
1327 : parrello 1.25 my $loadReactionURL = $self->_TableLoader('ReactionURL', $self->PrimaryOnly);
1328 :     my $loadCompound = $self->_TableLoader('Compound', $self->PrimaryOnly);
1329 :     my $loadCompoundName = $self->_TableLoader('CompoundName', $self->PrimaryOnly);
1330 :     my $loadCompoundCAS = $self->_TableLoader('CompoundCAS', $self->PrimaryOnly);
1331 :     my $loadIsAComponentOf = $self->_TableLoader('IsAComponentOf', $self->PrimaryOnly);
1332 : parrello 1.23 if ($self->{options}->{loadOnly}) {
1333 :     Trace("Loading from existing files.") if T(2);
1334 :     } else {
1335 :     Trace("Generating annotation data.") if T(2);
1336 :     # First we create the compounds.
1337 :     my @compounds = $fig->all_compounds();
1338 :     for my $cid (@compounds) {
1339 :     # Check for names.
1340 :     my @names = $fig->names_of_compound($cid);
1341 :     # Each name will be given a priority number, starting with 1.
1342 :     my $prio = 1;
1343 :     for my $name (@names) {
1344 :     $loadCompoundName->Put($cid, $name, $prio++);
1345 :     }
1346 :     # Create the main compound record. Note that the first name
1347 :     # becomes the label.
1348 :     my $label = (@names > 0 ? $names[0] : $cid);
1349 :     $loadCompound->Put($cid, $label);
1350 :     # Check for a CAS ID.
1351 :     my $cas = $fig->cas($cid);
1352 :     if ($cas) {
1353 :     $loadCompoundCAS->Put($cid, $cas);
1354 :     }
1355 : parrello 1.20 }
1356 : parrello 1.23 # All the compounds are set up, so we need to loop through the reactions next. First,
1357 :     # we initialize the discriminator index. This is a single integer used to insure
1358 :     # duplicate elements in a reaction are not accidentally collapsed.
1359 :     my $discrim = 0;
1360 :     my @reactions = $fig->all_reactions();
1361 :     for my $reactionID (@reactions) {
1362 :     # Create the reaction record.
1363 :     $loadReaction->Put($reactionID, $fig->reversible($reactionID));
1364 :     # Compute the reaction's URL.
1365 :     my $url = HTML::reaction_link($reactionID);
1366 :     # Put it in the ReactionURL table.
1367 :     $loadReactionURL->Put($reactionID, $url);
1368 :     # Now we need all of the reaction's compounds. We get these in two phases,
1369 :     # substrates first and then products.
1370 :     for my $product (0, 1) {
1371 :     # Get the compounds of the current type for the current reaction. FIG will
1372 :     # give us 3-tuples: [ID, stoichiometry, main-flag]. At this time we do not
1373 :     # have location data in SEED, so it defaults to the empty string.
1374 :     my @compounds = $fig->reaction2comp($reactionID, $product);
1375 :     for my $compData (@compounds) {
1376 :     # Extract the compound data from the current tuple.
1377 :     my ($cid, $stoich, $main) = @{$compData};
1378 :     # Link the compound to the reaction.
1379 :     $loadIsAComponentOf->Put($cid, $reactionID, $discrim++, "", $main,
1380 :     $product, $stoich);
1381 :     }
1382 : parrello 1.18 }
1383 :     }
1384 :     }
1385 :     # Finish the load.
1386 :     my $retVal = $self->_FinishAll();
1387 :     return $retVal;
1388 :     }
1389 :    
1390 : parrello 1.5 =head3 LoadGroupData
1391 :    
1392 :     C<< my $stats = $spl->LoadGroupData(); >>
1393 :    
1394 :     Load the genome Groups into Sprout.
1395 :    
1396 :     The following relations are loaded by this method.
1397 :    
1398 :     GenomeGroups
1399 :    
1400 : parrello 1.68 Currently, we do not use groups. We used to use them for NMPDR groups,
1401 :     butThere is no direct support for genome groups in FIG, so we access the SEED
1402 : parrello 1.5 files directly.
1403 :    
1404 :     =over 4
1405 :    
1406 :     =item RETURNS
1407 :    
1408 :     Returns a statistics object for the loads.
1409 :    
1410 :     =back
1411 :    
1412 :     =cut
1413 :     #: Return Type $%;
1414 :     sub LoadGroupData {
1415 :     # Get this object instance.
1416 :     my ($self) = @_;
1417 :     # Get the FIG object.
1418 :     my $fig = $self->{fig};
1419 :     # Get the genome hash.
1420 :     my $genomeHash = $self->{genomes};
1421 :     # Create a load object for the table we're loading.
1422 : parrello 1.23 my $loadGenomeGroups = $self->_TableLoader('GenomeGroups');
1423 :     if ($self->{options}->{loadOnly}) {
1424 :     Trace("Loading from existing files.") if T(2);
1425 :     } else {
1426 :     Trace("Generating group data.") if T(2);
1427 : parrello 1.68 # Currently there are no groups.
1428 : parrello 1.5 }
1429 :     # Finish the load.
1430 :     my $retVal = $self->_FinishAll();
1431 :     return $retVal;
1432 :     }
1433 :    
1434 : parrello 1.43 =head3 LoadSynonymData
1435 :    
1436 :     C<< my $stats = $spl->LoadSynonymData(); >>
1437 :    
1438 :     Load the synonym groups into Sprout.
1439 :    
1440 :     The following relations are loaded by this method.
1441 :    
1442 :     SynonymGroup
1443 :     IsSynonymGroupFor
1444 :    
1445 :     The source information for these relations is taken from the C<maps_to_id> method
1446 : parrello 1.56 of the B<FIG> object. Unfortunately, to make this work, we need to use direct
1447 :     SQL against the FIG database.
1448 : parrello 1.43
1449 :     =over 4
1450 :    
1451 :     =item RETURNS
1452 :    
1453 :     Returns a statistics object for the loads.
1454 :    
1455 :     =back
1456 :    
1457 :     =cut
1458 :     #: Return Type $%;
1459 :     sub LoadSynonymData {
1460 :     # Get this object instance.
1461 :     my ($self) = @_;
1462 :     # Get the FIG object.
1463 :     my $fig = $self->{fig};
1464 :     # Get the genome hash.
1465 :     my $genomeHash = $self->{genomes};
1466 :     # Create a load object for the table we're loading.
1467 :     my $loadSynonymGroup = $self->_TableLoader('SynonymGroup');
1468 :     my $loadIsSynonymGroupFor = $self->_TableLoader('IsSynonymGroupFor');
1469 :     if ($self->{options}->{loadOnly}) {
1470 :     Trace("Loading from existing files.") if T(2);
1471 :     } else {
1472 :     Trace("Generating synonym group data.") if T(2);
1473 : parrello 1.56 # Get the database handle.
1474 :     my $dbh = $fig->db_handle();
1475 :     # Ask for the synonyms.
1476 : parrello 1.59 my $sth = $dbh->prepare_command("SELECT maps_to, syn_id FROM peg_synonyms ORDER BY maps_to");
1477 : parrello 1.56 my $result = $sth->execute();
1478 :     if (! defined($result)) {
1479 :     Confess("Database error in Synonym load: " . $sth->errstr());
1480 :     } else {
1481 :     # Remember the current synonym.
1482 :     my $current_syn = "";
1483 :     # Count the features.
1484 :     my $featureCount = 0;
1485 :     # Loop through the synonym/peg pairs.
1486 :     while (my @row = $sth->fetchrow()) {
1487 :     # Get the synonym ID and feature ID.
1488 :     my ($syn_id, $peg) = @row;
1489 :     # Insure it's for one of our genomes.
1490 :     my $genomeID = FIG::genome_of($peg);
1491 :     if (exists $genomeHash->{$genomeID}) {
1492 :     # Verify the synonym.
1493 :     if ($syn_id ne $current_syn) {
1494 :     # It's new, so put it in the group table.
1495 :     $loadSynonymGroup->Put($syn_id);
1496 :     $current_syn = $syn_id;
1497 :     }
1498 :     # Connect the synonym to the peg.
1499 :     $loadIsSynonymGroupFor->Put($syn_id, $peg);
1500 :     # Count this feature.
1501 :     $featureCount++;
1502 :     if ($featureCount % 1000 == 0) {
1503 :     Trace("$featureCount features processed.") if T(3);
1504 :     }
1505 : parrello 1.43 }
1506 :     }
1507 :     }
1508 :     }
1509 :     # Finish the load.
1510 :     my $retVal = $self->_FinishAll();
1511 :     return $retVal;
1512 :     }
1513 :    
1514 : parrello 1.60 =head3 LoadFamilyData
1515 :    
1516 :     C<< my $stats = $spl->LoadFamilyData(); >>
1517 :    
1518 :     Load the protein families into Sprout.
1519 :    
1520 :     The following relations are loaded by this method.
1521 :    
1522 :     Family
1523 : parrello 1.63 IsFamilyForFeature
1524 : parrello 1.60
1525 :     The source information for these relations is taken from the C<families_for_protein>,
1526 :     C<family_function>, and C<sz_family> methods of the B<FIG> object.
1527 :    
1528 :     =over 4
1529 :    
1530 :     =item RETURNS
1531 :    
1532 :     Returns a statistics object for the loads.
1533 :    
1534 :     =back
1535 :    
1536 :     =cut
1537 :     #: Return Type $%;
1538 :     sub LoadFamilyData {
1539 :     # Get this object instance.
1540 :     my ($self) = @_;
1541 :     # Get the FIG object.
1542 :     my $fig = $self->{fig};
1543 :     # Get the genome hash.
1544 :     my $genomeHash = $self->{genomes};
1545 :     # Create load objects for the tables we're loading.
1546 :     my $loadFamily = $self->_TableLoader('Family');
1547 : parrello 1.63 my $loadIsFamilyForFeature = $self->_TableLoader('IsFamilyForFeature');
1548 : parrello 1.60 if ($self->{options}->{loadOnly}) {
1549 :     Trace("Loading from existing files.") if T(2);
1550 :     } else {
1551 :     Trace("Generating family data.") if T(2);
1552 :     # Create a hash for the family IDs.
1553 :     my %familyHash = ();
1554 :     # Loop through the genomes.
1555 :     for my $genomeID (sort keys %{$genomeHash}) {
1556 :     Trace("Processing features for $genomeID.") if T(2);
1557 :     # Loop through this genome's PEGs.
1558 :     for my $fid ($fig->all_features($genomeID, "peg")) {
1559 : parrello 1.63 $loadIsFamilyForFeature->Add("features", 1);
1560 : parrello 1.60 # Get this feature's families.
1561 :     my @families = $fig->families_for_protein($fid);
1562 :     # Loop through the families, connecting them to the feature.
1563 :     for my $family (@families) {
1564 : parrello 1.63 $loadIsFamilyForFeature->Put($family, $fid);
1565 : parrello 1.60 # If this is a new family, create a record for it.
1566 :     if (! exists $familyHash{$family}) {
1567 : parrello 1.62 $familyHash{$family} = 1;
1568 : parrello 1.60 $loadFamily->Add("families", 1);
1569 :     my $size = $fig->sz_family($family);
1570 :     my $func = $fig->family_function($family);
1571 : parrello 1.61 $loadFamily->Put($family, $size, $func);
1572 : parrello 1.60 }
1573 :     }
1574 :     }
1575 :     }
1576 :     }
1577 :     # Finish the load.
1578 :     my $retVal = $self->_FinishAll();
1579 :     return $retVal;
1580 :     }
1581 : parrello 1.43
1582 : parrello 1.69
1583 :    
1584 : parrello 1.1 =head2 Internal Utility Methods
1585 :    
1586 :     =head3 TableLoader
1587 :    
1588 :     Create an ERDBLoad object for the specified table. The object is also added to
1589 :     the internal list in the C<loaders> property of this object. That enables the
1590 :     L</FinishAll> method to terminate all the active loads.
1591 :    
1592 :     This is an instance method.
1593 :    
1594 :     =over 4
1595 :    
1596 :     =item tableName
1597 :    
1598 :     Name of the table (relation) being loaded.
1599 :    
1600 : parrello 1.25 =item ignore
1601 :    
1602 :     TRUE if the table should be ignored entirely, else FALSE.
1603 :    
1604 : parrello 1.1 =item RETURN
1605 :    
1606 :     Returns an ERDBLoad object for loading the specified table.
1607 :    
1608 :     =back
1609 :    
1610 :     =cut
1611 :    
1612 :     sub _TableLoader {
1613 :     # Get the parameters.
1614 : parrello 1.25 my ($self, $tableName, $ignore) = @_;
1615 : parrello 1.1 # Create the load object.
1616 : parrello 1.25 my $retVal = ERDBLoad->new($self->{erdb}, $tableName, $self->{loadDirectory}, $self->LoadOnly,
1617 :     $ignore);
1618 : parrello 1.1 # Cache it in the loader list.
1619 :     push @{$self->{loaders}}, $retVal;
1620 :     # Return it to the caller.
1621 :     return $retVal;
1622 :     }
1623 :    
1624 :     =head3 FinishAll
1625 :    
1626 :     Finish all the active loads on this object.
1627 :    
1628 :     When a load is started by L</TableLoader>, the controlling B<ERDBLoad> object is cached in
1629 :     the list pointed to be the C<loaders> property of this object. This method pops the loaders
1630 :     off the list and finishes them to flush out any accumulated residue.
1631 :    
1632 :     This is an instance method.
1633 :    
1634 :     =over 4
1635 :    
1636 :     =item RETURN
1637 :    
1638 :     Returns a statistics object containing the accumulated statistics for the load.
1639 :    
1640 :     =back
1641 :    
1642 :     =cut
1643 :    
1644 :     sub _FinishAll {
1645 :     # Get this object instance.
1646 :     my ($self) = @_;
1647 :     # Create the statistics object.
1648 :     my $retVal = Stats->new();
1649 :     # Get the loader list.
1650 :     my $loadList = $self->{loaders};
1651 : parrello 1.48 # Create a hash to hold the statistics objects, keyed on relation name.
1652 :     my %loaderHash = ();
1653 : parrello 1.1 # Loop through the list, finishing the loads. Note that if the finish fails, we die
1654 : parrello 1.48 # ignominiously. At some future point, we want to make the loads more restartable.
1655 : parrello 1.1 while (my $loader = pop @{$loadList}) {
1656 : parrello 1.26 # Get the relation name.
1657 : parrello 1.19 my $relName = $loader->RelName;
1658 : parrello 1.26 # Check the ignore flag.
1659 :     if ($loader->Ignore) {
1660 :     Trace("Relation $relName not loaded.") if T(2);
1661 :     } else {
1662 :     # Here we really need to finish.
1663 :     Trace("Finishing $relName.") if T(2);
1664 :     my $stats = $loader->Finish();
1665 : parrello 1.48 $loaderHash{$relName} = $stats;
1666 :     }
1667 :     }
1668 :     # Now we loop through again, actually loading the tables. We want to finish before
1669 :     # loading so that if something goes wrong at this point, all the load files are usable
1670 :     # and we don't have to redo all that work.
1671 :     for my $relName (sort keys %loaderHash) {
1672 :     # Get the statistics for this relation.
1673 :     my $stats = $loaderHash{$relName};
1674 :     # Check for a database load.
1675 :     if ($self->{options}->{dbLoad}) {
1676 :     # Here we want to use the load file just created to load the database.
1677 :     Trace("Loading relation $relName.") if T(2);
1678 :     my $newStats = $self->{sprout}->LoadUpdate(1, [$relName]);
1679 :     # Accumulate the statistics from the DB load.
1680 :     $stats->Accumulate($newStats);
1681 :     }
1682 :     $retVal->Accumulate($stats);
1683 :     Trace("Statistics for $relName:\n" . $stats->Show()) if T(2);
1684 : parrello 1.1 }
1685 :     # Return the load statistics.
1686 :     return $retVal;
1687 :     }
1688 :    
1689 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3