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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 package Sprout;
2 :    
3 :     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
40 :     =head2 Public Methods
41 :    
42 :     =head3 new
43 :    
44 :     C<< my $sprout = Sprout->new($dbName, \%options); >>
45 :    
46 :     This is the constructor for a sprout object. It connects to the database and loads the
47 :     database definition into memory. The positional first parameter specifies the name of the
48 :     database.
49 :    
50 :     =over 4
51 :    
52 :     =item dbName
53 :    
54 :     Name of the database.
55 :    
56 :     =item options
57 :    
58 :     Table of options.
59 :    
60 :     * B<dbType> type of database (currently C<mysql> for MySQL and C<pg> for PostgreSQL) (default C<mysql>)
61 :    
62 :     * B<dataDir> directory containing the database definition file and the flat files used to load the data (default C<Data>)
63 :    
64 :     * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
65 :    
66 :     * B<userData> user name and password, delimited by a slash (default C<root/>)
67 :    
68 :     * B<port> connection port (default C<0>)
69 :    
70 :     * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
71 :    
72 :     * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
73 :    
74 :     =back
75 :    
76 :     For example, the following constructor call specifies a database named I<Sprout> and a user name of
77 :     I<fig> with a password of I<admin>. The database load files are in the directory
78 :     F</usr/fig/SproutData>.
79 :    
80 :     C<< my $sprout = Sprout->new('Sprout', { userData =>; 'fig/admin', dataDir => '/usr/fig/SproutData' }); >>
81 :    
82 :     =cut
83 :    
84 :     sub new {
85 :     # Get the parameters.
86 :     my ($class, $dbName, $options) = @_;
87 :     # Compute the options. We do this by starting with a table of defaults and overwriting with
88 :     # the incoming data.
89 :     my $optionTable = Tracer::GetOptions({
90 :     dbType => 'mysql', # database type
91 :     dataDir => 'Data', # data file directory
92 :     xmlFileName => 'SproutDBD.xml', # database definition file name
93 :     userData => 'root/', # user name and password
94 :     port => 0, # database connection port
95 :     maxSegmentLength => 4500, # maximum feature segment length
96 :     maxSequenceLength => 8000, # maximum contig sequence length
97 :     }, $options);
98 :     # Get the data directory.
99 :     my $dataDir = $optionTable->{dataDir};
100 :     # Extract the user ID and password.
101 :     $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
102 :     my ($userName, $password) = ($1, $2);
103 :     # Connect to the database.
104 :     my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});
105 :     # Create the ERDB object.
106 :     my $xmlFileName = "$optionTable->{xmlFileName}";
107 :     my $erdb = ERDB->new($dbh, $xmlFileName);
108 :     # Create this object.
109 :     my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };
110 :     # Bless and return it.
111 :     bless $self;
112 :     return $self;
113 :     }
114 :    
115 :     =head3 MaxSegment
116 :    
117 :     C<< my $length = $sprout->MaxSegment(); >>
118 :    
119 :     This method returns the maximum permissible length of a feature segment. The length is important
120 :     because it enables us to make reasonable guesses at how to find features inside a particular
121 :     contig region. For example, if the maximum length is 4000 and we're looking for a feature that
122 :     overlaps the region from 6000 to 7000 we know that the starting position must be between 2001
123 :     and 10999.
124 :    
125 :     =cut
126 : parrello 1.3 #: Return Type $;
127 : parrello 1.1 sub MaxSegment {
128 :     my $self = shift @_;
129 :     return $self->{_options}->{maxSegmentLength};
130 :     }
131 :    
132 :     =head3 MaxSequence
133 :    
134 :     C<< my $length = $sprout->MaxSequence(); >>
135 :    
136 :     This method returns the maximum permissible length of a contig sequence. A contig is broken
137 :     into sequences in order to save memory resources. In particular, when manipulating features,
138 :     we generally only need a few sequences in memory rather than the entire contig.
139 :    
140 :     =cut
141 : parrello 1.3 #: Return Type $;
142 : parrello 1.1 sub MaxSequence {
143 :     my $self = shift @_;
144 :     return $self->{_options}->{maxSequenceLength};
145 :     }
146 :    
147 :     =head3 Get
148 :    
149 :     C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >>
150 :    
151 :     This method allows a general query against the Sprout data using a specified filter clause.
152 :    
153 :     The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each
154 :     field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the
155 :     following call requests all B<Genome> objects for the genus specified in the variable
156 :     $genus.
157 :    
158 :     C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>
159 :    
160 :     The WHERE clause contains a single question mark, so there is a single additional
161 :     parameter representing the parameter value. It would also be possible to code
162 :    
163 :     C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>
164 :    
165 :     however, this version of the call would generate a syntax error if there were any quote
166 :     characters inside the variable C<$genus>.
167 :    
168 :     The use of the strange parenthesized notation for field names enables us to distinguish
169 :     hyphens contained within field names from minus signs that participate in the computation
170 :     of the WHERE clause. All of the methods that manipulate fields will use this same notation.
171 :    
172 :     It is possible to specify multiple entity and relationship names in order to retrieve more than
173 :     one object's data at the same time, which allows highly complex joined queries. For example,
174 :    
175 :     C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>
176 :    
177 :     This query returns all the genomes for a particular genus and allows access to the
178 :     sources from which they came. The join clauses to go from Genome to Source are generated
179 :     automatically.
180 :    
181 :     Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>
182 :     clause at the end of the filter. Field references in the ORDER BY section follow the same rules
183 :     as they do in the filter itself; in other words, each one must be of the form B<I<objectName>(I<fieldName>)>.
184 :     For example, the following filter string gets all genomes for a particular genus and sorts
185 :     them by species name.
186 :    
187 :     C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>
188 :    
189 :     It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets
190 :     all genomes ordered by genus and species.
191 :    
192 :     C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>
193 :    
194 :     Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an
195 :     attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause
196 :     a single feature to appear more than once.
197 :    
198 :     If multiple names are specified, then the query processor will automatically determine a
199 :     join path between the entities and relationships. The algorithm used is very simplistic.
200 :     In particular, you can't specify any entity or relationship more than once, and if a
201 :     relationship is recursive, the path is determined by the order in which the entity
202 :     and the relationship appear. For example, consider a recursive relationship B<IsParentOf>
203 :     which relates B<People> objects to other B<People> objects. If the join path is
204 :     coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,
205 :     the join path is C<['IsParentOf', 'People']>, then the people returned will be children.
206 :    
207 :     =over 4
208 :    
209 :     =item objectNames
210 :    
211 :     List containing the names of the entity and relationship objects to be retrieved.
212 :    
213 :     =item filterClause
214 :    
215 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
216 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
217 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
218 :     parameter list as additional parameters. The fields in a filter clause can come from primary
219 :     entity relations, relationship relations, or secondary entity relations; however, all of the
220 :     entities and relationships involved must be included in the list of object names.
221 :    
222 :     =item parameterList
223 :    
224 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
225 :    
226 :     =item RETURN
227 :    
228 :     Returns a B<DBQuery> that can be used to iterate through all of the results.
229 :    
230 :     =back
231 :    
232 :     =cut
233 :    
234 :     sub Get {
235 :     # Get the parameters.
236 :     my $self = shift @_;
237 :     my ($objectNames, $filterClause, $parameterList) = @_;
238 :     # We differ from the ERDB Get method in that the parameter list is passed in as a list reference
239 :     # rather than a list of parameters. The next step is to convert the parameters from a reference
240 :     # to a real list. We can only do this if the parameters have been specified.
241 :     my @parameters;
242 :     if ($parameterList) { @parameters = @{$parameterList}; }
243 :     return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);
244 :     }
245 :    
246 :     =head3 GetEntity
247 :    
248 :     C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>
249 :    
250 :     Return an object describing the entity instance with a specified ID.
251 :    
252 :     =over 4
253 :    
254 :     =item entityType
255 :    
256 :     Entity type name.
257 :    
258 :     =item ID
259 :    
260 :     ID of the desired entity.
261 :    
262 :     =item RETURN
263 :    
264 :     Returns a B<DBObject> representing the desired entity instance, or an undefined value if no
265 :     instance is found with the specified key.
266 :    
267 :     =back
268 :    
269 :     =cut
270 :    
271 :     sub GetEntity {
272 :     # Get the parameters.
273 :     my $self = shift @_;
274 :     my ($entityType, $ID) = @_;
275 :     # Create a query.
276 :     my $query = $self->Get([$entityType], "$entityType(id) = ?", [$ID]);
277 :     # Get the first (and only) object.
278 :     my $retVal = $query->Fetch();
279 :     # Return the result.
280 :     return $retVal;
281 :     }
282 :    
283 :     =head3 GetEntityValues
284 :    
285 :     C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>
286 :    
287 :     Return a list of values from a specified entity instance.
288 :    
289 :     =over 4
290 :    
291 :     =item entityType
292 :    
293 :     Entity type name.
294 :    
295 :     =item ID
296 :    
297 :     ID of the desired entity.
298 :    
299 :     =item fields
300 :    
301 :     List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.
302 :    
303 :     =item RETURN
304 :    
305 :     Returns a flattened list of the values of the specified fields for the specified entity.
306 :    
307 :     =back
308 :    
309 :     =cut
310 : parrello 1.3 #: Return Type @;
311 : parrello 1.1 sub GetEntityValues {
312 :     # Get the parameters.
313 :     my $self = shift @_;
314 :     my ($entityType, $ID, $fields) = @_;
315 :     # Get the specified entity.
316 :     my $entity = $self->GetEntity($entityType, $ID);
317 :     # Declare the return list.
318 :     my @retVal = ();
319 :     # If we found the entity, push the values into the return list.
320 :     if ($entity) {
321 :     push @retVal, $entity->Values($fields);
322 :     }
323 :     # Return the result.
324 :     return @retVal;
325 :     }
326 :    
327 :     =head3 ShowMetaData
328 :    
329 :     C<< $sprout->ShowMetaData($fileName); >>
330 :    
331 :     This method outputs a description of the database to an HTML file in the data directory.
332 :    
333 :     =over 4
334 :    
335 :     =item fileName
336 :    
337 :     Fully-qualified name to give to the output file.
338 :    
339 :     =back
340 :    
341 :     =cut
342 :    
343 :     sub ShowMetaData {
344 :     # Get the parameters.
345 :     my $self = shift @_;
346 :     my ($fileName) = @_;
347 :     # Compute the file name.
348 :     my $options = $self->{_options};
349 :     # Call the show method on the underlying ERDB object.
350 :     $self->{_erdb}->ShowMetaData($fileName);
351 :     }
352 :    
353 :     =head3 Load
354 :    
355 :     C<< $sprout->Load($rebuild); >>;
356 :    
357 :     Load the database from files in the data directory, optionally re-creating the tables.
358 :    
359 :     This method always deletes the data from the database before loading, even if the tables are not
360 :     re-created. The data is loaded into the relations from files in the data directory either having the
361 :     same name as the target relation with no extension or with an extension of C<.dtx>. Files without an
362 :     extension are used in preference to the files with an extension.
363 :    
364 :     The files are loaded based on the presumption that each line of the file is a record in the
365 :     relation, and the individual fields are delimited by tabs. Tab and new-line characters inside
366 :     fields must be represented by the escape sequences C<\t> and C<\n>, respectively. The fields must
367 :     be presented in the order given in the relation tables produced by the L</ShowMetaData> method.
368 :    
369 :     =over 4
370 :    
371 :     =item rebuild
372 :    
373 :     TRUE if the data tables need to be created or re-created, else FALSE
374 :    
375 :     =item RETURN
376 :    
377 :     Returns a statistical object containing the number of records read, the number of duplicates found,
378 :     the number of errors, and a list of the error messages.
379 :    
380 :     =back
381 :    
382 :     =cut
383 : parrello 1.3 #: Return Type %;
384 : parrello 1.1 sub Load {
385 :     # Get the parameters.
386 :     my $self = shift @_;
387 :     my ($rebuild) = @_;
388 :     # Get the database object.
389 :     my $erdb = $self->{_erdb};
390 :     # Load the tables from the data directory.
391 :     my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);
392 :     # Return the statistics.
393 :     return $retVal;
394 :     }
395 :    
396 :     =head3 LoadUpdate
397 :    
398 :     C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
399 :    
400 :     Load updates to one or more database tables. This method enables the client to make changes to one
401 :     or two tables without reloading the whole database. For each table, there must be a corresponding
402 :     file in the data directory, either with the same name as the table, or with a C<.dtx> suffix. So,
403 :     for example, to make updates to the B<FeatureTranslation> relation, there must be a
404 :     C<FeatureTranslation.dtx> file in the data directory. Unlike a full load, files without an extension
405 :     are not examined. This allows update files to co-exist with files from an original load.
406 :    
407 :     =over 4
408 :    
409 :     =item truncateFlag
410 :    
411 :     TRUE if the tables should be rebuilt before loading, else FALSE. A value of TRUE therefore causes
412 :     current data and schema of the tables to be replaced, while a value of FALSE means the new data
413 :     is added to the existing data in the various relations.
414 :    
415 :     =item tableList
416 :    
417 :     List of the tables to be updated.
418 :    
419 :     =item RETURN
420 :    
421 :     Returns a statistical object containing the number of records read, the number of duplicates found,
422 :     the number of errors encountered, and a list of error messages.
423 :    
424 :     =back
425 :    
426 :     =cut
427 : parrello 1.3 #: Return Type %;
428 : parrello 1.1 sub LoadUpdate {
429 :     # Get the parameters.
430 :     my $self = shift @_;
431 :     my ($truncateFlag, $tableList) = @_;
432 :     # Get the database object.
433 :     my $erdb = $self->{_erdb};
434 :     # Declare the return value.
435 :     my $retVal = Stats->new();
436 :     # Get the data directory.
437 :     my $optionTable = $self->{_options};
438 :     my $dataDir = $optionTable->{dataDir};
439 :     # Loop through the incoming table names.
440 :     for my $tableName (@{$tableList}) {
441 :     # Find the table's file.
442 :     my $fileName = "$dataDir/$tableName";
443 :     if (! -e $fileName) {
444 :     $fileName = "$fileName.dtx";
445 :     }
446 :     # Attempt to load this table.
447 :     my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);
448 :     # Accumulate the resulting statistics.
449 :     $retVal->Accumulate($result);
450 :     }
451 :     # Return the statistics.
452 :     return $retVal;
453 :     }
454 :    
455 :     =head3 Build
456 :    
457 :     C<< $sprout->Build(); >>
458 :    
459 :     Build the database. The database will be cleared and the tables re-created from the metadata.
460 :     This method is useful when a database is brand new or when the database definition has
461 :     changed.
462 :    
463 :     =cut
464 : parrello 1.3 #: Return Type ;
465 : parrello 1.1 sub Build {
466 :     # Get the parameters.
467 :     my $self = shift @_;
468 :     # Create the tables.
469 :     $self->{_erdb}->CreateTables;
470 :     }
471 :    
472 :     =head3 Genomes
473 :    
474 :     C<< my @genomes = $sprout->Genomes(); >>
475 :    
476 :     Return a list of all the genome IDs.
477 :    
478 :     =cut
479 : parrello 1.3 #: Return Type @;
480 : parrello 1.1 sub Genomes {
481 :     # Get the parameters.
482 :     my $self = shift @_;
483 :     # Get all the genomes.
484 :     my @retVal = $self->GetFlat(['Genome'], "", [], 'Genome(id)');
485 :     # Return the list of IDs.
486 :     return @retVal;
487 :     }
488 :    
489 :     =head3 GenusSpecies
490 :    
491 :     C<< my $infoString = $sprout->GenusSpecies($genomeID); >>
492 :    
493 :     Return the genus, species, and unique characterization for a genome.
494 :    
495 :     =over 4
496 :    
497 :     =item genomeID
498 :    
499 :     ID of the genome whose genus and species is desired
500 :    
501 :     =item RETURN
502 :    
503 :     Returns the genus and species of the genome, with the unique characterization (if any). If the genome
504 :     does not exist, returns an undefined value.
505 :    
506 :     =back
507 :    
508 :     =cut
509 : parrello 1.3 #: Return Type $;
510 : parrello 1.1 sub GenusSpecies {
511 :     # Get the parameters.
512 :     my $self = shift @_;
513 :     my ($genomeID) = @_;
514 :     # Get the data for the specified genome.
515 :     my @values = $self->GetEntityValues('Genome', $genomeID, ['Genome(genus)', 'Genome(species)',
516 :     'Genome(unique-characterization)']);
517 :     # Format the result and return it.
518 :     my $retVal = join(' ', @values);
519 :     return $retVal;
520 :     }
521 :    
522 :     =head3 FeaturesOf
523 :    
524 :     C<< my @features = $sprout->FeaturesOf($genomeID, $ftype); >>
525 :    
526 :     Return a list of the features relevant to a specified genome.
527 :    
528 :     =over 4
529 :    
530 :     =item genomeID
531 :    
532 :     Genome whose features are desired.
533 :    
534 :     =item ftype
535 :    
536 :     Type of feature desired. If omitted, all features will be returned.
537 :    
538 :     =item RETURN
539 :    
540 :     Returns a list of the feature IDs for features relevant to the genome. If the genome does not exist,
541 :     will return an empty list.
542 :    
543 :     =back
544 :    
545 :     =cut
546 : parrello 1.3 #: Return Type @;
547 : parrello 1.1 sub FeaturesOf {
548 :     # Get the parameters.
549 :     my $self = shift @_;
550 :     my ($genomeID,$ftype) = @_;
551 :     # Get the features we want.
552 :     my @features;
553 :     if (!$ftype) {
554 :     @features = $self->GetFlat(['HasContig', 'IsLocatedIn'], "HasContig(from-link) = ?",
555 :     [$genomeID], 'IsLocatedIn(from-link)');
556 :     } else {
557 :     @features = $self->GetFlat(['HasContig', 'IsLocatedIn', 'Feature'],
558 :     "HasContig(from-link) = ? AND Feature(feature-type) = ?",
559 :     [$genomeID, $ftype], 'IsLocatedIn(from-link)');
560 :     }
561 :     # Return the list with duplicates merged out. We need to merge out duplicates because
562 :     # a feature will appear twice if it spans more than one contig.
563 :     my @retVal = Tracer::Merge(@features);
564 :     # Return the list of feature IDs.
565 :     return @retVal;
566 :     }
567 :    
568 :     =head3 FeatureLocation
569 :    
570 :     C<< my @locations = $sprout->FeatureLocation($featureID); >>
571 :    
572 :     Return the location of a feature in its genome's contig segments. In a list context, this method
573 :     will return a list of the locations. In a scalar context, it will return the locations as a space-
574 :     delimited string. Each location will be of the form I<contigID>C<_>I<begin>I<dir>I<len> where
575 :     I<begin> is the starting position, I<dir> is C<+> for a forward transcription or C<-> for a backward
576 :     transcription, and I<len> is the length. So, for example, C<1999.1_NC123_4000+200> describes a location
577 :     beginning at position 4000 of contig C<1999.1_NC123> and ending at position 4199. Similarly,
578 :     C<1999.1_NC123_2000-400> describes a location in the same contig starting at position 2000 and ending
579 :     at position 1601.
580 :    
581 :     This process is complicated by the fact that we automatically split up feature segments longer than
582 :     the maximum segment length. When we find two segments that are adjacent to each other, we must
583 :     put them back together.
584 :    
585 :     =over 4
586 :    
587 :     =item featureID
588 :    
589 :     FIG ID of the desired feature
590 :    
591 :     =item RETURN
592 :    
593 :     Returns a list of the feature's contig segments. The locations are returned as a list in a list
594 :     context and as a space-delimited string in a scalar context.
595 :    
596 :     =back
597 :    
598 :     =cut
599 : parrello 1.3 #: Return Type @;
600 :     #: Return Type $;
601 : parrello 1.1 sub FeatureLocation {
602 :     # Get the parameters.
603 :     my $self = shift @_;
604 :     my ($featureID) = @_;
605 :     # Create a query for the feature locations.
606 :     my $query = $self->Get(['IsLocatedIn'], "IsLocatedIn(from-link) = ? ORDER BY IsLocatedIn(locN)",
607 :     [$featureID]);
608 :     # Create the return list.
609 :     my @retVal = ();
610 :     # Set up the variables used to determine if we have adjacent segments. This initial setup will
611 :     # not match anything.
612 :     my ($prevContig, $prevBeg, $prevDir, $prevLen) = ("", 0, "0", 0);
613 :     # Loop through the query results, creating location specifiers.
614 :     while (my $location = $query->Fetch()) {
615 :     # Get the location parameters.
616 :     my ($contigID, $beg, $dir, $len) = $location->Values(['IsLocatedIn(to-link)',
617 :     'IsLocatedIn(beg)', 'IsLocatedIn(dir)', 'IsLocatedIn(len)']);
618 :     # Check to see if we are adjacent to the previous segment.
619 :     if ($prevContig eq $contigID && $dir eq $prevDir) {
620 :     # Here the new segment is in the same direction on the same contig. Insure the
621 :     # new segment's beginning is next to the old segment's end.
622 :     if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||
623 :     ($dir eq "+" && $beg == $prevBeg + $prevLen)) {
624 :     # Here we need to merge two segments. Adjust the beginning and length values
625 :     # to include both segments.
626 :     $beg = $prevBeg;
627 :     $len += $prevLen;
628 :     # Pop the old segment off. The new one will replace it later.
629 :     pop @retVal;
630 :     }
631 :     }
632 :     # Remember this specifier for the adjacent-segment test the next time through.
633 :     ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
634 :     # Add the specifier to the list.
635 :     push @retVal, "${contigID}_$beg$dir$len";
636 :     }
637 :     # Return the list in the format indicated by the context.
638 :     return (wantarray ? @retVal : join(' ', @retVal));
639 :     }
640 :    
641 :     =head3 ParseLocation
642 :    
643 :     C<< my ($contigID, $start, $dir, $len) = Sprout::ParseLocation($location); >>
644 :    
645 :     Split a location specifier into the contig ID, the starting point, the direction, and the
646 :     length.
647 :    
648 :     =over 4
649 :    
650 :     =item location
651 :    
652 :     A location specifier (see L</FeatureLocation> for a description).
653 :    
654 :     =item RETURN
655 :    
656 :     Returns a list containing the contig ID, the start position, the direction (C<+> or C<->),
657 :     and the length indicated by the incoming location specifier.
658 :    
659 :     =back
660 :    
661 :     =cut
662 : parrello 1.3 #: Return Type @;
663 : parrello 1.1 sub ParseLocation {
664 :     # Get the parameter.
665 :     my ($location) = @_;
666 :     # Parse it into segments.
667 :     $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;
668 :     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 :     }
682 :    
683 :     =head3 DNASeq
684 :    
685 :     C<< my $sequence = $sprout->DNASeq(\@locationList); >>
686 :    
687 :     This method returns the DNA sequence represented by a list of locations. The list of locations
688 : parrello 1.2 should be of the form returned by L</featureLocation> when in a list context. In other words,
689 : parrello 1.1 each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
690 :    
691 :     =over 4
692 :    
693 :     =item locationList
694 :    
695 :     List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see
696 :     L</FeatureLocation> for more about this format).
697 :    
698 :     =item RETURN
699 :    
700 :     Returns a string of nucleotides corresponding to the DNA segments in the location list.
701 :    
702 :     =back
703 :    
704 :     =cut
705 : parrello 1.3 #: Return Type $;
706 : parrello 1.1 sub DNASeq {
707 :     # Get the parameters.
708 :     my $self = shift @_;
709 :     my ($locationList) = @_;
710 :     # Create the return string.
711 :     my $retVal = "";
712 :     # Loop through the locations.
713 :     for my $location (@{$locationList}) {
714 :     # Set up a variable to contain the DNA at this location.
715 :     my $locationDNA = "";
716 :     # Parse out the contig ID, the beginning point, the direction, and the end point.
717 :     my ($contigID, $beg, $dir, $len) = ParseLocation($location);
718 :     # Now we must create a query to return all the sequences in the contig relevant to the region
719 :     # specified. First, we compute the start and stop points when reading through the sequences.
720 :     # For a forward transcription, the start point is the beginning; for a backward transcription,
721 :     # the start point is the ending. Note that in the latter case we must reverse the DNA string
722 :     # before putting it in the return value.
723 :     my ($start, $stop);
724 :     if ($dir eq "+") {
725 :     $start = $beg;
726 :     $stop = $beg + $len - 1;
727 :     } else {
728 :     $start = $beg + $len + 1;
729 :     $stop = $beg;
730 :     }
731 :     my $query = $self->Get(['IsMadeUpOf','Sequence'],
732 :     "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
733 :     " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
734 :     [$contigID, $start, $stop]);
735 :     # Loop through the sequences.
736 :     while (my $sequence = $query->Fetch()) {
737 :     # Determine whether the location starts, stops, or continues through this sequence.
738 :     my ($startPosition, $sequenceData, $sequenceLength) =
739 :     $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
740 :     'IsMadeUpOf(len)']);
741 :     my $stopPosition = $startPosition + $sequenceLength;
742 :     # Figure out the start point and length of the relevant section.
743 :     my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
744 :     my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;
745 :     # Add the relevant data to the location data.
746 :     $locationDNA .= substr($sequenceData, $pos1, $len);
747 :     }
748 :     # Add this location's data to the return string. Note that we may need to reverse it.
749 :     if ($dir eq '+') {
750 :     $retVal .= $locationDNA;
751 :     } else {
752 :     $locationDNA = join('', reverse split //, $locationDNA);
753 :     $retVal .= $locationDNA;
754 :     }
755 :     }
756 :     # Return the result.
757 :     return $retVal;
758 :     }
759 :    
760 :     =head3 AllContigs
761 :    
762 :     C<< my @idList = $sprout->AllContigs($genomeID); >>
763 :    
764 :     Return a list of all the contigs for a genome.
765 :    
766 :     =over 4
767 :    
768 :     =item genomeID
769 :    
770 :     Genome whose contigs are desired.
771 :    
772 :     =item RETURN
773 :    
774 :     Returns a list of the IDs for the genome's contigs.
775 :    
776 :     =back
777 :    
778 :     =cut
779 : parrello 1.3 #: Return Type @;
780 : parrello 1.1 sub AllContigs {
781 :     # Get the parameters.
782 :     my $self = shift @_;
783 :     my ($genomeID) = @_;
784 :     # Ask for the genome's Contigs.
785 :     my @retVal = $self->GetFlat(['HasContig'], "HasContig(from-link) = ?", [$genomeID],
786 :     'HasContig(to-link)');
787 :     # Return the list of Contigs.
788 :     return @retVal;
789 :     }
790 :    
791 :     =head3 ContigLength
792 :    
793 :     C<< my $length = $sprout->ContigLength($contigID); >>
794 :    
795 :     Compute the length of a contig.
796 :    
797 :     =over 4
798 :    
799 :     =item contigID
800 :    
801 :     ID of the contig whose length is desired.
802 :    
803 : parrello 1.3 =item RETURN
804 :    
805 :     Returns the number of positions in the contig.
806 :    
807 : parrello 1.1 =back
808 :    
809 :     =cut
810 : parrello 1.3 #: Return Type $;
811 : parrello 1.1 sub ContigLength {
812 :     # Get the parameters.
813 :     my $self = shift @_;
814 :     my ($contigID) = @_;
815 :     # Get the contig's last sequence.
816 :     my $query = $self->Get(['IsMadeUpOf'],
817 :     "IsMadeUpOf(from-link) = ? ORDER BY IsMadeUpOf(start-position) DESC",
818 :     [$contigID]);
819 :     my $sequence = $query->Fetch();
820 :     # Declare the return value.
821 :     my $retVal = 0;
822 :     # Set it from the sequence data, if any.
823 :     if ($sequence) {
824 :     my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
825 :     $retVal = $start + $len;
826 :     }
827 :     # Return the result.
828 :     return $retVal;
829 :     }
830 :    
831 :     =head3 GenesInRegion
832 :    
833 :     C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
834 :    
835 :     List the features which overlap a specified region in a contig.
836 :    
837 :     =over 4
838 :    
839 :     =item contigID
840 :    
841 :     ID of the contig containing the region of interest.
842 :    
843 :     =item start
844 :    
845 :     Offset of the first residue in the region of interest.
846 :    
847 :     =item stop
848 :    
849 :     Offset of the last residue in the region of interest.
850 :    
851 :     =item RETURN
852 :    
853 :     Returns a three-element list. The first element is a list of feature IDs for the features that
854 :     overlap the region of interest. The second and third elements are the minimum and maximum
855 :     locations of the features provided on the specified contig. These may extend outside
856 :     the start and stop values.
857 :    
858 :     =back
859 :    
860 :     =cut
861 : parrello 1.3 #: Return Type @;
862 : parrello 1.1 sub GenesInRegion {
863 :     # Get the parameters.
864 :     my $self = shift @_;
865 :     my ($contigID, $start, $stop) = @_;
866 :     # Get the maximum segment length.
867 :     my $maximumSegmentLength = $self->MaxSegment;
868 :     # Create a hash to receive the feature list. We use a hash so that we can eliminate
869 :     # duplicates easily.
870 :     my %featuresFound = ();
871 :     # Prime the values we'll use for the returned beginning and end.
872 :     my ($min, $max) = ($self->ContigLength($contigID), 0);
873 :     # Create a table of parameters for each query. Each query looks for features travelling in
874 :     # a particular direction. The query parameters include the contig ID, the feature direction,
875 :     # the lowest possible start position, and the highest possible start position. This works
876 :     # because each feature segment length must be no greater than the maximum segment length.
877 :     my %queryParms = (forward => [$contigID, '+', $start - $maximumSegmentLength + 1, $stop],
878 :     reverse => [$contigID, '-', $start, $stop + $maximumSegmentLength - 1]);
879 :     # Loop through the query parameters.
880 :     for my $parms (values %queryParms) {
881 :     # Create the query.
882 :     my $query = $self->Get(['IsLocatedIn'],
883 :     "IsLocatedIn(to-link)= ? AND IsLocatedIn(dir) = ? AND IsLocatedIn(beg) >= ? AND IsLocatedIn(beg) <= ?",
884 :     $parms);
885 :     # Loop through the feature segments found.
886 :     while (my $segment = $query->Fetch) {
887 :     # Get the data about this segment.
888 :     my ($featureID, $dir, $beg, $len) = $segment->Values(['IsLocatedIn(from-link)',
889 :     'IsLocatedIn(dir)', 'IsLocatedIn(beg)', 'IsLocatedIn(len)']);
890 :     # Determine if this feature actually overlaps the region. The query insures that
891 :     # this will be the case if the segment is the maximum length, so to fine-tune
892 :     # the results we insure that the inequality from the query holds using the actual
893 :     # length.
894 :     my ($found, $end) = (0, 0);
895 :     if ($dir eq '+') {
896 :     $end = $beg + $len;
897 :     if ($end >= $start) {
898 :     # Denote we found a useful feature.
899 :     $found = 1;
900 :     }
901 :     } elsif ($dir eq '-') {
902 :     $end = $beg - $len;
903 :     if ($end <= $stop) {
904 :     # Denote we found a useful feature.
905 :     $found = 1;
906 :     }
907 :     }
908 :     if ($found) {
909 :     # Here we need to record the feature and update the minimum and maximum.
910 :     $featuresFound{$featureID} = 1;
911 :     if ($beg < $min) { $min = $beg; }
912 :     if ($end < $min) { $min = $end; }
913 :     if ($beg > $max) { $max = $beg; }
914 :     if ($end > $max) { $max = $end; }
915 :     }
916 :     }
917 :     }
918 :     # Compute a list of the IDs for the features found.
919 :     my @list = (sort (keys %featuresFound));
920 :     # Return it along with the min and max.
921 :     return (\@list, $min, $max);
922 :     }
923 :    
924 :     =head3 FType
925 :    
926 :     C<< my $ftype = $sprout->FType($featureID); >>
927 :    
928 :     Return the type of a feature.
929 :    
930 :     =over 4
931 :    
932 :     =item featureID
933 :    
934 :     ID of the feature whose type is desired.
935 :    
936 :     =item RETURN
937 :    
938 :     A string indicating the type of feature (e.g. peg, rna). If the feature does not exist, returns an
939 :     undefined value.
940 :    
941 :     =back
942 :    
943 :     =cut
944 : parrello 1.3 #: Return Type $;
945 : parrello 1.1 sub FType {
946 :     # Get the parameters.
947 :     my $self = shift @_;
948 :     my ($featureID) = @_;
949 :     # Get the specified feature's type.
950 :     my ($retVal) = $self->GetEntityValues('Feature', $featureID, ['Feature(feature-type)']);
951 :     # Return the result.
952 :     return $retVal;
953 :     }
954 :    
955 :     =head3 FeatureAnnotations
956 :    
957 :     C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>
958 :    
959 :     Return the annotations of a feature.
960 :    
961 :     =over 4
962 :    
963 :     =item featureID
964 :    
965 :     ID of the feature whose annotations are desired.
966 :    
967 :     =item RETURN
968 :    
969 :     Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.
970 :    
971 :     * B<featureID> ID of the relevant feature.
972 :    
973 : parrello 1.3 * B<timeStamp> time the annotation was made, in user-friendly format.
974 : parrello 1.1
975 :     * B<user> ID of the user who made the annotation
976 :    
977 :     * B<text> text of the annotation.
978 :    
979 :     =back
980 :    
981 :     =cut
982 : parrello 1.3 #: Return Type @%;
983 : parrello 1.1 sub FeatureAnnotations {
984 :     # Get the parameters.
985 :     my $self = shift @_;
986 :     my ($featureID) = @_;
987 :     # Create a query to get the feature's annotations and the associated users.
988 :     my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
989 :     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
990 :     # Create the return list.
991 :     my @retVal = ();
992 :     # Loop through the annotations.
993 :     while (my $annotation = $query->Fetch) {
994 :     # Get the fields to return.
995 :     my ($featureID, $timeStamp, $user, $text) =
996 :     $annotation->Values(['IsTargetOfAnnotation(from-link)',
997 :     'Annotation(time)', 'MadeAnnotation(from-link)',
998 :     'Annotation(annotation)']);
999 :     # Assemble them into a hash.
1000 : parrello 1.3 my $annotationHash = { featureID => $featureID,
1001 :     timeStamp => FriendlyTimestamp($timeStamp),
1002 : parrello 1.1 user => $user, text => $text };
1003 :     # Add it to the return list.
1004 :     push @retVal, $annotationHash;
1005 :     }
1006 :     # Return the result list.
1007 :     return @retVal;
1008 :     }
1009 :    
1010 :     =head3 AllFunctionsOf
1011 :    
1012 :     C<< my %functions = $sprout->AllFunctionsOf($featureID); >>
1013 :    
1014 :     Return all of the functional assignments for a particular feature. The data is returned as a
1015 :     hash of functional assignments to user IDs. A functional assignment is a type of annotation.
1016 :     It has the format "XXXX\nset XXXX function to\nYYYYY". In this instance, XXXX is the user ID
1017 :     and YYYYY is the functional assignment text. Its worth noting that we cannot filter on the content
1018 :     of the annotation itself because it's a text field; however, this is not a big problem because most
1019 :     features only have a small number of annotations.
1020 :    
1021 :     =over 4
1022 :    
1023 :     =item featureID
1024 :    
1025 :     ID of the feature whose functional assignments are desired.
1026 :    
1027 : parrello 1.3 =item RETURN
1028 :    
1029 :     Returns a hash mapping the functional assignment IDs to user IDs.
1030 :    
1031 : parrello 1.1 =back
1032 :    
1033 :     =cut
1034 : parrello 1.3 #: Return Type %;
1035 : parrello 1.1 sub AllFunctionsOf {
1036 :     # Get the parameters.
1037 :     my $self = shift @_;
1038 :     my ($featureID) = @_;
1039 :     # Get all of the feature's annotations.
1040 :     my @query = $self->GetFlat(['IsTargetOfAnnotation', 'Annotation'],
1041 :     "IsTargetOfAnnotation(from-link) = ?",
1042 :     [$featureID], 'Annotation(annotation)');
1043 :     # Declare the return hash.
1044 :     my %retVal;
1045 :     # Loop until we run out of annotations.
1046 :     for my $text (@query) {
1047 :     # Check to see if this is a functional assignment.
1048 :     my ($user, $function) = ParseAssignment($text);
1049 :     if ($user) {
1050 :     # Here it is, so stuff it in the return hash.
1051 :     $retVal{$function} = $user;
1052 :     }
1053 :     }
1054 :     # Return the hash of assignments found.
1055 :     return %retVal;
1056 :     }
1057 :    
1058 :     =head3 FunctionOf
1059 :    
1060 :     C<< my $functionText = $sprout->FunctionOf($featureID, $userID); >>
1061 :    
1062 : parrello 1.3 Return the most recently-determined functional assignment of a particular feature.
1063 :    
1064 :     The functional assignment is handled differently depending on the type of feature. If
1065 :     the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1066 : parrello 1.1 assignment is a type of annotation. It has the format "XXXX\nset XXXX function to\nYYYYY". In this
1067 :     instance, XXXX is the user ID and YYYYY is the functional assignment text. Its worth noting that
1068 :     we cannot filter on the content of the annotation itself because it's a text field; however, this
1069 :     is not a big problem because most features only have a small number of annotations.
1070 :    
1071 : parrello 1.3 Each user has an associated list of trusted users. The assignment returned will be the most
1072 :     recent one by at least one of the trusted users. If no trusted user list is available, then
1073 :     the specified user and FIG are considered trusted. If the user ID is omitted, only FIG
1074 :     is trusted.
1075 :    
1076 :     If the feature is B<not> identified by a FIG ID, then the functional assignment
1077 :     information is taken from the B<ExternalAliasFunc> table. If the table does
1078 :     not contain an entry for the feature, an undefined value is returned.
1079 :    
1080 : parrello 1.1 =over 4
1081 :    
1082 :     =item featureID
1083 :    
1084 :     ID of the feature whose functional assignment is desired.
1085 :    
1086 :     =item userID (optional)
1087 :    
1088 : parrello 1.3 ID of the user whose function determination is desired. If omitted, only the latest
1089 :     C<FIG> assignment will be returned.
1090 : parrello 1.1
1091 :     =item RETURN
1092 :    
1093 :     Returns the text of the assigned function.
1094 :    
1095 :     =back
1096 :    
1097 :     =cut
1098 : parrello 1.3 #: Return Type $;
1099 : parrello 1.1 sub FunctionOf {
1100 :     # Get the parameters.
1101 :     my $self = shift @_;
1102 :     my ($featureID, $userID) = @_;
1103 : parrello 1.3 # Declare the return value.
1104 :     my $retVal;
1105 :     # Determine the ID type.
1106 :     if ($featureID =~ m/^fig\|/) {
1107 :     # Here we have a FIG feature ID. We must build the list of trusted
1108 :     # users.
1109 :     my %trusteeTable = ();
1110 :     # Check the user ID.
1111 :     if (!$userID) {
1112 :     # No user ID, so only FIG is trusted.
1113 :     $trusteeTable{FIG} = 1;
1114 :     } else {
1115 :     # Add this user's ID.
1116 :     $trusteeTable{$userID} = 1;
1117 :     # Look for the trusted users in the database.
1118 :     my @trustees = $self->GetFlat(['IsTrustedBy'], 'IsTrustedBy(from-link) = ?', [$userID], 'IsTrustedBy(to-link)');
1119 :     if (! @trustees) {
1120 :     # None were found, so build a default list.
1121 :     $trusteeTable{FIG} = 1;
1122 :     } else {
1123 :     # Otherwise, put all the trustees in.
1124 :     for my $trustee (@trustees) {
1125 :     $trusteeTable{$trustee} = 1;
1126 :     }
1127 :     }
1128 :     }
1129 :     # Build a query for all of the feature's annotations, sorted by date.
1130 :     my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],
1131 :     "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1132 :     [$featureID]);
1133 :     my $timeSelected = 0;
1134 :     # Loop until we run out of annotations.
1135 :     while (my $annotation = $query->Fetch()) {
1136 :     # Get the annotation text.
1137 :     my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);
1138 :     # Check to see if this is a functional assignment for a trusted user.
1139 :     my ($user, $type, $function) = split(/\n/, $text);
1140 :     if ($type =~ m/^set $user function to$/i) {
1141 :     # Here it is a functional assignment. Check the time and the user
1142 :     # name. The time must be recent and the user must be trusted.
1143 :     if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {
1144 :     $retVal = $function;
1145 :     $timeSelected = $time;
1146 :     }
1147 :     }
1148 :     }
1149 :     } else {
1150 :     # Here we have a non-FIG feature ID. In this case the user ID does not
1151 :     # matter. We simply get the information from the External Alias Function
1152 :     # table.
1153 : parrello 1.4 ($retVal) = $self->GetEntityValues('ExternalAliasFunc', $featureID, ['ExternalAliasFunc(func)']);
1154 : parrello 1.3 }
1155 : parrello 1.1 # Return the assignment found.
1156 :     return $retVal;
1157 :     }
1158 :    
1159 :     =head3 BBHList
1160 :    
1161 :     C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
1162 :    
1163 :     Return a hash mapping the features in a specified list to their bidirectional best hits
1164 :     on a specified target genome.
1165 :    
1166 :     =over 4
1167 :    
1168 :     =item genomeID
1169 :    
1170 :     ID of the genome from which the best hits should be taken.
1171 :    
1172 :     =item featureList
1173 :    
1174 :     List of the features whose best hits are desired.
1175 :    
1176 :     =item RETURN
1177 :    
1178 :     Returns a reference to a hash that maps the IDs of the incoming features to the IDs of
1179 :     their best hits.
1180 :    
1181 :     =back
1182 :    
1183 :     =cut
1184 : parrello 1.3 #: Return Type %;
1185 : parrello 1.1 sub BBHList {
1186 :     # Get the parameters.
1187 :     my $self = shift @_;
1188 :     my ($genomeID, $featureList) = @_;
1189 :     # Create the return structure.
1190 :     my %retVal = ();
1191 :     # Loop through the incoming features.
1192 :     for my $featureID (@{$featureList}) {
1193 :     # Create a query to get the feature's best hit.
1194 :     my $query = $self->Get(['IsBidirectionalBestHitOf'],
1195 :     "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",
1196 :     [$featureID, $genomeID]);
1197 :     # Look for the best hit.
1198 :     my $bbh = $query->Fetch;
1199 :     if ($bbh) {
1200 :     my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');
1201 :     $retVal{$featureID} = $targetFeature;
1202 :     }
1203 :     }
1204 :     # Return the mapping.
1205 :     return \%retVal;
1206 :     }
1207 :    
1208 :     =head3 FeatureAliases
1209 :    
1210 :     C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
1211 :    
1212 :     Return a list of the aliases for a specified feature.
1213 :    
1214 :     =over 4
1215 :    
1216 :     =item featureID
1217 :    
1218 :     ID of the feature whose aliases are desired.
1219 :    
1220 :     =item RETURN
1221 :    
1222 :     Returns a list of the feature's aliases. If the feature is not found or has no aliases, it will
1223 :     return an empty list.
1224 :    
1225 :     =back
1226 :    
1227 :     =cut
1228 : parrello 1.3 #: Return Type @;
1229 : parrello 1.1 sub FeatureAliases {
1230 :     # Get the parameters.
1231 :     my $self = shift @_;
1232 :     my ($featureID) = @_;
1233 :     # Get the desired feature's aliases
1234 :     my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(alias)']);
1235 :     # Return the result.
1236 :     return @retVal;
1237 :     }
1238 :    
1239 :     =head3 GenomeOf
1240 :    
1241 :     C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1242 :    
1243 :     Return the genome that contains a specified feature.
1244 :    
1245 :     =over 4
1246 :    
1247 :     =item featureID
1248 :    
1249 :     ID of the feature whose genome is desired.
1250 :    
1251 :     =item RETURN
1252 :    
1253 :     Returns the ID of the genome for the specified feature. If the feature is not found, returns
1254 :     an undefined value.
1255 :    
1256 :     =back
1257 :    
1258 :     =cut
1259 : parrello 1.3 #: Return Type $;
1260 : parrello 1.1 sub GenomeOf {
1261 :     # Get the parameters.
1262 :     my $self = shift @_;
1263 :     my ($featureID) = @_;
1264 :     # Create a query to find the genome associated with the feature.
1265 :     my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);
1266 :     # Declare the return value.
1267 :     my $retVal;
1268 :     # Get the genome ID.
1269 :     if (my $relationship = $query->Fetch()) {
1270 :     ($retVal) = $relationship->Value('HasContig(from-link)');
1271 :     }
1272 :     # Return the value found.
1273 :     return $retVal;
1274 :     }
1275 :    
1276 :     =head3 CoupledFeatures
1277 :    
1278 :     C<< my %coupleHash = $sprout->CoupledFeatures($featureID); >>
1279 :    
1280 :     Return the features functionally coupled with a specified feature. Features are considered
1281 :     functionally coupled if they tend to be clustered on the same chromosome.
1282 :    
1283 :     =over 4
1284 :    
1285 :     =item featureID
1286 :    
1287 :     ID of the feature whose functionally-coupled brethren are desired.
1288 :    
1289 :     =item RETURN
1290 :    
1291 :     A hash mapping the functionally-coupled feature IDs to the coupling score.
1292 :    
1293 :     =back
1294 :    
1295 :     =cut
1296 : parrello 1.3 #: Return Type %;
1297 : parrello 1.1 sub CoupledFeatures {
1298 :     # Get the parameters.
1299 :     my $self = shift @_;
1300 :     my ($featureID) = @_;
1301 :     # Create a query to retrieve the functionally-coupled features. Note that we depend on the
1302 :     # fact that the functional coupling is physically paired. If (A,B) is in the database, then
1303 :     # (B,A) will also be found.
1304 :     my $query = $self->Get(['IsClusteredOnChromosomeWith'],
1305 :     "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);
1306 :     # This value will be set to TRUE if we find at least one coupled feature.
1307 :     my $found = 0;
1308 :     # Create the return hash.
1309 :     my %retVal = ();
1310 :     # Retrieve the relationship records and store them in the hash.
1311 :     while (my $clustering = $query->Fetch()) {
1312 :     my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',
1313 :     'IsClusteredOnChromosomeWith(score)']);
1314 :     $retVal{$otherFeatureID} = $score;
1315 :     $found = 1;
1316 :     }
1317 :     # Functional coupling is reflexive. If we found at least one coupled feature, we must add
1318 :     # the incoming feature as well.
1319 :     if ($found) {
1320 :     $retVal{$featureID} = 9999;
1321 :     }
1322 :     # Return the hash.
1323 :     return %retVal;
1324 :     }
1325 :    
1326 :     =head3 GetEntityTypes
1327 :    
1328 :     C<< my @entityList = $sprout->GetEntityTypes(); >>
1329 :    
1330 :     Return the list of supported entity types.
1331 :    
1332 :     =cut
1333 : parrello 1.3 #: Return Type @;
1334 : parrello 1.1 sub GetEntityTypes {
1335 :     # Get the parameters.
1336 :     my $self = shift @_;
1337 :     # Get the underlying database object.
1338 :     my $erdb = $self->{_erdb};
1339 :     # Get its entity type list.
1340 :     my @retVal = $erdb->GetEntityTypes();
1341 :     }
1342 :    
1343 :     =head3 ReadFasta
1344 :    
1345 :     C<< my %sequenceData = Sprout::ReadFasta($fileName, $prefix); >>
1346 :    
1347 :     Read sequence data from a FASTA-format file. Each sequence in a FASTA file is represented by
1348 :     one or more lines of data. The first line begins with a > character and contains an ID.
1349 :     The remaining lines contain the sequence data in order.
1350 :    
1351 :     =over 4
1352 :    
1353 :     =item fileName
1354 :    
1355 :     Name of the FASTA file.
1356 :    
1357 :     =item prefix (optional)
1358 :    
1359 :     Prefix to be put in front of each ID found.
1360 :    
1361 :     =item RETURN
1362 :    
1363 :     Returns a hash that maps each ID to its sequence.
1364 :    
1365 :     =back
1366 :    
1367 :     =cut
1368 : parrello 1.3 #: Return Type %;
1369 : parrello 1.1 sub ReadFasta {
1370 :     # Get the parameters.
1371 :     my ($fileName, $prefix) = @_;
1372 :     # Create the return hash.
1373 :     my %retVal = ();
1374 :     # Open the file for input.
1375 :     open FASTAFILE, '<', $fileName;
1376 :     # Declare the ID variable and clear the sequence accumulator.
1377 :     my $sequence = "";
1378 :     my $id = "";
1379 :     # Loop through the file.
1380 :     while (<FASTAFILE>) {
1381 :     # Get the current line.
1382 :     my $line = $_;
1383 :     # Check for a header line.
1384 :     if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
1385 :     # Here we have a new header. Store the current sequence if we have one.
1386 :     if ($id) {
1387 :     $retVal{$id} = $sequence;
1388 :     }
1389 :     # Clear the sequence accumulator and save the new ID.
1390 :     ($id, $sequence) = ("$prefix$1", "");
1391 :     } else {
1392 :     # Here we have a data line, so we add it to the sequence accumulator.
1393 :     # First, we get the actual data out.
1394 :     $line =~ /^\s*(.*?)(\s|\n)/;
1395 :     $sequence .= $1;
1396 :     }
1397 :     }
1398 :     # Flush out the last sequence (if any).
1399 :     if ($sequence) {
1400 :     $retVal {$id} = $sequence;
1401 :     }
1402 :     # Return the hash constructed from the file.
1403 :     return %retVal;
1404 :     }
1405 :    
1406 :     =head3 FormatLocations
1407 :    
1408 :     C<< my @locations = $sprout->FormatLocations($prefix, \@locations, $oldFormat); >>
1409 :    
1410 :     Insure that a list of feature locations is in the Sprout format. The Sprout feature location
1411 :     format is I<contig>_I<beg*len> where I<*> is C<+> for a forward gene and C<-> for a backward
1412 :     gene. The old format is I<contig>_I<beg>_I<end>.
1413 :    
1414 :     =over 4
1415 :    
1416 :     =item prefix
1417 :    
1418 :     Prefix to be put in front of each contig ID (or an empty string if the contig ID should not
1419 :     be changed.
1420 :    
1421 :     =item locations
1422 :    
1423 :     List of locations to be normalized.
1424 :    
1425 :     =item oldFormat
1426 :    
1427 :     TRUE to convert the locations to the old format, else FALSE
1428 :    
1429 :     =item RETURN
1430 :    
1431 :     Returns a list of updated location descriptors.
1432 :    
1433 :     =back
1434 :    
1435 :     =cut
1436 : parrello 1.3 #: Return Type @;
1437 : parrello 1.1 sub FormatLocations {
1438 :     # Get the parameters.
1439 :     my $self = shift @_;
1440 :     my ($prefix, $locations, $oldFormat) = @_;
1441 :     # Create the return list.
1442 :     my @retVal = ();
1443 :     # Check to see if any locations were passed in.
1444 :     if ($locations eq '') {
1445 :     confess "No locations specified.";
1446 :     } else {
1447 :     # Loop through the locations, converting them to the new format.
1448 :     for my $location (@{$locations}) {
1449 :     # Parse the location elements.
1450 :     my ($contig, $beg, $dir, $len) = ParseLocation($location);
1451 :     # Process according to the desired output format.
1452 :     if (!$oldFormat) {
1453 :     # Here we're producing the new format. Add the location to the return list.
1454 :     push @retVal, "$prefix${contig}_$beg$dir$len";
1455 :     } elsif ($dir eq '+') {
1456 :     # Here we're producing the old format and it's a forward gene.
1457 :     my $end = $beg + $len - 1;
1458 :     push @retVal, "$prefix${contig}_${beg}_$end";
1459 :     } else {
1460 :     # Here we're producting the old format and it's a backward gene.
1461 :     my $end = $beg - $len + 1;
1462 :     push @retVal, "$prefix${contig}_${beg}_$end";
1463 :     }
1464 :     }
1465 :     }
1466 :     # Return the normalized list.
1467 :     return @retVal;
1468 :     }
1469 :    
1470 :     =head3 DumpData
1471 :    
1472 :     C<< $sprout->DumpData(); >>
1473 :    
1474 :     Dump all the tables to tab-delimited DTX files. The files will be stored in the data directory.
1475 :    
1476 :     =cut
1477 :    
1478 :     sub DumpData {
1479 :     # Get the parameters.
1480 :     my $self = shift @_;
1481 :     # Get the data directory name.
1482 :     my $outputDirectory = $self->{_options}->{dataDir};
1483 :     # Dump the relations.
1484 :     $self->{_erdb}->DumpRelations($outputDirectory);
1485 :     }
1486 :    
1487 :     =head3 XMLFileName
1488 :    
1489 :     C<< my $fileName = $sprout->XMLFileName(); >>
1490 :    
1491 :     Return the name of this database's XML definition file.
1492 :    
1493 :     =cut
1494 : parrello 1.3 #: Return Type $;
1495 : parrello 1.1 sub XMLFileName {
1496 :     my $self = shift @_;
1497 :     return $self->{_xmlName};
1498 :     }
1499 :    
1500 :     =head3 Insert
1501 :    
1502 :     C<< $sprout->Insert($objectType, \%fieldHash); >>
1503 :    
1504 :     Insert an entity or relationship instance into the database. The entity or relationship of interest
1505 :     is defined by a type name and then a hash of field names to values. Field values in the primary
1506 :     relation are represented by scalars. (Note that for relationships, the primary relation is
1507 :     the B<only> relation.) Field values for the other relations comprising the entity are always
1508 :     list references. For example, the following line inserts an inactive PEG feature named
1509 :     C<fig|188.1.peg.1> with aliases C<ZP_00210270.1> and C<gi|46206278>.
1510 :    
1511 :     C<< $sprout->Insert('Feature', { id => 'fig|188.1.peg.1', active => 0, feature-type => 'peg', alias => ['ZP_00210270.1', 'gi|46206278']}); >>
1512 :    
1513 :     The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
1514 :     property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
1515 :    
1516 :     C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
1517 :    
1518 :     =over 4
1519 :    
1520 :     =item newObjectType
1521 :    
1522 :     Type name of the entity or relationship to insert.
1523 :    
1524 :     =item fieldHash
1525 :    
1526 :     Hash of field names to values.
1527 :    
1528 :     =back
1529 :    
1530 :     =cut
1531 : parrello 1.3 #: Return Type ;
1532 : parrello 1.1 sub Insert {
1533 :     # Get the parameters.
1534 :     my $self = shift @_;
1535 :     my ($objectType, $fieldHash) = @_;
1536 :     # Call the underlying method.
1537 :     $self->{_erdb}->InsertObject($objectType, $fieldHash);
1538 :     }
1539 :    
1540 :     =head3 Annotate
1541 :    
1542 :     C<< my $ok = $sprout->Annotate($fid, $timestamp, $user, $text); >>
1543 :    
1544 :     Annotate a feature. This inserts an Annotation record into the database and links it to the
1545 :     specified feature and user.
1546 :    
1547 :     =over 4
1548 :    
1549 :     =item fid
1550 :    
1551 :     ID of the feature to be annotated.
1552 :    
1553 :     =item timestamp
1554 :    
1555 :     Numeric timestamp to apply to the annotation. This is concatenated to the feature ID to create the
1556 :     key.
1557 :    
1558 :     =item user
1559 :    
1560 :     ID of the user who is making the annotation.
1561 :    
1562 :     =item text
1563 :    
1564 :     Text of the annotation.
1565 :    
1566 :     =item RETURN
1567 :    
1568 :     Returns 1 if successful, 2 if an error occurred.
1569 :    
1570 :     =back
1571 :    
1572 :     =cut
1573 : parrello 1.3 #: Return Type $;
1574 : parrello 1.1 sub Annotate {
1575 :     # Get the parameters.
1576 :     my $self = shift @_;
1577 :     my ($fid, $timestamp, $user, $text) = @_;
1578 :     # Create the annotation ID.
1579 :     my $aid = "$fid:$timestamp";
1580 :     # Insert the Annotation object.
1581 :     my $retVal = $self->Insert('Annotation', { id => $aid, time => $timestamp, annotation => $text });
1582 :     if ($retVal) {
1583 :     # Connect it to the user.
1584 :     $retVal = $self->Insert('MadeAnnotation', { 'from-link' => $user, 'to-link' => $aid });
1585 :     if ($retVal) {
1586 :     # Connect it to the feature.
1587 :     $retVal = $self->Insert('IsTargetOfAnnotation', { 'from-link' => $fid,
1588 :     'to-link' => $aid });
1589 :     }
1590 :     }
1591 :     # Return the success indicator.
1592 :     return $retVal;
1593 :     }
1594 :    
1595 :     =head3 AssignFunction
1596 :    
1597 :     C<< my $ok = $sprout->AssignFunction($featureID, $user, $function); >>
1598 :    
1599 :     This method assigns a function to a feature. Functions are a special type of annotation. The general
1600 :     format is "XXXX\nset XXXX function to\nYYYYY" where XXXX is the feature type and YYYY is the functional
1601 :     assignment text.
1602 :    
1603 :     =over 4
1604 :    
1605 :     =item featureID
1606 :    
1607 :     ID of the feature to which the assignment is being made.
1608 :    
1609 :     =item user
1610 :    
1611 :     Name of the user making the assignment. This is frequently a group name, like C<kegg> or C<fig>.
1612 :    
1613 :     =item function
1614 :    
1615 :     Text of the function being assigned.
1616 :    
1617 :     =item RETURN
1618 :    
1619 :     Returns 1 if successful, 0 if an error occurred.
1620 :    
1621 :     =back
1622 :    
1623 :     =cut
1624 : parrello 1.3 #: Return Type $;
1625 : parrello 1.1 sub AssignFunction {
1626 :     # Get the parameters.
1627 :     my $self = shift @_;
1628 :     my ($featureID, $user, $function) = @_;
1629 :     # Create an annotation string from the parameters.
1630 :     my $annotationText = "$user\nset $user function to\n$function";
1631 :     # Get the current time.
1632 :     my $now = time;
1633 :     # Declare the return variable.
1634 :     my $retVal = 1;
1635 :     # Locate the genome containing the feature.
1636 :     my $genome = $self->GenomeOf($featureID);
1637 :     if (!$genome) {
1638 :     # Here the genome was not found. This probably means the feature ID is invalid.
1639 :     Trace("No genome found for feature $featureID.") if T(0);
1640 :     $retVal = 0;
1641 :     } else {
1642 :     # Here we know we have a feature with a genome. Store the annotation.
1643 :     $retVal = $self->Annotate($featureID, $now, $user, $annotationText);
1644 :     }
1645 :     # Return the success indicator.
1646 :     return $retVal;
1647 :     }
1648 :    
1649 :     =head3 FeaturesByAlias
1650 :    
1651 :     C<< my @features = $sprout->FeaturesByAlias($alias); >>
1652 :    
1653 :     Returns a list of features with the specified alias. The alias is parsed to determine
1654 :     the type of the alias. A string of digits is a GenBack ID and a string of exactly 6
1655 :     alphanumerics is a UniProt ID. A built-in FIG.pm method is used to analyze the alias
1656 :     string and attach the necessary prefix. If the result is a FIG ID then it is returned
1657 :     unmodified; otherwise, we look for an alias.
1658 :    
1659 :     =over 4
1660 :    
1661 :     =item alias
1662 :    
1663 :     Alias whose features are desired.
1664 :    
1665 :     =item RETURN
1666 :    
1667 :     Returns a list of the features with the given alias.
1668 :    
1669 :     =back
1670 :    
1671 :     =cut
1672 : parrello 1.3 #: Return Type @;
1673 : parrello 1.1 sub FeaturesByAlias {
1674 :     # Get the parameters.
1675 :     my $self = shift @_;
1676 :     my ($alias) = @_;
1677 :     # Declare the return variable.
1678 :     my @retVal = ();
1679 :     # Parse the alias.
1680 :     my ($mappedAlias, $flag) = FIGRules::NormalizeAlias($alias);
1681 :     # If it's a FIG alias, we're done.
1682 :     if ($flag) {
1683 :     push @retVal, $mappedAlias;
1684 :     } else {
1685 :     # Here we have a non-FIG alias. Get the features with the normalized alias.
1686 :     @retVal = $self->GetFlat(['Feature'], 'Feature(alias) = ?', [$mappedAlias], 'Feature(id)');
1687 :     }
1688 :     # Return the result.
1689 :     return @retVal;
1690 :     }
1691 :    
1692 :     =head3 Exists
1693 :    
1694 :     C<< my $found = $sprout->Exists($entityName, $entityID); >>
1695 :    
1696 :     Return TRUE if an entity exists, else FALSE.
1697 :    
1698 :     =over 4
1699 :    
1700 :     =item entityName
1701 :    
1702 :     Name of the entity type (e.g. C<Feature>) relevant to the existence check.
1703 :    
1704 :     =item entityID
1705 :    
1706 :     ID of the entity instance whose existence is to be checked.
1707 :    
1708 :     =item RETURN
1709 :    
1710 :     Returns TRUE if the entity instance exists, else FALSE.
1711 :    
1712 :     =back
1713 :    
1714 :     =cut
1715 : parrello 1.3 #: Return Type $;
1716 : parrello 1.1 sub Exists {
1717 :     # Get the parameters.
1718 :     my $self = shift @_;
1719 :     my ($entityName, $entityID) = @_;
1720 :     # Check for the entity instance.
1721 :     my $testInstance = $self->GetEntity($entityName, $entityID);
1722 :     # Return an existence indicator.
1723 :     my $retVal = ($testInstance ? 1 : 0);
1724 :     return $retVal;
1725 :     }
1726 :    
1727 :     =head3 FeatureTranslation
1728 :    
1729 :     C<< my $translation = $sprout->FeatureTranslation($featureID); >>
1730 :    
1731 :     Return the translation of a feature.
1732 :    
1733 :     =over 4
1734 :    
1735 :     =item featureID
1736 :    
1737 :     ID of the feature whose translation is desired
1738 :    
1739 :     =item RETURN
1740 :    
1741 :     Returns the translation of the specified feature.
1742 :    
1743 :     =back
1744 :    
1745 :     =cut
1746 : parrello 1.3 #: Return Type $;
1747 : parrello 1.1 sub FeatureTranslation {
1748 :     # Get the parameters.
1749 :     my $self = shift @_;
1750 :     my ($featureID) = @_;
1751 :     # Get the specified feature's translation.
1752 :     my ($retVal) = $self->GetEntityValues("Feature", $featureID, ['Feature(translation)']);
1753 :     return $retVal;
1754 :     }
1755 :    
1756 :     =head3 Taxonomy
1757 :    
1758 :     C<< my @taxonomyList = $sprout->Taxonomy($genome); >>
1759 :    
1760 :     Return the taxonomy of the specified genome. This will be in the form of a list
1761 :     containing the various classifications in order from domain (eg. C<Bacteria>, C<Archaea>,
1762 :     or C<Eukaryote>) to sub-species. For example,
1763 :    
1764 :     C<< (Bacteria, Proteobacteria, Gammaproteobacteria, Enterobacteriales, Enterobacteriaceae, Escherichia, Escherichia coli, Escherichia coli K12) >>
1765 :    
1766 :     =over 4
1767 :    
1768 :     =item genome
1769 :    
1770 :     ID of the genome whose taxonomy is desired.
1771 :    
1772 :     =item RETURN
1773 :    
1774 :     Returns a list containing all the taxonomy classifications for the specified genome's organism.
1775 :    
1776 :     =back
1777 :    
1778 :     =cut
1779 : parrello 1.3 #: Return Type @;
1780 : parrello 1.1 sub Taxonomy {
1781 :     # Get the parameters.
1782 :     my $self = shift @_;
1783 :     my ($genome) = @_;
1784 :     # Find the specified genome's taxonomy string.
1785 :     my ($list) = $self->GetEntityValues('Genome', $genome, ['Genome(taxonomy)']);
1786 :     # Declare the return variable.
1787 :     my @retVal = ();
1788 :     # If we found the genome, return its taxonomy string.
1789 :     if ($list) {
1790 :     @retVal = split /\s*;\s*/, $list;
1791 :     } else {
1792 :     Trace("Genome \"$genome\" does not have a taxonomy in the database.\n") if T(0);
1793 :     }
1794 :     # Return the value found.
1795 :     return @retVal;
1796 :     }
1797 :    
1798 :     =head3 CrudeDistance
1799 :    
1800 :     C<< my $distance = $sprout->CrudeDistance($genome1, $genome2); >>
1801 :    
1802 :     Returns a crude estimate of the distance between two genomes. The distance is construed so
1803 :     that it will be 0 for genomes with identical taxonomies and 1 for genomes from different domains.
1804 :    
1805 :     =over 4
1806 :    
1807 :     =item genome1
1808 :    
1809 :     ID of the first genome to compare.
1810 :    
1811 :     =item genome2
1812 :    
1813 :     ID of the second genome to compare.
1814 :    
1815 :     =item RETURN
1816 :    
1817 :     Returns a value from 0 to 1, with 0 meaning identical organisms, and 1 meaning organisms from
1818 :     different domains.
1819 :    
1820 :     =back
1821 :    
1822 :     =cut
1823 : parrello 1.3 #: Return Type $;
1824 : parrello 1.1 sub CrudeDistance {
1825 :     # Get the parameters.
1826 :     my $self = shift @_;
1827 :     my ($genome1, $genome2) = @_;
1828 :     # Insure that the distance is commutative by sorting the genome IDs.
1829 :     my ($genomeA, $genomeB);
1830 :     if ($genome2 < $genome2) {
1831 :     ($genomeA, $genomeB) = ($genome1, $genome2);
1832 :     } else {
1833 :     ($genomeA, $genomeB) = ($genome2, $genome1);
1834 :     }
1835 :     my @taxA = $self->Taxonomy($genomeA);
1836 :     my @taxB = $self->Taxonomy($genomeB);
1837 :     # Initialize the distance to 1. We'll reduce it each time we find a match between the
1838 :     # taxonomies.
1839 :     my $retVal = 1.0;
1840 :     # Initialize the subtraction amount. This amount determines the distance reduction caused
1841 :     # by a mismatch at the current level.
1842 :     my $v = 0.5;
1843 :     # Loop through the taxonomies.
1844 :     for (my $i = 0; ($i < @taxA) && ($i < @taxB) && ($taxA[$i] eq $taxB[$i]); $i++) {
1845 :     $retVal -= $v;
1846 :     $v /= 2;
1847 :     }
1848 :     return $retVal;
1849 :     }
1850 :    
1851 :     =head3 RoleName
1852 :    
1853 :     C<< my $roleName = $sprout->RoleName($roleID); >>
1854 :    
1855 :     Return the descriptive name of the role with the specified ID. In general, a role
1856 :     will only have a descriptive name if it is coded as an EC number.
1857 :    
1858 :     =over 4
1859 :    
1860 :     =item roleID
1861 :    
1862 :     ID of the role whose description is desired.
1863 :    
1864 :     =item RETURN
1865 :    
1866 :     Returns the descriptive name of the desired role.
1867 :    
1868 :     =back
1869 :    
1870 :     =cut
1871 : parrello 1.3 #: Return Type $;
1872 : parrello 1.1 sub RoleName {
1873 :     # Get the parameters.
1874 :     my $self = shift @_;
1875 :     my ($roleID) = @_;
1876 :     # Get the specified role's name.
1877 :     my ($retVal) = $self->GetEntityValues('Role', $roleID, ['Role(name)']);
1878 :     # Use the ID if the role has no name.
1879 :     if (!$retVal) {
1880 :     $retVal = $roleID;
1881 :     }
1882 :     # Return the name.
1883 :     return $retVal;
1884 :     }
1885 :    
1886 :     =head3 RoleDiagrams
1887 :    
1888 :     C<< my @diagrams = $sprout->RoleDiagrams($roleID); >>
1889 :    
1890 :     Return a list of the diagrams containing a specified functional role.
1891 :    
1892 :     =over 4
1893 :    
1894 :     =item roleID
1895 :    
1896 :     ID of the role whose diagrams are desired.
1897 :    
1898 :     =item RETURN
1899 :    
1900 :     Returns a list of the IDs for the diagrams that contain the specified functional role.
1901 :    
1902 :     =back
1903 :    
1904 :     =cut
1905 : parrello 1.3 #: Return Type @;
1906 : parrello 1.1 sub RoleDiagrams {
1907 :     # Get the parameters.
1908 :     my $self = shift @_;
1909 :     my ($roleID) = @_;
1910 :     # Query for the diagrams.
1911 :     my @retVal = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
1912 :     'RoleOccursIn(to-link)');
1913 :     # Return the result.
1914 :     return @retVal;
1915 :     }
1916 :    
1917 :     =head3 FeatureProperties
1918 :    
1919 :     C<< my @properties = $sprout->FeatureProperties($featureID); >>
1920 :    
1921 :     Return a list of the properties for the specified feature. Properties are key-value pairs
1922 :     that specify special characteristics of the feature. For example, a property could indicate
1923 :     that a feature is essential to the survival of the organism or that it has benign influence
1924 :     on the activities of a pathogen. Each property is returned as a triple of the form
1925 :     C<($key,$value,$url)>, where C<$key> is the property name, C<$value> is its value (commonly
1926 :     a 1 or a 0, but possibly a string or a floating-point value), and C<$url> is a string describing
1927 :     the web address or citation in which the property's value for the feature was identified.
1928 :    
1929 :     =over 4
1930 :    
1931 :     =item featureID
1932 :    
1933 :     ID of the feature whose properties are desired.
1934 :    
1935 :     =item RETURN
1936 :    
1937 :     Returns a list of triples, each triple containing the property name, its value, and a URL or
1938 :     citation.
1939 :    
1940 :     =back
1941 :    
1942 :     =cut
1943 : parrello 1.3 #: Return Type @@;
1944 : parrello 1.1 sub FeatureProperties {
1945 :     # Get the parameters.
1946 :     my $self = shift @_;
1947 :     my ($featureID) = @_;
1948 :     # Get the properties.
1949 :     my @retVal = $self->GetAll(['HasProperty', 'Property'], "HasProperty(from-link) = ?", [$featureID],
1950 :     ['Property(property-name)', 'Property(property-value)',
1951 :     'HasProperty(evidence)']);
1952 :     # Return the resulting list.
1953 :     return @retVal;
1954 :     }
1955 :    
1956 :     =head3 DiagramName
1957 :    
1958 :     C<< my $diagramName = $sprout->DiagramName($diagramID); >>
1959 :    
1960 :     Return the descriptive name of a diagram.
1961 :    
1962 :     =over 4
1963 :    
1964 :     =item diagramID
1965 :    
1966 :     ID of the diagram whose description is desired.
1967 :    
1968 :     =item RETURN
1969 :    
1970 :     Returns the descripive name of the specified diagram.
1971 :    
1972 :     =back
1973 :    
1974 :     =cut
1975 : parrello 1.3 #: Return Type $;
1976 : parrello 1.1 sub DiagramName {
1977 :     # Get the parameters.
1978 :     my $self = shift @_;
1979 :     my ($diagramID) = @_;
1980 :     # Get the specified diagram's name and return it.
1981 :     my ($retVal) = $self->GetEntityValues('Diagram', $diagramID, ['Diagram(name)']);
1982 :     return $retVal;
1983 :     }
1984 :    
1985 :     =head3 MergedAnnotations
1986 :    
1987 :     C<< my @annotationList = $sprout->MergedAnnotations(\@list); >>
1988 :    
1989 :     Returns a merged list of the annotations for the features in a list. Each annotation is
1990 :     represented by a 4-tuple of the form C<($fid, $timestamp, $userID, $annotation)>, where
1991 :     C<$fid> is the ID of a feature, C<$timestamp> is the time at which the annotation was made,
1992 :     C<$userID> is the ID of the user who made the annotation, and C<$annotation> is the annotation
1993 :     text. The list is sorted by timestamp.
1994 :    
1995 :     =over 4
1996 :    
1997 :     =item list
1998 :    
1999 :     List of the IDs for the features whose annotations are desired.
2000 :    
2001 :     =item RETURN
2002 :    
2003 :     Returns a list of annotation descriptions sorted by the annotation time.
2004 :    
2005 :     =back
2006 :    
2007 :     =cut
2008 : parrello 1.3 #: Return Type @;
2009 : parrello 1.1 sub MergedAnnotations {
2010 :     # Get the parameters.
2011 :     my $self = shift @_;
2012 :     my ($list) = @_;
2013 :     # Create a list to hold the annotation tuples found.
2014 :     my @tuples = ();
2015 :     # Loop through the features in the input list.
2016 :     for my $fid (@{$list}) {
2017 :     # Create a list of this feature's annotation tuples.
2018 :     my @newTuples = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
2019 :     "IsTargetOfAnnotation(from-link) = ?", [$fid],
2020 :     ['IsTargetOfAnnotation(from-link)', 'Annotation(time)',
2021 :     'MadeAnnotation(from-link)', 'Annotation(annotation)']);
2022 :     # Put it in the result list.
2023 :     push @tuples, @newTuples;
2024 :     }
2025 :     # Sort the result list by timestamp.
2026 :     my @retVal = sort { $a->[1] <=> $b->[1] } @tuples;
2027 : parrello 1.3 # Loop through and make the time stamps friendly.
2028 :     for my $tuple (@retVal) {
2029 :     $tuple->[1] = FriendlyTimestamp($tuple->[1]);
2030 :     }
2031 : parrello 1.1 # Return the sorted list.
2032 :     return @retVal;
2033 :     }
2034 :    
2035 :     =head3 RoleNeighbors
2036 :    
2037 :     C<< my @roleList = $sprout->RoleNeighbors($roleID); >>
2038 :    
2039 :     Returns a list of the roles that occur in the same diagram as the specified role. Because
2040 :     diagrams and roles are in a many-to-many relationship with each other, the list is
2041 :     essentially the set of roles from all of the maps that contain the incoming role. Such
2042 :     roles are considered neighbors because they are used together in cellular subsystems.
2043 :    
2044 :     =over 4
2045 :    
2046 :     =item roleID
2047 :    
2048 :     ID of the role whose neighbors are desired.
2049 :    
2050 :     =item RETURN
2051 :    
2052 :     Returns a list containing the IDs of the roles that are related to the incoming role.
2053 :    
2054 :     =back
2055 :    
2056 :     =cut
2057 : parrello 1.3 #: Return Type @;
2058 : parrello 1.1 sub RoleNeighbors {
2059 :     # Get the parameters.
2060 :     my $self = shift @_;
2061 :     my ($roleID) = @_;
2062 :     # Get all the diagrams containing this role.
2063 :     my @diagrams = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(from-link) = ?", [$roleID],
2064 :     'RoleOccursIn(to-link)');
2065 :     # Create the return list.
2066 :     my @retVal = ();
2067 :     # Loop through the diagrams.
2068 :     for my $diagramID (@diagrams) {
2069 :     # Get all the roles in this diagram.
2070 :     my @roles = $self->GetFlat(['RoleOccursIn'], "RoleOccursIn(to-link) = ?", [$diagramID],
2071 :     'RoleOccursIn(from-link)');
2072 :     # Add them to the return list.
2073 :     push @retVal, @roles;
2074 :     }
2075 :     # Merge the duplicates from the list.
2076 :     return Tracer::Merge(@retVal);
2077 :     }
2078 :    
2079 :     =head3 FeatureLinks
2080 :    
2081 :     C<< my @links = $sprout->FeatureLinks($featureID); >>
2082 :    
2083 :     Return a list of the web hyperlinks associated with a feature. The web hyperlinks are
2084 :     to external websites describing either the feature itself or the organism containing it
2085 :     and are represented in raw HTML.
2086 :    
2087 :     =over 4
2088 :    
2089 :     =item featureID
2090 :    
2091 :     ID of the feature whose links are desired.
2092 :    
2093 :     =item RETURN
2094 :    
2095 :     Returns a list of the web links for this feature.
2096 :    
2097 :     =back
2098 :    
2099 :     =cut
2100 : parrello 1.3 #: Return Type @;
2101 : parrello 1.1 sub FeatureLinks {
2102 :     # Get the parameters.
2103 :     my $self = shift @_;
2104 :     my ($featureID) = @_;
2105 :     # Get the feature's links.
2106 :     my @retVal = $self->GetEntityValues('Feature', $featureID, ['Feature(link)']);
2107 :     # Return the feature's links.
2108 :     return @retVal;
2109 :     }
2110 :    
2111 :     =head3 SubsystemsOf
2112 :    
2113 :     C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2114 :    
2115 :     Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
2116 :     to the role the feature performs.
2117 :    
2118 :     =over 4
2119 :    
2120 :     =item featureID
2121 :    
2122 :     ID of the feature whose subsystems are desired.
2123 :    
2124 :     =item RETURN
2125 :    
2126 :     Returns a hash mapping all the feature's subsystems to the feature's role.
2127 :    
2128 :     =back
2129 :    
2130 :     =cut
2131 : parrello 1.3 #: Return Type %;
2132 : parrello 1.1 sub SubsystemsOf {
2133 :     # Get the parameters.
2134 :     my $self = shift @_;
2135 :     my ($featureID) = @_;
2136 :     # Use the SSCell to connect features to subsystems.
2137 :     my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2138 :     "ContainsFeature(to-link) = ?", [$featureID],
2139 :     ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2140 :     # Create the return value.
2141 :     my %retVal = ();
2142 :     # Loop through the results, adding them to the hash.
2143 :     for my $record (@subsystems) {
2144 :     $retVal{$record->[0]} = $record->[1];
2145 :     }
2146 :     # Return the hash.
2147 :     return %retVal;
2148 :     }
2149 :    
2150 :     =head3 RelatedFeatures
2151 :    
2152 :     C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
2153 :    
2154 :     Return a list of the features which are bi-directional best hits of the specified feature and
2155 :     have been assigned the specified function by the specified user. If no such features exists,
2156 :     an empty list will be returned.
2157 :    
2158 :     =over 4
2159 :    
2160 :     =item featureID
2161 :    
2162 :     ID of the feature to whom the desired features are related.
2163 :    
2164 :     =item function
2165 :    
2166 :     Functional assignment (as returned by C</FunctionOf>) that is used to determine which related
2167 :     features should be selected.
2168 :    
2169 :     =item userID
2170 :    
2171 :     ID of the user whose functional assignments are to be used. If omitted, C<FIG> is assumed.
2172 :    
2173 :     =item RETURN
2174 :    
2175 :     Returns a list of the related features with the specified function.
2176 :    
2177 :     =back
2178 :    
2179 :     =cut
2180 : parrello 1.3 #: Return Type @;
2181 : parrello 1.1 sub RelatedFeatures {
2182 :     # Get the parameters.
2183 :     my $self = shift @_;
2184 :     my ($featureID, $function, $userID) = @_;
2185 :     # Get a list of the features that are BBHs of the incoming feature.
2186 :     my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],
2187 :     "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],
2188 :     'IsBidirectionalBestHitOf(to-link)');
2189 :     # Now we loop through the features, pulling out the ones that have the correct
2190 :     # functional assignment.
2191 :     my @retVal = ();
2192 :     for my $bbhFeature (@bbhFeatures) {
2193 :     # Get this feature's functional assignment.
2194 :     my $newFunction = $self->FunctionOf($bbhFeature, $userID);
2195 :     # If it matches, add it to the result list.
2196 :     if ($newFunction eq $function) {
2197 :     push @retVal, $bbhFeature;
2198 :     }
2199 :     }
2200 :     # Return the result list.
2201 :     return @retVal;
2202 :     }
2203 :    
2204 :     =head3 TaxonomySort
2205 :    
2206 :     C<< my @sortedFeatureIDs = $sprout->TaxonomySort(\@featureIDs); >>
2207 :    
2208 :     Return a list formed by sorting the specified features by the taxonomy of the containing
2209 :     genome. This will cause genomes from similar organisms to float close to each other.
2210 :    
2211 :     This task could almost be handled by the database; however, the taxonomy string in the
2212 :     database is a text field and can't be indexed. Instead, we create a hash table that maps
2213 :     taxonomy strings to lists of features. We then process the hash table using a key sort
2214 :     and merge the feature lists together to create the output.
2215 :    
2216 :     =over 4
2217 :    
2218 :     =item $featureIDs
2219 :    
2220 :     List of features to be taxonomically sorted.
2221 :    
2222 :     =item RETURN
2223 :    
2224 :     Returns the list of features sorted by the taxonomies of the containing genomes.
2225 :    
2226 :     =back
2227 :    
2228 :     =cut
2229 : parrello 1.3 #: Return Type @;
2230 : parrello 1.1 sub TaxonomySort {
2231 :     # Get the parameters.
2232 :     my $self = shift @_;
2233 :     my ($featureIDs) = @_;
2234 :     # Create the working hash table.
2235 :     my %hashBuffer = ();
2236 :     # Loop through the features.
2237 :     for my $fid (@{$featureIDs}) {
2238 :     # Get the taxonomy of the feature's genome.
2239 :     my ($taxonomy) = $self->GetFlat(['IsLocatedIn', 'HasContig', 'Genome'], "IsLocatedIn(from-link) = ?",
2240 :     [$fid], 'Genome(taxonomy)');
2241 :     # Add this feature to the hash buffer.
2242 :     if (exists $hashBuffer{$taxonomy}) {
2243 :     push @{$hashBuffer{$taxonomy}}, $fid;
2244 :     } else {
2245 :     $hashBuffer{$taxonomy} = [$fid];
2246 :     }
2247 :     }
2248 :     # Sort the keys and get the elements.
2249 :     my @retVal = ();
2250 :     for my $taxon (sort keys %hashBuffer) {
2251 :     push @retVal, @{$hashBuffer{$taxon}};
2252 :     }
2253 :     # Return the result.
2254 :     return @retVal;
2255 :     }
2256 :    
2257 :     =head3 GetAll
2258 :    
2259 :     C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>
2260 :    
2261 :     Return a list of values taken from the objects returned by a query. The first three
2262 :     parameters correspond to the parameters of the L</Get> method. The final parameter is
2263 :     a list of the fields desired from each record found by the query. The field name
2264 :     syntax is the standard syntax used for fields in the B<ERDB> system--
2265 :     B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity
2266 :     or relationship and I<fieldName> is the name of the field.
2267 :    
2268 :     The list returned will be a list of lists. Each element of the list will contain
2269 :     the values returned for the fields specified in the fourth parameter. If one of the
2270 :     fields specified returns multiple values, they are flattened in with the rest. For
2271 :     example, the following call will return a list of the features in a particular
2272 :     spreadsheet cell, and each feature will be represented by a list containing the
2273 :     feature ID followed by all of its aliases.
2274 :    
2275 :     C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>
2276 :    
2277 :     =over 4
2278 :    
2279 :     =item objectNames
2280 :    
2281 :     List containing the names of the entity and relationship objects to be retrieved.
2282 :    
2283 :     =item filterClause
2284 :    
2285 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
2286 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
2287 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
2288 :     parameter list as additional parameters. The fields in a filter clause can come from primary
2289 :     entity relations, relationship relations, or secondary entity relations; however, all of the
2290 :     entities and relationships involved must be included in the list of object names.
2291 :    
2292 :     =item parameterList
2293 :    
2294 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
2295 :    
2296 :     =item fields
2297 :    
2298 :     List of the fields to be returned in each element of the list returned.
2299 :    
2300 :     =item count
2301 :    
2302 :     Maximum number of records to return. If omitted or 0, all available records will be returned.
2303 :    
2304 :     =item RETURN
2305 :    
2306 :     Returns a list of list references. Each element of the return list contains the values for the
2307 :     fields specified in the B<fields> parameter.
2308 :    
2309 :     =back
2310 :    
2311 :     =cut
2312 : parrello 1.3 #: Return Type @@;
2313 : parrello 1.1 sub GetAll {
2314 :     # Get the parameters.
2315 :     my $self = shift @_;
2316 :     my ($objectNames, $filterClause, $parameterList, $fields, $count) = @_;
2317 :     # Create the query.
2318 :     my $query = $self->Get($objectNames, $filterClause, $parameterList);
2319 :     # Set up a counter of the number of records read.
2320 :     my $fetched = 0;
2321 :     # Insure the counter has a value.
2322 :     if (!defined $count) {
2323 :     $count = 0;
2324 :     }
2325 :     # Loop through the records returned, extracting the fields. Note that if the
2326 :     # counter is non-zero, we stop when the number of records read hits the count.
2327 :     my @retVal = ();
2328 :     while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {
2329 :     my @rowData = $row->Values($fields);
2330 :     push @retVal, \@rowData;
2331 :     $fetched++;
2332 :     }
2333 :     # Return the resulting list.
2334 :     return @retVal;
2335 :     }
2336 :    
2337 :     =head3 GetFlat
2338 :    
2339 :     C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>
2340 :    
2341 :     This is a variation of L</GetAll> that asks for only a single field per record and
2342 :     returns a single flattened list.
2343 :    
2344 :     =over 4
2345 :    
2346 :     =item objectNames
2347 :    
2348 :     List containing the names of the entity and relationship objects to be retrieved.
2349 :    
2350 :     =item filterClause
2351 :    
2352 :     WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can
2353 :     be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form
2354 :     B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the
2355 :     parameter list as additional parameters. The fields in a filter clause can come from primary
2356 :     entity relations, relationship relations, or secondary entity relations; however, all of the
2357 :     entities and relationships involved must be included in the list of object names.
2358 :    
2359 :     =item parameterList
2360 :    
2361 :     List of the parameters to be substituted in for the parameters marks in the filter clause.
2362 :    
2363 :     =item field
2364 :    
2365 :     Name of the field to be used to get the elements of the list returned.
2366 :    
2367 :     =item RETURN
2368 :    
2369 :     Returns a list of values.
2370 :    
2371 :     =back
2372 :    
2373 :     =cut
2374 : parrello 1.3 #: Return Type @;
2375 : parrello 1.1 sub GetFlat {
2376 :     # Get the parameters.
2377 :     my $self = shift @_;
2378 :     my ($objectNames, $filterClause, $parameterList, $field) = @_;
2379 :     # Construct the query.
2380 :     my $query = $self->Get($objectNames, $filterClause, $parameterList);
2381 :     # Create the result list.
2382 :     my @retVal = ();
2383 :     # Loop through the records, adding the field values found to the result list.
2384 :     while (my $row = $query->Fetch()) {
2385 :     push @retVal, $row->Value($field);
2386 :     }
2387 :     # Return the list created.
2388 :     return @retVal;
2389 :     }
2390 :    
2391 :     =head3 Protein
2392 :    
2393 :     C<< my $protein = Sprout::Protein($sequence, $table); >>
2394 :    
2395 :     Translate a DNA sequence into a protein sequence.
2396 :    
2397 :     =over 4
2398 :    
2399 :     =item sequence
2400 :    
2401 :     DNA sequence to translate.
2402 :    
2403 :     =item table (optional)
2404 :    
2405 :     Reference to a Hash that translates DNA triples to proteins. A triple that does not
2406 :     appear in the hash will be translated automatically to C<X>.
2407 :    
2408 :     =item RETURN
2409 :    
2410 :     Returns the protein sequence that would be created by the DNA sequence.
2411 :    
2412 :     =back
2413 :    
2414 :     =cut
2415 :    
2416 :     # This is the translation table for protein synthesis.
2417 :     my $ProteinTable = { AAA => 'K', AAG => 'K', AAT => 'N', AAC => 'N',
2418 :     AGA => 'R', AGG => 'R', AGT => 'S', AGC => 'S',
2419 :     ATA => 'I', ATG => 'M', ATT => 'I', ATC => 'I',
2420 :     ACA => 'T', ACG => 'T', ACT => 'T', ACC => 'T',
2421 :     GAA => 'E', GAG => 'E', GAT => 'D', GAC => 'D',
2422 :     GTA => 'V', GTG => 'V', GTT => 'V', GTC => 'V',
2423 :     GGA => 'G', GGG => 'G', GGT => 'G', GGC => 'G',
2424 :     GCA => 'A', GCG => 'A', GCT => 'A', GCC => 'A',
2425 :     CAA => 'Q', CAG => 'Q', CAT => 'H', CAC => 'H',
2426 :     CTA => 'L', CTG => 'L', CTT => 'L', CTC => 'L',
2427 :     CGA => 'R', CGG => 'R', CGT => 'R', CGC => 'R',
2428 :     CCA => 'P', CCG => 'P', CCT => 'P', CCC => 'P',
2429 :     TAA => '*', TAG => '*', TAT => 'Y', TAC => 'Y',
2430 :     TGA => '*', TGG => 'W', TGT => 'C', TGC => 'C',
2431 :     TTA => 'L', TTG => 'L', TTT => 'F', TTC => 'F',
2432 :     TCA => 'S', TCG => 'S', TCT => 'S', TCC => 'S',
2433 :     AAR => 'K', AAY => 'N',
2434 :     AGR => 'R', AGY => 'S',
2435 :     ATY => 'I',
2436 :     ACR => 'T', ACY => 'T', 'ACX' => 'T',
2437 :     GAR => 'E', GAY => 'D',
2438 :     GTR => 'V', GTY => 'V', GTX => 'V',
2439 :     GGR => 'G', GGY => 'G', GGX => 'G',
2440 :     GCR => 'A', GCY => 'A', GCX => 'A',
2441 :     CAR => 'Q', CAY => 'H',
2442 :     CTR => 'L', CTY => 'L', CTX => 'L',
2443 :     CGR => 'R', CGY => 'R', CGX => 'R',
2444 :     CCR => 'P', CCY => 'P', CCX => 'P',
2445 :     TAR => '*', TAY => 'Y',
2446 :     TGY => 'C',
2447 :     TTR => 'L', TTY => 'F',
2448 :     TCR => 'S', TCY => 'S', TCX => 'S'
2449 :     };
2450 :    
2451 :     sub Protein {
2452 :     # Get the paraeters.
2453 :     my ($sequence, $table) = @_;
2454 :     # If no table was specified, use the default.
2455 :     if (!$table) {
2456 :     $table = $ProteinTable;
2457 :     }
2458 :     # Create the return value.
2459 :     my $retVal = "";
2460 :     # Loop through the input triples.
2461 :     my $n = length $sequence;
2462 :     for (my $i = 0; $i < $n; $i += 3) {
2463 :     # Get the current triple from the sequence.
2464 :     my $triple = substr($sequence, $i, 3);
2465 :     # Translate it using the table.
2466 :     my $protein = "X";
2467 :     if (exists $table->{$triple}) { $protein = $table->{$triple}; }
2468 :     $retVal .= $protein;
2469 :     }
2470 :     # Remove the stop codon (if any).
2471 :     $retVal =~ s/\*$//;
2472 :     # Return the result.
2473 :     return $retVal;
2474 :     }
2475 :    
2476 :     =head3 LoadInfo
2477 :    
2478 :     C<< my ($dirName, @relNames) = $sprout->LoadInfo(); >>
2479 :    
2480 :     Return the name of the directory from which data is to be loaded and a list of the relation
2481 :     names. This information is useful when trying to analyze what needs to be put where in order
2482 :     to load the entire database.
2483 :    
2484 :     =cut
2485 : parrello 1.3 #: Return Type @;
2486 : parrello 1.1 sub LoadInfo {
2487 :     # Get the parameters.
2488 :     my $self = shift @_;
2489 :     # Create the return list, priming it with the name of the data directory.
2490 :     my @retVal = ($self->{_options}->{dataDir});
2491 :     # Concatenate the table names.
2492 :     push @retVal, $self->{_erdb}->GetTableNames();
2493 :     # Return the result.
2494 :     return @retVal;
2495 :     }
2496 :    
2497 :     =head3 LowBBHs
2498 :    
2499 :     C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>
2500 :    
2501 :     Return the bidirectional best hits of a feature whose score is no greater than a
2502 :     specified cutoff value. A higher cutoff value will allow inclusion of hits with
2503 :     a greater score. The value returned is a map of feature IDs to scores.
2504 :    
2505 :     =over 4
2506 :    
2507 :     =item featureID
2508 :    
2509 :     ID of the feature whose best hits are desired.
2510 :    
2511 :     =item cutoff
2512 :    
2513 :     Maximum permissible score for inclusion in the results.
2514 :    
2515 :     =item RETURN
2516 :    
2517 :     Returns a hash mapping feature IDs to scores.
2518 :    
2519 :     =back
2520 :    
2521 :     =cut
2522 : parrello 1.3 #: Return Type %;
2523 : parrello 1.1 sub LowBBHs {
2524 :     # Get the parsameters.
2525 :     my $self = shift @_;
2526 :     my ($featureID, $cutoff) = @_;
2527 :     # Create the return hash.
2528 :     my %retVal = ();
2529 :     # Create a query to get the desired BBHs.
2530 :     my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],
2531 :     'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',
2532 :     [$cutoff, $featureID],
2533 :     ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);
2534 :     # Form the results into the return hash.
2535 :     for my $pair (@bbhList) {
2536 :     $retVal{$pair->[0]} = $pair->[1];
2537 :     }
2538 :     # Return the result.
2539 :     return %retVal;
2540 :     }
2541 :    
2542 :     =head2 Internal Utility Methods
2543 :    
2544 :     =head3 ParseAssignment
2545 :    
2546 :     Parse annotation text to determine whether or not it is a functional assignment. If it is,
2547 :     the user and function text will be returned as a 2-element list. If it isn't, an empty list
2548 :     will be returned.
2549 :    
2550 :     This is a static method.
2551 :    
2552 :     =over 4
2553 :    
2554 :     =item text
2555 :    
2556 :     Text of the annotation.
2557 :    
2558 :     =item RETURN
2559 :    
2560 :     Returns an empty list if the annotation is not a functional assignment; otherwise, returns
2561 :     a two-element list containing the user name and the function text.
2562 :    
2563 :     =back
2564 :    
2565 :     =cut
2566 :    
2567 :     sub ParseAssignment {
2568 :     # Get the parameters.
2569 :     my ($text) = @_;
2570 :     # Declare the return value.
2571 :     my @retVal = ();
2572 :     # Check to see if this is a functional assignment.
2573 :     my ($user, $type, $function) = split(/\n/, $text);
2574 :     if ($type =~ m/^set $user function to$/i) {
2575 :     # Here it is, so we return the user name and function text.
2576 :     @retVal = ($user, $function);
2577 :     }
2578 :     # Return the result list.
2579 :     return @retVal;
2580 :     }
2581 :    
2582 : parrello 1.3 =head3 FriendlyTimestamp
2583 :    
2584 :     Convert a time number to a user-friendly time stamp for display.
2585 :    
2586 :     This is a static method.
2587 :    
2588 :     =over 4
2589 :    
2590 :     =item timeValue
2591 :    
2592 :     Numeric time value.
2593 :    
2594 :     =item RETURN
2595 :    
2596 :     Returns a string containing the same time in user-readable format.
2597 :    
2598 :     =back
2599 :    
2600 :     =cut
2601 :    
2602 :     sub FriendlyTimestamp {
2603 :     my ($timeValue) = @_;
2604 :     my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));
2605 :     return $retVal;
2606 :     }
2607 :    
2608 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3