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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3