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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3