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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3