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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (view) (download) (as text)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3