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

Annotation of /Sprout/SproutLoad.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3