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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (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 : parrello 1.56 Return the genome that contains a specified feature or contig.
1531 : parrello 1.1
1532 :     =over 4
1533 :    
1534 :     =item featureID
1535 :    
1536 : parrello 1.56 ID of the feature or contig whose genome is desired.
1537 : parrello 1.1
1538 :     =item RETURN
1539 :    
1540 : parrello 1.56 Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
1541 :     found, returns an undefined value.
1542 : parrello 1.1
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 : parrello 1.56 # Create a query to find the genome associated with the incoming ID.
1551 :     my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1552 :     [$featureID, $featureID]);
1553 : parrello 1.15 # Declare the return value.
1554 :     my $retVal;
1555 :     # Get the genome ID.
1556 :     if (my $relationship = $query->Fetch()) {
1557 :     ($retVal) = $relationship->Value('HasContig(from-link)');
1558 :     }
1559 :     # Return the value found.
1560 :     return $retVal;
1561 : parrello 1.1 }
1562 :    
1563 :     =head3 CoupledFeatures
1564 :    
1565 :     C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>
1566 :    
1567 :     Return the features functionally coupled with a specified feature. Features are considered
1568 :     functionally coupled if they tend to be clustered on the same chromosome.
1569 :    
1570 :     =over 4
1571 :    
1572 :     =item featureID
1573 :    
1574 :     ID of the feature whose functionally-coupled brethren are desired.
1575 :    
1576 :     =item RETURN
1577 :    
1578 :     A hash mapping the functionally-coupled feature IDs to the coupling score.
1579 :    
1580 :     =back
1581 :    
1582 :     =cut
1583 : parrello 1.3 #: Return Type %;
1584 : parrello 1.1 sub CoupledFeatures {
1585 : parrello 1.15 # Get the parameters.
1586 :     my ($self, $featureID) = @_;
1587 :     # Create a query to retrieve the functionally-coupled features.
1588 :     my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1589 :     "ParticipatesInCoupling(from-link) = ?", [$featureID]);
1590 :     # This value will be set to TRUE if we find at least one coupled feature.
1591 :     my $found = 0;
1592 :     # Create the return hash.
1593 :     my %retVal = ();
1594 :     # Retrieve the relationship records and store them in the hash.
1595 :     while (my $clustering = $query->Fetch()) {
1596 :     # Get the ID and score of the coupling.
1597 :     my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1598 :     'Coupling(score)']);
1599 :     # The coupling ID contains the two feature IDs separated by a space. We use
1600 :     # this information to find the ID of the other feature.
1601 :     my ($fid1, $fid2) = split / /, $couplingID;
1602 :     my $otherFeatureID = ($featureID eq $fid1 ? $fid2 : $fid1);
1603 :     # Attach the other feature's score to its ID.
1604 :     $retVal{$otherFeatureID} = $score;
1605 :     $found = 1;
1606 :     }
1607 :     # Functional coupling is reflexive. If we found at least one coupled feature, we must add
1608 :     # the incoming feature as well.
1609 :     if ($found) {
1610 :     $retVal{$featureID} = 9999;
1611 :     }
1612 :     # Return the hash.
1613 :     return %retVal;
1614 :     }
1615 :    
1616 :     =head3 CouplingEvidence
1617 :    
1618 :     C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1619 :    
1620 :     Return the evidence for a functional coupling.
1621 :    
1622 :     A pair of features is considered evidence of a coupling between two other
1623 :     features if they occur close together on a contig and both are similar to
1624 :     the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1625 :     B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1626 :     B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1627 :     similar to B<A2>.
1628 :    
1629 :     The score of a coupling is determined by the number of pieces of evidence
1630 :     that are considered I<representative>. If several evidence items belong to
1631 :     a group of genomes that are close to each other, only one of those items
1632 :     is considered representative. The other evidence items are presumed to be
1633 :     there because of the relationship between the genomes rather than because
1634 :     the two proteins generated by the features have a related functionality.
1635 :    
1636 :     Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1637 :     I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1638 :     is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1639 :     and FALSE otherwise.
1640 :    
1641 :     =over 4
1642 :    
1643 :     =item peg1
1644 :    
1645 :     ID of the feature of interest.
1646 :    
1647 :     =item peg2
1648 :    
1649 :     ID of a feature functionally coupled to the feature of interest.
1650 :    
1651 :     =item RETURN
1652 :    
1653 :     Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1654 :     of interest, a feature similar to the functionally coupled feature, and a flag
1655 :     that is TRUE for a representative piece of evidence and FALSE otherwise.
1656 :    
1657 :     =back
1658 :    
1659 :     =cut
1660 :     #: Return Type @@;
1661 :     sub CouplingEvidence {
1662 :     # Get the parameters.
1663 :     my ($self, $peg1, $peg2) = @_;
1664 :     # Declare the return variable.
1665 :     my @retVal = ();
1666 : parrello 1.18 # Our first task is to find out the nature of the coupling: whether or not
1667 :     # it exists, its score, and whether the features are stored in the same
1668 :     # order as the ones coming in.
1669 : parrello 1.15 my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1670 :     # Only proceed if a coupling exists.
1671 :     if ($couplingID) {
1672 :     # Determine the ordering to place on the evidence items. If we're
1673 : parrello 1.18 # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1674 :     # we want feature 1 before feature 2 (normal).
1675 : parrello 1.21 Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1676 : parrello 1.15 my $ordering = ($inverted ? "DESC" : "");
1677 :     # Get the coupling evidence.
1678 :     my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1679 :     "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1680 :     [$couplingID],
1681 : overbeek 1.17 ['PCH(used)', 'UsesAsEvidence(to-link)']);
1682 : parrello 1.15 # Loop through the evidence items. Each piece of evidence is represented by two
1683 :     # positions in the evidence list, one for each feature on the other side of the
1684 :     # evidence link. If at some point we want to generalize to couplings with
1685 :     # more than two positions, this section of code will need to be re-done.
1686 :     while (@evidenceList > 0) {
1687 :     my $peg1Data = shift @evidenceList;
1688 :     my $peg2Data = shift @evidenceList;
1689 : parrello 1.21 Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1690 : parrello 1.15 push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1691 :     }
1692 : parrello 1.22 Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1693 : parrello 1.1 }
1694 : parrello 1.15 # Return the result.
1695 :     return @retVal;
1696 :     }
1697 :    
1698 :     =head3 GetCoupling
1699 :    
1700 :     C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1701 :    
1702 :     Return the coupling (if any) for the specified pair of PEGs. If a coupling
1703 :     exists, we return the coupling ID along with an indicator of whether the
1704 :     coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1705 :     In the second case, we say the coupling is I<inverted>. The importance of an
1706 :     inverted coupling is that the PEGs in the evidence will appear in reverse order.
1707 :    
1708 :     =over 4
1709 :    
1710 :     =item peg1
1711 :    
1712 :     ID of the feature of interest.
1713 :    
1714 :     =item peg2
1715 :    
1716 :     ID of the potentially coupled feature.
1717 :    
1718 :     =item RETURN
1719 :    
1720 :     Returns a three-element list. The first element contains the database ID of
1721 :     the coupling. The second element is FALSE if the coupling is stored in the
1722 :     database in the caller specified order and TRUE if it is stored in the
1723 :     inverted order. The third element is the coupling's score. If the coupling
1724 :     does not exist, all three list elements will be C<undef>.
1725 :    
1726 :     =back
1727 :    
1728 :     =cut
1729 :     #: Return Type $%@;
1730 :     sub GetCoupling {
1731 :     # Get the parameters.
1732 :     my ($self, $peg1, $peg2) = @_;
1733 :     # Declare the return values. We'll start with the coupling ID and undefine the
1734 :     # flag and score until we have more information.
1735 :     my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);
1736 :     # Find the coupling data.
1737 :     my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1738 :     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1739 : overbeek 1.17 [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1740 : parrello 1.15 # Check to see if we found anything.
1741 :     if (!@pegs) {
1742 : parrello 1.23 Trace("No coupling found.") if T(Coupling => 4);
1743 : parrello 1.15 # No coupling, so undefine the return value.
1744 :     $retVal = undef;
1745 :     } else {
1746 :     # We have a coupling! Get the score and check for inversion.
1747 :     $score = $pegs[0]->[1];
1748 : parrello 1.23 my $firstFound = $pegs[0]->[0];
1749 :     $inverted = ($firstFound ne $peg1);
1750 :     Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1751 : parrello 1.15 }
1752 :     # Return the result.
1753 :     return ($retVal, $inverted, $score);
1754 :     }
1755 :    
1756 :     =head3 CouplingID
1757 :    
1758 :     C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>
1759 :    
1760 :     Return the coupling ID for a pair of feature IDs.
1761 :    
1762 :     The coupling ID is currently computed by joining the feature IDs in
1763 :     sorted order with a space. Client modules (that is, modules which
1764 :     use Sprout) should not, however, count on this always being the
1765 :     case. This method provides a way for abstracting the concept of a
1766 :     coupling ID. All that we know for sure about it is that it can be
1767 :     generated easily from the feature IDs and the order of the IDs
1768 :     in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1769 :     will have the same value as C<CouplingID("b1", "a1")>.
1770 :    
1771 :     =over 4
1772 :    
1773 :     =item peg1
1774 :    
1775 :     First feature of interest.
1776 :    
1777 :     =item peg2
1778 :    
1779 :     Second feature of interest.
1780 :    
1781 :     =item RETURN
1782 :    
1783 :     Returns the ID that would be used to represent a functional coupling of
1784 :     the two specified PEGs.
1785 :    
1786 :     =back
1787 :    
1788 :     =cut
1789 :     #: Return Type $;
1790 :     sub CouplingID {
1791 :     return join " ", sort @_;
1792 : parrello 1.1 }
1793 :    
1794 :     =head3 GetEntityTypes
1795 :    
1796 :     C<< my @entityList = $sprout->GetEntityTypes(); >>
1797 :    
1798 :     Return the list of supported entity types.
1799 :    
1800 :     =cut
1801 : parrello 1.3 #: Return Type @;
1802 : parrello 1.1 sub GetEntityTypes {
1803 : parrello 1.15 # Get the parameters.
1804 :     my ($self) = @_;
1805 :     # Get the underlying database object.
1806 :     my $erdb = $self->{_erdb};
1807 :     # Get its entity type list.
1808 :     my @retVal = $erdb->GetEntityTypes();
1809 : parrello 1.1 }
1810 :    
1811 :     =head3 ReadFasta
1812 :    
1813 :     C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>
1814 :    
1815 :     Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
1816 :     one or more lines of data. The first line begins with a > character and contains an ID.
1817 :     The remaining lines contain the sequence data in order.
1818 :    
1819 :     =over 4
1820 :    
1821 :     =item fileName
1822 :    
1823 :     Name of the FASTA file.
1824 :    
1825 :     =item prefix (optional)
1826 :    
1827 :     Prefix to be put in front of each ID found.
1828 :    
1829 :     =item RETURN
1830 :    
1831 :     Returns a hash that maps each ID to its sequence.
1832 :    
1833 :     =back
1834 :    
1835 :     =cut
1836 : parrello 1.3 #: Return Type %;
1837 : parrello 1.1 sub ReadFasta {
1838 : parrello 1.15 # Get the parameters.
1839 :     my ($fileName, $prefix) = @_;
1840 :     # Create the return hash.
1841 :     my %retVal = ();
1842 :     # Open the file for input.
1843 :     open FASTAFILE, '<', $fileName;
1844 :     # Declare the ID variable and clear the sequence accumulator.
1845 :     my $sequence = "";
1846 :     my $id = "";
1847 :     # Loop through the file.
1848 :     while (<FASTAFILE>) {
1849 :     # Get the current line.
1850 :     my $line = $_;
1851 :     # Check for a header line.
1852 :     if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
1853 :     # Here we have a new header. Store the current sequence if we have one.
1854 :     if ($id) {
1855 : parrello 1.24 $retVal{$id} = lc $sequence;
1856 : parrello 1.15 }
1857 :     # Clear the sequence accumulator and save the new ID.
1858 :     ($id, $sequence) = ("$prefix$1", "");
1859 :     } else {
1860 :     # Here we have a data line, so we add it to the sequence accumulator.
1861 : parrello 1.24 # First, we get the actual data out. Note that we normalize to lower
1862 : parrello 1.15 # case.
1863 :     $line =~ /^\s*(.*?)(\s|\n)/;
1864 :     $sequence .= $1;
1865 :     }
1866 :     }
1867 :     # Flush out the last sequence (if any).
1868 :     if ($sequence) {
1869 : parrello 1.24 $retVal{$id} = lc $sequence;
1870 : parrello 1.15 }
1871 :     # Close the file.
1872 :     close FASTAFILE;
1873 :     # Return the hash constructed from the file.
1874 :     return %retVal;
1875 : parrello 1.1 }
1876 :    
1877 :     =head3 FormatLocations
1878 :    
1879 :     C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>
1880 :    
1881 :     Insure that a list of feature locations is in the Sprout format. The Sprout feature location
1882 :     format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward
1883 : parrello 1.10 gene. The old format is I<contig>_I<beg>_I<end>. If a feature is in the new format already,
1884 : parrello 1.19 it will not be changed; otherwise, it will be converted. This method can also be used to
1885 : parrello 1.10 perform the reverse task-- insuring that all the locations are in the old format.
1886 : parrello 1.1
1887 :     =over 4
1888 :    
1889 :     =item prefix
1890 :    
1891 :     Prefix to be put in front of each contig ID (or an empty string if the contig ID should not
1892 :     be changed.
1893 :    
1894 :     =item locations
1895 :    
1896 :     List of locations to be normalized.
1897 :    
1898 :     =item oldFormat
1899 :    
1900 :     TRUE to convert the locations to the old format, else FALSE
1901 :    
1902 :     =item RETURN
1903 :    
1904 :     Returns a list of updated location descriptors.
1905 :    
1906 :     =back
1907 :    
1908 :     =cut
1909 : parrello 1.3 #: Return Type @;
1910 : parrello 1.1 sub FormatLocations {
1911 : parrello 1.15 # Get the parameters.
1912 :     my ($self, $prefix, $locations, $oldFormat) = @_;
1913 :     # Create the return list.
1914 :     my @retVal = ();
1915 :     # Check to see if any locations were passed in.
1916 :     if ($locations eq '') {
1917 :     Confess("No locations specified.");
1918 :     } else {
1919 :     # Loop through the locations, converting them to the new format.
1920 :     for my $location (@{$locations}) {
1921 :     # Parse the location elements.
1922 :     my ($contig, $beg, $dir, $len) = ParseLocation($location);
1923 :     # Process according to the desired output format.
1924 :     if (!$oldFormat) {
1925 :     # Here we're producing the new format. Add the location to the return list.
1926 :     push @retVal, "$prefix${contig}_$beg$dir$len";
1927 :     } elsif ($dir eq '+') {
1928 :     # Here we're producing the old format and it's a forward gene.
1929 :     my $end = $beg + $len - 1;
1930 :     push @retVal, "$prefix${contig}_${beg}_$end";
1931 :     } else {
1932 :     # Here we're producting the old format and it's a backward gene.
1933 :     my $end = $beg - $len + 1;
1934 :     push @retVal, "$prefix${contig}_${beg}_$end";
1935 :     }
1936 :     }
1937 :     }
1938 :     # Return the normalized list.
1939 :     return @retVal;
1940 : parrello 1.1 }
1941 :    
1942 :     =head3 DumpData
1943 :    
1944 :     C<< $sprout->DumpData(); >>
1945 :    
1946 :     Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.
1947 :    
1948 :     =cut
1949 :    
1950 :     sub DumpData {
1951 : parrello 1.15 # Get the parameters.
1952 :     my ($self) = @_;
1953 :     # Get the data directory name.
1954 :     my $outputDirectory = $self->{_options}->{dataDir};
1955 :     # Dump the relations.
1956 :     $self->{_erdb}->DumpRelations($outputDirectory);
1957 : parrello 1.1 }
1958 :    
1959 :     =head3 XMLFileName
1960 :    
1961 :     C<< my $fileName = $sprout->XMLFileName(); >>
1962 :    
1963 :     Return the name of this database's XML definition file.
1964 :    
1965 :     =cut
1966 : parrello 1.3 #: Return Type $;
1967 : parrello 1.1 sub XMLFileName {
1968 : parrello 1.15 my ($self) = @_;
1969 :     return $self->{_xmlName};
1970 : parrello 1.1 }
1971 :    
1972 :     =head3 Insert
1973 :    
1974 :     C<< $sprout->Insert($objectType, \%fieldHash); >>
1975 :    
1976 :     Insert an entity or relationship instance into the database. The entity or relationship of interest
1977 :     is defined by a type name and then a hash of field names to values. Field values in the primary
1978 :     relation are represented by scalars. (Note that for relationships, the primary relation is
1979 :     the B<only> relation.) Field values for the other relations comprising the entity are always
1980 :     list references. For example, the following line inserts an inactive PEG feature named
1981 :     C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.
1982 :    
1983 :     C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>
1984 :    
1985 :     The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1986 :     property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1987 :    
1988 : 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'}); >>
1989 : parrello 1.1
1990 :     =over 4
1991 :    
1992 :     =item newObjectType
1993 :    
1994 :     Type name of the entity or relationship to insert.
1995 :    
1996 :     =item fieldHash
1997 :    
1998 :     Hash of field names to values.
1999 :    
2000 :     =back
2001 :    
2002 :     =cut
2003 : parrello 1.3 #: Return Type ;
2004 : parrello 1.1 sub Insert {
2005 : parrello 1.15 # Get the parameters.
2006 :     my ($self, $objectType, $fieldHash) = @_;
2007 :     # Call the underlying method.
2008 :     $self->{_erdb}->InsertObject($objectType, $fieldHash);
2009 : parrello 1.1 }
2010 :    
2011 :     =head3 Annotate
2012 :    
2013 :     C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>
2014 :    
2015 :     Annotate a feature. This inserts an Annotation record into the database and links it to the
2016 :     specified feature and user.
2017 :    
2018 :     =over 4
2019 :    
2020 :     =item fid
2021 :    
2022 :     ID of the feature to be annotated.
2023 :    
2024 :     =item timestamp
2025 :    
2026 :     Numeric timestamp to apply to the annotation. This is concatenated to the feature ID to create the
2027 :     key.
2028 :    
2029 :     =item user
2030 :    
2031 :     ID of the user who is making the annotation.
2032 :    
2033 :     =item text
2034 :    
2035 :     Text of the annotation.
2036 :    
2037 :     =item RETURN
2038 :    
2039 :     Returns 1 if successful, 2 if an error occurred.
2040 :    
2041 :     =back
2042 :    
2043 :     =cut
2044 : parrello 1.3 #: Return Type $;
2045 : parrello 1.1 sub Annotate {
2046 : parrello 1.15 # Get the parameters.
2047 :     my ($self, $fid, $timestamp, $user, $text) = @_;
2048 :     # Create the annotation ID.
2049 :     my $aid = "$fid:$timestamp";
2050 :     # Insert the Annotation object.
2051 :     my $retVal = $self->Insert('Annotation', { id => $aid, time => $timestamp, annotation => $text });
2052 :     if ($retVal) {
2053 :     # Connect it to the user.
2054 :     $retVal = $self->Insert('MadeAnnotation', { 'from-link' => $user, 'to-link' => $aid });
2055 :     if ($retVal) {
2056 :     # Connect it to the feature.
2057 :     $retVal = $self->Insert('IsTargetOfAnnotation', { 'from-link' => $fid,
2058 :     'to-link' => $aid });
2059 :     }
2060 :     }
2061 :     # Return the success indicator.
2062 :     return $retVal;
2063 : parrello 1.1 }
2064 :    
2065 :     =head3 AssignFunction
2066 :    
2067 : parrello 1.11 C<< my $ok = $sprout->AssignFunction($featureID, $user, $function, $assigningUser); >>
2068 : parrello 1.1
2069 :     This method assigns a function to a feature. Functions are a special type of annotation. The general
2070 : parrello 1.11 format is described in L</ParseAssignment>.
2071 : parrello 1.1
2072 :     =over 4
2073 :    
2074 :     =item featureID
2075 :    
2076 :     ID of the feature to which the assignment is being made.
2077 :    
2078 :     =item user
2079 :    
2080 : parrello 1.11 Name of the user group making the assignment, such as C<kegg> or C<fig>.
2081 : parrello 1.1
2082 :     =item function
2083 :    
2084 :     Text of the function being assigned.
2085 :    
2086 : parrello 1.11 =item assigningUser (optional)
2087 :    
2088 :     Name of the individual user making the assignment. If omitted, defaults to the user group.
2089 :    
2090 : parrello 1.1 =item RETURN
2091 :    
2092 :     Returns 1 if successful, 0 if an error occurred.
2093 :    
2094 :     =back
2095 :    
2096 :     =cut
2097 : parrello 1.3 #: Return Type $;
2098 : parrello 1.1 sub AssignFunction {
2099 : parrello 1.15 # Get the parameters.
2100 :     my ($self, $featureID, $user, $function, $assigningUser) = @_;
2101 : parrello 1.11 # Default the assigning user.
2102 :     if (! $assigningUser) {
2103 :     $assigningUser = $user;
2104 :     }
2105 : parrello 1.15 # Create an annotation string from the parameters.
2106 :     my $annotationText = "$assigningUser\nset $user function to\n$function";
2107 :     # Get the current time.
2108 :     my $now = time;
2109 :     # Declare the return variable.
2110 :     my $retVal = 1;
2111 :     # Locate the genome containing the feature.
2112 :     my $genome = $self->GenomeOf($featureID);
2113 :     if (!$genome) {
2114 :     # Here the genome was not found. This probably means the feature ID is invalid.
2115 :     Trace("No genome found for feature $featureID.") if T(0);
2116 :     $retVal = 0;
2117 :     } else {
2118 :     # Here we know we have a feature with a genome. Store the annotation.
2119 : parrello 1.5 $retVal = $self->Annotate($featureID, $now, $user, $annotationText);
2120 : parrello 1.15 }
2121 :     # Return the success indicator.
2122 :     return $retVal;
2123 : parrello 1.1 }
2124 :    
2125 :     =head3 FeaturesByAlias
2126 :    
2127 :     C<< my @features = $sprout->FeaturesByAlias($alias); >>
2128 :    
2129 :     Returns a list of features with the specified alias. The alias is parsed to determine
2130 :     the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
2131 :     alphanumerics is a UniProt ID. A built-in FIG.pm method is used to analyze the alias
2132 :     string and attach the necessary prefix. If the result is a FIG ID then it is returned
2133 :     unmodified; otherwise, we look for an alias.
2134 :    
2135 :     =over 4
2136 :    
2137 :     =item alias
2138 :    
2139 :     Alias whose features are desired.
2140 :    
2141 :     =item RETURN
2142 :    
2143 :     Returns a list of the features with the given alias.
2144 :    
2145 :     =back
2146 :    
2147 :     =cut
2148 : parrello 1.3 #: Return Type @;
2149 : parrello 1.1 sub FeaturesByAlias {
2150 : parrello 1.15 # Get the parameters.
2151 :     my ($self, $alias) = @_;
2152 :     # Declare the return variable.
2153 :     my @retVal = ();
2154 :     # Parse the alias.
2155 :     my ($mappedAlias, $flag) = FIGRules::NormalizeAlias($alias);
2156 :     # If it's a FIG alias, we're done.
2157 :     if ($flag) {
2158 :     push @retVal, $mappedAlias;
2159 :     } else {
2160 :     # Here we have a non-FIG alias. Get the features with the normalized alias.
2161 :     @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');
2162 :     }
2163 :     # Return the result.
2164 :     return @retVal;
2165 : parrello 1.1 }
2166 :    
2167 :     =head3 Exists
2168 :    
2169 :     C<< my $found = $sprout->Exists($entityName, $entityID); >>
2170 :    
2171 :     Return TRUE if an entity exists, else FALSE.
2172 :    
2173 :     =over 4
2174 :    
2175 :     =item entityName
2176 :    
2177 :     Name of the entity type (e.g. C<Feature>) relevant to the existence check.
2178 :    
2179 :     =item entityID
2180 :    
2181 :     ID of the entity instance whose existence is to be checked.
2182 :    
2183 :     =item RETURN
2184 :    
2185 :     Returns TRUE if the entity instance exists, else FALSE.
2186 :    
2187 :     =back
2188 :    
2189 :     =cut
2190 : parrello 1.3 #: Return Type $;
2191 : parrello 1.1 sub Exists {
2192 : parrello 1.15 # Get the parameters.
2193 :     my ($self, $entityName, $entityID) = @_;
2194 :     # Check for the entity instance.
2195 : parrello 1.26 Trace("Checking existence of $entityName with ID=$entityID.") if T(4);
2196 : parrello 1.15 my $testInstance = $self->GetEntity($entityName, $entityID);
2197 :     # Return an existence indicator.
2198 :     my $retVal = ($testInstance ? 1 : 0);
2199 :     return $retVal;
2200 : parrello 1.1 }
2201 :    
2202 :     =head3 FeatureTranslation
2203 :    
2204 :     C<< my $translation = $sprout->FeatureTranslation($featureID); >>
2205 :    
2206 :     Return the translation of a feature.
2207 :    
2208 :     =over 4
2209 :    
2210 :     =item featureID
2211 :    
2212 :     ID of the feature whose translation is desired
2213 :    
2214 :     =item RETURN
2215 :    
2216 :     Returns the translation of the specified feature.
2217 :    
2218 :     =back
2219 :    
2220 :     =cut
2221 : parrello 1.3 #: Return Type $;
2222 : parrello 1.1 sub FeatureTranslation {
2223 : parrello 1.15 # Get the parameters.
2224 :     my ($self, $featureID) = @_;
2225 :     # Get the specified feature's translation.
2226 :     my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
2227 :     return $retVal;
2228 : parrello 1.1 }
2229 :    
2230 :     =head3 Taxonomy
2231 :    
2232 :     C<< my @taxonomyList = $sprout->Taxonomy($genome); >>
2233 :    
2234 :     Return the taxonomy of the specified genome. This will be in the form of a list
2235 :     containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,
2236 :     or C<Eukaryote>) to sub-species. For example,
2237 :    
2238 :     C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>
2239 :    
2240 :     =over 4
2241 :    
2242 :     =item genome
2243 :    
2244 :     ID of the genome whose taxonomy is desired.
2245 :    
2246 :     =item RETURN
2247 :    
2248 :     Returns a list containing all the taxonomy classifications for the specified genome's organism.
2249 :    
2250 :     =back
2251 :    
2252 :     =cut
2253 : parrello 1.3 #: Return Type @;
2254 : parrello 1.1 sub Taxonomy {
2255 : parrello 1.15 # Get the parameters.
2256 :     my ($self, $genome) = @_;
2257 :     # Find the specified genome's taxonomy string.
2258 :     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
2259 :     # Declare the return variable.
2260 :     my @retVal = ();
2261 :     # If we found the genome, return its taxonomy string.
2262 :     if ($list) {
2263 :     @retVal = split /\s*;\s*/, $list;
2264 :     } else {
2265 :     Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
2266 :     }
2267 :     # Return the value found.
2268 :     return @retVal;
2269 : parrello 1.1 }
2270 :    
2271 :     =head3 CrudeDistance
2272 :    
2273 :     C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>
2274 :    
2275 :     Returns a crude estimate of the distance between two genomes. The distance is construed so
2276 :     that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
2277 :    
2278 :     =over 4
2279 :    
2280 :     =item genome1
2281 :    
2282 :     ID of the first genome to compare.
2283 :    
2284 :     =item genome2
2285 :    
2286 :     ID of the second genome to compare.
2287 :    
2288 :     =item RETURN
2289 :    
2290 :     Returns a value from 0 to 1, with 0 meaning identical organisms, and 1 meaning organisms from
2291 :     different domains.
2292 :    
2293 :     =back
2294 :    
2295 :     =cut
2296 : parrello 1.3 #: Return Type $;
2297 : parrello 1.1 sub CrudeDistance {
2298 : parrello 1.15 # Get the parameters.
2299 :     my ($self, $genome1, $genome2) = @_;
2300 :     # Insure that the distance is commutative by sorting the genome IDs.
2301 :     my ($genomeA, $genomeB);
2302 :     if ($genome2 < $genome2) {
2303 :     ($genomeA, $genomeB) = ($genome1, $genome2);
2304 :     } else {
2305 :     ($genomeA, $genomeB) = ($genome2, $genome1);
2306 :     }
2307 :     my @taxA = $self->Taxonomy($genomeA);
2308 :     my @taxB = $self->Taxonomy($genomeB);
2309 :     # Initialize the distance to 1. We'll reduce it each time we find a match between the
2310 :     # taxonomies.
2311 :     my $retVal = 1.0;
2312 :     # Initialize the subtraction amount. This amount determines the distance reduction caused
2313 :     # by a mismatch at the current level.
2314 :     my $v = 0.5;
2315 :     # Loop through the taxonomies.
2316 :     for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) {
2317 :     $retVal -= $v;
2318 :     $v /= 2;
2319 :     }
2320 : parrello 1.1 return $retVal;
2321 :     }
2322 :    
2323 :     =head3 RoleName
2324 :    
2325 :     C<< my $roleName = $sprout->RoleName($roleID); >>
2326 :    
2327 :     Return the descriptive name of the role with the specified ID. In general, a role
2328 :     will only have a descriptive name if it is coded as an EC number.
2329 :    
2330 :     =over 4
2331 :    
2332 :     =item roleID
2333 :    
2334 :     ID of the role whose description is desired.
2335 :    
2336 :     =item RETURN
2337 :    
2338 :     Returns the descriptive name of the desired role.
2339 :    
2340 :     =back
2341 :    
2342 :     =cut
2343 : parrello 1.3 #: Return Type $;
2344 : parrello 1.1 sub RoleName {
2345 : parrello 1.15 # Get the parameters.
2346 :     my ($self, $roleID) = @_;
2347 :     # Get the specified role's name.
2348 :     my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
2349 :     # Use the ID if the role has no name.
2350 :     if (!$retVal) {
2351 :     $retVal = $roleID;
2352 :     }
2353 :     # Return the name.
2354 :     return $retVal;
2355 : parrello 1.1 }
2356 :    
2357 :     =head3 RoleDiagrams
2358 :    
2359 :     C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>
2360 :    
2361 :     Return a list of the diagrams containing a specified functional role.
2362 :    
2363 :     =over 4
2364 :    
2365 :     =item roleID
2366 :    
2367 :     ID of the role whose diagrams are desired.
2368 :    
2369 :     =item RETURN
2370 :    
2371 :     Returns a list of the IDs for the diagrams that contain the specified functional role.
2372 :    
2373 :     =back
2374 :    
2375 :     =cut
2376 : parrello 1.3 #: Return Type @;
2377 : parrello 1.1 sub RoleDiagrams {
2378 : parrello 1.15 # Get the parameters.
2379 :     my ($self, $roleID) = @_;
2380 :     # Query for the diagrams.
2381 :     my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2382 :     'RoleOccursIn(to-link)');
2383 :     # Return the result.
2384 :     return @retVal;
2385 : parrello 1.1 }
2386 :    
2387 : parrello 1.19 =head3 GetProperties
2388 :    
2389 :     C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2390 :    
2391 :     Return a list of the properties with the specified characteristics.
2392 :    
2393 :     Properties are arbitrary key-value pairs associated with a feature. (At some point they
2394 :     will also be associated with genomes.) A property value is represented by a 4-tuple of
2395 :     the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2396 :    
2397 :     =over 4
2398 :    
2399 :     =item fid
2400 :    
2401 :     ID of the feature possessing the property.
2402 :    
2403 :     =item key
2404 :    
2405 :     Name or key of the property.
2406 :    
2407 :     =item value
2408 :    
2409 :     Value of the property.
2410 :    
2411 :     =item url
2412 :    
2413 :     URL of the document that indicated the property should have this particular value, or an
2414 :     empty string if no such document exists.
2415 :    
2416 :     =back
2417 :    
2418 :     The parameters act as a filter for the desired data. Any non-null parameter will
2419 :     automatically match all the tuples returned. So, specifying just the I<$fid> will
2420 :     return all the properties of the specified feature; similarly, specifying the I<$key>
2421 :     and I<$value> parameters will return all the features having the specified property
2422 :     value.
2423 :    
2424 :     A single property key can have many values, representing different ideas about the
2425 :     feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2426 :     virulent, and another may declare that it is not virulent. A query about the virulence of
2427 :     C<fig|83333.1.peg.10> would be coded as
2428 :    
2429 :     my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2430 :    
2431 :     Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2432 :     not to be filtered. The tuples returned would be
2433 :    
2434 :     ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2435 :     ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2436 :    
2437 :     =cut
2438 :     #: Return Type @@;
2439 :     sub GetProperties {
2440 :     # Get the parameters.
2441 :     my ($self, @parms) = @_;
2442 :     # Declare the return variable.
2443 :     my @retVal = ();
2444 :     # Now we need to create a WHERE clause that will get us the data we want. First,
2445 :     # we create a list of the columns containing the data for each parameter.
2446 :     my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2447 :     'Property(property-value)', 'HasProperty(evidence)');
2448 :     # Now we build the WHERE clause and the list of parameter values.
2449 :     my @where = ();
2450 :     my @values = ();
2451 :     for (my $i = 0; $i <= $#colNames; $i++) {
2452 :     my $parm = $parms[$i];
2453 :     if (defined $parm && ($parm ne '')) {
2454 :     push @where, "$colNames[$i] = ?";
2455 :     push @values, $parm;
2456 :     }
2457 :     }
2458 :     # Format the WHERE clause.
2459 :     my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2460 :     # Ask for all the propertie values with the desired characteristics.
2461 :     my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2462 :     while (my $valueObject = $query->Fetch()) {
2463 :     my @tuple = $valueObject->Values(\@colNames);
2464 :     push @retVal, \@tuple;
2465 :     }
2466 :     # Return the result.
2467 :     return @retVal;
2468 :     }
2469 :    
2470 : parrello 1.1 =head3 FeatureProperties
2471 :    
2472 :     C<< my @properties = $sprout->FeatureProperties($featureID); >>
2473 :    
2474 :     Return a list of the properties for the specified feature. Properties are key-value pairs
2475 :     that specify special characteristics of the feature. For example, a property could indicate
2476 :     that a feature is essential to the survival of the organism or that it has benign influence
2477 :     on the activities of a pathogen. Each property is returned as a triple of the form
2478 :     C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly
2479 :     a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing
2480 :     the web address or citation in which the property's value for the feature was identified.
2481 :    
2482 :     =over 4
2483 :    
2484 :     =item featureID
2485 :    
2486 :     ID of the feature whose properties are desired.
2487 :    
2488 :     =item RETURN
2489 :    
2490 :     Returns a list of triples, each triple containing the property name, its value, and a URL or
2491 :     citation.
2492 :    
2493 :     =back
2494 :    
2495 :     =cut
2496 : parrello 1.3 #: Return Type @@;
2497 : parrello 1.1 sub FeatureProperties {
2498 : parrello 1.15 # Get the parameters.
2499 :     my ($self, $featureID) = @_;
2500 :     # Get the properties.
2501 :     my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],
2502 :     ['Property(property-name)', 'Property(property-value)',
2503 :     'HasProperty(evidence)']);
2504 :     # Return the resulting list.
2505 :     return @retVal;
2506 : parrello 1.1 }
2507 :    
2508 :     =head3 DiagramName
2509 :    
2510 :     C<< my $diagramName = $sprout->DiagramName($diagramID); >>
2511 :    
2512 :     Return the descriptive name of a diagram.
2513 :    
2514 :     =over 4
2515 :    
2516 :     =item diagramID
2517 :    
2518 :     ID of the diagram whose description is desired.
2519 :    
2520 :     =item RETURN
2521 :    
2522 :     Returns the descripive name of the specified diagram.
2523 :    
2524 :     =back
2525 :    
2526 :     =cut
2527 : parrello 1.3 #: Return Type $;
2528 : parrello 1.1 sub DiagramName {
2529 : parrello 1.15 # Get the parameters.
2530 :     my ($self, $diagramID) = @_;
2531 :     # Get the specified diagram's name and return it.
2532 :     my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
2533 :     return $retVal;
2534 : parrello 1.1 }
2535 :    
2536 :     =head3 MergedAnnotations
2537 :    
2538 :     C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
2539 :    
2540 :     Returns a merged list of the annotations for the features in a list. Each annotation is
2541 :     represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
2542 :     C<$fid> is the ID of a feature, C<$timestamp> is the time at which the annotation was made,
2543 :     C<$userID> is the ID of the user who made the annotation, and C<$annotation> is the annotation
2544 :     text. The list is sorted by timestamp.
2545 :    
2546 :     =over 4
2547 :    
2548 :     =item list
2549 :    
2550 :     List of the IDs for the features whose annotations are desired.
2551 :    
2552 :     =item RETURN
2553 :    
2554 :     Returns a list of annotation descriptions sorted by the annotation time.
2555 :    
2556 :     =back
2557 :    
2558 :     =cut
2559 : parrello 1.3 #: Return Type @;
2560 : parrello 1.1 sub MergedAnnotations {
2561 : parrello 1.15 # Get the parameters.
2562 :     my ($self, $list) = @_;
2563 :     # Create a list to hold the annotation tuples found.
2564 :     my @tuples = ();
2565 :     # Loop through the features in the input list.
2566 :     for my $fid (@{$list}) {
2567 :     # Create a list of this feature's annotation tuples.
2568 :     my @newTuples = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
2569 :     "IsTargetOfAnnotation(from-link) = ?", [$fid],
2570 :     ['IsTargetOfAnnotation(from-link)', 'Annotation(time)',
2571 :     'MadeAnnotation(from-link)', 'Annotation(annotation)']);
2572 :     # Put it in the result list.
2573 :     push @tuples, @newTuples;
2574 :     }
2575 :     # Sort the result list by timestamp.
2576 :     my @retVal = sort { $a->[1] <=> $b->[1] } @tuples;
2577 : parrello 1.3 # Loop through and make the time stamps friendly.
2578 :     for my $tuple (@retVal) {
2579 :     $tuple->[1] = FriendlyTimestamp($tuple->[1]);
2580 :     }
2581 : parrello 1.15 # Return the sorted list.
2582 :     return @retVal;
2583 : parrello 1.1 }
2584 :    
2585 :     =head3 RoleNeighbors
2586 :    
2587 :     C<< my @roleList = $sprout->RoleNeighbors($roleID); >>
2588 :    
2589 :     Returns a list of the roles that occur in the same diagram as the specified role. Because
2590 :     diagrams and roles are in a many-to-many relationship with each other, the list is
2591 :     essentially the set of roles from all of the maps that contain the incoming role. Such
2592 :     roles are considered neighbors because they are used together in cellular subsystems.
2593 :    
2594 :     =over 4
2595 :    
2596 :     =item roleID
2597 :    
2598 :     ID of the role whose neighbors are desired.
2599 :    
2600 :     =item RETURN
2601 :    
2602 :     Returns a list containing the IDs of the roles that are related to the incoming role.
2603 :    
2604 :     =back
2605 :    
2606 :     =cut
2607 : parrello 1.3 #: Return Type @;
2608 : parrello 1.1 sub RoleNeighbors {
2609 : parrello 1.15 # Get the parameters.
2610 :     my ($self, $roleID) = @_;
2611 :     # Get all the diagrams containing this role.
2612 :     my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2613 :     'RoleOccursIn(to-link)');
2614 :     # Create the return list.
2615 :     my @retVal = ();
2616 :     # Loop through the diagrams.
2617 :     for my $diagramID (@diagrams) {
2618 :     # Get all the roles in this diagram.
2619 :     my @roles = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(to-link) = ?", [$diagramID],
2620 :     'RoleOccursIn(from-link)');
2621 :     # Add them to the return list.
2622 :     push @retVal, @roles;
2623 :     }
2624 :     # Merge the duplicates from the list.
2625 :     return Tracer::Merge(@retVal);
2626 : parrello 1.1 }
2627 :    
2628 :     =head3 FeatureLinks
2629 :    
2630 :     C<< my @links = $sprout->FeatureLinks($featureID); >>
2631 :    
2632 :     Return a list of the web hyperlinks associated with a feature. The web hyperlinks are
2633 :     to external websites describing either the feature itself or the organism containing it
2634 :     and are represented in raw HTML.
2635 :    
2636 :     =over 4
2637 :    
2638 :     =item featureID
2639 :    
2640 :     ID of the feature whose links are desired.
2641 :    
2642 :     =item RETURN
2643 :    
2644 :     Returns a list of the web links for this feature.
2645 :    
2646 :     =back
2647 :    
2648 :     =cut
2649 : parrello 1.3 #: Return Type @;
2650 : parrello 1.1 sub FeatureLinks {
2651 : parrello 1.15 # Get the parameters.
2652 :     my ($self, $featureID) = @_;
2653 :     # Get the feature's links.
2654 :     my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
2655 :     # Return the feature's links.
2656 :     return @retVal;
2657 : parrello 1.1 }
2658 :    
2659 :     =head3 SubsystemsOf
2660 :    
2661 :     C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2662 :    
2663 :     Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
2664 : parrello 1.20 to the roles the feature performs.
2665 : parrello 1.1
2666 :     =over 4
2667 :    
2668 :     =item featureID
2669 :    
2670 :     ID of the feature whose subsystems are desired.
2671 :    
2672 :     =item RETURN
2673 :    
2674 : parrello 1.20 Returns a hash mapping all the feature's subsystems to a list of the feature's roles.
2675 : parrello 1.1
2676 :     =back
2677 :    
2678 :     =cut
2679 : parrello 1.20 #: Return Type %@;
2680 : parrello 1.1 sub SubsystemsOf {
2681 : parrello 1.15 # Get the parameters.
2682 :     my ($self, $featureID) = @_;
2683 : parrello 1.16 # Get the subsystem list.
2684 : parrello 1.15 my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2685 :     "ContainsFeature(to-link) = ?", [$featureID],
2686 :     ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2687 :     # Create the return value.
2688 :     my %retVal = ();
2689 : parrello 1.42 # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2690 :     # in two spreadsheet cells.
2691 :     my %dupHash = ();
2692 : parrello 1.15 # Loop through the results, adding them to the hash.
2693 :     for my $record (@subsystems) {
2694 : parrello 1.42 # Get this subsystem and role.
2695 : parrello 1.20 my ($subsys, $role) = @{$record};
2696 : parrello 1.42 # Insure it's the first time for both.
2697 :     my $dupKey = "$subsys\n$role";
2698 :     if (! exists $dupHash{"$subsys\n$role"}) {
2699 :     $dupHash{$dupKey} = 1;
2700 : parrello 1.20 push @{$retVal{$subsys}}, $role;
2701 :     }
2702 : parrello 1.15 }
2703 :     # Return the hash.
2704 :     return %retVal;
2705 : parrello 1.1 }
2706 :    
2707 : parrello 1.16 =head3 SubsystemList
2708 :    
2709 :     C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2710 :    
2711 :     Return a list containing the names of the subsystems in which the specified
2712 :     feature participates. Unlike L</SubsystemsOf>, this method only returns the
2713 :     subsystem names, not the roles.
2714 :    
2715 :     =over 4
2716 :    
2717 :     =item featureID
2718 :    
2719 :     ID of the feature whose subsystem names are desired.
2720 :    
2721 :     =item RETURN
2722 :    
2723 :     Returns a list of the names of the subsystems in which the feature participates.
2724 :    
2725 :     =back
2726 :    
2727 :     =cut
2728 :     #: Return Type @;
2729 :     sub SubsystemList {
2730 :     # Get the parameters.
2731 :     my ($self, $featureID) = @_;
2732 :     # Get the list of names.
2733 :     my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2734 :     [$featureID], 'HasSSCell(from-link)');
2735 :     # Return the result.
2736 :     return @retVal;
2737 :     }
2738 :    
2739 : parrello 1.56
2740 :    
2741 : parrello 1.1 =head3 RelatedFeatures
2742 :    
2743 :     C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
2744 :    
2745 :     Return a list of the features which are bi-directional best hits of the specified feature and
2746 :     have been assigned the specified function by the specified user. If no such features exists,
2747 :     an empty list will be returned.
2748 :    
2749 :     =over 4
2750 :    
2751 :     =item featureID
2752 :    
2753 :     ID of the feature to whom the desired features are related.
2754 :    
2755 :     =item function
2756 :    
2757 :     Functional assignment (as returned by C</FunctionOf>) that is used to determine which related
2758 :     features should be selected.
2759 :    
2760 :     =item userID
2761 :    
2762 :     ID of the user whose functional assignments are to be used. If omitted, C<FIG> is assumed.
2763 :    
2764 :     =item RETURN
2765 :    
2766 :     Returns a list of the related features with the specified function.
2767 :    
2768 :     =back
2769 :    
2770 :     =cut
2771 : parrello 1.3 #: Return Type @;
2772 : parrello 1.1 sub RelatedFeatures {
2773 : parrello 1.15 # Get the parameters.
2774 :     my ($self, $featureID, $function, $userID) = @_;
2775 :     # Get a list of the features that are BBHs of the incoming feature.
2776 :     my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],
2777 :     "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],
2778 :     'IsBidirectionalBestHitOf(to-link)');
2779 :     # Now we loop through the features, pulling out the ones that have the correct
2780 :     # functional assignment.
2781 :     my @retVal = ();
2782 :     for my $bbhFeature (@bbhFeatures) {
2783 :     # Get this feature's functional assignment.
2784 :     my $newFunction = $self->FunctionOf($bbhFeature, $userID);
2785 :     # If it matches, add it to the result list.
2786 :     if ($newFunction eq $function) {
2787 :     push @retVal, $bbhFeature;
2788 :     }
2789 :     }
2790 :     # Return the result list.
2791 :     return @retVal;
2792 : parrello 1.1 }
2793 :    
2794 :     =head3 TaxonomySort
2795 :    
2796 :     C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>
2797 :    
2798 :     Return a list formed by sorting the specified features by the taxonomy of the containing
2799 :     genome. This will cause genomes from similar organisms to float close to each other.
2800 :    
2801 :     This task could almost be handled by the database; however, the taxonomy string in the
2802 :     database is a text field and can't be indexed. Instead, we create a hash table that maps
2803 :     taxonomy strings to lists of features. We then process the hash table using a key sort
2804 :     and merge the feature lists together to create the output.
2805 :    
2806 :     =over 4
2807 :    
2808 :     =item $featureIDs
2809 :    
2810 :     List of features to be taxonomically sorted.
2811 :    
2812 :     =item RETURN
2813 :    
2814 :     Returns the list of features sorted by the taxonomies of the containing genomes.
2815 :    
2816 :     =back
2817 :    
2818 :     =cut
2819 : parrello 1.3 #: Return Type @;
2820 : parrello 1.1 sub TaxonomySort {
2821 : parrello 1.15 # Get the parameters.
2822 :     my ($self, $featureIDs) = @_;
2823 :     # Create the working hash table.
2824 :     my %hashBuffer = ();
2825 :     # Loop through the features.
2826 :     for my $fid (@{$featureIDs}) {
2827 :     # Get the taxonomy of the feature's genome.
2828 :     my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
2829 :     [$fid], 'Genome(taxonomy)');
2830 :     # Add this feature to the hash buffer.
2831 : parrello 1.10 Tracer::AddToListMap(\%hashBuffer, $taxonomy, $fid);
2832 : parrello 1.15 }
2833 :     # Sort the keys and get the elements.
2834 :     my @retVal = ();
2835 :     for my $taxon (sort keys %hashBuffer) {
2836 :     push @retVal, @{$hashBuffer{$taxon}};
2837 :     }
2838 :     # Return the result.
2839 :     return @retVal;
2840 : parrello 1.1 }
2841 :    
2842 :     =head3 GetAll
2843 :    
2844 :     C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>
2845 :    
2846 :     Return a list of values taken from the objects returned by a query. The first three
2847 :     parameters correspond to the parameters of the L</Get> method. The final parameter is
2848 :     a list of the fields desired from each record found by the query. The field name
2849 :     syntax is the standard syntax used for fields in the B<ERDB> system--
2850 :     B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity
2851 :     or relationship and I<fieldName> is the name of the field.
2852 :    
2853 :     The list returned will be a list of lists. Each element of the list will contain
2854 :     the values returned for the fields specified in the fourth parameter. If one of the
2855 :     fields specified returns multiple values, they are flattened in with the rest. For
2856 :     example, the following call will return a list of the features in a particular
2857 :     spreadsheet cell, and each feature will be represented by a list containing the
2858 :     feature ID followed by all of its aliases.
2859 :    
2860 :     C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>
2861 :    
2862 :     =over 4
2863 :    
2864 :     =item objectNames
2865 :    
2866 :     List containing the names of the entity and relationship objects to be retrieved.
2867 :    
2868 :     =item filterClause
2869 :    
2870 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
2871 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
2872 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
2873 :     parameter list as additional parameters. The fields in a filter clause can come from primary
2874 :     entity relations, relationship relations, or secondary entity relations; however, all of the
2875 :     entities and relationships involved must be included in the list of object names.
2876 :    
2877 :     =item parameterList
2878 :    
2879 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
2880 :    
2881 :     =item fields
2882 :    
2883 :     List of the fields to be returned in each element of the list returned.
2884 :    
2885 :     =item count
2886 :    
2887 :     Maximum number of records to return. If omitted or 0, all available records will be returned.
2888 :    
2889 :     =item RETURN
2890 :    
2891 :     Returns a list of list references. Each element of the return list contains the values for the
2892 :     fields specified in the B<fields> parameter.
2893 :    
2894 :     =back
2895 :    
2896 :     =cut
2897 : parrello 1.3 #: Return Type @@;
2898 : parrello 1.1 sub GetAll {
2899 : parrello 1.15 # Get the parameters.
2900 :     my ($self, $objectNames, $filterClause, $parameterList, $fields, $count) = @_;
2901 :     # Call the ERDB method.
2902 :     my @retVal = $self->{_erdb}->GetAll($objectNames, $filterClause, $parameterList,
2903 :     $fields, $count);
2904 :     # Return the resulting list.
2905 :     return @retVal;
2906 : parrello 1.1 }
2907 :    
2908 :     =head3 GetFlat
2909 :    
2910 :     C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>
2911 :    
2912 :     This is a variation of L</GetAll> that asks for only a single field per record and
2913 :     returns a single flattened list.
2914 :    
2915 :     =over 4
2916 :    
2917 :     =item objectNames
2918 :    
2919 :     List containing the names of the entity and relationship objects to be retrieved.
2920 :    
2921 :     =item filterClause
2922 :    
2923 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
2924 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
2925 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
2926 :     parameter list as additional parameters. The fields in a filter clause can come from primary
2927 :     entity relations, relationship relations, or secondary entity relations; however, all of the
2928 :     entities and relationships involved must be included in the list of object names.
2929 :    
2930 :     =item parameterList
2931 :    
2932 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
2933 :    
2934 :     =item field
2935 :    
2936 :     Name of the field to be used to get the elements of the list returned.
2937 :    
2938 :     =item RETURN
2939 :    
2940 :     Returns a list of values.
2941 :    
2942 :     =back
2943 :    
2944 :     =cut
2945 : parrello 1.3 #: Return Type @;
2946 : parrello 1.1 sub GetFlat {
2947 : parrello 1.15 # Get the parameters.
2948 :     my ($self, $objectNames, $filterClause, $parameterList, $field) = @_;
2949 :     # Construct the query.
2950 :     my $query = $self->Get($objectNames, $filterClause, $parameterList);
2951 :     # Create the result list.
2952 :     my @retVal = ();
2953 :     # Loop through the records, adding the field values found to the result list.
2954 :     while (my $row = $query->Fetch()) {
2955 :     push @retVal, $row->Value($field);
2956 :     }
2957 :     # Return the list created.
2958 :     return @retVal;
2959 : parrello 1.1 }
2960 :    
2961 :     =head3 Protein
2962 :    
2963 :     C<< my $protein = Sprout::Protein($sequence, $table); >>
2964 :    
2965 :     Translate a DNA sequence into a protein sequence.
2966 :    
2967 :     =over 4
2968 :    
2969 :     =item sequence
2970 :    
2971 :     DNA sequence to translate.
2972 :    
2973 :     =item table (optional)
2974 :    
2975 :     Reference to a Hash that translates DNA triples to proteins. A triple that does not
2976 :     appear in the hash will be translated automatically to C<X>.
2977 :    
2978 :     =item RETURN
2979 :    
2980 :     Returns the protein sequence that would be created by the DNA sequence.
2981 :    
2982 :     =back
2983 :    
2984 :     =cut
2985 :    
2986 :     # This is the translation table for protein synthesis.
2987 :     my $ProteinTable = { AAA => 'K', AAG => 'K', AAT => 'N', AAC => 'N',
2988 : parrello 1.15 AGA => 'R', AGG => 'R', AGT => 'S', AGC => 'S',
2989 :     ATA => 'I', ATG => 'M', ATT => 'I', ATC => 'I',
2990 :     ACA => 'T', ACG => 'T', ACT => 'T', ACC => 'T',
2991 :     GAA => 'E', GAG => 'E', GAT => 'D', GAC => 'D',
2992 :     GTA => 'V', GTG => 'V', GTT => 'V', GTC => 'V',
2993 :     GGA => 'G', GGG => 'G', GGT => 'G', GGC => 'G',
2994 :     GCA => 'A', GCG => 'A', GCT => 'A', GCC => 'A',
2995 :     CAA => 'Q', CAG => 'Q', CAT => 'H', CAC => 'H',
2996 :     CTA => 'L', CTG => 'L', CTT => 'L', CTC => 'L',
2997 :     CGA => 'R', CGG => 'R', CGT => 'R', CGC => 'R',
2998 :     CCA => 'P', CCG => 'P', CCT => 'P', CCC => 'P',
2999 :     TAA => '*', TAG => '*', TAT => 'Y', TAC => 'Y',
3000 :     TGA => '*', TGG => 'W', TGT => 'C', TGC => 'C',
3001 :     TTA => 'L', TTG => 'L', TTT => 'F', TTC => 'F',
3002 :     TCA => 'S', TCG => 'S', TCT => 'S', TCC => 'S',
3003 :     AAR => 'K', AAY => 'N',
3004 :     AGR => 'R', AGY => 'S',
3005 :     ATY => 'I',
3006 :     ACR => 'T', ACY => 'T', 'ACX' => 'T',
3007 :     GAR => 'E', GAY => 'D',
3008 :     GTR => 'V', GTY => 'V', GTX => 'V',
3009 :     GGR => 'G', GGY => 'G', GGX => 'G',
3010 :     GCR => 'A', GCY => 'A', GCX => 'A',
3011 :     CAR => 'Q', CAY => 'H',
3012 :     CTR => 'L', CTY => 'L', CTX => 'L',
3013 :     CGR => 'R', CGY => 'R', CGX => 'R',
3014 :     CCR => 'P', CCY => 'P', CCX => 'P',
3015 :     TAR => '*', TAY => 'Y',
3016 :     TGY => 'C',
3017 :     TTR => 'L', TTY => 'F',
3018 :     TCR => 'S', TCY => 'S', TCX => 'S'
3019 :     };
3020 : parrello 1.1
3021 :     sub Protein {
3022 : parrello 1.15 # Get the paraeters.
3023 :     my ($sequence, $table) = @_;
3024 :     # If no table was specified, use the default.
3025 :     if (!$table) {
3026 :     $table = $ProteinTable;
3027 :     }
3028 :     # Create the return value.
3029 :     my $retVal = "";
3030 :     # Loop through the input triples.
3031 :     my $n = length $sequence;
3032 :     for (my $i = 0; $i < $n; $i += 3) {
3033 :     # Get the current triple from the sequence.
3034 :     my $triple = substr($sequence, $i, 3);
3035 :     # Translate it using the table.
3036 :     my $protein = "X";
3037 :     if (exists $table->{$triple}) { $protein = $table->{$triple}; }
3038 :     $retVal .= $protein;
3039 :     }
3040 :     # Remove the stop codon (if any).
3041 :     $retVal =~ s/\*$//;
3042 :     # Return the result.
3043 :     return $retVal;
3044 : parrello 1.1 }
3045 :    
3046 :     =head3 LoadInfo
3047 :    
3048 :     C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>
3049 :    
3050 :     Return the name of the directory from which data is to be loaded and a list of the relation
3051 :     names. This information is useful when trying to analyze what needs to be put where in order
3052 :     to load the entire database.
3053 :    
3054 :     =cut
3055 : parrello 1.3 #: Return Type @;
3056 : parrello 1.1 sub LoadInfo {
3057 : parrello 1.15 # Get the parameters.
3058 :     my ($self) = @_;
3059 :     # Create the return list, priming it with the name of the data directory.
3060 :     my @retVal = ($self->{_options}->{dataDir});
3061 :     # Concatenate the table names.
3062 :     push @retVal, $self->{_erdb}->GetTableNames();
3063 :     # Return the result.
3064 :     return @retVal;
3065 : parrello 1.1 }
3066 :    
3067 :     =head3 LowBBHs
3068 :    
3069 :     C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>
3070 :    
3071 :     Return the bidirectional best hits of a feature whose score is no greater than a
3072 :     specified cutoff value. A higher cutoff value will allow inclusion of hits with
3073 :     a greater score. The value returned is a map of feature IDs to scores.
3074 :    
3075 :     =over 4
3076 :    
3077 :     =item featureID
3078 :    
3079 :     ID of the feature whose best hits are desired.
3080 :    
3081 :     =item cutoff
3082 :    
3083 :     Maximum permissible score for inclusion in the results.
3084 :    
3085 :     =item RETURN
3086 :    
3087 :     Returns a hash mapping feature IDs to scores.
3088 :    
3089 :     =back
3090 :    
3091 :     =cut
3092 : parrello 1.3 #: Return Type %;
3093 : parrello 1.1 sub LowBBHs {
3094 : parrello 1.15 # Get the parsameters.
3095 :     my ($self, $featureID, $cutoff) = @_;
3096 :     # Create the return hash.
3097 :     my %retVal = ();
3098 :     # Create a query to get the desired BBHs.
3099 :     my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],
3100 :     'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',
3101 :     [$cutoff, $featureID],
3102 :     ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);
3103 :     # Form the results into the return hash.
3104 :     for my $pair (@bbhList) {
3105 :     $retVal{$pair->[0]} = $pair->[1];
3106 :     }
3107 :     # Return the result.
3108 :     return %retVal;
3109 : parrello 1.1 }
3110 :    
3111 : parrello 1.7 =head3 GetGroups
3112 :    
3113 : parrello 1.8 C<< my %groups = $sprout->GetGroups(\@groupList); >>
3114 :    
3115 :     Return a hash mapping each group to the IDs of the genomes in the group.
3116 :     A list of groups may be specified, in which case only those groups will be
3117 :     shown. Alternatively, if no parameter is supplied, all groups will be
3118 :     included. Genomes that are not in any group are omitted.
3119 :    
3120 : parrello 1.7 =cut
3121 :     #: Return Type %@;
3122 :     sub GetGroups {
3123 : parrello 1.8 # Get the parameters.
3124 : parrello 1.10 my ($self, $groupList) = @_;
3125 : parrello 1.8 # Declare the return value.
3126 :     my %retVal = ();
3127 :     # Determine whether we are getting all the groups or just some.
3128 :     if (defined $groupList) {
3129 :     # Here we have a group list. Loop through them individually,
3130 :     # getting a list of the relevant genomes.
3131 :     for my $group (@{$groupList}) {
3132 :     my @genomeIDs = $self->GetFlat(['Genome'], "Genome(group-name) = ?",
3133 :     [$group], "Genome(id)");
3134 :     $retVal{$group} = \@genomeIDs;
3135 :     }
3136 :     } else {
3137 :     # Here we need all of the groups. In this case, we run through all
3138 :     # of the genome records, putting each one found into the appropriate
3139 :     # group. Note that we use a filter clause to insure that only genomes
3140 :     # in groups are included in the return set.
3141 :     my @genomes = $self->GetAll(['Genome'], "Genome(group-name) > ' '", [],
3142 :     ['Genome(id)', 'Genome(group-name)']);
3143 :     # Loop through the genomes found.
3144 :     for my $genome (@genomes) {
3145 :     # Pop this genome's ID off the current list.
3146 :     my @groups = @{$genome};
3147 :     my $genomeID = shift @groups;
3148 :     # Loop through the groups, adding the genome ID to each group's
3149 :     # list.
3150 :     for my $group (@groups) {
3151 : parrello 1.10 Tracer::AddToListMap(\%retVal, $group, $genomeID);
3152 : parrello 1.8 }
3153 :     }
3154 :     }
3155 :     # Return the hash we just built.
3156 :     return %retVal;
3157 : parrello 1.7 }
3158 :    
3159 : parrello 1.18 =head3 MyGenomes
3160 :    
3161 :     C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3162 :    
3163 :     Return a list of the genomes to be included in the Sprout.
3164 :    
3165 :     This method is provided for use during the Sprout load. It presumes the Genome load file has
3166 :     already been created. (It will be in the Sprout data directory and called either C<Genome>
3167 :     or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3168 :     IDs.
3169 :    
3170 :     =over 4
3171 :    
3172 :     =item dataDir
3173 :    
3174 :     Directory containing the Sprout load files.
3175 :    
3176 :     =back
3177 :    
3178 :     =cut
3179 :     #: Return Type @;
3180 :     sub MyGenomes {
3181 :     # Get the parameters.
3182 :     my ($dataDir) = @_;
3183 :     # Compute the genome file name.
3184 :     my $genomeFileName = LoadFileName($dataDir, "Genome");
3185 :     # Extract the genome IDs from the files.
3186 :     my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3187 :     # Return the result.
3188 :     return @retVal;
3189 :     }
3190 :    
3191 :     =head3 LoadFileName
3192 :    
3193 :     C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3194 :    
3195 :     Return the name of the load file for the specified table in the specified data
3196 :     directory.
3197 :    
3198 :     =over 4
3199 :    
3200 :     =item dataDir
3201 :    
3202 :     Directory containing the Sprout load files.
3203 :    
3204 :     =item tableName
3205 :    
3206 :     Name of the table whose load file is desired.
3207 :    
3208 :     =item RETURN
3209 :    
3210 :     Returns the name of the file containing the load data for the specified table, or
3211 :     C<undef> if no load file is present.
3212 :    
3213 :     =back
3214 :    
3215 :     =cut
3216 :     #: Return Type $;
3217 :     sub LoadFileName {
3218 :     # Get the parameters.
3219 :     my ($dataDir, $tableName) = @_;
3220 :     # Declare the return variable.
3221 :     my $retVal;
3222 :     # Check for the various file names.
3223 :     if (-e "$dataDir/$tableName") {
3224 :     $retVal = "$dataDir/$tableName";
3225 :     } elsif (-e "$dataDir/$tableName.dtx") {
3226 :     $retVal = "$dataDir/$tableName.dtx";
3227 :     }
3228 :     # Return the result.
3229 :     return $retVal;
3230 :     }
3231 :    
3232 : parrello 1.53 =head3 DeleteGenome
3233 :    
3234 :     C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3235 :    
3236 :     Delete a genome from the database.
3237 :    
3238 :     =over 4
3239 :    
3240 :     =item genomeID
3241 :    
3242 :     ID of the genome to delete
3243 :    
3244 :     =item testFlag
3245 :    
3246 :     If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3247 :    
3248 :     =item RETURN
3249 :    
3250 :     Returns a statistics object describing the rows deleted.
3251 :    
3252 :     =back
3253 :    
3254 :     =cut
3255 :     #: Return Type $%;
3256 :     sub DeleteGenome {
3257 :     # Get the parameters.
3258 :     my ($self, $genomeID, $testFlag) = @_;
3259 :     # Perform the delete for the genome's features.
3260 :     my $retVal = $self->{_erdb}->Delete('Feature', "fig|$genomeID.%", $testFlag);
3261 :     # Perform the delete for the primary genome data.
3262 :     my $stats = $self->{_erdb}->Delete('Genome', $genomeID, $testFlag);
3263 :     $retVal->Accumulate($stats);
3264 :     # Return the result.
3265 :     return $retVal;
3266 :     }
3267 :    
3268 : parrello 1.1 =head2 Internal Utility Methods
3269 :    
3270 :     =head3 ParseAssignment
3271 :    
3272 :     Parse annotation text to determine whether or not it is a functional assignment. If it is,
3273 : parrello 1.19 the user, function text, and assigning user will be returned as a 3-element list. If it
3274 : parrello 1.11 isn't, an empty list will be returned.
3275 :    
3276 :     A functional assignment is always of the form
3277 :    
3278 : parrello 1.48 C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3279 : parrello 1.19
3280 : parrello 1.48 where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3281 :     the user and the assigning user (from MadeAnnotation) will be the same, but that is
3282 :     not always the case.
3283 : parrello 1.1
3284 : parrello 1.52 In addition, the functional role may contain extra data that is stripped, such as
3285 :     terminating spaces or a comment separated from the rest of the text by a tab.
3286 :    
3287 : parrello 1.1 This is a static method.
3288 :    
3289 :     =over 4
3290 :    
3291 : parrello 1.48 =item user
3292 :    
3293 :     Name of the assigning user.
3294 :    
3295 : parrello 1.1 =item text
3296 :    
3297 :     Text of the annotation.
3298 :    
3299 :     =item RETURN
3300 :    
3301 :     Returns an empty list if the annotation is not a functional assignment; otherwise, returns
3302 :     a two-element list containing the user name and the function text.
3303 :    
3304 :     =back
3305 :    
3306 :     =cut
3307 :    
3308 : parrello 1.11 sub _ParseAssignment {
3309 : parrello 1.15 # Get the parameters.
3310 : parrello 1.48 my ($user, $text) = @_;
3311 : parrello 1.15 # Declare the return value.
3312 :     my @retVal = ();
3313 :     # Check to see if this is a functional assignment.
3314 : parrello 1.43 my ($type, $function) = split(/\n/, $text);
3315 : parrello 1.48 if ($type =~ m/^set function to$/i) {
3316 :     # Here we have an assignment without a user, so we use the incoming user ID.
3317 :     @retVal = ($user, $function);
3318 :     } elsif ($type =~ m/^set (\S+) function to$/i) {
3319 : parrello 1.52 # Here we have an assignment with a user that is passed back to the caller.
3320 : parrello 1.43 @retVal = ($1, $function);
3321 : parrello 1.15 }
3322 : parrello 1.52 # If we have an assignment, we need to clean the function text. There may be
3323 :     # extra junk at the end added as a note from the user.
3324 :     if (@retVal) {
3325 :     $retVal[1] =~ s/(\t\S)?\s*$//;
3326 :     }
3327 : parrello 1.15 # Return the result list.
3328 :     return @retVal;
3329 : parrello 1.1 }
3330 :    
3331 : parrello 1.3 =head3 FriendlyTimestamp
3332 :    
3333 :     Convert a time number to a user-friendly time stamp for display.
3334 :    
3335 :     This is a static method.
3336 :    
3337 :     =over 4
3338 :    
3339 :     =item timeValue
3340 :    
3341 :     Numeric time value.
3342 :    
3343 :     =item RETURN
3344 :    
3345 :     Returns a string containing the same time in user-readable format.
3346 :    
3347 :     =back
3348 :    
3349 :     =cut
3350 :    
3351 :     sub FriendlyTimestamp {
3352 :     my ($timeValue) = @_;
3353 : parrello 1.40 my $retVal = localtime($timeValue);
3354 : parrello 1.3 return $retVal;
3355 :     }
3356 :    
3357 : parrello 1.15 =head3 AddProperty
3358 :    
3359 :     C<< my = $sprout->AddProperty($featureID, $key, $value, $url); >>
3360 :    
3361 :     Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3362 :     be added to almost any object. In Sprout, they can only be added to features. In
3363 :     Sprout, attributes are implemented using I<properties>. A property represents a key/value
3364 :     pair. If the particular key/value pair coming in is not already in the database, a new
3365 :     B<Property> record is created to hold it.
3366 :    
3367 :     =over 4
3368 :    
3369 :     =item peg
3370 :    
3371 :     ID of the feature to which the attribute is to be replied.
3372 :    
3373 :     =item key
3374 :    
3375 :     Name of the attribute (key).
3376 :    
3377 :     =item value
3378 :    
3379 :     Value of the attribute.
3380 :    
3381 :     =item url
3382 :    
3383 :     URL or text citation from which the property was obtained.
3384 :    
3385 :     =back
3386 :    
3387 :     =cut
3388 :     #: Return Type ;
3389 :     sub AddProperty {
3390 :     # Get the parameters.
3391 :     my ($self, $featureID, $key, $value, $url) = @_;
3392 :     # Declare the variable to hold the desired property ID.
3393 :     my $propID;
3394 :     # Attempt to find a property record for this key/value pair.
3395 :     my @properties = $self->GetFlat(['Property'],
3396 :     "Property(property-name) = ? AND Property(property-value) = ?",
3397 :     [$key, $value], 'Property(id)');
3398 :     if (@properties) {
3399 :     # Here the property is already in the database. We save its ID.
3400 :     $propID = $properties[0];
3401 :     # Here the property value does not exist. We need to generate an ID. It will be set
3402 :     # to a number one greater than the maximum value in the database. This call to
3403 :     # GetAll will stop after one record.
3404 :     my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3405 :     1);
3406 :     $propID = $maxProperty[0]->[0] + 1;
3407 :     # Insert the new property value.
3408 :     $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3409 :     }
3410 :     # Now we connect the incoming feature to the property.
3411 :     $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3412 :     }
3413 :    
3414 : parrello 1.51
3415 : parrello 1.48 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3