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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3