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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3