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

Annotation of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3