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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster