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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3