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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3