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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3