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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 package Sprout;
2 :    
3 : parrello 1.58 require Exporter;
4 :     use ERDB;
5 :     @ISA = qw(Exporter ERDB);
6 : parrello 1.15 use Data::Dumper;
7 :     use strict;
8 :     use Carp;
9 :     use DBKernel;
10 :     use XML::Simple;
11 :     use DBQuery;
12 :     use DBObject;
13 :     use Tracer;
14 :     use FIGRules;
15 : parrello 1.71 use FidCheck;
16 : parrello 1.15 use Stats;
17 : parrello 1.3 use POSIX qw(strftime);
18 : parrello 1.76 use BasicLocation;
19 : parrello 1.1
20 :     =head1 Sprout Database Manipulation Object
21 :    
22 :     =head2 Introduction
23 :    
24 :     This object enables the user to load and query the Sprout genome database using a few simple methods.
25 :     To construct the object, specify the name of the database. By default, the database is assumed to be a
26 :     MySQL database accessed via the user ID I<root> with no password and the database definition will
27 :     be in a file called F<SproutDBD.xml>. All of these defaults can be overridden
28 :     on the constructor. For example, the following invocation specifies a PostgreSQL database named I<GenDB>
29 :     whose definition and data files are in a co-directory named F<Data>.
30 :    
31 :     C<< my $sprout = Sprout->new('GenDB', { dbType => 'pg', dataDir => '../Data', xmlFileName => '../Data/SproutDBD.xml' }); >>
32 :    
33 :     Once you have a sprout object, you may use it to re-create the database, load the tables from
34 :     tab-delimited flat files and perform queries. Several special methods are provided for common
35 :     query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and
36 :     L</dna_seq> returns the DNA sequence for a specified genome location.
37 :    
38 : parrello 1.58 The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
39 :    
40 : parrello 1.1 =cut
41 :    
42 : parrello 1.3 #: Constructor SFXlate->new_sprout_only();
43 :    
44 : parrello 1.1 =head2 Public Methods
45 :    
46 :     =head3 new
47 :    
48 :     C<< my $sprout = Sprout->new($dbName, \%options); >>
49 :    
50 :     This is the constructor for a sprout object. It connects to the database and loads the
51 :     database definition into memory. The positional first parameter specifies the name of the
52 :     database.
53 :    
54 :     =over 4
55 :    
56 :     =item dbName
57 :    
58 :     Name of the database.
59 :    
60 :     =item options
61 :    
62 :     Table of options.
63 :    
64 :     * B<dbType> type of database (currently C<mysql> for MySQL and C<pg> for PostgreSQL) (default C<mysql>)
65 :    
66 :     * B<dataDir> directory containing the database definition file and the flat files used to load the data (default C<Data>)
67 :    
68 :     * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69 :    
70 : parrello 1.57 * B<userData> user name and password, delimited by a slash (default same as SEED)
71 : parrello 1.1
72 :     * B<port> connection port (default C<0>)
73 :    
74 : parrello 1.57 * B<sock> connection socket (default same as SEED)
75 :    
76 : parrello 1.1 * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77 :    
78 :     * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79 :    
80 : parrello 1.25 * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81 :    
82 : parrello 1.1 =back
83 :    
84 :     For example, the following constructor call specifies a database named I<Sprout> and a user name of
85 :     I<fig> with a password of I<admin>. The database load files are in the directory
86 :     F</usr/fig/SproutData>.
87 :    
88 :     C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >>
89 :    
90 :     =cut
91 :    
92 :     sub new {
93 : parrello 1.15 # Get the parameters.
94 :     my ($class, $dbName, $options) = @_;
95 :     # Compute the options. We do this by starting with a table of defaults and overwriting with
96 :     # the incoming data.
97 :     my $optionTable = Tracer::GetOptions({
98 : parrello 1.18 dbType => $FIG_Config::dbms,
99 :     # database type
100 :     dataDir => $FIG_Config::sproutData,
101 :     # data file directory
102 : parrello 1.54 xmlFileName => "$FIG_Config::fig/SproutDBD.xml",
103 : parrello 1.18 # database definition file name
104 :     userData => "$FIG_Config::dbuser/$FIG_Config::dbpass",
105 :     # user name and password
106 :     port => $FIG_Config::dbport,
107 :     # database connection port
108 : parrello 1.69 sock => $FIG_Config::dbsock,
109 : parrello 1.15 maxSegmentLength => 4500, # maximum feature segment length
110 :     maxSequenceLength => 8000, # maximum contig sequence length
111 : parrello 1.25 noDBOpen => 0, # 1 to suppress the database open
112 : parrello 1.15 }, $options);
113 :     # Get the data directory.
114 :     my $dataDir = $optionTable->{dataDir};
115 :     # Extract the user ID and password.
116 :     $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
117 :     my ($userName, $password) = ($1, $2);
118 :     # Connect to the database.
119 : parrello 1.25 my $dbh;
120 :     if (! $optionTable->{noDBOpen}) {
121 :     $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
122 : parrello 1.57 $password, $optionTable->{port}, undef, $optionTable->{sock});
123 : parrello 1.25 }
124 : parrello 1.15 # Create the ERDB object.
125 :     my $xmlFileName = "$optionTable->{xmlFileName}";
126 : parrello 1.58 my $retVal = ERDB::new($class, $dbh, $xmlFileName);
127 :     # Add the option table and XML file name.
128 :     $retVal->{_options} = $optionTable;
129 :     $retVal->{_xmlName} = $xmlFileName;
130 :     # Return it.
131 :     return $retVal;
132 : parrello 1.1 }
133 :    
134 :     =head3 MaxSegment
135 :    
136 :     C<< my $length = $sprout->MaxSegment(); >>
137 :    
138 :     This method returns the maximum permissible length of a feature segment. The length is important
139 :     because it enables us to make reasonable guesses at how to find features inside a particular
140 :     contig region. For example, if the maximum length is 4000 and we're looking for a feature that
141 :     overlaps the region from 6000 to 7000 we know that the starting position must be between 2001
142 :     and 10999.
143 :    
144 :     =cut
145 : parrello 1.3 #: Return Type $;
146 : parrello 1.1 sub MaxSegment {
147 : parrello 1.15 my ($self) = @_;
148 :     return $self->{_options}->{maxSegmentLength};
149 : parrello 1.1 }
150 :    
151 :     =head3 MaxSequence
152 :    
153 :     C<< my $length = $sprout->MaxSequence(); >>
154 :    
155 :     This method returns the maximum permissible length of a contig sequence. A contig is broken
156 :     into sequences in order to save memory resources. In particular, when manipulating features,
157 :     we generally only need a few sequences in memory rather than the entire contig.
158 :    
159 :     =cut
160 : parrello 1.3 #: Return Type $;
161 : parrello 1.1 sub MaxSequence {
162 : parrello 1.15 my ($self) = @_;
163 :     return $self->{_options}->{maxSequenceLength};
164 : parrello 1.1 }
165 :    
166 :     =head3 Load
167 :    
168 :     C<< $sprout->Load($rebuild); >>;
169 :    
170 :     Load the database from files in the data directory, optionally re-creating the tables.
171 :    
172 :     This method always deletes the data from the database before loading, even if the tables are not
173 :     re-created. The data is loaded into the relations from files in the data directory either having the
174 :     same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
175 :     extension are used in preference to the files with an extension.
176 :    
177 :     The files are loaded based on the presumption that each line of the file is a record in the
178 :     relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
179 :     fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
180 :     be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
181 :    
182 :     =over 4
183 :    
184 :     =item rebuild
185 :    
186 :     TRUE if the data tables need to be created or re-created, else FALSE
187 :    
188 :     =item RETURN
189 :    
190 :     Returns a statistical object containing the number of records read, the number of duplicates found,
191 :     the number of errors, and a list of the error messages.
192 :    
193 :     =back
194 :    
195 :     =cut
196 : parrello 1.3 #: Return Type %;
197 : parrello 1.1 sub Load {
198 : parrello 1.15 # Get the parameters.
199 :     my ($self, $rebuild) = @_;
200 :     # Load the tables from the data directory.
201 : parrello 1.58 my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
202 : parrello 1.15 # Return the statistics.
203 :     return $retVal;
204 : parrello 1.1 }
205 :    
206 :     =head3 LoadUpdate
207 :    
208 : parrello 1.44 C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
209 : parrello 1.1
210 :     Load updates to one or more database tables. This method enables the client to make changes to one
211 :     or two tables without reloading the whole database. For each table, there must be a corresponding
212 :     file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
213 :     for example, to make updates to the B<FeatureTranslation> relation, there must be a
214 :     C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
215 :     are not examined. This allows update files to co-exist with files from an original load.
216 :    
217 :     =over 4
218 :    
219 :     =item truncateFlag
220 :    
221 :     TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
222 :     current data and schema of the tables to be replaced, while a value of FALSE means the new data
223 :     is added to the existing data in the various relations.
224 :    
225 :     =item tableList
226 :    
227 :     List of the tables to be updated.
228 :    
229 :     =item RETURN
230 :    
231 :     Returns a statistical object containing the number of records read, the number of duplicates found,
232 :     the number of errors encountered, and a list of error messages.
233 :    
234 :     =back
235 :    
236 :     =cut
237 : parrello 1.7 #: Return Type $%;
238 : parrello 1.1 sub LoadUpdate {
239 : parrello 1.15 # Get the parameters.
240 :     my ($self, $truncateFlag, $tableList) = @_;
241 :     # Declare the return value.
242 :     my $retVal = Stats->new();
243 :     # Get the data directory.
244 :     my $optionTable = $self->{_options};
245 :     my $dataDir = $optionTable->{dataDir};
246 :     # Loop through the incoming table names.
247 :     for my $tableName (@{$tableList}) {
248 :     # Find the table's file.
249 : parrello 1.18 my $fileName = LoadFileName($dataDir, $tableName);
250 :     if (! $fileName) {
251 :     Trace("No load file found for $tableName in $dataDir.") if T(0);
252 :     } else {
253 :     # Attempt to load this table.
254 : parrello 1.58 my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
255 : parrello 1.18 # Accumulate the resulting statistics.
256 :     $retVal->Accumulate($result);
257 : parrello 1.15 }
258 :     }
259 :     # Return the statistics.
260 :     return $retVal;
261 : parrello 1.1 }
262 :    
263 : parrello 1.58 =head3 GenomeCounts
264 :    
265 :     C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
266 :    
267 :     Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
268 :     genomes will be included in the counts.
269 :    
270 :     =over 4
271 :    
272 :     =item complete
273 :    
274 :     TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
275 :     counted
276 :    
277 :     =item RETURN
278 :    
279 :     A six-element list containing the number of genomes in each of six categories--
280 :     Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
281 :    
282 :     =back
283 :    
284 :     =cut
285 :    
286 :     sub GenomeCounts {
287 :     # Get the parameters.
288 :     my ($self, $complete) = @_;
289 :     # Set the filter based on the completeness flag.
290 :     my $filter = ($complete ? "Genome(complete) = 1" : "");
291 :     # Get all the genomes and the related taxonomy information.
292 :     my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
293 :     # Clear the counters.
294 :     my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
295 :     # Loop through, counting the domains.
296 :     for my $genome (@genomes) {
297 :     if ($genome->[1] =~ /^archaea/i) { ++$arch }
298 :     elsif ($genome->[1] =~ /^bacter/i) { ++$bact }
299 :     elsif ($genome->[1] =~ /^eukar/i) { ++$euk }
300 :     elsif ($genome->[1] =~ /^vir/i) { ++$vir }
301 :     elsif ($genome->[1] =~ /^env/i) { ++$env }
302 :     else { ++$unk }
303 :     }
304 :     # Return the counts.
305 :     return ($arch, $bact, $euk, $vir, $env, $unk);
306 :     }
307 :    
308 : parrello 1.59 =head3 ContigCount
309 :    
310 :     C<< my $count = $sprout->ContigCount($genomeID); >>
311 :    
312 :     Return the number of contigs for the specified genome ID.
313 :    
314 :     =over 4
315 :    
316 :     =item genomeID
317 :    
318 :     ID of the genome whose contig count is desired.
319 :    
320 :     =item RETURN
321 :    
322 :     Returns the number of contigs for the specified genome.
323 :    
324 :     =back
325 :    
326 :     =cut
327 :    
328 :     sub ContigCount {
329 :     # Get the parameters.
330 :     my ($self, $genomeID) = @_;
331 :     # Get the contig count.
332 :     my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
333 :     # Return the result.
334 :     return $retVal;
335 :     }
336 :    
337 :     =head3 GeneMenu
338 :    
339 :     C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params); >>
340 :    
341 :     Return an HTML select menu of genomes. Each genome will be an option in the menu,
342 :     and will be displayed by name with the ID and a contig count attached. The selection
343 :     value will be the genome ID. The genomes will be sorted by genus/species name.
344 :    
345 :     =over 4
346 :    
347 :     =item attributes
348 :    
349 :     Reference to a hash mapping attributes to values for the SELECT tag generated.
350 :    
351 :     =item filterString
352 :    
353 :     A filter string for use in selecting the genomes. The filter string must conform
354 :     to the rules for the C<< ERDB->Get >> method.
355 :    
356 :     =item params
357 :    
358 :     Reference to a list of values to be substituted in for the parameter marks in
359 :     the filter string.
360 :    
361 :     =item RETURN
362 :    
363 :     Returns an HTML select menu with the specified genomes as selectable options.
364 :    
365 :     =back
366 :    
367 :     =cut
368 :    
369 :     sub GeneMenu {
370 :     # Get the parameters.
371 :     my ($self, $attributes, $filterString, $params) = @_;
372 :     # Start the menu.
373 :     my $retVal = "<select " .
374 :     join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
375 :     ">\n";
376 :     # Get the genomes.
377 :     my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
378 :     'Genome(genus)',
379 : parrello 1.69 'Genome(species)',
380 :     'Genome(unique-characterization)']);
381 : parrello 1.59 # Sort them by name.
382 :     my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
383 :     # Loop through the genomes, creating the option tags.
384 :     for my $genomeData (@sorted) {
385 :     # Get the data for this genome.
386 : parrello 1.69 my ($genomeID, $genus, $species, $strain) = @{$genomeData};
387 : parrello 1.59 # Get the contig count.
388 :     my $count = $self->ContigCount($genomeID);
389 :     my $counting = ($count == 1 ? "contig" : "contigs");
390 :     # Build the option tag.
391 : parrello 1.69 $retVal .= "<option value=\"$genomeID\">$genus $species $strain ($genomeID) [$count $counting]</option>\n";
392 : parrello 1.70 Trace("Option tag built for $genomeID: $genus $species $strain.") if T(3);
393 : parrello 1.59 }
394 :     # Close the SELECT tag.
395 :     $retVal .= "</select>\n";
396 :     # Return the result.
397 :     return $retVal;
398 :     }
399 : parrello 1.1 =head3 Build
400 :    
401 :     C<< $sprout->Build(); >>
402 :    
403 :     Build the database. The database will be cleared and the tables re-created from the metadata.
404 :     This method is useful when a database is brand new or when the database definition has
405 :     changed.
406 :    
407 :     =cut
408 : parrello 1.3 #: Return Type ;
409 : parrello 1.1 sub Build {
410 : parrello 1.15 # Get the parameters.
411 :     my ($self) = @_;
412 :     # Create the tables.
413 : parrello 1.58 $self->CreateTables();
414 : parrello 1.1 }
415 :    
416 :     =head3 Genomes
417 :    
418 :     C<< my @genomes = $sprout->Genomes(); >>
419 :    
420 :     Return a list of all the genome IDs.
421 :    
422 :     =cut
423 : parrello 1.3 #: Return Type @;
424 : parrello 1.1 sub Genomes {
425 : parrello 1.15 # Get the parameters.
426 :     my ($self) = @_;
427 :     # Get all the genomes.
428 :     my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
429 :     # Return the list of IDs.
430 :     return @retVal;
431 : parrello 1.1 }
432 :    
433 :     =head3 GenusSpecies
434 :    
435 :     C<< my $infoString = $sprout->GenusSpecies($genomeID); >>
436 :    
437 :     Return the genus, species, and unique characterization for a genome.
438 :    
439 :     =over 4
440 :    
441 :     =item genomeID
442 :    
443 :     ID of the genome whose genus and species is desired
444 :    
445 :     =item RETURN
446 :    
447 :     Returns the genus and species of the genome, with the unique characterization (if any). If the genome
448 :     does not exist, returns an undefined value.
449 :    
450 :     =back
451 :    
452 :     =cut
453 : parrello 1.3 #: Return Type $;
454 : parrello 1.1 sub GenusSpecies {
455 : parrello 1.15 # Get the parameters.
456 :     my ($self, $genomeID) = @_;
457 :     # Get the data for the specified genome.
458 :     my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',
459 :     'Genome(unique-characterization)']);
460 :     # Format the result and return it.
461 :     my $retVal = join(' ', @values);
462 :     return $retVal;
463 : parrello 1.1 }
464 :    
465 :     =head3 FeaturesOf
466 :    
467 :     C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>
468 :    
469 :     Return a list of the features relevant to a specified genome.
470 :    
471 :     =over 4
472 :    
473 :     =item genomeID
474 :    
475 :     Genome whose features are desired.
476 :    
477 :     =item ftype
478 :    
479 :     Type of feature desired. If omitted, all features will be returned.
480 :    
481 :     =item RETURN
482 :    
483 :     Returns a list of the feature IDs for features relevant to the genome. If the genome does not exist,
484 :     will return an empty list.
485 :    
486 :     =back
487 :    
488 :     =cut
489 : parrello 1.3 #: Return Type @;
490 : parrello 1.1 sub FeaturesOf {
491 : parrello 1.15 # Get the parameters.
492 :     my ($self, $genomeID,$ftype) = @_;
493 :     # Get the features we want.
494 :     my @features;
495 :     if (!$ftype) {
496 :     @features = $self->GetFlat(['HasContig', 'IsLocatedIn'], "HasContig(from-link) = ?",
497 :     [$genomeID], 'IsLocatedIn(from-link)');
498 :     } else {
499 :     @features = $self->GetFlat(['HasContig', 'IsLocatedIn', 'Feature'],
500 :     "HasContig(from-link) = ? AND Feature(feature-type) = ?",
501 :     [$genomeID, $ftype], 'IsLocatedIn(from-link)');
502 :     }
503 :     # Return the list with duplicates merged out. We need to merge out duplicates because
504 :     # a feature will appear twice if it spans more than one contig.
505 :     my @retVal = Tracer::Merge(@features);
506 :     # Return the list of feature IDs.
507 :     return @retVal;
508 : parrello 1.1 }
509 :    
510 :     =head3 FeatureLocation
511 :    
512 :     C<< my @locations = $sprout->FeatureLocation($featureID); >>
513 :    
514 :     Return the location of a feature in its genome's contig segments. In a list context, this method
515 :     will return a list of the locations. In a scalar context, it will return the locations as a space-
516 :     delimited string. Each location will be of the form I<contigID>C<_>I<begin>I<dir>I<len> where
517 :     I<begin> is the starting position, I<dir> is C<+> for a forward transcription or C<-> for a backward
518 :     transcription, and I<len> is the length. So, for example, C<1999.1_NC123_4000+200> describes a location
519 :     beginning at position 4000 of contig C<1999.1_NC123> and ending at position 4199. Similarly,
520 :     C<1999.1_NC123_2000-400> describes a location in the same contig starting at position 2000 and ending
521 :     at position 1601.
522 :    
523 :     This process is complicated by the fact that we automatically split up feature segments longer than
524 :     the maximum segment length. When we find two segments that are adjacent to each other, we must
525 :     put them back together.
526 :    
527 :     =over 4
528 :    
529 :     =item featureID
530 :    
531 :     FIG ID of the desired feature
532 :    
533 :     =item RETURN
534 :    
535 :     Returns a list of the feature's contig segments. The locations are returned as a list in a list
536 : parrello 1.20 context and as a comma-delimited string in a scalar context.
537 : parrello 1.1
538 :     =back
539 :    
540 :     =cut
541 : parrello 1.3 #: Return Type @;
542 :     #: Return Type $;
543 : parrello 1.1 sub FeatureLocation {
544 : parrello 1.15 # Get the parameters.
545 :     my ($self, $featureID) = @_;
546 :     # Create a query for the feature locations.
547 :     my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",
548 :     [$featureID]);
549 :     # Create the return list.
550 :     my @retVal = ();
551 :     # Set up the variables used to determine if we have adjacent segments. This initial setup will
552 :     # not match anything.
553 :     my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);
554 :     # Loop through the query results, creating location specifiers.
555 :     while (my $location = $query->Fetch()) {
556 :     # Get the location parameters.
557 :     my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',
558 :     'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);
559 :     # Check to see if we are adjacent to the previous segment.
560 :     if ($prevContig eq $contigID && $dir eq $prevDir) {
561 :     # Here the new segment is in the same direction on the same contig. Insure the
562 :     # new segment's beginning is next to the old segment's end.
563 : parrello 1.41 if ($dir eq "-" && $beg + $len == $prevBeg) {
564 :     # Here we're merging two backward blocks, so we keep the new begin point
565 :     # and adjust the length.
566 :     $len += $prevLen;
567 :     # Pop the old segment off. The new one will replace it later.
568 :     pop @retVal;
569 :     } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
570 :     # Here we need to merge two forward blocks. Adjust the beginning and
571 :     # length values to include both segments.
572 : parrello 1.15 $beg = $prevBeg;
573 :     $len += $prevLen;
574 :     # Pop the old segment off. The new one will replace it later.
575 :     pop @retVal;
576 :     }
577 :     }
578 :     # Remember this specifier for the adjacent-segment test the next time through.
579 :     ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
580 : parrello 1.32 # Compute the initial base pair.
581 :     my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
582 : parrello 1.15 # Add the specifier to the list.
583 : parrello 1.33 push @retVal, "${contigID}_$start$dir$len";
584 : parrello 1.15 }
585 :     # Return the list in the format indicated by the context.
586 : parrello 1.20 return (wantarray ? @retVal : join(',', @retVal));
587 : parrello 1.1 }
588 :    
589 :     =head3 ParseLocation
590 :    
591 :     C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>
592 :    
593 :     Split a location specifier into the contig ID, the starting point, the direction, and the
594 :     length.
595 :    
596 :     =over 4
597 :    
598 :     =item location
599 :    
600 :     A location specifier (see L</FeatureLocation> for a description).
601 :    
602 :     =item RETURN
603 :    
604 :     Returns a list containing the contig ID, the start position, the direction (C<+> or C<->),
605 :     and the length indicated by the incoming location specifier.
606 :    
607 :     =back
608 :    
609 :     =cut
610 : parrello 1.3 #: Return Type @;
611 : parrello 1.1 sub ParseLocation {
612 : parrello 1.15 # Get the parameter. Note that if we're called as an instance method, we ignore
613 : parrello 1.10 # the first parameter.
614 :     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
615 : parrello 1.15 my ($location) = @_;
616 :     # Parse it into segments.
617 : parrello 1.40 $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
618 : parrello 1.15 my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
619 :     # If the direction is an underscore, convert it to a + or -.
620 :     if ($dir eq "_") {
621 :     if ($start < $len) {
622 :     $dir = "+";
623 :     $len = $len - $start + 1;
624 :     } else {
625 :     $dir = "-";
626 :     $len = $start - $len + 1;
627 :     }
628 :     }
629 :     # Return the result.
630 :     return ($contigID, $start, $dir, $len);
631 : parrello 1.1 }
632 :    
633 : parrello 1.10 =head3 PointLocation
634 :    
635 :     C<< my $found = Sprout::PointLocation($location, $point); >>
636 :    
637 :     Return the offset into the specified location of the specified point on the contig. If
638 :     the specified point is before the location, a negative value will be returned. If it is
639 : parrello 1.19 beyond the location, an undefined value will be returned. It is assumed that the offset
640 :     is for the location's contig. The location can either be new-style (using a C<+> or C<->
641 : parrello 1.10 and a length) or old-style (using C<_> and start and end positions.
642 :    
643 :     =over 4
644 :    
645 :     =item location
646 :    
647 :     A location specifier (see L</FeatureLocation> for a description).
648 :    
649 :     =item point
650 :    
651 :     The offset into the contig of the point in which we're interested.
652 :    
653 :     =item RETURN
654 :    
655 :     Returns the offset inside the specified location of the specified point, a negative
656 :     number if the point is before the location, or an undefined value if the point is past
657 :     the location. If the length of the location is 0, this method will B<always> denote
658 :     that it is outside the location. The offset will always be relative to the left-most
659 :     position in the location.
660 :    
661 :     =back
662 :    
663 :     =cut
664 :     #: Return Type $;
665 :     sub PointLocation {
666 : parrello 1.15 # Get the parameter. Note that if we're called as an instance method, we ignore
667 : parrello 1.10 # the first parameter.
668 :     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
669 : parrello 1.15 my ($location, $point) = @_;
670 : parrello 1.10 # Parse out the location elements. Note that this works on both old-style and new-style
671 :     # locations.
672 :     my ($contigID, $start, $dir, $len) = ParseLocation($location);
673 :     # Declare the return variable.
674 :     my $retVal;
675 :     # Compute the offset. The computation is dependent on the direction of the location.
676 :     my $offset = (($dir == '+') ? $point - $start : $point - ($start - $len + 1));
677 :     # Return the offset if it's valid.
678 :     if ($offset < $len) {
679 :     $retVal = $offset;
680 :     }
681 :     # Return the offset found.
682 :     return $retVal;
683 :     }
684 :    
685 : parrello 1.1 =head3 DNASeq
686 :    
687 :     C<< my $sequence = $sprout->DNASeq(\@locationList); >>
688 :    
689 :     This method returns the DNA sequence represented by a list of locations. The list of locations
690 : parrello 1.2 should be of the form returned by L</featureLocation> when in a list context. In other words,
691 : parrello 1.1 each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
692 :    
693 : parrello 1.55 For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
694 :     between positions 1401 and 1532, inclusive.
695 :    
696 :     my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
697 :    
698 : parrello 1.1 =over 4
699 :    
700 :     =item locationList
701 :    
702 : parrello 1.55 List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
703 :     I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
704 : parrello 1.1
705 :     =item RETURN
706 :    
707 :     Returns a string of nucleotides corresponding to the DNA segments in the location list.
708 :    
709 :     =back
710 :    
711 :     =cut
712 : parrello 1.3 #: Return Type $;
713 : parrello 1.1 sub DNASeq {
714 : parrello 1.15 # Get the parameters.
715 :     my ($self, $locationList) = @_;
716 :     # Create the return string.
717 :     my $retVal = "";
718 :     # Loop through the locations.
719 :     for my $location (@{$locationList}) {
720 :     # Set up a variable to contain the DNA at this location.
721 :     my $locationDNA = "";
722 :     # Parse out the contig ID, the beginning point, the direction, and the end point.
723 :     my ($contigID, $beg, $dir, $len) = ParseLocation($location);
724 :     # Now we must create a query to return all the sequences in the contig relevant to the region
725 :     # specified. First, we compute the start and stop points when reading through the sequences.
726 :     # For a forward transcription, the start point is the beginning; for a backward transcription,
727 :     # the start point is the ending. Note that in the latter case we must reverse the DNA string
728 :     # before putting it in the return value.
729 :     my ($start, $stop);
730 : parrello 1.34 Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
731 : parrello 1.15 if ($dir eq "+") {
732 :     $start = $beg;
733 : parrello 1.38 $stop = $beg + $len - 1;
734 : parrello 1.15 } else {
735 : parrello 1.38 $start = $beg - $len + 1;
736 :     $stop = $beg;
737 : parrello 1.15 }
738 : parrello 1.38 Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
739 : parrello 1.15 my $query = $self->Get(['IsMadeUpOf','Sequence'],
740 :     "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
741 : parrello 1.38 " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
742 : parrello 1.15 [$contigID, $start, $stop]);
743 :     # Loop through the sequences.
744 :     while (my $sequence = $query->Fetch()) {
745 :     # Determine whether the location starts, stops, or continues through this sequence.
746 :     my ($startPosition, $sequenceData, $sequenceLength) =
747 :     $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
748 :     'IsMadeUpOf(len)']);
749 :     my $stopPosition = $startPosition + $sequenceLength;
750 : parrello 1.29 Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
751 : parrello 1.15 # Figure out the start point and length of the relevant section.
752 :     my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
753 : parrello 1.39 my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
754 : parrello 1.29 Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
755 : parrello 1.15 # Add the relevant data to the location data.
756 : parrello 1.29 $locationDNA .= substr($sequenceData, $pos1, $len1);
757 : parrello 1.15 }
758 :     # Add this location's data to the return string. Note that we may need to reverse it.
759 :     if ($dir eq '+') {
760 :     $retVal .= $locationDNA;
761 :     } else {
762 : parrello 1.27 $retVal .= FIG::reverse_comp($locationDNA);
763 : parrello 1.15 }
764 :     }
765 :     # Return the result.
766 :     return $retVal;
767 : parrello 1.1 }
768 :    
769 :     =head3 AllContigs
770 :    
771 :     C<< my @idList = $sprout->AllContigs($genomeID); >>
772 :    
773 :     Return a list of all the contigs for a genome.
774 :    
775 :     =over 4
776 :    
777 :     =item genomeID
778 :    
779 :     Genome whose contigs are desired.
780 :    
781 :     =item RETURN
782 :    
783 :     Returns a list of the IDs for the genome's contigs.
784 :    
785 :     =back
786 :    
787 :     =cut
788 : parrello 1.3 #: Return Type @;
789 : parrello 1.1 sub AllContigs {
790 : parrello 1.15 # Get the parameters.
791 :     my ($self, $genomeID) = @_;
792 :     # Ask for the genome's Contigs.
793 :     my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
794 :     'HasContig(to-link)');
795 :     # Return the list of Contigs.
796 :     return @retVal;
797 : parrello 1.1 }
798 :    
799 : parrello 1.62 =head3 GenomeLength
800 :    
801 :     C<< my $length = $sprout->GenomeLength($genomeID); >>
802 :    
803 :     Return the length of the specified genome in base pairs.
804 :    
805 :     =over 4
806 :    
807 :     =item genomeID
808 :    
809 :     ID of the genome whose base pair count is desired.
810 :    
811 :     =item RETURN
812 :    
813 :     Returns the number of base pairs in all the contigs of the specified
814 :     genome.
815 :    
816 :     =back
817 :    
818 :     =cut
819 :    
820 :     sub GenomeLength {
821 :     # Get the parameters.
822 :     my ($self, $genomeID) = @_;
823 :     # Declare the return variable.
824 :     my $retVal = 0;
825 :     # Get the genome's contig sequence lengths.
826 : parrello 1.63 my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
827 : parrello 1.62 [$genomeID], 'IsMadeUpOf(len)');
828 :     # Sum the lengths.
829 :     map { $retVal += $_ } @lens;
830 :     # Return the result.
831 :     return $retVal;
832 :     }
833 :    
834 :     =head3 FeatureCount
835 :    
836 :     C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
837 :    
838 :     Return the number of features of the specified type in the specified genome.
839 :    
840 :     =over 4
841 :    
842 : parrello 1.68 =item genomeID
843 : parrello 1.62
844 :     ID of the genome whose feature count is desired.
845 :    
846 :     =item type
847 :    
848 :     Type of feature to count (eg. C<peg>, C<rna>, etc.).
849 :    
850 :     =item RETURN
851 :    
852 :     Returns the number of features of the specified type for the specified genome.
853 :    
854 :     =back
855 :    
856 :     =cut
857 :    
858 :     sub FeatureCount {
859 :     # Get the parameters.
860 :     my ($self, $genomeID, $type) = @_;
861 :     # Compute the count.
862 :     my $retVal = $self->GetCount(['HasFeature', 'Feature'],
863 : parrello 1.64 "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
864 : parrello 1.62 [$genomeID, $type]);
865 :     # Return the result.
866 :     return $retVal;
867 :     }
868 :    
869 :     =head3 GenomeAssignments
870 :    
871 :     C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
872 :    
873 :     Return a list of a genome's assigned features. The return hash will contain each
874 :     assigned feature of the genome mapped to the text of its most recent functional
875 :     assignment.
876 :    
877 :     =over 4
878 :    
879 :     =item genomeID
880 :    
881 :     ID of the genome whose functional assignments are desired.
882 :    
883 :     =item RETURN
884 :    
885 :     Returns a reference to a hash which maps each feature to its most recent
886 :     functional assignment.
887 :    
888 :     =back
889 :    
890 :     =cut
891 :    
892 :     sub GenomeAssignments {
893 :     # Get the parameters.
894 :     my ($self, $genomeID) = @_;
895 :     # Declare the return variable.
896 :     my $retVal = {};
897 :     # Query the genome's features and annotations. We'll put the oldest annotations
898 :     # first so that the last assignment to go into the hash will be the correct one.
899 :     my $query = $self->Get(['HasFeature', 'IsTargetOfAnnotation', 'Annotation'],
900 :     "HasFeature(from-link) = ? ORDER BY Annotation(time)",
901 :     [$genomeID]);
902 :     # Loop through the annotations.
903 :     while (my $data = $query->Fetch) {
904 :     # Get the feature ID and annotation text.
905 : parrello 1.67 my ($fid, $annotation) = $data->Values(['HasFeature(to-link)',
906 : parrello 1.65 'Annotation(annotation)']);
907 : parrello 1.62 # Check to see if this is an assignment. Note that the user really
908 :     # doesn't matter to us, other than we use it to determine whether or
909 :     # not this is an assignment.
910 : parrello 1.66 my ($user, $assignment) = _ParseAssignment('fig', $annotation);
911 : parrello 1.62 if ($user) {
912 :     # Here it's an assignment. We put it in the return hash, overwriting
913 :     # any older assignment that might be present.
914 :     $retVal->{$fid} = $assignment;
915 :     }
916 :     }
917 :     # Return the result.
918 :     return $retVal;
919 :     }
920 :    
921 : parrello 1.1 =head3 ContigLength
922 :    
923 :     C<< my $length = $sprout->ContigLength($contigID); >>
924 :    
925 :     Compute the length of a contig.
926 :    
927 :     =over 4
928 :    
929 :     =item contigID
930 :    
931 :     ID of the contig whose length is desired.
932 :    
933 : parrello 1.3 =item RETURN
934 :    
935 :     Returns the number of positions in the contig.
936 :    
937 : parrello 1.1 =back
938 :    
939 :     =cut
940 : parrello 1.3 #: Return Type $;
941 : parrello 1.1 sub ContigLength {
942 : parrello 1.15 # Get the parameters.
943 :     my ($self, $contigID) = @_;
944 :     # Get the contig's last sequence.
945 :     my $query = $self->Get(['IsMadeUpOf'],
946 :     "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
947 :     [$contigID]);
948 :     my $sequence = $query->Fetch();
949 :     # Declare the return value.
950 :     my $retVal = 0;
951 :     # Set it from the sequence data, if any.
952 :     if ($sequence) {
953 :     my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
954 : parrello 1.40 $retVal = $start + $len - 1;
955 :     }
956 :     # Return the result.
957 :     return $retVal;
958 :     }
959 :    
960 :     =head3 ClusterPEGs
961 :    
962 :     C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
963 :    
964 :     Cluster the PEGs in a list according to the cluster coding scheme of the specified
965 :     subsystem. In order for this to work properly, the subsystem object must have
966 :     been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
967 :     This causes the cluster numbers to be pulled into the subsystem's color hash.
968 :     If a PEG is not found in the color hash, it will not appear in the output
969 :     sequence.
970 :    
971 :     =over 4
972 :    
973 :     =item sub
974 :    
975 :     Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
976 :     method.
977 :    
978 :     =item pegs
979 :    
980 :     Reference to the list of PEGs to be clustered.
981 :    
982 :     =item RETURN
983 :    
984 :     Returns a list of the PEGs, grouped into smaller lists by cluster number.
985 :    
986 :     =back
987 :    
988 :     =cut
989 :     #: Return Type $@@;
990 :     sub ClusterPEGs {
991 :     # Get the parameters.
992 :     my ($self, $sub, $pegs) = @_;
993 :     # Declare the return variable.
994 :     my $retVal = [];
995 :     # Loop through the PEGs, creating arrays for each cluster.
996 :     for my $pegID (@{$pegs}) {
997 :     my $clusterNumber = $sub->get_cluster_number($pegID);
998 :     # Only proceed if the PEG is in a cluster.
999 :     if ($clusterNumber >= 0) {
1000 :     # Push this PEG onto the sub-list for the specified cluster number.
1001 :     push @{$retVal->[$clusterNumber]}, $pegID;
1002 :     }
1003 : parrello 1.15 }
1004 :     # Return the result.
1005 :     return $retVal;
1006 : parrello 1.1 }
1007 :    
1008 :     =head3 GenesInRegion
1009 :    
1010 :     C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1011 :    
1012 :     List the features which overlap a specified region in a contig.
1013 :    
1014 :     =over 4
1015 :    
1016 :     =item contigID
1017 :    
1018 :     ID of the contig containing the region of interest.
1019 :    
1020 :     =item start
1021 :    
1022 :     Offset of the first residue in the region of interest.
1023 :    
1024 :     =item stop
1025 :    
1026 :     Offset of the last residue in the region of interest.
1027 :    
1028 :     =item RETURN
1029 :    
1030 :     Returns a three-element list. The first element is a list of feature IDs for the features that
1031 :     overlap the region of interest. The second and third elements are the minimum and maximum
1032 :     locations of the features provided on the specified contig. These may extend outside
1033 : parrello 1.9 the start and stop values. The first element (that is, the list of features) is sorted
1034 :     roughly by location.
1035 : parrello 1.1
1036 :     =back
1037 :    
1038 :     =cut
1039 : parrello 1.9 #: Return Type @@;
1040 : parrello 1.1 sub GenesInRegion {
1041 : parrello 1.15 # Get the parameters.
1042 :     my ($self, $contigID, $start, $stop) = @_;
1043 :     # Get the maximum segment length.
1044 :     my $maximumSegmentLength = $self->MaxSegment;
1045 :     # Create a hash to receive the feature list. We use a hash so that we can eliminate
1046 :     # duplicates easily. The hash key will be the feature ID. The value will be a two-element
1047 : parrello 1.19 # containing the minimum and maximum offsets. We will use the offsets to sort the results
1048 : parrello 1.15 # when we're building the result set.
1049 :     my %featuresFound = ();
1050 :     # Prime the values we'll use for the returned beginning and end.
1051 :     my @initialMinMax = ($self->ContigLength($contigID), 0);
1052 :     my ($min, $max) = @initialMinMax;
1053 :     # Create a table of parameters for each query. Each query looks for features travelling in
1054 :     # a particular direction. The query parameters include the contig ID, the feature direction,
1055 :     # the lowest possible start position, and the highest possible start position. This works
1056 :     # because each feature segment length must be no greater than the maximum segment length.
1057 :     my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop],
1058 :     reverse => [$contigID, '-', $start, $stop + $maximumSegmentLength - 1]);
1059 :     # Loop through the query parameters.
1060 :     for my $parms (values %queryParms) {
1061 :     # Create the query.
1062 :     my $query = $self->Get(['IsLocatedIn'],
1063 :     "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
1064 :     $parms);
1065 :     # Loop through the feature segments found.
1066 :     while (my $segment = $query->Fetch) {
1067 :     # Get the data about this segment.
1068 :     my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',
1069 :     'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);
1070 :     # Determine if this feature actually overlaps the region. The query insures that
1071 :     # this will be the case if the segment is the maximum length, so to fine-tune
1072 :     # the results we insure that the inequality from the query holds using the actual
1073 :     # length.
1074 :     my ($found, $end) = (0, 0);
1075 :     if ($dir eq '+') {
1076 :     $end = $beg + $len;
1077 :     if ($end >= $start) {
1078 :     # Denote we found a useful feature.
1079 :     $found = 1;
1080 :     }
1081 :     } elsif ($dir eq '-') {
1082 :     # Note we switch things around so that the beginning is to the left of the
1083 :     # ending.
1084 :     ($beg, $end) = ($beg - $len, $beg);
1085 :     if ($beg <= $stop) {
1086 :     # Denote we found a useful feature.
1087 :     $found = 1;
1088 :     }
1089 :     }
1090 :     if ($found) {
1091 :     # Here we need to record the feature and update the minima and maxima. First,
1092 :     # get the current entry for the specified feature.
1093 :     my ($loc1, $loc2) = (exists $featuresFound{$featureID} ? @{$featuresFound{$featureID}} :
1094 :     @initialMinMax);
1095 :     # Merge the current segment's begin and end into the feature begin and end and the
1096 :     # global min and max.
1097 :     if ($beg < $loc1) {
1098 :     $loc1 = $beg;
1099 :     $min = $beg if $beg < $min;
1100 :     }
1101 :     if ($end > $loc2) {
1102 :     $loc2 = $end;
1103 :     $max = $end if $end > $max;
1104 :     }
1105 :     # Store the entry back into the hash table.
1106 :     $featuresFound{$featureID} = [$loc1, $loc2];
1107 :     }
1108 :     }
1109 :     }
1110 :     # Now we must compute the list of the IDs for the features found. We start with a list
1111 :     # of midpoints / feature ID pairs. (It's not really a midpoint, it's twice the midpoint,
1112 :     # but the result of the sort will be the same.)
1113 :     my @list = map { [$featuresFound{$_}->[0] + $featuresFound{$_}->[1], $_] } keys %featuresFound;
1114 :     # Now we sort by midpoint and yank out the feature IDs.
1115 :     my @retVal = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @list;
1116 :     # Return it along with the min and max.
1117 :     return (\@retVal, $min, $max);
1118 : parrello 1.1 }
1119 :    
1120 :     =head3 FType
1121 :    
1122 :     C<< my $ftype = $sprout->FType($featureID); >>
1123 :    
1124 :     Return the type of a feature.
1125 :    
1126 :     =over 4
1127 :    
1128 :     =item featureID
1129 :    
1130 :     ID of the feature whose type is desired.
1131 :    
1132 :     =item RETURN
1133 :    
1134 :     A string indicating the type of feature (e.g. peg, rna). If the feature does not exist, returns an
1135 :     undefined value.
1136 :    
1137 :     =back
1138 :    
1139 :     =cut
1140 : parrello 1.3 #: Return Type $;
1141 : parrello 1.1 sub FType {
1142 : parrello 1.15 # Get the parameters.
1143 :     my ($self, $featureID) = @_;
1144 :     # Get the specified feature's type.
1145 :     my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
1146 :     # Return the result.
1147 :     return $retVal;
1148 : parrello 1.1 }
1149 :    
1150 :     =head3 FeatureAnnotations
1151 :    
1152 : parrello 1.40 C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1153 : parrello 1.1
1154 :     Return the annotations of a feature.
1155 :    
1156 :     =over 4
1157 :    
1158 :     =item featureID
1159 :    
1160 :     ID of the feature whose annotations are desired.
1161 :    
1162 : parrello 1.40 =item rawFlag
1163 :    
1164 :     If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1165 :     will be returned in human-readable form.
1166 :    
1167 : parrello 1.1 =item RETURN
1168 :    
1169 :     Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.
1170 :    
1171 :     * B<featureID> ID of the relevant feature.
1172 :    
1173 : parrello 1.40 * B<timeStamp> time the annotation was made.
1174 : parrello 1.1
1175 :     * B<user> ID of the user who made the annotation
1176 :    
1177 :     * B<text> text of the annotation.
1178 :    
1179 :     =back
1180 :    
1181 :     =cut
1182 : parrello 1.3 #: Return Type @%;
1183 : parrello 1.1 sub FeatureAnnotations {
1184 : parrello 1.15 # Get the parameters.
1185 : parrello 1.40 my ($self, $featureID, $rawFlag) = @_;
1186 : parrello 1.15 # Create a query to get the feature's annotations and the associated users.
1187 :     my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1188 :     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
1189 :     # Create the return list.
1190 :     my @retVal = ();
1191 :     # Loop through the annotations.
1192 :     while (my $annotation = $query->Fetch) {
1193 :     # Get the fields to return.
1194 :     my ($featureID, $timeStamp, $user, $text) =
1195 :     $annotation->Values(['IsTargetOfAnnotation(from-link)',
1196 :     'Annotation(time)', 'MadeAnnotation(from-link)',
1197 :     'Annotation(annotation)']);
1198 : parrello 1.40 # Convert the time, if necessary.
1199 :     if (! $rawFlag) {
1200 :     $timeStamp = FriendlyTimestamp($timeStamp);
1201 :     }
1202 : parrello 1.15 # Assemble them into a hash.
1203 : parrello 1.3 my $annotationHash = { featureID => $featureID,
1204 : parrello 1.40 timeStamp => $timeStamp,
1205 : parrello 1.15 user => $user, text => $text };
1206 :     # Add it to the return list.
1207 :     push @retVal, $annotationHash;
1208 :     }
1209 :     # Return the result list.
1210 :     return @retVal;
1211 : parrello 1.1 }
1212 :    
1213 :     =head3 AllFunctionsOf
1214 :    
1215 :     C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1216 :    
1217 :     Return all of the functional assignments for a particular feature. The data is returned as a
1218 : parrello 1.11 hash of functional assignments to user IDs. A functional assignment is a type of annotation,
1219 : parrello 1.19 Functional assignments are described in the L</ParseAssignment> function. Its worth noting that
1220 :     we cannot filter on the content of the annotation itself because it's a text field; however,
1221 :     this is not a big problem because most features only have a small number of annotations.
1222 :     Finally, if a single user has multiple functional assignments, we will only keep the most
1223 : parrello 1.11 recent one.
1224 : parrello 1.1
1225 :     =over 4
1226 :    
1227 :     =item featureID
1228 :    
1229 :     ID of the feature whose functional assignments are desired.
1230 :    
1231 : parrello 1.3 =item RETURN
1232 :    
1233 : parrello 1.46 Returns a hash mapping the user IDs to functional assignment IDs.
1234 : parrello 1.3
1235 : parrello 1.1 =back
1236 :    
1237 :     =cut
1238 : parrello 1.3 #: Return Type %;
1239 : parrello 1.1 sub AllFunctionsOf {
1240 : parrello 1.15 # Get the parameters.
1241 :     my ($self, $featureID) = @_;
1242 :     # Get all of the feature's annotations.
1243 : parrello 1.47 my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1244 : parrello 1.15 "IsTargetOfAnnotation(from-link) = ?",
1245 : parrello 1.47 [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1246 :     'MadeAnnotation(from-link)']);
1247 : parrello 1.15 # Declare the return hash.
1248 :     my %retVal;
1249 : parrello 1.5 # Now we sort the assignments by timestamp in reverse.
1250 :     my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1251 : parrello 1.15 # Loop until we run out of annotations.
1252 : parrello 1.5 for my $annotation (@sortedQuery) {
1253 :     # Get the annotation fields.
1254 : parrello 1.47 my ($timeStamp, $text, $user) = @{$annotation};
1255 : parrello 1.15 # Check to see if this is a functional assignment.
1256 : parrello 1.48 my ($actualUser, $function) = _ParseAssignment($user, $text);
1257 :     if ($actualUser && ! exists $retVal{$actualUser}) {
1258 : parrello 1.5 # Here it is a functional assignment and there has been no
1259 :     # previous assignment for this user, so we stuff it in the
1260 :     # return hash.
1261 : parrello 1.48 $retVal{$actualUser} = $function;
1262 : parrello 1.15 }
1263 :     }
1264 :     # Return the hash of assignments found.
1265 :     return %retVal;
1266 : parrello 1.1 }
1267 :    
1268 :     =head3 FunctionOf
1269 :    
1270 :     C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>
1271 :    
1272 : parrello 1.3 Return the most recently-determined functional assignment of a particular feature.
1273 :    
1274 :     The functional assignment is handled differently depending on the type of feature. If
1275 :     the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1276 : parrello 1.11 assignment is a type of annotation. The format of an assignment is described in
1277 : parrello 1.45 L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1278 : parrello 1.19 annotation itself because it's a text field; however, this is not a big problem because
1279 : parrello 1.11 most features only have a small number of annotations.
1280 : parrello 1.1
1281 : parrello 1.3 Each user has an associated list of trusted users. The assignment returned will be the most
1282 :     recent one by at least one of the trusted users. If no trusted user list is available, then
1283 :     the specified user and FIG are considered trusted. If the user ID is omitted, only FIG
1284 :     is trusted.
1285 :    
1286 :     If the feature is B<not> identified by a FIG ID, then the functional assignment
1287 :     information is taken from the B<ExternalAliasFunc> table. If the table does
1288 :     not contain an entry for the feature, an undefined value is returned.
1289 :    
1290 : parrello 1.1 =over 4
1291 :    
1292 :     =item featureID
1293 :    
1294 :     ID of the feature whose functional assignment is desired.
1295 :    
1296 :     =item userID (optional)
1297 :    
1298 : parrello 1.3 ID of the user whose function determination is desired. If omitted, only the latest
1299 :     C<FIG> assignment will be returned.
1300 : parrello 1.1
1301 :     =item RETURN
1302 :    
1303 :     Returns the text of the assigned function.
1304 :    
1305 :     =back
1306 :    
1307 :     =cut
1308 : parrello 1.3 #: Return Type $;
1309 : parrello 1.1 sub FunctionOf {
1310 : parrello 1.15 # Get the parameters.
1311 :     my ($self, $featureID, $userID) = @_;
1312 : parrello 1.3 # Declare the return value.
1313 :     my $retVal;
1314 :     # Determine the ID type.
1315 :     if ($featureID =~ m/^fig\|/) {
1316 :     # Here we have a FIG feature ID. We must build the list of trusted
1317 :     # users.
1318 :     my %trusteeTable = ();
1319 :     # Check the user ID.
1320 :     if (!$userID) {
1321 :     # No user ID, so only FIG is trusted.
1322 :     $trusteeTable{FIG} = 1;
1323 :     } else {
1324 :     # Add this user's ID.
1325 :     $trusteeTable{$userID} = 1;
1326 :     # Look for the trusted users in the database.
1327 :     my @trustees = $self->GetFlat(['IsTrustedBy'], 'IsTrustedBy(from-link) = ?', [$userID], 'IsTrustedBy(to-link)');
1328 :     if (! @trustees) {
1329 :     # None were found, so build a default list.
1330 :     $trusteeTable{FIG} = 1;
1331 :     } else {
1332 :     # Otherwise, put all the trustees in.
1333 :     for my $trustee (@trustees) {
1334 :     $trusteeTable{$trustee} = 1;
1335 :     }
1336 :     }
1337 :     }
1338 :     # Build a query for all of the feature's annotations, sorted by date.
1339 : parrello 1.48 my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1340 : parrello 1.3 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1341 :     [$featureID]);
1342 :     my $timeSelected = 0;
1343 :     # Loop until we run out of annotations.
1344 :     while (my $annotation = $query->Fetch()) {
1345 :     # Get the annotation text.
1346 : parrello 1.48 my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1347 :     'Annotation(time)', 'MadeAnnotation(from-link)']);
1348 : parrello 1.3 # Check to see if this is a functional assignment for a trusted user.
1349 : parrello 1.48 my ($actualUser, $function) = _ParseAssignment($user, $text);
1350 : parrello 1.49 Trace("Assignment user is $actualUser, text is $function.") if T(4);
1351 : parrello 1.48 if ($actualUser) {
1352 : parrello 1.3 # Here it is a functional assignment. Check the time and the user
1353 :     # name. The time must be recent and the user must be trusted.
1354 : parrello 1.50 if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1355 : parrello 1.3 $retVal = $function;
1356 :     $timeSelected = $time;
1357 :     }
1358 :     }
1359 :     }
1360 :     } else {
1361 :     # Here we have a non-FIG feature ID. In this case the user ID does not
1362 :     # matter. We simply get the information from the External Alias Function
1363 :     # table.
1364 : parrello 1.4 ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']);
1365 : parrello 1.3 }
1366 : parrello 1.15 # Return the assignment found.
1367 :     return $retVal;
1368 : parrello 1.1 }
1369 :    
1370 : parrello 1.45 =head3 FunctionsOf
1371 :    
1372 :     C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1373 :    
1374 :     Return the functional assignments of a particular feature.
1375 :    
1376 :     The functional assignment is handled differently depending on the type of feature. If
1377 :     the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1378 :     assignment is a type of annotation. The format of an assignment is described in
1379 :     L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1380 :     annotation itself because it's a text field; however, this is not a big problem because
1381 :     most features only have a small number of annotations.
1382 :    
1383 :     If the feature is B<not> identified by a FIG ID, then the functional assignment
1384 :     information is taken from the B<ExternalAliasFunc> table. If the table does
1385 :     not contain an entry for the feature, an empty list is returned.
1386 :    
1387 :     =over 4
1388 :    
1389 :     =item featureID
1390 :    
1391 :     ID of the feature whose functional assignments are desired.
1392 :    
1393 :     =item RETURN
1394 :    
1395 :     Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1396 :     that user.
1397 :    
1398 :     =back
1399 :    
1400 :     =cut
1401 :     #: Return Type @@;
1402 :     sub FunctionsOf {
1403 :     # Get the parameters.
1404 :     my ($self, $featureID) = @_;
1405 :     # Declare the return value.
1406 :     my @retVal = ();
1407 :     # Determine the ID type.
1408 :     if ($featureID =~ m/^fig\|/) {
1409 :     # Here we have a FIG feature ID. We must build the list of trusted
1410 :     # users.
1411 :     my %trusteeTable = ();
1412 :     # Build a query for all of the feature's annotations, sorted by date.
1413 : parrello 1.48 my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1414 : parrello 1.45 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1415 :     [$featureID]);
1416 :     my $timeSelected = 0;
1417 :     # Loop until we run out of annotations.
1418 :     while (my $annotation = $query->Fetch()) {
1419 :     # Get the annotation text.
1420 : parrello 1.48 my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1421 :     'Annotation(time)',
1422 :     'MadeAnnotation(user)']);
1423 : parrello 1.45 # Check to see if this is a functional assignment for a trusted user.
1424 : parrello 1.48 my ($actualUser, $function) = _ParseAssignment($user, $text);
1425 :     if ($actualUser) {
1426 : parrello 1.45 # Here it is a functional assignment.
1427 : parrello 1.48 push @retVal, [$actualUser, $function];
1428 : parrello 1.45 }
1429 :     }
1430 :     } else {
1431 :     # Here we have a non-FIG feature ID. In this case the user ID does not
1432 :     # matter. We simply get the information from the External Alias Function
1433 :     # table.
1434 : parrello 1.48 my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1435 :     ['ExternalAliasFunc(func)']);
1436 :     push @retVal, map { ['master', $_] } @assignments;
1437 : parrello 1.45 }
1438 :     # Return the assignments found.
1439 :     return @retVal;
1440 :     }
1441 :    
1442 : parrello 1.1 =head3 BBHList
1443 :    
1444 :     C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
1445 :    
1446 :     Return a hash mapping the features in a specified list to their bidirectional best hits
1447 :     on a specified target genome.
1448 :    
1449 :     =over 4
1450 :    
1451 :     =item genomeID
1452 :    
1453 :     ID of the genome from which the best hits should be taken.
1454 :    
1455 :     =item featureList
1456 :    
1457 :     List of the features whose best hits are desired.
1458 :    
1459 :     =item RETURN
1460 :    
1461 : parrello 1.15 Returns a reference to a hash that maps the IDs of the incoming features to the best hits
1462 :     on the target genome.
1463 : parrello 1.1
1464 :     =back
1465 :    
1466 :     =cut
1467 : parrello 1.3 #: Return Type %;
1468 : parrello 1.1 sub BBHList {
1469 : parrello 1.15 # Get the parameters.
1470 :     my ($self, $genomeID, $featureList) = @_;
1471 :     # Create the return structure.
1472 :     my %retVal = ();
1473 :     # Loop through the incoming features.
1474 :     for my $featureID (@{$featureList}) {
1475 :     # Create a query to get the feature's best hit.
1476 :     my $query = $self->Get(['IsBidirectionalBestHitOf'],
1477 :     "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",
1478 :     [$featureID, $genomeID]);
1479 : parrello 1.40 # Peel off the BBHs found.
1480 :     my @found = ();
1481 :     while (my $bbh = $query->Fetch) {
1482 :     push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');
1483 : parrello 1.15 }
1484 : parrello 1.40 $retVal{$featureID} = \@found;
1485 : parrello 1.15 }
1486 :     # Return the mapping.
1487 :     return \%retVal;
1488 :     }
1489 :    
1490 :     =head3 SimList
1491 :    
1492 :     C<< my %similarities = $sprout->SimList($featureID, $count); >>
1493 :    
1494 :     Return a list of the similarities to the specified feature.
1495 :    
1496 :     Sprout does not support real similarities, so this method just returns the bidirectional
1497 :     best hits.
1498 :    
1499 :     =over 4
1500 :    
1501 :     =item featureID
1502 :    
1503 :     ID of the feature whose similarities are desired.
1504 :    
1505 :     =item count
1506 :    
1507 :     Maximum number of similar features to be returned, or C<0> to return them all.
1508 :    
1509 :     =back
1510 :    
1511 :     =cut
1512 :     #: Return Type %;
1513 :     sub SimList {
1514 :     # Get the parameters.
1515 :     my ($self, $featureID, $count) = @_;
1516 :     # Ask for the best hits.
1517 :     my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],
1518 :     "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",
1519 :     [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],
1520 :     $count);
1521 :     # Create the return value.
1522 :     my %retVal = ();
1523 :     for my $tuple (@lists) {
1524 :     $retVal{$tuple->[0]} = $tuple->[1];
1525 :     }
1526 :     # Return the result.
1527 :     return %retVal;
1528 :     }
1529 :    
1530 :    
1531 :    
1532 :     =head3 IsComplete
1533 :    
1534 :     C<< my $flag = $sprout->IsComplete($genomeID); >>
1535 :    
1536 :     Return TRUE if the specified genome is complete, else FALSE.
1537 :    
1538 :     =over 4
1539 :    
1540 :     =item genomeID
1541 :    
1542 :     ID of the genome whose completeness status is desired.
1543 :    
1544 :     =item RETURN
1545 :    
1546 :     Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1547 :     not found.
1548 :    
1549 :     =back
1550 :    
1551 :     =cut
1552 :     #: Return Type $;
1553 :     sub IsComplete {
1554 :     # Get the parameters.
1555 :     my ($self, $genomeID) = @_;
1556 :     # Declare the return variable.
1557 :     my $retVal;
1558 :     # Get the genome's data.
1559 :     my $genomeData = $self->GetEntity('Genome', $genomeID);
1560 :     if ($genomeData) {
1561 :     # The genome exists, so get the completeness flag.
1562 : parrello 1.51 ($retVal) = $genomeData->Value('Genome(complete)');
1563 : parrello 1.15 }
1564 :     # Return the result.
1565 :     return $retVal;
1566 : parrello 1.1 }
1567 :    
1568 :     =head3 FeatureAliases
1569 :    
1570 :     C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
1571 :    
1572 :     Return a list of the aliases for a specified feature.
1573 :    
1574 :     =over 4
1575 :    
1576 :     =item featureID
1577 :    
1578 :     ID of the feature whose aliases are desired.
1579 :    
1580 :     =item RETURN
1581 :    
1582 :     Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will
1583 :     return an empty list.
1584 :    
1585 :     =back
1586 :    
1587 :     =cut
1588 : parrello 1.3 #: Return Type @;
1589 : parrello 1.1 sub FeatureAliases {
1590 : parrello 1.15 # Get the parameters.
1591 :     my ($self, $featureID) = @_;
1592 :     # Get the desired feature's aliases
1593 :     my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);
1594 :     # Return the result.
1595 :     return @retVal;
1596 : parrello 1.1 }
1597 :    
1598 :     =head3 GenomeOf
1599 :    
1600 :     C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1601 :    
1602 : parrello 1.56 Return the genome that contains a specified feature or contig.
1603 : parrello 1.1
1604 :     =over 4
1605 :    
1606 :     =item featureID
1607 :    
1608 : parrello 1.56 ID of the feature or contig whose genome is desired.
1609 : parrello 1.1
1610 :     =item RETURN
1611 :    
1612 : parrello 1.56 Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
1613 :     found, returns an undefined value.
1614 : parrello 1.1
1615 :     =back
1616 :    
1617 :     =cut
1618 : parrello 1.3 #: Return Type $;
1619 : parrello 1.1 sub GenomeOf {
1620 : parrello 1.15 # Get the parameters.
1621 :     my ($self, $featureID) = @_;
1622 : parrello 1.56 # Create a query to find the genome associated with the incoming ID.
1623 :     my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1624 :     [$featureID, $featureID]);
1625 : parrello 1.15 # Declare the return value.
1626 :     my $retVal;
1627 :     # Get the genome ID.
1628 :     if (my $relationship = $query->Fetch()) {
1629 :     ($retVal) = $relationship->Value('HasContig(from-link)');
1630 :     }
1631 :     # Return the value found.
1632 :     return $retVal;
1633 : parrello 1.1 }
1634 :    
1635 :     =head3 CoupledFeatures
1636 :    
1637 :     C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>
1638 :    
1639 :     Return the features functionally coupled with a specified feature. Features are considered
1640 :     functionally coupled if they tend to be clustered on the same chromosome.
1641 :    
1642 :     =over 4
1643 :    
1644 :     =item featureID
1645 :    
1646 :     ID of the feature whose functionally-coupled brethren are desired.
1647 :    
1648 :     =item RETURN
1649 :    
1650 :     A hash mapping the functionally-coupled feature IDs to the coupling score.
1651 :    
1652 :     =back
1653 :    
1654 :     =cut
1655 : parrello 1.3 #: Return Type %;
1656 : parrello 1.1 sub CoupledFeatures {
1657 : parrello 1.15 # Get the parameters.
1658 :     my ($self, $featureID) = @_;
1659 : parrello 1.77 Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1660 : parrello 1.15 # Create a query to retrieve the functionally-coupled features.
1661 :     my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1662 :     "ParticipatesInCoupling(from-link) = ?", [$featureID]);
1663 :     # This value will be set to TRUE if we find at least one coupled feature.
1664 :     my $found = 0;
1665 :     # Create the return hash.
1666 :     my %retVal = ();
1667 :     # Retrieve the relationship records and store them in the hash.
1668 :     while (my $clustering = $query->Fetch()) {
1669 :     # Get the ID and score of the coupling.
1670 :     my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1671 :     'Coupling(score)']);
1672 : parrello 1.77 Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1673 : parrello 1.60 # Get the other feature that participates in the coupling.
1674 :     my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1675 :     "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1676 : parrello 1.61 [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1677 : parrello 1.77 Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1678 : parrello 1.15 # Attach the other feature's score to its ID.
1679 :     $retVal{$otherFeatureID} = $score;
1680 :     $found = 1;
1681 :     }
1682 :     # Functional coupling is reflexive. If we found at least one coupled feature, we must add
1683 :     # the incoming feature as well.
1684 :     if ($found) {
1685 :     $retVal{$featureID} = 9999;
1686 :     }
1687 :     # Return the hash.
1688 :     return %retVal;
1689 :     }
1690 :    
1691 :     =head3 CouplingEvidence
1692 :    
1693 :     C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1694 :    
1695 :     Return the evidence for a functional coupling.
1696 :    
1697 :     A pair of features is considered evidence of a coupling between two other
1698 :     features if they occur close together on a contig and both are similar to
1699 :     the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1700 :     B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1701 :     B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1702 :     similar to B<A2>.
1703 :    
1704 :     The score of a coupling is determined by the number of pieces of evidence
1705 :     that are considered I<representative>. If several evidence items belong to
1706 :     a group of genomes that are close to each other, only one of those items
1707 :     is considered representative. The other evidence items are presumed to be
1708 :     there because of the relationship between the genomes rather than because
1709 :     the two proteins generated by the features have a related functionality.
1710 :    
1711 :     Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1712 :     I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1713 :     is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1714 :     and FALSE otherwise.
1715 :    
1716 :     =over 4
1717 :    
1718 :     =item peg1
1719 :    
1720 :     ID of the feature of interest.
1721 :    
1722 :     =item peg2
1723 :    
1724 :     ID of a feature functionally coupled to the feature of interest.
1725 :    
1726 :     =item RETURN
1727 :    
1728 :     Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1729 :     of interest, a feature similar to the functionally coupled feature, and a flag
1730 :     that is TRUE for a representative piece of evidence and FALSE otherwise.
1731 :    
1732 :     =back
1733 :    
1734 :     =cut
1735 :     #: Return Type @@;
1736 :     sub CouplingEvidence {
1737 :     # Get the parameters.
1738 :     my ($self, $peg1, $peg2) = @_;
1739 :     # Declare the return variable.
1740 :     my @retVal = ();
1741 : parrello 1.18 # Our first task is to find out the nature of the coupling: whether or not
1742 :     # it exists, its score, and whether the features are stored in the same
1743 :     # order as the ones coming in.
1744 : parrello 1.15 my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1745 :     # Only proceed if a coupling exists.
1746 :     if ($couplingID) {
1747 :     # Determine the ordering to place on the evidence items. If we're
1748 : parrello 1.18 # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1749 :     # we want feature 1 before feature 2 (normal).
1750 : parrello 1.21 Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1751 : parrello 1.15 my $ordering = ($inverted ? "DESC" : "");
1752 :     # Get the coupling evidence.
1753 :     my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1754 :     "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1755 :     [$couplingID],
1756 : overbeek 1.17 ['PCH(used)', 'UsesAsEvidence(to-link)']);
1757 : parrello 1.15 # Loop through the evidence items. Each piece of evidence is represented by two
1758 :     # positions in the evidence list, one for each feature on the other side of the
1759 :     # evidence link. If at some point we want to generalize to couplings with
1760 :     # more than two positions, this section of code will need to be re-done.
1761 :     while (@evidenceList > 0) {
1762 :     my $peg1Data = shift @evidenceList;
1763 :     my $peg2Data = shift @evidenceList;
1764 : parrello 1.21 Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1765 : parrello 1.15 push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1766 :     }
1767 : parrello 1.22 Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1768 : parrello 1.1 }
1769 : parrello 1.15 # Return the result.
1770 :     return @retVal;
1771 :     }
1772 :    
1773 :     =head3 GetCoupling
1774 :    
1775 :     C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1776 :    
1777 :     Return the coupling (if any) for the specified pair of PEGs. If a coupling
1778 :     exists, we return the coupling ID along with an indicator of whether the
1779 :     coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1780 :     In the second case, we say the coupling is I<inverted>. The importance of an
1781 :     inverted coupling is that the PEGs in the evidence will appear in reverse order.
1782 :    
1783 :     =over 4
1784 :    
1785 :     =item peg1
1786 :    
1787 :     ID of the feature of interest.
1788 :    
1789 :     =item peg2
1790 :    
1791 :     ID of the potentially coupled feature.
1792 :    
1793 :     =item RETURN
1794 :    
1795 :     Returns a three-element list. The first element contains the database ID of
1796 :     the coupling. The second element is FALSE if the coupling is stored in the
1797 :     database in the caller specified order and TRUE if it is stored in the
1798 :     inverted order. The third element is the coupling's score. If the coupling
1799 :     does not exist, all three list elements will be C<undef>.
1800 :    
1801 :     =back
1802 :    
1803 :     =cut
1804 :     #: Return Type $%@;
1805 :     sub GetCoupling {
1806 :     # Get the parameters.
1807 :     my ($self, $peg1, $peg2) = @_;
1808 :     # Declare the return values. We'll start with the coupling ID and undefine the
1809 :     # flag and score until we have more information.
1810 : parrello 1.73 my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1811 : parrello 1.15 # Find the coupling data.
1812 :     my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1813 :     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1814 : overbeek 1.17 [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1815 : parrello 1.15 # Check to see if we found anything.
1816 :     if (!@pegs) {
1817 : parrello 1.23 Trace("No coupling found.") if T(Coupling => 4);
1818 : parrello 1.15 # No coupling, so undefine the return value.
1819 :     $retVal = undef;
1820 :     } else {
1821 :     # We have a coupling! Get the score and check for inversion.
1822 :     $score = $pegs[0]->[1];
1823 : parrello 1.23 my $firstFound = $pegs[0]->[0];
1824 :     $inverted = ($firstFound ne $peg1);
1825 :     Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1826 : parrello 1.15 }
1827 :     # Return the result.
1828 :     return ($retVal, $inverted, $score);
1829 :     }
1830 :    
1831 : parrello 1.74 =head3 GetSynonymGroup
1832 :    
1833 :     C<< my $id = $sprout->GetSynonymGroup($fid); >>
1834 :    
1835 :     Return the synonym group name for the specified feature.
1836 :    
1837 :     =over 4
1838 :    
1839 :     =item fid
1840 :    
1841 :     ID of the feature whose synonym group is desired.
1842 :    
1843 :     =item RETURN
1844 :    
1845 :     The name of the synonym group to which the feature belongs. If the feature does
1846 :     not belong to a synonym group, the feature ID itself is returned.
1847 :    
1848 :     =back
1849 :    
1850 :     =cut
1851 :    
1852 :     sub GetSynonymGroup {
1853 :     # Get the parameters.
1854 :     my ($self, $fid) = @_;
1855 :     # Declare the return variable.
1856 :     my $retVal;
1857 :     # Find the synonym group.
1858 :     my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1859 :     [$fid], 'IsSynonymGroupFor(from-link)');
1860 :     # Check to see if we found anything.
1861 :     if (@groups) {
1862 :     $retVal = $groups[0];
1863 :     } else {
1864 :     $retVal = $fid;
1865 :     }
1866 :     # Return the result.
1867 :     return $retVal;
1868 :     }
1869 :    
1870 : parrello 1.75 =head3 GetBoundaries
1871 :    
1872 :     C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1873 :    
1874 :     Determine the begin and end boundaries for the locations in a list. All of the
1875 :     locations must belong to the same contig and have mostly the same direction in
1876 :     order for this method to produce a meaningful result. The resulting
1877 :     begin/end pair will contain all of the bases in any of the locations.
1878 :    
1879 :     =over 4
1880 :    
1881 :     =item locList
1882 :    
1883 :     List of locations to process.
1884 :    
1885 :     =item RETURN
1886 :    
1887 :     Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1888 :     and the ending boundary. The beginning boundary will be left of the
1889 :     end for mostly-forward locations and right of the end for mostly-backward
1890 :     locations.
1891 :    
1892 :     =back
1893 :    
1894 :     =cut
1895 :    
1896 :     sub GetBoundaries {
1897 :     # Get the parameters.
1898 :     my ($self, @locList) = @_;
1899 :     # Set up the counters used to determine the most popular direction.
1900 :     my %counts = ( '+' => 0, '-' => 0 );
1901 :     # Get the last location and parse it.
1902 :     my $locObject = BasicLocation->new(pop @locList);
1903 :     # Prime the loop with its data.
1904 :     my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1905 :     # Count its direction.
1906 :     $counts{$locObject->Dir}++;
1907 :     # Loop through the remaining locations. Note that in most situations, this loop
1908 :     # will not iterate at all, because most of the time we will be dealing with a
1909 :     # singleton list.
1910 :     for my $loc (@locList) {
1911 :     # Create a location object.
1912 :     my $locObject = BasicLocation->new($loc);
1913 :     # Count the direction.
1914 :     $counts{$locObject->Dir}++;
1915 :     # Get the left end and the right end.
1916 :     my $left = $locObject->Left;
1917 :     my $right = $locObject->Right;
1918 :     # Merge them into the return variables.
1919 :     if ($left < $beg) {
1920 :     $beg = $left;
1921 :     }
1922 :     if ($right > $end) {
1923 :     $end = $right;
1924 :     }
1925 :     }
1926 :     # If the most common direction is reverse, flip the begin and end markers.
1927 :     if ($counts{'-'} > $counts{'+'}) {
1928 :     ($beg, $end) = ($end, $beg);
1929 :     }
1930 :     # Return the result.
1931 :     return ($contig, $beg, $end);
1932 :     }
1933 :    
1934 : parrello 1.15 =head3 CouplingID
1935 :    
1936 : parrello 1.73 C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1937 : parrello 1.15
1938 :     Return the coupling ID for a pair of feature IDs.
1939 :    
1940 :     The coupling ID is currently computed by joining the feature IDs in
1941 :     sorted order with a space. Client modules (that is, modules which
1942 :     use Sprout) should not, however, count on this always being the
1943 :     case. This method provides a way for abstracting the concept of a
1944 :     coupling ID. All that we know for sure about it is that it can be
1945 :     generated easily from the feature IDs and the order of the IDs
1946 :     in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1947 :     will have the same value as C<CouplingID("b1", "a1")>.
1948 :    
1949 :     =over 4
1950 :    
1951 :     =item peg1
1952 :    
1953 :     First feature of interest.
1954 :    
1955 :     =item peg2
1956 :    
1957 :     Second feature of interest.
1958 :    
1959 :     =item RETURN
1960 :    
1961 :     Returns the ID that would be used to represent a functional coupling of
1962 :     the two specified PEGs.
1963 :    
1964 :     =back
1965 :    
1966 :     =cut
1967 :     #: Return Type $;
1968 :     sub CouplingID {
1969 : parrello 1.73 my ($self, @pegs) = @_;
1970 :     return $self->DigestKey(join " ", sort @pegs);
1971 : parrello 1.1 }
1972 :    
1973 :     =head3 ReadFasta
1974 :    
1975 :     C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>
1976 :    
1977 :     Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
1978 :     one or more lines of data. The first line begins with a > character and contains an ID.
1979 :     The remaining lines contain the sequence data in order.
1980 :    
1981 :     =over 4
1982 :    
1983 :     =item fileName
1984 :    
1985 :     Name of the FASTA file.
1986 :    
1987 :     =item prefix (optional)
1988 :    
1989 :     Prefix to be put in front of each ID found.
1990 :    
1991 :     =item RETURN
1992 :    
1993 :     Returns a hash that maps each ID to its sequence.
1994 :    
1995 :     =back
1996 :    
1997 :     =cut
1998 : parrello 1.3 #: Return Type %;
1999 : parrello 1.1 sub ReadFasta {
2000 : parrello 1.15 # Get the parameters.
2001 :     my ($fileName, $prefix) = @_;
2002 :     # Create the return hash.
2003 :     my %retVal = ();
2004 :     # Open the file for input.
2005 :     open FASTAFILE, '<', $fileName;
2006 :     # Declare the ID variable and clear the sequence accumulator.
2007 :     my $sequence = "";
2008 :     my $id = "";
2009 :     # Loop through the file.
2010 :     while (<FASTAFILE>) {
2011 :     # Get the current line.
2012 :     my $line = $_;
2013 :     # Check for a header line.
2014 :     if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2015 :     # Here we have a new header. Store the current sequence if we have one.
2016 :     if ($id) {
2017 : parrello 1.24 $retVal{$id} = lc $sequence;
2018 : parrello 1.15 }
2019 :     # Clear the sequence accumulator and save the new ID.
2020 :     ($id, $sequence) = ("$prefix$1", "");
2021 :     } else {
2022 :     # Here we have a data line, so we add it to the sequence accumulator.
2023 : parrello 1.24 # First, we get the actual data out. Note that we normalize to lower
2024 : parrello 1.15 # case.
2025 :     $line =~ /^\s*(.*?)(\s|\n)/;
2026 :     $sequence .= $1;
2027 :     }
2028 :     }
2029 :     # Flush out the last sequence (if any).
2030 :     if ($sequence) {
2031 : parrello 1.24 $retVal{$id} = lc $sequence;
2032 : parrello 1.15 }
2033 :     # Close the file.
2034 :     close FASTAFILE;
2035 :     # Return the hash constructed from the file.
2036 :     return %retVal;
2037 : parrello 1.1 }
2038 :    
2039 :     =head3 FormatLocations
2040 :    
2041 :     C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>
2042 :    
2043 :     Insure that a list of feature locations is in the Sprout format. The Sprout feature location
2044 :     format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward
2045 : parrello 1.10 gene. The old format is I<contig>_I<beg>_I<end>. If a feature is in the new format already,
2046 : parrello 1.19 it will not be changed; otherwise, it will be converted. This method can also be used to
2047 : parrello 1.10 perform the reverse task-- insuring that all the locations are in the old format.
2048 : parrello 1.1
2049 :     =over 4
2050 :    
2051 :     =item prefix
2052 :    
2053 :     Prefix to be put in front of each contig ID (or an empty string if the contig ID should not
2054 :     be changed.
2055 :    
2056 :     =item locations
2057 :    
2058 :     List of locations to be normalized.
2059 :    
2060 :     =item oldFormat
2061 :    
2062 :     TRUE to convert the locations to the old format, else FALSE
2063 :    
2064 :     =item RETURN
2065 :    
2066 :     Returns a list of updated location descriptors.
2067 :    
2068 :     =back
2069 :    
2070 :     =cut
2071 : parrello 1.3 #: Return Type @;
2072 : parrello 1.1 sub FormatLocations {
2073 : parrello 1.15 # Get the parameters.
2074 :     my ($self, $prefix, $locations, $oldFormat) = @_;
2075 :     # Create the return list.
2076 :     my @retVal = ();
2077 :     # Check to see if any locations were passed in.
2078 :     if ($locations eq '') {
2079 :     Confess("No locations specified.");
2080 :     } else {
2081 :     # Loop through the locations, converting them to the new format.
2082 :     for my $location (@{$locations}) {
2083 :     # Parse the location elements.
2084 :     my ($contig, $beg, $dir, $len) = ParseLocation($location);
2085 :     # Process according to the desired output format.
2086 :     if (!$oldFormat) {
2087 :     # Here we're producing the new format. Add the location to the return list.
2088 :     push @retVal, "$prefix${contig}_$beg$dir$len";
2089 :     } elsif ($dir eq '+') {
2090 :     # Here we're producing the old format and it's a forward gene.
2091 :     my $end = $beg + $len - 1;
2092 :     push @retVal, "$prefix${contig}_${beg}_$end";
2093 :     } else {
2094 :     # Here we're producting the old format and it's a backward gene.
2095 :     my $end = $beg - $len + 1;
2096 :     push @retVal, "$prefix${contig}_${beg}_$end";
2097 :     }
2098 :     }
2099 :     }
2100 :     # Return the normalized list.
2101 :     return @retVal;
2102 : parrello 1.1 }
2103 :    
2104 :     =head3 DumpData
2105 :    
2106 :     C<< $sprout->DumpData(); >>
2107 :    
2108 :     Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.
2109 :    
2110 :     =cut
2111 :    
2112 :     sub DumpData {
2113 : parrello 1.15 # Get the parameters.
2114 :     my ($self) = @_;
2115 :     # Get the data directory name.
2116 :     my $outputDirectory = $self->{_options}->{dataDir};
2117 :     # Dump the relations.
2118 : parrello 1.58 $self->DumpRelations($outputDirectory);
2119 : parrello 1.1 }
2120 :    
2121 :     =head3 XMLFileName
2122 :    
2123 :     C<< my $fileName = $sprout->XMLFileName(); >>
2124 :    
2125 :     Return the name of this database's XML definition file.
2126 :    
2127 :     =cut
2128 : parrello 1.3 #: Return Type $;
2129 : parrello 1.1 sub XMLFileName {
2130 : parrello 1.15 my ($self) = @_;
2131 :     return $self->{_xmlName};
2132 : parrello 1.1 }
2133 :    
2134 :     =head3 Insert
2135 :    
2136 :     C<< $sprout->Insert($objectType, \%fieldHash); >>
2137 :    
2138 :     Insert an entity or relationship instance into the database. The entity or relationship of interest
2139 :     is defined by a type name and then a hash of field names to values. Field values in the primary
2140 :     relation are represented by scalars. (Note that for relationships, the primary relation is
2141 :     the B<only> relation.) Field values for the other relations comprising the entity are always
2142 :     list references. For example, the following line inserts an inactive PEG feature named
2143 :     C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.
2144 :    
2145 :     C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>
2146 :    
2147 :     The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
2148 :     property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
2149 :    
2150 : parrello 1.15 C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
2151 : parrello 1.1
2152 :     =over 4
2153 :    
2154 :     =item newObjectType
2155 :    
2156 :     Type name of the entity or relationship to insert.
2157 :    
2158 :     =item fieldHash
2159 :    
2160 :     Hash of field names to values.
2161 :    
2162 :     =back
2163 :    
2164 :     =cut
2165 : parrello 1.3 #: Return Type ;
2166 : parrello 1.1 sub Insert {
2167 : parrello 1.15 # Get the parameters.
2168 :     my ($self, $objectType, $fieldHash) = @_;
2169 :     # Call the underlying method.
2170 : parrello 1.58 $self->InsertObject($objectType, $fieldHash);
2171 : parrello 1.1 }
2172 :    
2173 :     =head3 Annotate
2174 :    
2175 :     C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>
2176 :    
2177 :     Annotate a feature. This inserts an Annotation record into the database and links it to the
2178 :     specified feature and user.
2179 :    
2180 :     =over 4
2181 :    
2182 :     =item fid
2183 :    
2184 :     ID of the feature to be annotated.
2185 :    
2186 :     =item timestamp
2187 :    
2188 :     Numeric timestamp to apply to the annotation. This is concatenated to the feature ID to create the
2189 :     key.
2190 :    
2191 :     =item user
2192 :    
2193 :     ID of the user who is making the annotation.
2194 :    
2195 :     =item text
2196 :    
2197 :     Text of the annotation.
2198 :    
2199 :     =item RETURN
2200 :    
2201 :     Returns 1 if successful, 2 if an error occurred.
2202 :    
2203 :     =back
2204 :    
2205 :     =cut
2206 : parrello 1.3 #: Return Type $;
2207 : parrello 1.1 sub Annotate {
2208 : parrello 1.15 # Get the parameters.
2209 :     my ($self, $fid, $timestamp, $user, $text) = @_;
2210 :     # Create the annotation ID.
2211 :     my $aid = "$fid:$timestamp";
2212 :     # Insert the Annotation object.
2213 :     my $retVal = $self->Insert('Annotation', { id => $aid, time => $timestamp, annotation => $text });
2214 :     if ($retVal) {
2215 :     # Connect it to the user.
2216 :     $retVal = $self->Insert('MadeAnnotation', { 'from-link' => $user, 'to-link' => $aid });
2217 :     if ($retVal) {
2218 :     # Connect it to the feature.
2219 :     $retVal = $self->Insert('IsTargetOfAnnotation', { 'from-link' => $fid,
2220 :     'to-link' => $aid });
2221 :     }
2222 :     }
2223 :     # Return the success indicator.
2224 :     return $retVal;
2225 : parrello 1.1 }
2226 :    
2227 :     =head3 AssignFunction
2228 :    
2229 : parrello 1.11 C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2230 : parrello 1.1
2231 :     This method assigns a function to a feature. Functions are a special type of annotation. The general
2232 : parrello 1.11 format is described in L</ParseAssignment>.
2233 : parrello 1.1
2234 :     =over 4
2235 :    
2236 :     =item featureID
2237 :    
2238 :     ID of the feature to which the assignment is being made.
2239 :    
2240 :     =item user
2241 :    
2242 : parrello 1.11 Name of the user group making the assignment, such as C<kegg> or C<fig>.
2243 : parrello 1.1
2244 :     =item function
2245 :    
2246 :     Text of the function being assigned.
2247 :    
2248 : parrello 1.11 =item assigningUser (optional)
2249 :    
2250 :     Name of the individual user making the assignment. If omitted, defaults to the user group.
2251 :    
2252 : parrello 1.1 =item RETURN
2253 :    
2254 :     Returns 1 if successful, 0 if an error occurred.
2255 :    
2256 :     =back
2257 :    
2258 :     =cut
2259 : parrello 1.3 #: Return Type $;
2260 : parrello 1.1 sub AssignFunction {
2261 : parrello 1.15 # Get the parameters.
2262 :     my ($self, $featureID, $user, $function, $assigningUser) = @_;
2263 : parrello 1.11 # Default the assigning user.
2264 :     if (! $assigningUser) {
2265 :     $assigningUser = $user;
2266 :     }
2267 : parrello 1.15 # Create an annotation string from the parameters.
2268 :     my $annotationText = "$assigningUser\nset $user function to\n$function";
2269 :     # Get the current time.
2270 :     my $now = time;
2271 :     # Declare the return variable.
2272 :     my $retVal = 1;
2273 :     # Locate the genome containing the feature.
2274 :     my $genome = $self->GenomeOf($featureID);
2275 :     if (!$genome) {
2276 :     # Here the genome was not found. This probably means the feature ID is invalid.
2277 :     Trace("No genome found for feature $featureID.") if T(0);
2278 :     $retVal = 0;
2279 :     } else {
2280 :     # Here we know we have a feature with a genome. Store the annotation.
2281 : parrello 1.5 $retVal = $self->Annotate($featureID, $now, $user, $annotationText);
2282 : parrello 1.15 }
2283 :     # Return the success indicator.
2284 :     return $retVal;
2285 : parrello 1.1 }
2286 :    
2287 :     =head3 FeaturesByAlias
2288 :    
2289 :     C<< my @features = $sprout->FeaturesByAlias($alias); >>
2290 :    
2291 :     Returns a list of features with the specified alias. The alias is parsed to determine
2292 :     the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
2293 :     alphanumerics is a UniProt ID. A built-in FIG.pm method is used to analyze the alias
2294 :     string and attach the necessary prefix. If the result is a FIG ID then it is returned
2295 :     unmodified; otherwise, we look for an alias.
2296 :    
2297 :     =over 4
2298 :    
2299 :     =item alias
2300 :    
2301 :     Alias whose features are desired.
2302 :    
2303 :     =item RETURN
2304 :    
2305 :     Returns a list of the features with the given alias.
2306 :    
2307 :     =back
2308 :    
2309 :     =cut
2310 : parrello 1.3 #: Return Type @;
2311 : parrello 1.1 sub FeaturesByAlias {
2312 : parrello 1.15 # Get the parameters.
2313 :     my ($self, $alias) = @_;
2314 :     # Declare the return variable.
2315 :     my @retVal = ();
2316 :     # Parse the alias.
2317 :     my ($mappedAlias, $flag) = FIGRules::NormalizeAlias($alias);
2318 :     # If it's a FIG alias, we're done.
2319 :     if ($flag) {
2320 :     push @retVal, $mappedAlias;
2321 :     } else {
2322 :     # Here we have a non-FIG alias. Get the features with the normalized alias.
2323 :     @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');
2324 :     }
2325 :     # Return the result.
2326 :     return @retVal;
2327 : parrello 1.1 }
2328 :    
2329 :     =head3 FeatureTranslation
2330 :    
2331 :     C<< my $translation = $sprout->FeatureTranslation($featureID); >>
2332 :    
2333 :     Return the translation of a feature.
2334 :    
2335 :     =over 4
2336 :    
2337 :     =item featureID
2338 :    
2339 :     ID of the feature whose translation is desired
2340 :    
2341 :     =item RETURN
2342 :    
2343 :     Returns the translation of the specified feature.
2344 :    
2345 :     =back
2346 :    
2347 :     =cut
2348 : parrello 1.3 #: Return Type $;
2349 : parrello 1.1 sub FeatureTranslation {
2350 : parrello 1.15 # Get the parameters.
2351 :     my ($self, $featureID) = @_;
2352 :     # Get the specified feature's translation.
2353 :     my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2354 :     return $retVal;
2355 : parrello 1.1 }
2356 :    
2357 :     =head3 Taxonomy
2358 :    
2359 :     C<< my @taxonomyList = $sprout->Taxonomy($genome); >>
2360 :    
2361 :     Return the taxonomy of the specified genome. This will be in the form of a list
2362 :     containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,
2363 :     or C<Eukaryote>) to sub-species. For example,
2364 :    
2365 :     C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>
2366 :    
2367 :     =over 4
2368 :    
2369 :     =item genome
2370 :    
2371 :     ID of the genome whose taxonomy is desired.
2372 :    
2373 :     =item RETURN
2374 :    
2375 :     Returns a list containing all the taxonomy classifications for the specified genome's organism.
2376 :    
2377 :     =back
2378 :    
2379 :     =cut
2380 : parrello 1.3 #: Return Type @;
2381 : parrello 1.1 sub Taxonomy {
2382 : parrello 1.15 # Get the parameters.
2383 :     my ($self, $genome) = @_;
2384 :     # Find the specified genome's taxonomy string.
2385 :     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
2386 :     # Declare the return variable.
2387 :     my @retVal = ();
2388 :     # If we found the genome, return its taxonomy string.
2389 :     if ($list) {
2390 :     @retVal = split /\s*;\s*/, $list;
2391 :     } else {
2392 :     Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
2393 :     }
2394 :     # Return the value found.
2395 :     return @retVal;
2396 : parrello 1.1 }
2397 :    
2398 :     =head3 CrudeDistance
2399 :    
2400 :     C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>
2401 :    
2402 :     Returns a crude estimate of the distance between two genomes. The distance is construed so
2403 :     that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
2404 :    
2405 :     =over 4
2406 :    
2407 :     =item genome1
2408 :    
2409 :     ID of the first genome to compare.
2410 :    
2411 :     =item genome2
2412 :    
2413 :     ID of the second genome to compare.
2414 :    
2415 :     =item RETURN
2416 :    
2417 :     Returns a value from 0 to 1, with 0 meaning identical organisms, and 1 meaning organisms from
2418 :     different domains.
2419 :    
2420 :     =back
2421 :    
2422 :     =cut
2423 : parrello 1.3 #: Return Type $;
2424 : parrello 1.1 sub CrudeDistance {
2425 : parrello 1.15 # Get the parameters.
2426 :     my ($self, $genome1, $genome2) = @_;
2427 :     # Insure that the distance is commutative by sorting the genome IDs.
2428 :     my ($genomeA, $genomeB);
2429 :     if ($genome2 < $genome2) {
2430 :     ($genomeA, $genomeB) = ($genome1, $genome2);
2431 :     } else {
2432 :     ($genomeA, $genomeB) = ($genome2, $genome1);
2433 :     }
2434 :     my @taxA = $self->Taxonomy($genomeA);
2435 :     my @taxB = $self->Taxonomy($genomeB);
2436 :     # Initialize the distance to 1. We'll reduce it each time we find a match between the
2437 :     # taxonomies.
2438 :     my $retVal = 1.0;
2439 :     # Initialize the subtraction amount. This amount determines the distance reduction caused
2440 :     # by a mismatch at the current level.
2441 :     my $v = 0.5;
2442 :     # Loop through the taxonomies.
2443 :     for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) {
2444 :     $retVal -= $v;
2445 :     $v /= 2;
2446 :     }
2447 : parrello 1.1 return $retVal;
2448 :     }
2449 :    
2450 :     =head3 RoleName
2451 :    
2452 :     C<< my $roleName = $sprout->RoleName($roleID); >>
2453 :    
2454 :     Return the descriptive name of the role with the specified ID. In general, a role
2455 :     will only have a descriptive name if it is coded as an EC number.
2456 :    
2457 :     =over 4
2458 :    
2459 :     =item roleID
2460 :    
2461 :     ID of the role whose description is desired.
2462 :    
2463 :     =item RETURN
2464 :    
2465 :     Returns the descriptive name of the desired role.
2466 :    
2467 :     =back
2468 :    
2469 :     =cut
2470 : parrello 1.3 #: Return Type $;
2471 : parrello 1.1 sub RoleName {
2472 : parrello 1.15 # Get the parameters.
2473 :     my ($self, $roleID) = @_;
2474 :     # Get the specified role's name.
2475 :     my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2476 :     # Use the ID if the role has no name.
2477 :     if (!$retVal) {
2478 :     $retVal = $roleID;
2479 :     }
2480 :     # Return the name.
2481 :     return $retVal;
2482 : parrello 1.1 }
2483 :    
2484 :     =head3 RoleDiagrams
2485 :    
2486 :     C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>
2487 :    
2488 :     Return a list of the diagrams containing a specified functional role.
2489 :    
2490 :     =over 4
2491 :    
2492 :     =item roleID
2493 :    
2494 :     ID of the role whose diagrams are desired.
2495 :    
2496 :     =item RETURN
2497 :    
2498 :     Returns a list of the IDs for the diagrams that contain the specified functional role.
2499 :    
2500 :     =back
2501 :    
2502 :     =cut
2503 : parrello 1.3 #: Return Type @;
2504 : parrello 1.1 sub RoleDiagrams {
2505 : parrello 1.15 # Get the parameters.
2506 :     my ($self, $roleID) = @_;
2507 :     # Query for the diagrams.
2508 :     my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2509 :     'RoleOccursIn(to-link)');
2510 :     # Return the result.
2511 :     return @retVal;
2512 : parrello 1.1 }
2513 :    
2514 : parrello 1.19 =head3 GetProperties
2515 :    
2516 :     C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2517 :    
2518 :     Return a list of the properties with the specified characteristics.
2519 :    
2520 :     Properties are arbitrary key-value pairs associated with a feature. (At some point they
2521 :     will also be associated with genomes.) A property value is represented by a 4-tuple of
2522 :     the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2523 :    
2524 :     =over 4
2525 :    
2526 :     =item fid
2527 :    
2528 :     ID of the feature possessing the property.
2529 :    
2530 :     =item key
2531 :    
2532 :     Name or key of the property.
2533 :    
2534 :     =item value
2535 :    
2536 :     Value of the property.
2537 :    
2538 :     =item url
2539 :    
2540 :     URL of the document that indicated the property should have this particular value, or an
2541 :     empty string if no such document exists.
2542 :    
2543 :     =back
2544 :    
2545 :     The parameters act as a filter for the desired data. Any non-null parameter will
2546 :     automatically match all the tuples returned. So, specifying just the I<$fid> will
2547 :     return all the properties of the specified feature; similarly, specifying the I<$key>
2548 :     and I<$value> parameters will return all the features having the specified property
2549 :     value.
2550 :    
2551 :     A single property key can have many values, representing different ideas about the
2552 :     feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2553 :     virulent, and another may declare that it is not virulent. A query about the virulence of
2554 :     C<fig|83333.1.peg.10> would be coded as
2555 :    
2556 :     my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2557 :    
2558 :     Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2559 :     not to be filtered. The tuples returned would be
2560 :    
2561 :     ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2562 :     ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2563 :    
2564 :     =cut
2565 :     #: Return Type @@;
2566 :     sub GetProperties {
2567 :     # Get the parameters.
2568 :     my ($self, @parms) = @_;
2569 :     # Declare the return variable.
2570 :     my @retVal = ();
2571 :     # Now we need to create a WHERE clause that will get us the data we want. First,
2572 :     # we create a list of the columns containing the data for each parameter.
2573 :     my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2574 :     'Property(property-value)', 'HasProperty(evidence)');
2575 :     # Now we build the WHERE clause and the list of parameter values.
2576 :     my @where = ();
2577 :     my @values = ();
2578 :     for (my $i = 0; $i <= $#colNames; $i++) {
2579 :     my $parm = $parms[$i];
2580 :     if (defined $parm && ($parm ne '')) {
2581 :     push @where, "$colNames[$i] = ?";
2582 :     push @values, $parm;
2583 :     }
2584 :     }
2585 :     # Format the WHERE clause.
2586 :     my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2587 :     # Ask for all the propertie values with the desired characteristics.
2588 :     my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2589 :     while (my $valueObject = $query->Fetch()) {
2590 :     my @tuple = $valueObject->Values(\@colNames);
2591 :     push @retVal, \@tuple;
2592 :     }
2593 :     # Return the result.
2594 :     return @retVal;
2595 :     }
2596 :    
2597 : parrello 1.1 =head3 FeatureProperties
2598 :    
2599 :     C<< my @properties = $sprout->FeatureProperties($featureID); >>
2600 :    
2601 :     Return a list of the properties for the specified feature. Properties are key-value pairs
2602 :     that specify special characteristics of the feature. For example, a property could indicate
2603 :     that a feature is essential to the survival of the organism or that it has benign influence
2604 :     on the activities of a pathogen. Each property is returned as a triple of the form
2605 :     C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly
2606 :     a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing
2607 :     the web address or citation in which the property's value for the feature was identified.
2608 :    
2609 :     =over 4
2610 :    
2611 :     =item featureID
2612 :    
2613 :     ID of the feature whose properties are desired.
2614 :    
2615 :     =item RETURN
2616 :    
2617 :     Returns a list of triples, each triple containing the property name, its value, and a URL or
2618 :     citation.
2619 :    
2620 :     =back
2621 :    
2622 :     =cut
2623 : parrello 1.3 #: Return Type @@;
2624 : parrello 1.1 sub FeatureProperties {
2625 : parrello 1.15 # Get the parameters.
2626 :     my ($self, $featureID) = @_;
2627 :     # Get the properties.
2628 :     my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],
2629 :     ['Property(property-name)', 'Property(property-value)',
2630 :     'HasProperty(evidence)']);
2631 :     # Return the resulting list.
2632 :     return @retVal;
2633 : parrello 1.1 }
2634 :    
2635 :     =head3 DiagramName
2636 :    
2637 :     C<< my $diagramName = $sprout->DiagramName($diagramID); >>
2638 :    
2639 :     Return the descriptive name of a diagram.
2640 :    
2641 :     =over 4
2642 :    
2643 :     =item diagramID
2644 :    
2645 :     ID of the diagram whose description is desired.
2646 :    
2647 :     =item RETURN
2648 :    
2649 :     Returns the descripive name of the specified diagram.
2650 :    
2651 :     =back
2652 :    
2653 :     =cut
2654 : parrello 1.3 #: Return Type $;
2655 : parrello 1.1 sub DiagramName {
2656 : parrello 1.15 # Get the parameters.
2657 :     my ($self, $diagramID) = @_;
2658 :     # Get the specified diagram's name and return it.
2659 :     my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2660 :     return $retVal;
2661 : parrello 1.1 }
2662 :    
2663 :     =head3 MergedAnnotations
2664 :    
2665 :     C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
2666 :    
2667 :     Returns a merged list of the annotations for the features in a list. Each annotation is
2668 :     represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
2669 :     C<$fid> is the ID of a feature, C<$timestamp> is the time at which the annotation was made,
2670 :     C<$userID> is the ID of the user who made the annotation, and C<$annotation> is the annotation
2671 :     text. The list is sorted by timestamp.
2672 :    
2673 :     =over 4
2674 :    
2675 :     =item list
2676 :    
2677 :     List of the IDs for the features whose annotations are desired.
2678 :    
2679 :     =item RETURN
2680 :    
2681 :     Returns a list of annotation descriptions sorted by the annotation time.
2682 :    
2683 :     =back
2684 :    
2685 :     =cut
2686 : parrello 1.3 #: Return Type @;
2687 : parrello 1.1 sub MergedAnnotations {
2688 : parrello 1.15 # Get the parameters.
2689 :     my ($self, $list) = @_;
2690 :     # Create a list to hold the annotation tuples found.
2691 :     my @tuples = ();
2692 :     # Loop through the features in the input list.
2693 :     for my $fid (@{$list}) {
2694 :     # Create a list of this feature's annotation tuples.
2695 :     my @newTuples = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
2696 :     "IsTargetOfAnnotation(from-link) = ?", [$fid],
2697 :     ['IsTargetOfAnnotation(from-link)', 'Annotation(time)',
2698 :     'MadeAnnotation(from-link)', 'Annotation(annotation)']);
2699 :     # Put it in the result list.
2700 :     push @tuples, @newTuples;
2701 :     }
2702 :     # Sort the result list by timestamp.
2703 :     my @retVal = sort { $a->[1] <=> $b->[1] } @tuples;
2704 : parrello 1.3 # Loop through and make the time stamps friendly.
2705 :     for my $tuple (@retVal) {
2706 :     $tuple->[1] = FriendlyTimestamp($tuple->[1]);
2707 :     }
2708 : parrello 1.15 # Return the sorted list.
2709 :     return @retVal;
2710 : parrello 1.1 }
2711 :    
2712 :     =head3 RoleNeighbors
2713 :    
2714 :     C<< my @roleList = $sprout->RoleNeighbors($roleID); >>
2715 :    
2716 :     Returns a list of the roles that occur in the same diagram as the specified role. Because
2717 :     diagrams and roles are in a many-to-many relationship with each other, the list is
2718 :     essentially the set of roles from all of the maps that contain the incoming role. Such
2719 :     roles are considered neighbors because they are used together in cellular subsystems.
2720 :    
2721 :     =over 4
2722 :    
2723 :     =item roleID
2724 :    
2725 :     ID of the role whose neighbors are desired.
2726 :    
2727 :     =item RETURN
2728 :    
2729 :     Returns a list containing the IDs of the roles that are related to the incoming role.
2730 :    
2731 :     =back
2732 :    
2733 :     =cut
2734 : parrello 1.3 #: Return Type @;
2735 : parrello 1.1 sub RoleNeighbors {
2736 : parrello 1.15 # Get the parameters.
2737 :     my ($self, $roleID) = @_;
2738 :     # Get all the diagrams containing this role.
2739 :     my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2740 :     'RoleOccursIn(to-link)');
2741 :     # Create the return list.
2742 :     my @retVal = ();
2743 :     # Loop through the diagrams.
2744 :     for my $diagramID (@diagrams) {
2745 :     # Get all the roles in this diagram.
2746 :     my @roles = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(to-link) = ?", [$diagramID],
2747 :     'RoleOccursIn(from-link)');
2748 :     # Add them to the return list.
2749 :     push @retVal, @roles;
2750 :     }
2751 :     # Merge the duplicates from the list.
2752 :     return Tracer::Merge(@retVal);
2753 : parrello 1.1 }
2754 :    
2755 :     =head3 FeatureLinks
2756 :    
2757 :     C<< my @links = $sprout->FeatureLinks($featureID); >>
2758 :    
2759 :     Return a list of the web hyperlinks associated with a feature. The web hyperlinks are
2760 :     to external websites describing either the feature itself or the organism containing it
2761 :     and are represented in raw HTML.
2762 :    
2763 :     =over 4
2764 :    
2765 :     =item featureID
2766 :    
2767 :     ID of the feature whose links are desired.
2768 :    
2769 :     =item RETURN
2770 :    
2771 :     Returns a list of the web links for this feature.
2772 :    
2773 :     =back
2774 :    
2775 :     =cut
2776 : parrello 1.3 #: Return Type @;
2777 : parrello 1.1 sub FeatureLinks {
2778 : parrello 1.15 # Get the parameters.
2779 :     my ($self, $featureID) = @_;
2780 :     # Get the feature's links.
2781 :     my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
2782 :     # Return the feature's links.
2783 :     return @retVal;
2784 : parrello 1.1 }
2785 :    
2786 :     =head3 SubsystemsOf
2787 :    
2788 :     C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2789 :    
2790 :     Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
2791 : parrello 1.20 to the roles the feature performs.
2792 : parrello 1.1
2793 :     =over 4
2794 :    
2795 :     =item featureID
2796 :    
2797 :     ID of the feature whose subsystems are desired.
2798 :    
2799 :     =item RETURN
2800 :    
2801 : parrello 1.20 Returns a hash mapping all the feature's subsystems to a list of the feature's roles.
2802 : parrello 1.1
2803 :     =back
2804 :    
2805 :     =cut
2806 : parrello 1.20 #: Return Type %@;
2807 : parrello 1.1 sub SubsystemsOf {
2808 : parrello 1.15 # Get the parameters.
2809 :     my ($self, $featureID) = @_;
2810 : parrello 1.16 # Get the subsystem list.
2811 : parrello 1.15 my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2812 :     "ContainsFeature(to-link) = ?", [$featureID],
2813 :     ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2814 :     # Create the return value.
2815 :     my %retVal = ();
2816 : parrello 1.42 # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2817 :     # in two spreadsheet cells.
2818 :     my %dupHash = ();
2819 : parrello 1.15 # Loop through the results, adding them to the hash.
2820 :     for my $record (@subsystems) {
2821 : parrello 1.42 # Get this subsystem and role.
2822 : parrello 1.20 my ($subsys, $role) = @{$record};
2823 : parrello 1.42 # Insure it's the first time for both.
2824 :     my $dupKey = "$subsys\n$role";
2825 :     if (! exists $dupHash{"$subsys\n$role"}) {
2826 :     $dupHash{$dupKey} = 1;
2827 : parrello 1.20 push @{$retVal{$subsys}}, $role;
2828 :     }
2829 : parrello 1.15 }
2830 :     # Return the hash.
2831 :     return %retVal;
2832 : parrello 1.1 }
2833 :    
2834 : parrello 1.16 =head3 SubsystemList
2835 :    
2836 :     C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2837 :    
2838 :     Return a list containing the names of the subsystems in which the specified
2839 :     feature participates. Unlike L</SubsystemsOf>, this method only returns the
2840 :     subsystem names, not the roles.
2841 :    
2842 :     =over 4
2843 :    
2844 :     =item featureID
2845 :    
2846 :     ID of the feature whose subsystem names are desired.
2847 :    
2848 :     =item RETURN
2849 :    
2850 :     Returns a list of the names of the subsystems in which the feature participates.
2851 :    
2852 :     =back
2853 :    
2854 :     =cut
2855 :     #: Return Type @;
2856 :     sub SubsystemList {
2857 :     # Get the parameters.
2858 :     my ($self, $featureID) = @_;
2859 :     # Get the list of names.
2860 :     my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2861 :     [$featureID], 'HasSSCell(from-link)');
2862 :     # Return the result.
2863 :     return @retVal;
2864 :     }
2865 :    
2866 : parrello 1.1 =head3 RelatedFeatures
2867 :    
2868 :     C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
2869 :    
2870 :     Return a list of the features which are bi-directional best hits of the specified feature and
2871 :     have been assigned the specified function by the specified user. If no such features exists,
2872 :     an empty list will be returned.
2873 :    
2874 :     =over 4
2875 :    
2876 :     =item featureID
2877 :    
2878 :     ID of the feature to whom the desired features are related.
2879 :    
2880 :     =item function
2881 :    
2882 :     Functional assignment (as returned by C</FunctionOf>) that is used to determine which related
2883 :     features should be selected.
2884 :    
2885 :     =item userID
2886 :    
2887 :     ID of the user whose functional assignments are to be used. If omitted, C<FIG> is assumed.
2888 :    
2889 :     =item RETURN
2890 :    
2891 :     Returns a list of the related features with the specified function.
2892 :    
2893 :     =back
2894 :    
2895 :     =cut
2896 : parrello 1.3 #: Return Type @;
2897 : parrello 1.1 sub RelatedFeatures {
2898 : parrello 1.15 # Get the parameters.
2899 :     my ($self, $featureID, $function, $userID) = @_;
2900 :     # Get a list of the features that are BBHs of the incoming feature.
2901 :     my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],
2902 :     "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],
2903 :     'IsBidirectionalBestHitOf(to-link)');
2904 :     # Now we loop through the features, pulling out the ones that have the correct
2905 :     # functional assignment.
2906 :     my @retVal = ();
2907 :     for my $bbhFeature (@bbhFeatures) {
2908 :     # Get this feature's functional assignment.
2909 :     my $newFunction = $self->FunctionOf($bbhFeature, $userID);
2910 :     # If it matches, add it to the result list.
2911 :     if ($newFunction eq $function) {
2912 :     push @retVal, $bbhFeature;
2913 :     }
2914 :     }
2915 :     # Return the result list.
2916 :     return @retVal;
2917 : parrello 1.1 }
2918 :    
2919 :     =head3 TaxonomySort
2920 :    
2921 :     C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>
2922 :    
2923 :     Return a list formed by sorting the specified features by the taxonomy of the containing
2924 :     genome. This will cause genomes from similar organisms to float close to each other.
2925 :    
2926 :     This task could almost be handled by the database; however, the taxonomy string in the
2927 :     database is a text field and can't be indexed. Instead, we create a hash table that maps
2928 :     taxonomy strings to lists of features. We then process the hash table using a key sort
2929 :     and merge the feature lists together to create the output.
2930 :    
2931 :     =over 4
2932 :    
2933 :     =item $featureIDs
2934 :    
2935 :     List of features to be taxonomically sorted.
2936 :    
2937 :     =item RETURN
2938 :    
2939 :     Returns the list of features sorted by the taxonomies of the containing genomes.
2940 :    
2941 :     =back
2942 :    
2943 :     =cut
2944 : parrello 1.3 #: Return Type @;
2945 : parrello 1.1 sub TaxonomySort {
2946 : parrello 1.15 # Get the parameters.
2947 :     my ($self, $featureIDs) = @_;
2948 :     # Create the working hash table.
2949 :     my %hashBuffer = ();
2950 :     # Loop through the features.
2951 :     for my $fid (@{$featureIDs}) {
2952 :     # Get the taxonomy of the feature's genome.
2953 :     my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
2954 :     [$fid], 'Genome(taxonomy)');
2955 :     # Add this feature to the hash buffer.
2956 : parrello 1.10 Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);
2957 : parrello 1.15 }
2958 :     # Sort the keys and get the elements.
2959 :     my @retVal = ();
2960 :     for my $taxon (sort keys %hashBuffer) {
2961 :     push @retVal, @{$hashBuffer{$taxon}};
2962 :     }
2963 :     # Return the result.
2964 :     return @retVal;
2965 : parrello 1.1 }
2966 :    
2967 :     =head3 Protein
2968 :    
2969 :     C<< my $protein = Sprout::Protein($sequence, $table); >>
2970 :    
2971 :     Translate a DNA sequence into a protein sequence.
2972 :    
2973 :     =over 4
2974 :    
2975 :     =item sequence
2976 :    
2977 :     DNA sequence to translate.
2978 :    
2979 :     =item table (optional)
2980 :    
2981 :     Reference to a Hash that translates DNA triples to proteins. A triple that does not
2982 :     appear in the hash will be translated automatically to C<X>.
2983 :    
2984 :     =item RETURN
2985 :    
2986 :     Returns the protein sequence that would be created by the DNA sequence.
2987 :    
2988 :     =back
2989 :    
2990 :     =cut
2991 :    
2992 :     # This is the translation table for protein synthesis.
2993 :     my $ProteinTable = { AAA => 'K', AAG => 'K', AAT => 'N', AAC => 'N',
2994 : parrello 1.15 AGA => 'R', AGG => 'R', AGT => 'S', AGC => 'S',
2995 :     ATA => 'I', ATG => 'M', ATT => 'I', ATC => 'I',
2996 :     ACA => 'T', ACG => 'T', ACT => 'T', ACC => 'T',
2997 :     GAA => 'E', GAG => 'E', GAT => 'D', GAC => 'D',
2998 :     GTA => 'V', GTG => 'V', GTT => 'V', GTC => 'V',
2999 :     GGA => 'G', GGG => 'G', GGT => 'G', GGC => 'G',
3000 :     GCA => 'A', GCG => 'A', GCT => 'A', GCC => 'A',
3001 :     CAA => 'Q', CAG => 'Q', CAT => 'H', CAC => 'H',
3002 :     CTA => 'L', CTG => 'L', CTT => 'L', CTC => 'L',
3003 :     CGA => 'R', CGG => 'R', CGT => 'R', CGC => 'R',
3004 :     CCA => 'P', CCG => 'P', CCT => 'P', CCC => 'P',
3005 :     TAA => '*', TAG => '*', TAT => 'Y', TAC => 'Y',
3006 :     TGA => '*', TGG => 'W', TGT => 'C', TGC => 'C',
3007 :     TTA => 'L', TTG => 'L', TTT => 'F', TTC => 'F',
3008 :     TCA => 'S', TCG => 'S', TCT => 'S', TCC => 'S',
3009 :     AAR => 'K', AAY => 'N',
3010 :     AGR => 'R', AGY => 'S',
3011 :     ATY => 'I',
3012 :     ACR => 'T', ACY => 'T', 'ACX' => 'T',
3013 :     GAR => 'E', GAY => 'D',
3014 :     GTR => 'V', GTY => 'V', GTX => 'V',
3015 :     GGR => 'G', GGY => 'G', GGX => 'G',
3016 :     GCR => 'A', GCY => 'A', GCX => 'A',
3017 :     CAR => 'Q', CAY => 'H',
3018 :     CTR => 'L', CTY => 'L', CTX => 'L',
3019 :     CGR => 'R', CGY => 'R', CGX => 'R',
3020 :     CCR => 'P', CCY => 'P', CCX => 'P',
3021 :     TAR => '*', TAY => 'Y',
3022 :     TGY => 'C',
3023 :     TTR => 'L', TTY => 'F',
3024 :     TCR => 'S', TCY => 'S', TCX => 'S'
3025 :     };
3026 : parrello 1.1
3027 :     sub Protein {
3028 : parrello 1.15 # Get the paraeters.
3029 :     my ($sequence, $table) = @_;
3030 :     # If no table was specified, use the default.
3031 :     if (!$table) {
3032 :     $table = $ProteinTable;
3033 :     }
3034 :     # Create the return value.
3035 :     my $retVal = "";
3036 :     # Loop through the input triples.
3037 :     my $n = length $sequence;
3038 :     for (my $i = 0; $i < $n; $i += 3) {
3039 :     # Get the current triple from the sequence.
3040 :     my $triple = substr($sequence, $i, 3);
3041 :     # Translate it using the table.
3042 :     my $protein = "X";
3043 :     if (exists $table->{$triple}) { $protein = $table->{$triple}; }
3044 :     $retVal .= $protein;
3045 :     }
3046 :     # Remove the stop codon (if any).
3047 :     $retVal =~ s/\*$//;
3048 :     # Return the result.
3049 :     return $retVal;
3050 : parrello 1.1 }
3051 :    
3052 :     =head3 LoadInfo
3053 :    
3054 :     C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>
3055 :    
3056 :     Return the name of the directory from which data is to be loaded and a list of the relation
3057 :     names. This information is useful when trying to analyze what needs to be put where in order
3058 :     to load the entire database.
3059 :    
3060 :     =cut
3061 : parrello 1.3 #: Return Type @;
3062 : parrello 1.1 sub LoadInfo {
3063 : parrello 1.15 # Get the parameters.
3064 :     my ($self) = @_;
3065 :     # Create the return list, priming it with the name of the data directory.
3066 :     my @retVal = ($self->{_options}->{dataDir});
3067 :     # Concatenate the table names.
3068 : parrello 1.58 push @retVal, $self->GetTableNames();
3069 : parrello 1.15 # Return the result.
3070 :     return @retVal;
3071 : parrello 1.1 }
3072 :    
3073 :     =head3 LowBBHs
3074 :    
3075 : parrello 1.58 C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3076 : parrello 1.1
3077 :     Return the bidirectional best hits of a feature whose score is no greater than a
3078 :     specified cutoff value. A higher cutoff value will allow inclusion of hits with
3079 :     a greater score. The value returned is a map of feature IDs to scores.
3080 :    
3081 :     =over 4
3082 :    
3083 :     =item featureID
3084 :    
3085 :     ID of the feature whose best hits are desired.
3086 :    
3087 :     =item cutoff
3088 :    
3089 :     Maximum permissible score for inclusion in the results.
3090 :    
3091 :     =item RETURN
3092 :    
3093 :     Returns a hash mapping feature IDs to scores.
3094 :    
3095 :     =back
3096 :    
3097 :     =cut
3098 : parrello 1.3 #: Return Type %;
3099 : parrello 1.1 sub LowBBHs {
3100 : parrello 1.15 # Get the parsameters.
3101 :     my ($self, $featureID, $cutoff) = @_;
3102 :     # Create the return hash.
3103 :     my %retVal = ();
3104 :     # Create a query to get the desired BBHs.
3105 :     my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],
3106 :     'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',
3107 :     [$cutoff, $featureID],
3108 :     ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);
3109 :     # Form the results into the return hash.
3110 :     for my $pair (@bbhList) {
3111 :     $retVal{$pair->[0]} = $pair->[1];
3112 :     }
3113 :     # Return the result.
3114 :     return %retVal;
3115 : parrello 1.1 }
3116 :    
3117 : parrello 1.71 =head3 Sims
3118 :    
3119 :     C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3120 :    
3121 :     Get a list of similarities for a specified feature. Similarity information is not kept in the
3122 :     Sprout database; rather, they are retrieved from a network server. The similarities are
3123 :     returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3124 :     so that its elements can be accessed by name.
3125 :    
3126 :     Similarities can be either raw or expanded. The raw similarities are basic
3127 :     hits between features with similar DNA. Expanding a raw similarity drags in any
3128 :     features considered substantially identical. So, for example, if features B<A1>,
3129 :     B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3130 :     B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3131 :    
3132 :     =over 4
3133 :    
3134 :     =item fid
3135 :    
3136 :     ID of the feature whose similarities are desired.
3137 :    
3138 :     =item maxN
3139 :    
3140 :     Maximum number of similarities to return.
3141 :    
3142 :     =item maxP
3143 :    
3144 :     Minumum allowable similarity score.
3145 :    
3146 :     =item select
3147 :    
3148 :     Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3149 :     means only similarities to FIG features are returned; C<all> means all expanded
3150 :     similarities are returned; and C<figx> means similarities are expanded until the
3151 :     number of FIG features equals the maximum.
3152 :    
3153 :     =item max_expand
3154 :    
3155 :     The maximum number of features to expand.
3156 :    
3157 :     =item filters
3158 :    
3159 :     Reference to a hash containing filter information, or a subroutine that can be
3160 :     used to filter the sims.
3161 :    
3162 :     =item RETURN
3163 :    
3164 :     Returns a reference to a list of similarity objects, or C<undef> if an error
3165 :     occurred.
3166 :    
3167 :     =back
3168 :    
3169 :     =cut
3170 :    
3171 :     sub Sims {
3172 :     # Get the parameters.
3173 :     my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3174 :     # Create the shim object to test for deleted FIDs.
3175 :     my $shim = FidCheck->new($self);
3176 :     # Ask the network for sims.
3177 :     my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3178 :     # Return the result.
3179 :     return $retVal;
3180 :     }
3181 :    
3182 : parrello 1.7 =head3 GetGroups
3183 :    
3184 : parrello 1.8 C<< my %groups = $sprout->GetGroups(\@groupList); >>
3185 :    
3186 :     Return a hash mapping each group to the IDs of the genomes in the group.
3187 :     A list of groups may be specified, in which case only those groups will be
3188 :     shown. Alternatively, if no parameter is supplied, all groups will be
3189 :     included. Genomes that are not in any group are omitted.
3190 :    
3191 : parrello 1.7 =cut
3192 :     #: Return Type %@;
3193 :     sub GetGroups {
3194 : parrello 1.8 # Get the parameters.
3195 : parrello 1.10 my ($self, $groupList) = @_;
3196 : parrello 1.8 # Declare the return value.
3197 :     my %retVal = ();
3198 :     # Determine whether we are getting all the groups or just some.
3199 :     if (defined $groupList) {
3200 :     # Here we have a group list. Loop through them individually,
3201 :     # getting a list of the relevant genomes.
3202 :     for my $group (@{$groupList}) {
3203 :     my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",
3204 :     [$group], "Genome(id)");
3205 :     $retVal{$group} = \@genomeIDs;
3206 :     }
3207 :     } else {
3208 :     # Here we need all of the groups. In this case, we run through all
3209 :     # of the genome records, putting each one found into the appropriate
3210 :     # group. Note that we use a filter clause to insure that only genomes
3211 :     # in groups are included in the return set.
3212 :     my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],
3213 :     ['Genome(id)', 'Genome(group-name)']);
3214 :     # Loop through the genomes found.
3215 :     for my $genome (@genomes) {
3216 :     # Pop this genome's ID off the current list.
3217 :     my @groups = @{$genome};
3218 :     my $genomeID = shift @groups;
3219 :     # Loop through the groups, adding the genome ID to each group's
3220 :     # list.
3221 :     for my $group (@groups) {
3222 : parrello 1.10 Tracer::AddToListMap(\%retVal, $group, $genomeID);
3223 : parrello 1.8 }
3224 :     }
3225 :     }
3226 :     # Return the hash we just built.
3227 :     return %retVal;
3228 : parrello 1.7 }
3229 :    
3230 : parrello 1.18 =head3 MyGenomes
3231 :    
3232 :     C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3233 :    
3234 :     Return a list of the genomes to be included in the Sprout.
3235 :    
3236 :     This method is provided for use during the Sprout load. It presumes the Genome load file has
3237 :     already been created. (It will be in the Sprout data directory and called either C<Genome>
3238 :     or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3239 :     IDs.
3240 :    
3241 :     =over 4
3242 :    
3243 :     =item dataDir
3244 :    
3245 :     Directory containing the Sprout load files.
3246 :    
3247 :     =back
3248 :    
3249 :     =cut
3250 :     #: Return Type @;
3251 :     sub MyGenomes {
3252 :     # Get the parameters.
3253 :     my ($dataDir) = @_;
3254 :     # Compute the genome file name.
3255 :     my $genomeFileName = LoadFileName($dataDir, "Genome");
3256 :     # Extract the genome IDs from the files.
3257 :     my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3258 :     # Return the result.
3259 :     return @retVal;
3260 :     }
3261 :    
3262 :     =head3 LoadFileName
3263 :    
3264 :     C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3265 :    
3266 :     Return the name of the load file for the specified table in the specified data
3267 :     directory.
3268 :    
3269 :     =over 4
3270 :    
3271 :     =item dataDir
3272 :    
3273 :     Directory containing the Sprout load files.
3274 :    
3275 :     =item tableName
3276 :    
3277 :     Name of the table whose load file is desired.
3278 :    
3279 :     =item RETURN
3280 :    
3281 :     Returns the name of the file containing the load data for the specified table, or
3282 :     C<undef> if no load file is present.
3283 :    
3284 :     =back
3285 :    
3286 :     =cut
3287 :     #: Return Type $;
3288 :     sub LoadFileName {
3289 :     # Get the parameters.
3290 :     my ($dataDir, $tableName) = @_;
3291 :     # Declare the return variable.
3292 :     my $retVal;
3293 :     # Check for the various file names.
3294 :     if (-e "$dataDir/$tableName") {
3295 :     $retVal = "$dataDir/$tableName";
3296 :     } elsif (-e "$dataDir/$tableName.dtx") {
3297 :     $retVal = "$dataDir/$tableName.dtx";
3298 :     }
3299 :     # Return the result.
3300 :     return $retVal;
3301 :     }
3302 :    
3303 : parrello 1.53 =head3 DeleteGenome
3304 :    
3305 :     C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3306 :    
3307 :     Delete a genome from the database.
3308 :    
3309 :     =over 4
3310 :    
3311 :     =item genomeID
3312 :    
3313 :     ID of the genome to delete
3314 :    
3315 :     =item testFlag
3316 :    
3317 :     If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3318 :    
3319 :     =item RETURN
3320 :    
3321 :     Returns a statistics object describing the rows deleted.
3322 :    
3323 :     =back
3324 :    
3325 :     =cut
3326 :     #: Return Type $%;
3327 :     sub DeleteGenome {
3328 :     # Get the parameters.
3329 :     my ($self, $genomeID, $testFlag) = @_;
3330 :     # Perform the delete for the genome's features.
3331 : parrello 1.58 my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3332 : parrello 1.53 # Perform the delete for the primary genome data.
3333 : parrello 1.58 my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3334 : parrello 1.53 $retVal->Accumulate($stats);
3335 :     # Return the result.
3336 :     return $retVal;
3337 :     }
3338 :    
3339 : parrello 1.1 =head2 Internal Utility Methods
3340 :    
3341 :     =head3 ParseAssignment
3342 :    
3343 :     Parse annotation text to determine whether or not it is a functional assignment. If it is,
3344 : parrello 1.19 the user, function text, and assigning user will be returned as a 3-element list. If it
3345 : parrello 1.11 isn't, an empty list will be returned.
3346 :    
3347 :     A functional assignment is always of the form
3348 :    
3349 : parrello 1.48 C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3350 : parrello 1.19
3351 : parrello 1.48 where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3352 :     the user and the assigning user (from MadeAnnotation) will be the same, but that is
3353 :     not always the case.
3354 : parrello 1.1
3355 : parrello 1.52 In addition, the functional role may contain extra data that is stripped, such as
3356 :     terminating spaces or a comment separated from the rest of the text by a tab.
3357 :    
3358 : parrello 1.1 This is a static method.
3359 :    
3360 :     =over 4
3361 :    
3362 : parrello 1.48 =item user
3363 :    
3364 :     Name of the assigning user.
3365 :    
3366 : parrello 1.1 =item text
3367 :    
3368 :     Text of the annotation.
3369 :    
3370 :     =item RETURN
3371 :    
3372 :     Returns an empty list if the annotation is not a functional assignment; otherwise, returns
3373 :     a two-element list containing the user name and the function text.
3374 :    
3375 :     =back
3376 :    
3377 :     =cut
3378 :    
3379 : parrello 1.11 sub _ParseAssignment {
3380 : parrello 1.15 # Get the parameters.
3381 : parrello 1.48 my ($user, $text) = @_;
3382 : parrello 1.15 # Declare the return value.
3383 :     my @retVal = ();
3384 :     # Check to see if this is a functional assignment.
3385 : parrello 1.43 my ($type, $function) = split(/\n/, $text);
3386 : parrello 1.48 if ($type =~ m/^set function to$/i) {
3387 :     # Here we have an assignment without a user, so we use the incoming user ID.
3388 :     @retVal = ($user, $function);
3389 :     } elsif ($type =~ m/^set (\S+) function to$/i) {
3390 : parrello 1.52 # Here we have an assignment with a user that is passed back to the caller.
3391 : parrello 1.43 @retVal = ($1, $function);
3392 : parrello 1.15 }
3393 : parrello 1.52 # If we have an assignment, we need to clean the function text. There may be
3394 :     # extra junk at the end added as a note from the user.
3395 :     if (@retVal) {
3396 :     $retVal[1] =~ s/(\t\S)?\s*$//;
3397 :     }
3398 : parrello 1.15 # Return the result list.
3399 :     return @retVal;
3400 : parrello 1.1 }
3401 :    
3402 : parrello 1.3 =head3 FriendlyTimestamp
3403 :    
3404 :     Convert a time number to a user-friendly time stamp for display.
3405 :    
3406 :     This is a static method.
3407 :    
3408 :     =over 4
3409 :    
3410 :     =item timeValue
3411 :    
3412 :     Numeric time value.
3413 :    
3414 :     =item RETURN
3415 :    
3416 :     Returns a string containing the same time in user-readable format.
3417 :    
3418 :     =back
3419 :    
3420 :     =cut
3421 :    
3422 :     sub FriendlyTimestamp {
3423 :     my ($timeValue) = @_;
3424 : parrello 1.40 my $retVal = localtime($timeValue);
3425 : parrello 1.3 return $retVal;
3426 :     }
3427 :    
3428 : parrello 1.15 =head3 AddProperty
3429 :    
3430 :     C<< my = $sprout->AddProperty($featureID, $key, $value, $url); >>
3431 :    
3432 :     Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3433 :     be added to almost any object. In Sprout, they can only be added to features. In
3434 :     Sprout, attributes are implemented using I<properties>. A property represents a key/value
3435 :     pair. If the particular key/value pair coming in is not already in the database, a new
3436 :     B<Property> record is created to hold it.
3437 :    
3438 :     =over 4
3439 :    
3440 :     =item peg
3441 :    
3442 :     ID of the feature to which the attribute is to be replied.
3443 :    
3444 :     =item key
3445 :    
3446 :     Name of the attribute (key).
3447 :    
3448 :     =item value
3449 :    
3450 :     Value of the attribute.
3451 :    
3452 :     =item url
3453 :    
3454 :     URL or text citation from which the property was obtained.
3455 :    
3456 :     =back
3457 :    
3458 :     =cut
3459 :     #: Return Type ;
3460 :     sub AddProperty {
3461 :     # Get the parameters.
3462 :     my ($self, $featureID, $key, $value, $url) = @_;
3463 :     # Declare the variable to hold the desired property ID.
3464 :     my $propID;
3465 :     # Attempt to find a property record for this key/value pair.
3466 :     my @properties = $self->GetFlat(['Property'],
3467 :     "Property(property-name) = ? AND Property(property-value) = ?",
3468 :     [$key, $value], 'Property(id)');
3469 :     if (@properties) {
3470 :     # Here the property is already in the database. We save its ID.
3471 :     $propID = $properties[0];
3472 :     # Here the property value does not exist. We need to generate an ID. It will be set
3473 :     # to a number one greater than the maximum value in the database. This call to
3474 :     # GetAll will stop after one record.
3475 :     my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3476 :     1);
3477 :     $propID = $maxProperty[0]->[0] + 1;
3478 :     # Insert the new property value.
3479 :     $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3480 :     }
3481 :     # Now we connect the incoming feature to the property.
3482 :     $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3483 :     }
3484 :    
3485 : parrello 1.51
3486 : parrello 1.48 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3