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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3