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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3