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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3