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

Diff of /Sprout/Sprout.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12, Wed May 4 03:24:43 2005 UTC revision 1.83, Wed Sep 6 19:30:22 2006 UTC
# Line 1  Line 1 
1  package Sprout;  package Sprout;
2    
3        require Exporter;
4        use ERDB;
5        @ISA = qw(Exporter ERDB);
6          use Data::Dumper;          use Data::Dumper;
7          use strict;          use strict;
8          use Carp;          use Carp;
# Line 7  Line 10 
10          use XML::Simple;          use XML::Simple;
11          use DBQuery;          use DBQuery;
12          use DBObject;          use DBObject;
         use ERDB;  
13          use Tracer;          use Tracer;
14          use FIGRules;          use FIGRules;
15        use FidCheck;
16          use Stats;          use Stats;
17      use POSIX qw(strftime);      use POSIX qw(strftime);
18        use BasicLocation;
19    
20  =head1 Sprout Database Manipulation Object  =head1 Sprout Database Manipulation Object
21    
# Line 32  Line 35 
35  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and  query tasks. For example, L</genomes> lists the IDs of all the genomes in the database and
36  L</dna_seq> returns the DNA sequence for a specified genome location.  L</dna_seq> returns the DNA sequence for a specified genome location.
37    
38    The Sprout object is a subclass of the ERDB object and inherits all its properties and methods.
39    
40  =cut  =cut
41    
42  #: Constructor SFXlate->new_sprout_only();  #: Constructor SFXlate->new_sprout_only();
# Line 62  Line 67 
67    
68  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)  * B<xmlFileName> name of the XML file containing the database definition (default C<SproutDBD.xml>)
69    
70  * B<userData> user name and password, delimited by a slash (default C<root/>)  * B<userData> user name and password, delimited by a slash (default same as SEED)
71    
72  * B<port> connection port (default C<0>)  * B<port> connection port (default C<0>)
73    
74    * B<sock> connection socket (default same as SEED)
75    
76  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)  * B<maxSegmentLength> maximum number of residues per feature segment, (default C<4500>)
77    
78  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)  * B<maxSequenceLength> maximum number of residues per sequence, (default C<8000>)
79    
80    * B<noDBOpen> suppresses the connection to the database if TRUE, else FALSE
81    
82  =back  =back
83    
84  For example, the following constructor call specifies a database named I<Sprout> and a user name of  For example, the following constructor call specifies a database named I<Sprout> and a user name of
# Line 83  Line 92 
92  sub new {  sub new {
93          # Get the parameters.          # Get the parameters.
94          my ($class, $dbName, $options) = @_;          my ($class, $dbName, $options) = @_;
95        # Compute the DBD directory.
96        my $dbd_dir = (defined($FIG_Config::dbd_dir) ? $FIG_Config::dbd_dir :
97                                                      $FIG_Config::fig );
98          # Compute the options. We do this by starting with a table of defaults and overwriting with          # Compute the options. We do this by starting with a table of defaults and overwriting with
99          # the incoming data.          # the incoming data.
100          my $optionTable = Tracer::GetOptions({          my $optionTable = Tracer::GetOptions({
101                                             dbType               => 'mysql',                     # database type                         dbType       => $FIG_Config::dbms,
102                                             dataDir              => 'Data',                      # data file directory                                                          # database type
103                                             xmlFileName  => 'SproutDBD.xml', # database definition file name                         dataDir      => $FIG_Config::sproutData,
104                                             userData             => 'root/',                     # user name and password                                                          # data file directory
105                                             port                 => 0,                           # database connection port                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
106                                                            # database definition file name
107                           userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
108                                                            # user name and password
109                           port         => $FIG_Config::dbport,
110                                                            # database connection port
111                           sock         => $FIG_Config::dbsock,
112                           host         => $FIG_Config::dbhost,
113                                             maxSegmentLength => 4500,            # maximum feature segment length                                             maxSegmentLength => 4500,            # maximum feature segment length
114                                             maxSequenceLength => 8000,           # maximum contig sequence length                                             maxSequenceLength => 8000,           # maximum contig sequence length
115                           noDBOpen     => 0,               # 1 to suppress the database open
116                                            }, $options);                                            }, $options);
117          # Get the data directory.          # Get the data directory.
118          my $dataDir = $optionTable->{dataDir};          my $dataDir = $optionTable->{dataDir};
# Line 100  Line 120 
120          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;          $optionTable->{userData} =~ m!([^/]*)/(.*)$!;
121          my ($userName, $password) = ($1, $2);          my ($userName, $password) = ($1, $2);
122          # Connect to the database.          # Connect to the database.
123          my $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName, $password, $optionTable->{port});      my $dbh;
124        if (! $optionTable->{noDBOpen}) {
125            $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
126                                    $password, $optionTable->{port}, $optionTable->{host}, $optionTable->{sock});
127        }
128          # Create the ERDB object.          # Create the ERDB object.
129          my $xmlFileName = "$optionTable->{xmlFileName}";          my $xmlFileName = "$optionTable->{xmlFileName}";
130          my $erdb = ERDB->new($dbh, $xmlFileName);      my $retVal = ERDB::new($class, $dbh, $xmlFileName);
131          # Create this object.      # Add the option table and XML file name.
132          my $self = { _erdb => $erdb, _options => $optionTable, _xmlName => $xmlFileName };      $retVal->{_options} = $optionTable;
133          # Bless and return it.      $retVal->{_xmlName} = $xmlFileName;
134          bless $self;      # Return it.
135          return $self;      return $retVal;
136  }  }
137    
138  =head3 MaxSegment  =head3 MaxSegment
# Line 143  Line 167 
167          return $self->{_options}->{maxSequenceLength};          return $self->{_options}->{maxSequenceLength};
168  }  }
169    
 =head3 Get  
   
 C<< my $query = $sprout->Get(\@objectNames, $filterClause, \@parameterList); >>  
   
 This method allows a general query against the Sprout data using a specified filter clause.  
   
 The filter is a standard WHERE/ORDER BY clause with question marks as parameter markers and each  
 field name represented in the form B<I<objectName>(I<fieldName>)>. For example, the  
 following call requests all B<Genome> objects for the genus specified in the variable  
 $genus.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ?", [$genus]); >>  
   
 The WHERE clause contains a single question mark, so there is a single additional  
 parameter representing the parameter value. It would also be possible to code  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = \'$genus\'"); >>  
   
 however, this version of the call would generate a syntax error if there were any quote  
 characters inside the variable C<$genus>.  
   
 The use of the strange parenthesized notation for field names enables us to distinguish  
 hyphens contained within field names from minus signs that participate in the computation  
 of the WHERE clause. All of the methods that manipulate fields will use this same notation.  
   
 It is possible to specify multiple entity and relationship names in order to retrieve more than  
 one object's data at the same time, which allows highly complex joined queries. For example,  
   
 C<< $query = $sprout->Get(['Genome', 'ComesFrom', 'Source'], "Genome(genus) = ?", [$genus]); >>  
   
 This query returns all the genomes for a particular genus and allows access to the  
 sources from which they came. The join clauses to go from Genome to Source are generated  
 automatically.  
   
 Finally, the filter clause can contain sort information. To do this, simply put an C<ORDER BY>  
 clause at the end of the filter. Field references in the ORDER BY section follow the same rules  
 as they do in the filter itself; in other words, each one must be of the form B<I<objectName>(I<fieldName>)>.  
 For example, the following filter string gets all genomes for a particular genus and sorts  
 them by species name.  
   
 C<< $query = $sprout->Get(['Genome'], "Genome(genus) = ? ORDER BY Genome(species)", [$genus]); >>  
   
 It is also permissible to specify I<only> an ORDER BY clause. For example, the following invocation gets  
 all genomes ordered by genus and species.  
   
 C<< $query = $sprout->Get(['Genome'], "ORDER BY Genome(genus), Genome(species)"); >>  
   
 Odd things may happen if one of the ORDER BY fields is in a secondary relation. So, for example, an  
 attempt to order B<Feature>s by alias may (depending on the underlying database engine used) cause  
 a single feature to appear more than once.  
   
 If multiple names are specified, then the query processor will automatically determine a  
 join path between the entities and relationships. The algorithm used is very simplistic.  
 In particular, you can't specify any entity or relationship more than once, and if a  
 relationship is recursive, the path is determined by the order in which the entity  
 and the relationship appear. For example, consider a recursive relationship B<IsParentOf>  
 which relates B<People> objects to other B<People> objects. If the join path is  
 coded as C<['People', 'IsParentOf']>, then the people returned will be parents. If, however,  
 the join path is C<['IsParentOf', 'People']>, then the people returned will be children.  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item RETURN  
   
 Returns a B<DBQuery> that can be used to iterate through all of the results.  
   
 =back  
   
 =cut  
   
 sub Get {  
         # Get the parameters.  
         my ($self, $objectNames, $filterClause, $parameterList) = @_;  
         # We differ from the ERDB Get method in that the parameter list is passed in as a list reference  
         # rather than a list of parameters. The next step is to convert the parameters from a reference  
         # to a real list. We can only do this if the parameters have been specified.  
         my @parameters;  
         if ($parameterList) { @parameters = @{$parameterList}; }  
         return $self->{_erdb}->Get($objectNames, $filterClause, @parameters);  
 }  
   
 =head3 GetEntity  
   
 C<< my $entityObject = $sprout->GetEntity($entityType, $ID); >>  
   
 Return an object describing the entity instance with a specified ID.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item RETURN  
   
 Returns a B<DBObject> representing the desired entity instance, or an undefined value if no  
 instance is found with the specified key.  
   
 =back  
   
 =cut  
   
 sub GetEntity {  
         # Get the parameters.  
         my ($self, $entityType, $ID) = @_;  
         # Call the ERDB method.  
         return $self->{_erdb}->GetEntity($entityType, $ID);  
 }  
   
 =head3 GetEntityValues  
   
 C<< my @values = GetEntityValues($entityType, $ID, \@fields); >>  
   
 Return a list of values from a specified entity instance.  
   
 =over 4  
   
 =item entityType  
   
 Entity type name.  
   
 =item ID  
   
 ID of the desired entity.  
   
 =item fields  
   
 List of field names, each of the form I<objectName>C<(>I<fieldName>C<)>.  
   
 =item RETURN  
   
 Returns a flattened list of the values of the specified fields for the specified entity.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetEntityValues {  
         # Get the parameters.  
         my ($self, $entityType, $ID, $fields) = @_;  
         # Call the ERDB method.  
         return $self->{_erdb}->GetEntityValues($entityType, $ID, $fields);  
 }  
   
 =head3 ShowMetaData  
   
 C<< $sprout->ShowMetaData($fileName); >>  
   
 This method outputs a description of the database to an HTML file in the data directory.  
   
 =over 4  
   
 =item fileName  
   
 Fully-qualified name to give to the output file.  
   
 =back  
   
 =cut  
   
 sub ShowMetaData {  
         # Get the parameters.  
         my ($self, $fileName) = @_;  
         # Compute the file name.  
         my $options = $self->{_options};  
         # Call the show method on the underlying ERDB object.  
         $self->{_erdb}->ShowMetaData($fileName);  
 }  
   
170  =head3 Load  =head3 Load
171    
172  C<< $sprout->Load($rebuild); >>;  C<< $sprout->Load($rebuild); >>;
# Line 367  Line 201 
201  sub Load {  sub Load {
202          # Get the parameters.          # Get the parameters.
203          my ($self, $rebuild) = @_;          my ($self, $rebuild) = @_;
         # Get the database object.  
         my $erdb = $self->{_erdb};  
204          # Load the tables from the data directory.          # Load the tables from the data directory.
205          my $retVal = $erdb->LoadTables($self->{_options}->{dataDir}, $rebuild);      my $retVal = $self->LoadTables($self->{_options}->{dataDir}, $rebuild);
206          # Return the statistics.          # Return the statistics.
207          return $retVal;          return $retVal;
208  }  }
209    
210  =head3 LoadUpdate  =head3 LoadUpdate
211    
212  C<< my %stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>  C<< my $stats = $sprout->LoadUpdate($truncateFlag, \@tableList); >>
213    
214  Load updates to one or more database tables. This method enables the client to make changes to one  Load updates to one or more database tables. This method enables the client to make changes to one
215  or two tables without reloading the whole database. For each table, there must be a corresponding  or two tables without reloading the whole database. For each table, there must be a corresponding
# Line 410  Line 242 
242  sub LoadUpdate {  sub LoadUpdate {
243          # Get the parameters.          # Get the parameters.
244          my ($self, $truncateFlag, $tableList) = @_;          my ($self, $truncateFlag, $tableList) = @_;
         # Get the database object.  
         my $erdb = $self->{_erdb};  
245          # Declare the return value.          # Declare the return value.
246          my $retVal = Stats->new();          my $retVal = Stats->new();
247          # Get the data directory.          # Get the data directory.
# Line 420  Line 250 
250          # Loop through the incoming table names.          # Loop through the incoming table names.
251          for my $tableName (@{$tableList}) {          for my $tableName (@{$tableList}) {
252                  # Find the table's file.                  # Find the table's file.
253                  my $fileName = "$dataDir/$tableName";          my $fileName = LoadFileName($dataDir, $tableName);
254                  if (! -e $fileName) {          if (! $fileName) {
255                          $fileName = "$fileName.dtx";              Trace("No load file found for $tableName in $dataDir.") if T(0);
256                  }          } else {
257                  # Attempt to load this table.                  # Attempt to load this table.
258                  my $result = $erdb->LoadTable($fileName, $tableName, $truncateFlag);              my $result = $self->LoadTable($fileName, $tableName, $truncateFlag);
259                  # Accumulate the resulting statistics.                  # Accumulate the resulting statistics.
260                  $retVal->Accumulate($result);                  $retVal->Accumulate($result);
261          }          }
262        }
263          # Return the statistics.          # Return the statistics.
264          return $retVal;          return $retVal;
265  }  }
266    
267    =head3 GenomeCounts
268    
269    C<< my ($arch, $bact, $euk, $vir, $env, $unk) = $sprout->GenomeCounts($complete); >>
270    
271    Count the number of genomes in each domain. If I<$complete> is TRUE, only complete
272    genomes will be included in the counts.
273    
274    =over 4
275    
276    =item complete
277    
278    TRUE if only complete genomes are to be counted, FALSE if all genomes are to be
279    counted
280    
281    =item RETURN
282    
283    A six-element list containing the number of genomes in each of six categories--
284    Archaea, Bacteria, Eukaryota, Viral, Environmental, and Unknown, respectively.
285    
286    =back
287    
288    =cut
289    
290    sub GenomeCounts {
291        # Get the parameters.
292        my ($self, $complete) = @_;
293        # Set the filter based on the completeness flag.
294        my $filter = ($complete ? "Genome(complete) = 1" : "");
295        # Get all the genomes and the related taxonomy information.
296        my @genomes = $self->GetAll(['Genome'], $filter, [], ['Genome(id)', 'Genome(taxonomy)']);
297        # Clear the counters.
298        my ($arch, $bact, $euk, $vir, $env, $unk) = (0, 0, 0, 0, 0, 0);
299        # Loop through, counting the domains.
300        for my $genome (@genomes) {
301            if    ($genome->[1] =~ /^archaea/i)  { ++$arch }
302            elsif ($genome->[1] =~ /^bacter/i)   { ++$bact }
303            elsif ($genome->[1] =~ /^eukar/i)    { ++$euk }
304            elsif ($genome->[1] =~ /^vir/i)      { ++$vir }
305            elsif ($genome->[1] =~ /^env/i)      { ++$env }
306            else  { ++$unk }
307        }
308        # Return the counts.
309        return ($arch, $bact, $euk, $vir, $env, $unk);
310    }
311    
312    =head3 ContigCount
313    
314    C<< my $count = $sprout->ContigCount($genomeID); >>
315    
316    Return the number of contigs for the specified genome ID.
317    
318    =over 4
319    
320    =item genomeID
321    
322    ID of the genome whose contig count is desired.
323    
324    =item RETURN
325    
326    Returns the number of contigs for the specified genome.
327    
328    =back
329    
330    =cut
331    
332    sub ContigCount {
333        # Get the parameters.
334        my ($self, $genomeID) = @_;
335        # Get the contig count.
336        my $retVal = $self->GetCount(['Contig', 'HasContig'], "HasContig(from-link) = ?", [$genomeID]);
337        # Return the result.
338        return $retVal;
339    }
340    
341    =head3 GeneMenu
342    
343    C<< my $selectHtml = $sprout->GeneMenu(\%attributes, $filterString, \@params); >>
344    
345    Return an HTML select menu of genomes. Each genome will be an option in the menu,
346    and will be displayed by name with the ID and a contig count attached. The selection
347    value will be the genome ID. The genomes will be sorted by genus/species name.
348    
349    =over 4
350    
351    =item attributes
352    
353    Reference to a hash mapping attributes to values for the SELECT tag generated.
354    
355    =item filterString
356    
357    A filter string for use in selecting the genomes. The filter string must conform
358    to the rules for the C<< ERDB->Get >> method.
359    
360    =item params
361    
362    Reference to a list of values to be substituted in for the parameter marks in
363    the filter string.
364    
365    =item RETURN
366    
367    Returns an HTML select menu with the specified genomes as selectable options.
368    
369    =back
370    
371    =cut
372    
373    sub GeneMenu {
374        # Get the parameters.
375        my ($self, $attributes, $filterString, $params) = @_;
376        # Start the menu.
377        my $retVal = "<select " .
378            join(" ", map { "$_=\"$attributes->{$_}\"" } keys %{$attributes}) .
379            ">\n";
380        # Get the genomes.
381        my @genomes = $self->GetAll(['Genome'], $filterString, $params, ['Genome(id)',
382                                                                         'Genome(genus)',
383                                                                         'Genome(species)',
384                                                                         'Genome(unique-characterization)']);
385        # Sort them by name.
386        my @sorted = sort { lc("$a->[1] $a->[2]") cmp lc("$b->[1] $b->[2]") } @genomes;
387        # Loop through the genomes, creating the option tags.
388        for my $genomeData (@sorted) {
389            # Get the data for this genome.
390            my ($genomeID, $genus, $species, $strain) = @{$genomeData};
391            # Get the contig count.
392            my $count = $self->ContigCount($genomeID);
393            my $counting = ($count == 1 ? "contig" : "contigs");
394            # Build the option tag.
395            $retVal .= "<option value=\"$genomeID\">$genus $species $strain ($genomeID) [$count $counting]</option>\n";
396            Trace("Option tag built for $genomeID: $genus $species $strain.") if T(3);
397        }
398        # Close the SELECT tag.
399        $retVal .= "</select>\n";
400        # Return the result.
401        return $retVal;
402    }
403  =head3 Build  =head3 Build
404    
405  C<< $sprout->Build(); >>  C<< $sprout->Build(); >>
# Line 447  Line 414 
414          # Get the parameters.          # Get the parameters.
415          my ($self) = @_;          my ($self) = @_;
416          # Create the tables.          # Create the tables.
417          $self->{_erdb}->CreateTables;      $self->CreateTables();
418  }  }
419    
420  =head3 Genomes  =head3 Genomes
# Line 570  Line 537 
537  =item RETURN  =item RETURN
538    
539  Returns a list of the feature's contig segments. The locations are returned as a list in a list  Returns a list of the feature's contig segments. The locations are returned as a list in a list
540  context and as a space-delimited string in a scalar context.  context and as a comma-delimited string in a scalar context.
541    
542  =back  =back
543    
# Line 597  Line 564 
564                  if ($prevContig eq $contigID && $dir eq $prevDir) {                  if ($prevContig eq $contigID && $dir eq $prevDir) {
565                          # Here the new segment is in the same direction on the same contig. Insure the                          # Here the new segment is in the same direction on the same contig. Insure the
566                          # new segment's beginning is next to the old segment's end.                          # new segment's beginning is next to the old segment's end.
567                          if (($dir eq "-" && $beg == $prevBeg - $prevLen) ||              if ($dir eq "-" && $beg + $len == $prevBeg) {
568                                  ($dir eq "+" && $beg == $prevBeg + $prevLen)) {                  # Here we're merging two backward blocks, so we keep the new begin point
569                                  # Here we need to merge two segments. Adjust the beginning and length values                  # and adjust the length.
570                                  # to include both segments.                  $len += $prevLen;
571                    # Pop the old segment off. The new one will replace it later.
572                    pop @retVal;
573                } elsif ($dir eq "+" && $beg == $prevBeg + $prevLen) {
574                    # Here we need to merge two forward blocks. Adjust the beginning and
575                    # length values to include both segments.
576                                  $beg = $prevBeg;                                  $beg = $prevBeg;
577                                  $len += $prevLen;                                  $len += $prevLen;
578                                  # Pop the old segment off. The new one will replace it later.                                  # Pop the old segment off. The new one will replace it later.
# Line 609  Line 581 
581                  }                  }
582                  # Remember this specifier for the adjacent-segment test the next time through.                  # Remember this specifier for the adjacent-segment test the next time through.
583                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);                  ($prevContig, $prevBeg, $prevDir, $prevLen) = ($contigID, $beg, $dir, $len);
584            # Compute the initial base pair.
585            my $start = ($dir eq "+" ? $beg : $beg + $len - 1);
586                  # Add the specifier to the list.                  # Add the specifier to the list.
587                  push @retVal, "${contigID}_$beg$dir$len";          push @retVal, "${contigID}_$start$dir$len";
588          }          }
589          # Return the list in the format indicated by the context.          # Return the list in the format indicated by the context.
590          return (wantarray ? @retVal : join(' ', @retVal));      return (wantarray ? @retVal : join(',', @retVal));
591  }  }
592    
593  =head3 ParseLocation  =head3 ParseLocation
# Line 644  Line 618 
618      shift if UNIVERSAL::isa($_[0],__PACKAGE__);      shift if UNIVERSAL::isa($_[0],__PACKAGE__);
619          my ($location) = @_;          my ($location) = @_;
620          # Parse it into segments.          # Parse it into segments.
621          $location =~ /^(.*)_(\d*)([+-_])(\d*)$/;      $location =~ /^(.+)_(\d+)([+\-_])(\d+)$/;
622          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);          my ($contigID, $start, $dir, $len) = ($1, $2, $3, $4);
623          # If the direction is an underscore, convert it to a + or -.          # If the direction is an underscore, convert it to a + or -.
624          if ($dir eq "_") {          if ($dir eq "_") {
# Line 720  Line 694 
694  should be of the form returned by L</featureLocation> when in a list context. In other words,  should be of the form returned by L</featureLocation> when in a list context. In other words,
695  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.  each location is of the form I<contigID>C<_>I<begin>I<dir>I<end>.
696    
697    For example, the following would return the DNA sequence for contig C<83333.1:NC_000913>
698    between positions 1401 and 1532, inclusive.
699    
700        my $sequence = $sprout->DNASeq('83333.1:NC_000913_1401_1532');
701    
702  =over 4  =over 4
703    
704  =item locationList  =item locationList
705    
706  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<end> (see  List of location specifiers, each in the form I<contigID>C<_>I<begin>I<dir>I<len> or
707  L</FeatureLocation> for more about this format).  I<contigID>C<_>I<begin>C<_>I<end> (see L</FeatureLocation> for more about this format).
708    
709  =item RETURN  =item RETURN
710    
# Line 752  Line 731 
731                  # the start point is the ending. Note that in the latter case we must reverse the DNA string                  # the start point is the ending. Note that in the latter case we must reverse the DNA string
732                  # before putting it in the return value.                  # before putting it in the return value.
733                  my ($start, $stop);                  my ($start, $stop);
734            Trace("Parse of \"$location\" is $beg$dir$len.") if T(SDNA => 4);
735                  if ($dir eq "+") {                  if ($dir eq "+") {
736                          $start = $beg;                          $start = $beg;
737                          $stop = $beg + $len - 1;                          $stop = $beg + $len - 1;
738                  } else {                  } else {
739                          $start = $beg + $len + 1;              $start = $beg - $len + 1;
740                          $stop = $beg;                          $stop = $beg;
741                  }                  }
742            Trace("Looking for sequences containing $start through $stop.") if T(SDNA => 4);
743                  my $query = $self->Get(['IsMadeUpOf','Sequence'],                  my $query = $self->Get(['IsMadeUpOf','Sequence'],
744                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .                          "IsMadeUpOf(from-link) = ? AND IsMadeUpOf(start-position) + IsMadeUpOf(len) > ? AND " .
745                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",                          " IsMadeUpOf(start-position) <= ? ORDER BY IsMadeUpOf(start-position)",
# Line 770  Line 751 
751                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',                                  $sequence->Values(['IsMadeUpOf(start-position)', 'Sequence(sequence)',
752                                                                     'IsMadeUpOf(len)']);                                                                     'IsMadeUpOf(len)']);
753                          my $stopPosition = $startPosition + $sequenceLength;                          my $stopPosition = $startPosition + $sequenceLength;
754                Trace("Sequence is from $startPosition to $stopPosition.") if T(SDNA => 4);
755                          # Figure out the start point and length of the relevant section.                          # Figure out the start point and length of the relevant section.
756                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);                          my $pos1 = ($start < $startPosition ? 0 : $start - $startPosition);
757                          my $len = ($stopPosition <= $stop ? $stopPosition : $stop) - $startPosition - $pos1;              my $len1 = ($stopPosition < $stop ? $stopPosition : $stop) + 1 - $startPosition - $pos1;
758                Trace("Position is $pos1 for length $len1.") if T(SDNA => 4);
759                          # Add the relevant data to the location data.                          # Add the relevant data to the location data.
760                          $locationDNA .= substr($sequenceData, $pos1, $len);              $locationDNA .= substr($sequenceData, $pos1, $len1);
761                  }                  }
762                  # Add this location's data to the return string. Note that we may need to reverse it.                  # Add this location's data to the return string. Note that we may need to reverse it.
763                  if ($dir eq '+') {                  if ($dir eq '+') {
764                          $retVal .= $locationDNA;                          $retVal .= $locationDNA;
765                  } else {                  } else {
766                          $locationDNA = join('', reverse split //, $locationDNA);              $retVal .= FIG::reverse_comp($locationDNA);
                         $retVal .= $locationDNA;  
767                  }                  }
768          }          }
769          # Return the result.          # Return the result.
# Line 818  Line 800 
800          return @retVal;          return @retVal;
801  }  }
802    
803    =head3 GenomeLength
804    
805    C<< my $length = $sprout->GenomeLength($genomeID); >>
806    
807    Return the length of the specified genome in base pairs.
808    
809    =over 4
810    
811    =item genomeID
812    
813    ID of the genome whose base pair count is desired.
814    
815    =item RETURN
816    
817    Returns the number of base pairs in all the contigs of the specified
818    genome.
819    
820    =back
821    
822    =cut
823    
824    sub GenomeLength {
825        # Get the parameters.
826        my ($self, $genomeID) = @_;
827        # Declare the return variable.
828        my $retVal = 0;
829        # Get the genome's contig sequence lengths.
830        my @lens = $self->GetFlat(['HasContig', 'IsMadeUpOf'], 'HasContig(from-link) = ?',
831                           [$genomeID], 'IsMadeUpOf(len)');
832        # Sum the lengths.
833        map { $retVal += $_ } @lens;
834        # Return the result.
835        return $retVal;
836    }
837    
838    =head3 FeatureCount
839    
840    C<< my $count = $sprout->FeatureCount($genomeID, $type); >>
841    
842    Return the number of features of the specified type in the specified genome.
843    
844    =over 4
845    
846    =item genomeID
847    
848    ID of the genome whose feature count is desired.
849    
850    =item type
851    
852    Type of feature to count (eg. C<peg>, C<rna>, etc.).
853    
854    =item RETURN
855    
856    Returns the number of features of the specified type for the specified genome.
857    
858    =back
859    
860    =cut
861    
862    sub FeatureCount {
863        # Get the parameters.
864        my ($self, $genomeID, $type) = @_;
865        # Compute the count.
866        my $retVal = $self->GetCount(['HasFeature', 'Feature'],
867                                    "HasFeature(from-link) = ? AND Feature(feature-type) = ?",
868                                    [$genomeID, $type]);
869        # Return the result.
870        return $retVal;
871    }
872    
873    =head3 GenomeAssignments
874    
875    C<< my $fidHash = $sprout->GenomeAssignments($genomeID); >>
876    
877    Return a list of a genome's assigned features. The return hash will contain each
878    assigned feature of the genome mapped to the text of its most recent functional
879    assignment.
880    
881    =over 4
882    
883    =item genomeID
884    
885    ID of the genome whose functional assignments are desired.
886    
887    =item RETURN
888    
889    Returns a reference to a hash which maps each feature to its most recent
890    functional assignment.
891    
892    =back
893    
894    =cut
895    
896    sub GenomeAssignments {
897        # Get the parameters.
898        my ($self, $genomeID) = @_;
899        # Declare the return variable.
900        my $retVal = {};
901        # Query the genome's features and annotations. We'll put the oldest annotations
902        # first so that the last assignment to go into the hash will be the correct one.
903        my $query = $self->Get(['HasFeature', 'IsTargetOfAnnotation', 'Annotation'],
904                               "HasFeature(from-link) = ? ORDER BY Annotation(time)",
905                               [$genomeID]);
906        # Loop through the annotations.
907        while (my $data = $query->Fetch) {
908            # Get the feature ID and annotation text.
909            my ($fid, $annotation) = $data->Values(['HasFeature(to-link)',
910                                                    'Annotation(annotation)']);
911            # Check to see if this is an assignment. Note that the user really
912            # doesn't matter to us, other than we use it to determine whether or
913            # not this is an assignment.
914            my ($user, $assignment) = _ParseAssignment('fig', $annotation);
915            if ($user) {
916                # Here it's an assignment. We put it in the return hash, overwriting
917                # any older assignment that might be present.
918                $retVal->{$fid} = $assignment;
919            }
920        }
921        # Return the result.
922        return $retVal;
923    }
924    
925  =head3 ContigLength  =head3 ContigLength
926    
927  C<< my $length = $sprout->ContigLength($contigID); >>  C<< my $length = $sprout->ContigLength($contigID); >>
# Line 851  Line 955 
955          # Set it from the sequence data, if any.          # Set it from the sequence data, if any.
956          if ($sequence) {          if ($sequence) {
957                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);                  my ($start, $len) = $sequence->Values(['IsMadeUpOf(start-position)', 'IsMadeUpOf(len)']);
958                  $retVal = $start + $len;          $retVal = $start + $len - 1;
959          }          }
960          # Return the result.          # Return the result.
961          return $retVal;          return $retVal;
962  }  }
963    
964  =head3 GenesInRegion  =head3 ClusterPEGs
965    
966  C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>  C<< my $clusteredList = $sprout->ClusterPEGs($sub, \@pegs); >>
967    
968  List the features which overlap a specified region in a contig.  Cluster the PEGs in a list according to the cluster coding scheme of the specified
969    subsystem. In order for this to work properly, the subsystem object must have
970    been used recently to retrieve the PEGs using the B<get_pegs_from_cell> method.
971    This causes the cluster numbers to be pulled into the subsystem's color hash.
972    If a PEG is not found in the color hash, it will not appear in the output
973    sequence.
974    
975  =over 4  =over 4
976    
977  =item contigID  =item sub
978    
979  ID of the contig containing the region of interest.  Sprout subsystem object for the relevant subsystem, from the L</get_subsystem>
980    method.
981    
982  =item start  =item pegs
983    
984  Offset of the first residue in the region of interest.  Reference to the list of PEGs to be clustered.
985    
986  =item stop  =item RETURN
987    
988  Offset of the last residue in the region of interest.  Returns a list of the PEGs, grouped into smaller lists by cluster number.
989    
990  =item RETURN  =back
991    
992    =cut
993    #: Return Type $@@;
994    sub ClusterPEGs {
995        # Get the parameters.
996        my ($self, $sub, $pegs) = @_;
997        # Declare the return variable.
998        my $retVal = [];
999        # Loop through the PEGs, creating arrays for each cluster.
1000        for my $pegID (@{$pegs}) {
1001            my $clusterNumber = $sub->get_cluster_number($pegID);
1002            # Only proceed if the PEG is in a cluster.
1003            if ($clusterNumber >= 0) {
1004                # Push this PEG onto the sub-list for the specified cluster number.
1005                push @{$retVal->[$clusterNumber]}, $pegID;
1006            }
1007        }
1008        # Return the result.
1009        return $retVal;
1010    }
1011    
1012    =head3 GenesInRegion
1013    
1014    C<< my (\@featureIDList, $beg, $end) = $sprout->GenesInRegion($contigID, $start, $stop); >>
1015    
1016    List the features which overlap a specified region in a contig.
1017    
1018    =over 4
1019    
1020    =item contigID
1021    
1022    ID of the contig containing the region of interest.
1023    
1024    =item start
1025    
1026    Offset of the first residue in the region of interest.
1027    
1028    =item stop
1029    
1030    Offset of the last residue in the region of interest.
1031    
1032    =item RETURN
1033    
1034  Returns a three-element list. The first element is a list of feature IDs for the features that  Returns a three-element list. The first element is a list of feature IDs for the features that
1035  overlap the region of interest. The second and third elements are the minimum and maximum  overlap the region of interest. The second and third elements are the minimum and maximum
# Line 1001  Line 1153 
1153    
1154  =head3 FeatureAnnotations  =head3 FeatureAnnotations
1155    
1156  C<< my @descriptors = $sprout->FeatureAnnotations($featureID); >>  C<< my @descriptors = $sprout->FeatureAnnotations($featureID, $rawFlag); >>
1157    
1158  Return the annotations of a feature.  Return the annotations of a feature.
1159    
# Line 1011  Line 1163 
1163    
1164  ID of the feature whose annotations are desired.  ID of the feature whose annotations are desired.
1165    
1166    =item rawFlag
1167    
1168    If TRUE, the annotation timestamps will be returned in raw form; otherwise, they
1169    will be returned in human-readable form.
1170    
1171  =item RETURN  =item RETURN
1172    
1173  Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.  Returns a list of annotation descriptors. Each descriptor is a hash with the following fields.
1174    
1175  * B<featureID> ID of the relevant feature.  * B<featureID> ID of the relevant feature.
1176    
1177  * B<timeStamp> time the annotation was made, in user-friendly format.  * B<timeStamp> time the annotation was made.
1178    
1179  * B<user> ID of the user who made the annotation  * B<user> ID of the user who made the annotation
1180    
# Line 1029  Line 1186 
1186  #: Return Type @%;  #: Return Type @%;
1187  sub FeatureAnnotations {  sub FeatureAnnotations {
1188          # Get the parameters.          # Get the parameters.
1189          my ($self, $featureID) = @_;      my ($self, $featureID, $rawFlag) = @_;
1190          # Create a query to get the feature's annotations and the associated users.          # Create a query to get the feature's annotations and the associated users.
1191          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1192                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);                                                     "IsTargetOfAnnotation(from-link) = ?", [$featureID]);
# Line 1042  Line 1199 
1199                          $annotation->Values(['IsTargetOfAnnotation(from-link)',                          $annotation->Values(['IsTargetOfAnnotation(from-link)',
1200                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',                                                                   'Annotation(time)', 'MadeAnnotation(from-link)',
1201                                                                   'Annotation(annotation)']);                                                                   'Annotation(annotation)']);
1202            # Convert the time, if necessary.
1203            if (! $rawFlag) {
1204                $timeStamp = FriendlyTimestamp($timeStamp);
1205            }
1206                  # Assemble them into a hash.                  # Assemble them into a hash.
1207          my $annotationHash = { featureID => $featureID,          my $annotationHash = { featureID => $featureID,
1208                                 timeStamp => FriendlyTimestamp($timeStamp),                                 timeStamp => $timeStamp,
1209                                                             user => $user, text => $text };                                                             user => $user, text => $text };
1210                  # Add it to the return list.                  # Add it to the return list.
1211                  push @retVal, $annotationHash;                  push @retVal, $annotationHash;
# Line 1073  Line 1234 
1234    
1235  =item RETURN  =item RETURN
1236    
1237  Returns a hash mapping the functional assignment IDs to user IDs.  Returns a hash mapping the user IDs to functional assignment IDs.
1238    
1239  =back  =back
1240    
# Line 1083  Line 1244 
1244          # Get the parameters.          # Get the parameters.
1245          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1246          # Get all of the feature's annotations.          # Get all of the feature's annotations.
1247      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation'],      my @query = $self->GetAll(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1248                                                      "IsTargetOfAnnotation(from-link) = ?",                                                      "IsTargetOfAnnotation(from-link) = ?",
1249                              [$featureID], ['Annotation(time)', 'Annotation(annotation)']);                              [$featureID], ['Annotation(time)', 'Annotation(annotation)',
1250                                               'MadeAnnotation(from-link)']);
1251          # Declare the return hash.          # Declare the return hash.
1252          my %retVal;          my %retVal;
     # Declare a hash for insuring we only make one assignment per user.  
     my %timeHash = ();  
1253      # Now we sort the assignments by timestamp in reverse.      # Now we sort the assignments by timestamp in reverse.
1254      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;      my @sortedQuery = sort { -($a->[0] <=> $b->[0]) } @query;
1255          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1256      for my $annotation (@sortedQuery) {      for my $annotation (@sortedQuery) {
1257          # Get the annotation fields.          # Get the annotation fields.
1258          my ($timeStamp, $text) = @{$annotation};          my ($timeStamp, $text, $user) = @{$annotation};
1259                  # Check to see if this is a functional assignment.                  # Check to see if this is a functional assignment.
1260                  my ($user, $function) = _ParseAssignment($text);          my ($actualUser, $function) = _ParseAssignment($user, $text);
1261          if ($user && ! exists $timeHash{$user}) {          if ($actualUser && ! exists $retVal{$actualUser}) {
1262              # Here it is a functional assignment and there has been no              # Here it is a functional assignment and there has been no
1263              # previous assignment for this user, so we stuff it in the              # previous assignment for this user, so we stuff it in the
1264              # return hash.              # return hash.
1265                          $retVal{$function} = $user;              $retVal{$actualUser} = $function;
             # Insure we don't assign to this user again.  
             $timeHash{$user} = 1;  
1266                  }                  }
1267          }          }
1268          # Return the hash of assignments found.          # Return the hash of assignments found.
# Line 1120  Line 1278 
1278  The functional assignment is handled differently depending on the type of feature. If  The functional assignment is handled differently depending on the type of feature. If
1279  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional  the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1280  assignment is a type of annotation. The format of an assignment is described in  assignment is a type of annotation. The format of an assignment is described in
1281  L</ParseLocation>. Its worth noting that we cannot filter on the content of the  L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1282  annotation itself because it's a text field; however, this is not a big problem because  annotation itself because it's a text field; however, this is not a big problem because
1283  most features only have a small number of annotations.  most features only have a small number of annotations.
1284    
# Line 1182  Line 1340 
1340              }              }
1341          }          }
1342          # Build a query for all of the feature's annotations, sorted by date.          # Build a query for all of the feature's annotations, sorted by date.
1343          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation'],          my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1344                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",                                 "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1345                                 [$featureID]);                                 [$featureID]);
1346          my $timeSelected = 0;          my $timeSelected = 0;
1347          # Loop until we run out of annotations.          # Loop until we run out of annotations.
1348          while (my $annotation = $query->Fetch()) {          while (my $annotation = $query->Fetch()) {
1349              # Get the annotation text.              # Get the annotation text.
1350              my ($text, $time) = $annotation->Values(['Annotation(annotation)','Annotation(time)']);              my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1351                                                         'Annotation(time)', 'MadeAnnotation(from-link)']);
1352              # Check to see if this is a functional assignment for a trusted user.              # Check to see if this is a functional assignment for a trusted user.
1353              my ($user, $function) = _ParseAssignment($text);              my ($actualUser, $function) = _ParseAssignment($user, $text);
1354              if ($user) {              Trace("Assignment user is $actualUser, text is $function.") if T(4);
1355                if ($actualUser) {
1356                  # Here it is a functional assignment. Check the time and the user                  # Here it is a functional assignment. Check the time and the user
1357                  # name. The time must be recent and the user must be trusted.                  # name. The time must be recent and the user must be trusted.
1358                  if ((exists $trusteeTable{$user}) && ($time > $timeSelected)) {                  if ((exists $trusteeTable{$actualUser}) && ($time > $timeSelected)) {
1359                      $retVal = $function;                      $retVal = $function;
1360                      $timeSelected = $time;                      $timeSelected = $time;
1361                  }                  }
# Line 1211  Line 1371 
1371          return $retVal;          return $retVal;
1372  }  }
1373    
1374    =head3 FunctionsOf
1375    
1376    C<< my @functionList = $sprout->FunctionOf($featureID, $userID); >>
1377    
1378    Return the functional assignments of a particular feature.
1379    
1380    The functional assignment is handled differently depending on the type of feature. If
1381    the feature is identified by a FIG ID (begins with the string C<fig|>), then a functional
1382    assignment is a type of annotation. The format of an assignment is described in
1383    L</ParseAssignment>. Its worth noting that we cannot filter on the content of the
1384    annotation itself because it's a text field; however, this is not a big problem because
1385    most features only have a small number of annotations.
1386    
1387    If the feature is B<not> identified by a FIG ID, then the functional assignment
1388    information is taken from the B<ExternalAliasFunc> table. If the table does
1389    not contain an entry for the feature, an empty list is returned.
1390    
1391    =over 4
1392    
1393    =item featureID
1394    
1395    ID of the feature whose functional assignments are desired.
1396    
1397    =item RETURN
1398    
1399    Returns a list of 2-tuples, each consisting of a user ID and the text of an assignment by
1400    that user.
1401    
1402    =back
1403    
1404    =cut
1405    #: Return Type @@;
1406    sub FunctionsOf {
1407        # Get the parameters.
1408        my ($self, $featureID) = @_;
1409        # Declare the return value.
1410        my @retVal = ();
1411        # Determine the ID type.
1412        if ($featureID =~ m/^fig\|/) {
1413            # Here we have a FIG feature ID. We must build the list of trusted
1414            # users.
1415            my %trusteeTable = ();
1416            # Build a query for all of the feature's annotations, sorted by date.
1417            my $query = $self->Get(['IsTargetOfAnnotation', 'Annotation', 'MadeAnnotation'],
1418                                   "IsTargetOfAnnotation(from-link) = ? ORDER BY Annotation(time) DESC",
1419                                   [$featureID]);
1420            my $timeSelected = 0;
1421            # Loop until we run out of annotations.
1422            while (my $annotation = $query->Fetch()) {
1423                # Get the annotation text.
1424                my ($text, $time, $user) = $annotation->Values(['Annotation(annotation)',
1425                                                                'Annotation(time)',
1426                                                                'MadeAnnotation(user)']);
1427                # Check to see if this is a functional assignment for a trusted user.
1428                my ($actualUser, $function) = _ParseAssignment($user, $text);
1429                if ($actualUser) {
1430                    # Here it is a functional assignment.
1431                    push @retVal, [$actualUser, $function];
1432                }
1433            }
1434        } else {
1435            # Here we have a non-FIG feature ID. In this case the user ID does not
1436            # matter. We simply get the information from the External Alias Function
1437            # table.
1438            my @assignments = $self->GetEntityValues('ExternalAliasFunc', $featureID,
1439                                                     ['ExternalAliasFunc(func)']);
1440            push @retVal, map { ['master', $_] } @assignments;
1441        }
1442        # Return the assignments found.
1443        return @retVal;
1444    }
1445    
1446  =head3 BBHList  =head3 BBHList
1447    
1448  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>  C<< my $bbhHash = $sprout->BBHList($genomeID, \@featureList); >>
# Line 1230  Line 1462 
1462    
1463  =item RETURN  =item RETURN
1464    
1465  Returns a reference to a hash that maps the IDs of the incoming features to the IDs of  Returns a reference to a hash that maps the IDs of the incoming features to the best hits
1466  their best hits.  on the target genome.
1467    
1468  =back  =back
1469    
# Line 1248  Line 1480 
1480                  my $query = $self->Get(['IsBidirectionalBestHitOf'],                  my $query = $self->Get(['IsBidirectionalBestHitOf'],
1481                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",                                                             "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",
1482                                                             [$featureID, $genomeID]);                                                             [$featureID, $genomeID]);
1483                  # Look for the best hit.          # Peel off the BBHs found.
1484                  my $bbh = $query->Fetch;          my @found = ();
1485                  if ($bbh) {          while (my $bbh = $query->Fetch) {
1486                          my ($targetFeature) = $bbh->Value('IsBidirectionalBestHitOf(to-link)');              push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');
                         $retVal{$featureID} = $targetFeature;  
1487                  }                  }
1488            $retVal{$featureID} = \@found;
1489          }          }
1490          # Return the mapping.          # Return the mapping.
1491          return \%retVal;          return \%retVal;
1492  }  }
1493    
1494    =head3 SimList
1495    
1496    C<< my %similarities = $sprout->SimList($featureID, $count); >>
1497    
1498    Return a list of the similarities to the specified feature.
1499    
1500    Sprout does not support real similarities, so this method just returns the bidirectional
1501    best hits.
1502    
1503    =over 4
1504    
1505    =item featureID
1506    
1507    ID of the feature whose similarities are desired.
1508    
1509    =item count
1510    
1511    Maximum number of similar features to be returned, or C<0> to return them all.
1512    
1513    =back
1514    
1515    =cut
1516    #: Return Type %;
1517    sub SimList {
1518        # Get the parameters.
1519        my ($self, $featureID, $count) = @_;
1520        # Ask for the best hits.
1521        my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],
1522                                  "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",
1523                                  [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],
1524                                  $count);
1525        # Create the return value.
1526        my %retVal = ();
1527        for my $tuple (@lists) {
1528            $retVal{$tuple->[0]} = $tuple->[1];
1529        }
1530        # Return the result.
1531        return %retVal;
1532    }
1533    
1534    
1535    
1536    =head3 IsComplete
1537    
1538    C<< my $flag = $sprout->IsComplete($genomeID); >>
1539    
1540    Return TRUE if the specified genome is complete, else FALSE.
1541    
1542    =over 4
1543    
1544    =item genomeID
1545    
1546    ID of the genome whose completeness status is desired.
1547    
1548    =item RETURN
1549    
1550    Returns TRUE if the genome is complete, FALSE if it is incomplete, and C<undef> if it is
1551    not found.
1552    
1553    =back
1554    
1555    =cut
1556    #: Return Type $;
1557    sub IsComplete {
1558        # Get the parameters.
1559        my ($self, $genomeID) = @_;
1560        # Declare the return variable.
1561        my $retVal;
1562        # Get the genome's data.
1563        my $genomeData = $self->GetEntity('Genome', $genomeID);
1564        if ($genomeData) {
1565            # The genome exists, so get the completeness flag.
1566            ($retVal) = $genomeData->Value('Genome(complete)');
1567        }
1568        # Return the result.
1569        return $retVal;
1570    }
1571    
1572  =head3 FeatureAliases  =head3 FeatureAliases
1573    
1574  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>  C<< my @aliasList = $sprout->FeatureAliases($featureID); >>
# Line 1293  Line 1603 
1603    
1604  C<< my $genomeID = $sprout->GenomeOf($featureID); >>  C<< my $genomeID = $sprout->GenomeOf($featureID); >>
1605    
1606  Return the genome that contains a specified feature.  Return the genome that contains a specified feature or contig.
1607    
1608  =over 4  =over 4
1609    
1610  =item featureID  =item featureID
1611    
1612  ID of the feature whose genome is desired.  ID of the feature or contig whose genome is desired.
1613    
1614  =item RETURN  =item RETURN
1615    
1616  Returns the ID of the genome for the specified feature. If the feature is not found, returns  Returns the ID of the genome for the specified feature or contig. If the feature or contig is not
1617  an undefined value.  found, returns an undefined value.
1618    
1619  =back  =back
1620    
# Line 1313  Line 1623 
1623  sub GenomeOf {  sub GenomeOf {
1624          # Get the parameters.          # Get the parameters.
1625          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1626          # Create a query to find the genome associated with the feature.      # Create a query to find the genome associated with the incoming ID.
1627          my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ?", [$featureID]);      my $query = $self->Get(['IsLocatedIn', 'HasContig'], "IsLocatedIn(from-link) = ? OR HasContig(to-link) = ?",
1628                               [$featureID, $featureID]);
1629          # Declare the return value.          # Declare the return value.
1630          my $retVal;          my $retVal;
1631          # Get the genome ID.          # Get the genome ID.
# Line 1349  Line 1660 
1660  sub CoupledFeatures {  sub CoupledFeatures {
1661          # Get the parameters.          # Get the parameters.
1662          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
1663          # Create a query to retrieve the functionally-coupled features. Note that we depend on the      Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1664          # fact that the functional coupling is physically paired. If (A,B) is in the database, then      # Create a query to retrieve the functionally-coupled features.
1665          # (B,A) will also be found.      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1666          my $query = $self->Get(['IsClusteredOnChromosomeWith'],                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
                                                    "IsClusteredOnChromosomeWith(from-link) = ?", [$featureID]);  
1667          # This value will be set to TRUE if we find at least one coupled feature.          # This value will be set to TRUE if we find at least one coupled feature.
1668          my $found = 0;          my $found = 0;
1669          # Create the return hash.          # Create the return hash.
1670          my %retVal = ();          my %retVal = ();
1671          # Retrieve the relationship records and store them in the hash.          # Retrieve the relationship records and store them in the hash.
1672          while (my $clustering = $query->Fetch()) {          while (my $clustering = $query->Fetch()) {
1673                  my ($otherFeatureID, $score) = $clustering->Values(['IsClusteredOnChromosomeWith(to-link)',          # Get the ID and score of the coupling.
1674                                                                      'IsClusteredOnChromosomeWith(score)']);          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1675                                                            'Coupling(score)']);
1676            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1677            # Get the other feature that participates in the coupling.
1678            my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1679                                               "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1680                                               [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1681            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1682            # Attach the other feature's score to its ID.
1683                  $retVal{$otherFeatureID} = $score;                  $retVal{$otherFeatureID} = $score;
1684                  $found = 1;                  $found = 1;
1685          }          }
# Line 1374  Line 1692 
1692          return %retVal;          return %retVal;
1693  }  }
1694    
1695  =head3 GetEntityTypes  =head3 CouplingEvidence
1696    
1697    C<< my @evidence = $sprout->CouplingEvidence($peg1, $peg2); >>
1698    
1699    Return the evidence for a functional coupling.
1700    
1701    A pair of features is considered evidence of a coupling between two other
1702    features if they occur close together on a contig and both are similar to
1703    the coupled features. So, if B<A1> and B<A2> are close together on a contig,
1704    B<B1> and B<B2> are considered evidence for the coupling if (1) B<B1> and
1705    B<B2> are close together, (2) B<B1> is similar to B<A1>, and (3) B<B2> is
1706    similar to B<A2>.
1707    
1708    The score of a coupling is determined by the number of pieces of evidence
1709    that are considered I<representative>. If several evidence items belong to
1710    a group of genomes that are close to each other, only one of those items
1711    is considered representative. The other evidence items are presumed to be
1712    there because of the relationship between the genomes rather than because
1713    the two proteins generated by the features have a related functionality.
1714    
1715  C<< my @entityList = $sprout->GetEntityTypes(); >>  Each evidence item is returned as a three-tuple in the form C<[>I<$peg1a>C<,>
1716    I<$peg2a>C<,> I<$rep>C<]>, where I<$peg1a> is similar to I<$peg1>, I<$peg2a>
1717    is similar to I<$peg2>, and I<$rep> is TRUE if the evidence is representative
1718    and FALSE otherwise.
1719    
1720  Return the list of supported entity types.  =over 4
1721    
1722    =item peg1
1723    
1724    ID of the feature of interest.
1725    
1726    =item peg2
1727    
1728    ID of a feature functionally coupled to the feature of interest.
1729    
1730    =item RETURN
1731    
1732    Returns a list of 3-tuples. Each tuple consists of a feature similar to the feature
1733    of interest, a feature similar to the functionally coupled feature, and a flag
1734    that is TRUE for a representative piece of evidence and FALSE otherwise.
1735    
1736    =back
1737    
1738  =cut  =cut
1739  #: Return Type @;  #: Return Type @@;
1740  sub GetEntityTypes {  sub CouplingEvidence {
1741          # Get the parameters.          # Get the parameters.
1742          my ($self) = @_;      my ($self, $peg1, $peg2) = @_;
1743          # Get the underlying database object.      # Declare the return variable.
1744          my $erdb = $self->{_erdb};      my @retVal = ();
1745          # Get its entity type list.      # Our first task is to find out the nature of the coupling: whether or not
1746          my @retVal = $erdb->GetEntityTypes();      # it exists, its score, and whether the features are stored in the same
1747        # order as the ones coming in.
1748        my ($couplingID, $inverted, $score) = $self->GetCoupling($peg1, $peg2);
1749        # Only proceed if a coupling exists.
1750        if ($couplingID) {
1751            # Determine the ordering to place on the evidence items. If we're
1752            # inverted, we want to see feature 2 before feature 1 (descending); otherwise,
1753            # we want feature 1 before feature 2 (normal).
1754            Trace("Coupling evidence for ($peg1, $peg2) with inversion flag $inverted.") if T(Coupling => 4);
1755            my $ordering = ($inverted ? "DESC" : "");
1756            # Get the coupling evidence.
1757            my @evidenceList = $self->GetAll(['IsEvidencedBy', 'PCH', 'UsesAsEvidence'],
1758                                              "IsEvidencedBy(from-link) = ? ORDER BY PCH(id), UsesAsEvidence(pos) $ordering",
1759                                              [$couplingID],
1760                                              ['PCH(used)', 'UsesAsEvidence(to-link)']);
1761            # Loop through the evidence items. Each piece of evidence is represented by two
1762            # positions in the evidence list, one for each feature on the other side of the
1763            # evidence link. If at some point we want to generalize to couplings with
1764            # more than two positions, this section of code will need to be re-done.
1765            while (@evidenceList > 0) {
1766                my $peg1Data = shift @evidenceList;
1767                my $peg2Data = shift @evidenceList;
1768                Trace("Peg 1 is " . $peg1Data->[1] . " and Peg 2 is " . $peg2Data->[1] . ".") if T(Coupling => 4);
1769                push @retVal, [$peg1Data->[1], $peg2Data->[1], $peg1Data->[0]];
1770            }
1771            Trace("Last index in evidence result is is $#retVal.") if T(Coupling => 4);
1772        }
1773        # Return the result.
1774        return @retVal;
1775    }
1776    
1777    =head3 GetCoupling
1778    
1779    C<< my ($couplingID, $inverted, $score) = $sprout->GetCoupling($peg1, $peg2); >>
1780    
1781    Return the coupling (if any) for the specified pair of PEGs. If a coupling
1782    exists, we return the coupling ID along with an indicator of whether the
1783    coupling is stored as C<(>I<$peg1>C<, >I<$peg2>C<)> or C<(>I<$peg2>C<, >I<$peg1>C<)>.
1784    In the second case, we say the coupling is I<inverted>. The importance of an
1785    inverted coupling is that the PEGs in the evidence will appear in reverse order.
1786    
1787    =over 4
1788    
1789    =item peg1
1790    
1791    ID of the feature of interest.
1792    
1793    =item peg2
1794    
1795    ID of the potentially coupled feature.
1796    
1797    =item RETURN
1798    
1799    Returns a three-element list. The first element contains the database ID of
1800    the coupling. The second element is FALSE if the coupling is stored in the
1801    database in the caller specified order and TRUE if it is stored in the
1802    inverted order. The third element is the coupling's score. If the coupling
1803    does not exist, all three list elements will be C<undef>.
1804    
1805    =back
1806    
1807    =cut
1808    #: Return Type $%@;
1809    sub GetCoupling {
1810        # Get the parameters.
1811        my ($self, $peg1, $peg2) = @_;
1812        # Declare the return values. We'll start with the coupling ID and undefine the
1813        # flag and score until we have more information.
1814        my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1815        # Find the coupling data.
1816        my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1817                                     "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
1818                                     [$retVal], ["ParticipatesInCoupling(from-link)", "Coupling(score)"]);
1819        # Check to see if we found anything.
1820        if (!@pegs) {
1821            Trace("No coupling found.") if T(Coupling => 4);
1822            # No coupling, so undefine the return value.
1823            $retVal = undef;
1824        } else {
1825            # We have a coupling! Get the score and check for inversion.
1826            $score = $pegs[0]->[1];
1827            my $firstFound = $pegs[0]->[0];
1828            $inverted = ($firstFound ne $peg1);
1829            Trace("Coupling score is $score. First peg is $firstFound, peg 1 is $peg1.") if T(Coupling => 4);
1830        }
1831        # Return the result.
1832        return ($retVal, $inverted, $score);
1833    }
1834    
1835    =head3 GetSynonymGroup
1836    
1837    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1838    
1839    Return the synonym group name for the specified feature.
1840    
1841    =over 4
1842    
1843    =item fid
1844    
1845    ID of the feature whose synonym group is desired.
1846    
1847    =item RETURN
1848    
1849    The name of the synonym group to which the feature belongs. If the feature does
1850    not belong to a synonym group, the feature ID itself is returned.
1851    
1852    =back
1853    
1854    =cut
1855    
1856    sub GetSynonymGroup {
1857        # Get the parameters.
1858        my ($self, $fid) = @_;
1859        # Declare the return variable.
1860        my $retVal;
1861        # Find the synonym group.
1862        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1863                                       [$fid], 'IsSynonymGroupFor(from-link)');
1864        # Check to see if we found anything.
1865        if (@groups) {
1866            $retVal = $groups[0];
1867        } else {
1868            $retVal = $fid;
1869        }
1870        # Return the result.
1871        return $retVal;
1872    }
1873    
1874    =head3 GetBoundaries
1875    
1876    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1877    
1878    Determine the begin and end boundaries for the locations in a list. All of the
1879    locations must belong to the same contig and have mostly the same direction in
1880    order for this method to produce a meaningful result. The resulting
1881    begin/end pair will contain all of the bases in any of the locations.
1882    
1883    =over 4
1884    
1885    =item locList
1886    
1887    List of locations to process.
1888    
1889    =item RETURN
1890    
1891    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1892    and the ending boundary. The beginning boundary will be left of the
1893    end for mostly-forward locations and right of the end for mostly-backward
1894    locations.
1895    
1896    =back
1897    
1898    =cut
1899    
1900    sub GetBoundaries {
1901        # Get the parameters.
1902        my ($self, @locList) = @_;
1903        # Set up the counters used to determine the most popular direction.
1904        my %counts = ( '+' => 0, '-' => 0 );
1905        # Get the last location and parse it.
1906        my $locObject = BasicLocation->new(pop @locList);
1907        # Prime the loop with its data.
1908        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1909        # Count its direction.
1910        $counts{$locObject->Dir}++;
1911        # Loop through the remaining locations. Note that in most situations, this loop
1912        # will not iterate at all, because most of the time we will be dealing with a
1913        # singleton list.
1914        for my $loc (@locList) {
1915            # Create a location object.
1916            my $locObject = BasicLocation->new($loc);
1917            # Count the direction.
1918            $counts{$locObject->Dir}++;
1919            # Get the left end and the right end.
1920            my $left = $locObject->Left;
1921            my $right = $locObject->Right;
1922            # Merge them into the return variables.
1923            if ($left < $beg) {
1924                $beg = $left;
1925            }
1926            if ($right > $end) {
1927                $end = $right;
1928            }
1929        }
1930        # If the most common direction is reverse, flip the begin and end markers.
1931        if ($counts{'-'} > $counts{'+'}) {
1932            ($beg, $end) = ($end, $beg);
1933        }
1934        # Return the result.
1935        return ($contig, $beg, $end);
1936    }
1937    
1938    =head3 CouplingID
1939    
1940    C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1941    
1942    Return the coupling ID for a pair of feature IDs.
1943    
1944    The coupling ID is currently computed by joining the feature IDs in
1945    sorted order with a space. Client modules (that is, modules which
1946    use Sprout) should not, however, count on this always being the
1947    case. This method provides a way for abstracting the concept of a
1948    coupling ID. All that we know for sure about it is that it can be
1949    generated easily from the feature IDs and the order of the IDs
1950    in the parameter list does not matter (i.e. C<CouplingID("a1", "b1")>
1951    will have the same value as C<CouplingID("b1", "a1")>.
1952    
1953    =over 4
1954    
1955    =item peg1
1956    
1957    First feature of interest.
1958    
1959    =item peg2
1960    
1961    Second feature of interest.
1962    
1963    =item RETURN
1964    
1965    Returns the ID that would be used to represent a functional coupling of
1966    the two specified PEGs.
1967    
1968    =back
1969    
1970    =cut
1971    #: Return Type $;
1972    sub CouplingID {
1973        my ($self, @pegs) = @_;
1974        return $self->DigestKey(join " ", sort @pegs);
1975  }  }
1976    
1977  =head3 ReadFasta  =head3 ReadFasta
# Line 1435  Line 2018 
2018                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {                  if ($line =~ m/^>\s*(.+?)(\s|\n)/) {
2019                          # Here we have a new header. Store the current sequence if we have one.                          # Here we have a new header. Store the current sequence if we have one.
2020                          if ($id) {                          if ($id) {
2021                                  $retVal{$id} = uc $sequence;                  $retVal{$id} = lc $sequence;
2022                          }                          }
2023                          # Clear the sequence accumulator and save the new ID.                          # Clear the sequence accumulator and save the new ID.
2024                          ($id, $sequence) = ("$prefix$1", "");                          ($id, $sequence) = ("$prefix$1", "");
2025                  } else {                  } else {
2026                          # Here we have a data line, so we add it to the sequence accumulator.                          # Here we have a data line, so we add it to the sequence accumulator.
2027                          # First, we get the actual data out. Note that we normalize to upper              # First, we get the actual data out. Note that we normalize to lower
2028                          # case.                          # case.
2029                          $line =~ /^\s*(.*?)(\s|\n)/;                          $line =~ /^\s*(.*?)(\s|\n)/;
2030                          $sequence .= $1;                          $sequence .= $1;
# Line 1449  Line 2032 
2032          }          }
2033          # Flush out the last sequence (if any).          # Flush out the last sequence (if any).
2034          if ($sequence) {          if ($sequence) {
2035                  $retVal{$id} = uc $sequence;          $retVal{$id} = lc $sequence;
2036          }          }
2037          # Close the file.          # Close the file.
2038          close FASTAFILE;          close FASTAFILE;
# Line 1536  Line 2119 
2119          # Get the data directory name.          # Get the data directory name.
2120          my $outputDirectory = $self->{_options}->{dataDir};          my $outputDirectory = $self->{_options}->{dataDir};
2121          # Dump the relations.          # Dump the relations.
2122          $self->{_erdb}->DumpRelations($outputDirectory);      $self->DumpRelations($outputDirectory);
2123  }  }
2124    
2125  =head3 XMLFileName  =head3 XMLFileName
# Line 1568  Line 2151 
2151  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and  The next statement inserts a C<HasProperty> relationship between feature C<fig|158879.1.peg.1> and
2152  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.  property C<4> with an evidence URL of C<http://seedu.uchicago.edu/query.cgi?article_id=142>.
2153    
2154  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence = 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>  C<< $sprout->InsertObject('HasProperty', { 'from-link' => 'fig|158879.1.peg.1', 'to-link' => 4, evidence => 'http://seedu.uchicago.edu/query.cgi?article_id=142'}); >>
2155    
2156  =over 4  =over 4
2157    
# Line 1588  Line 2171 
2171          # Get the parameters.          # Get the parameters.
2172          my ($self, $objectType, $fieldHash) = @_;          my ($self, $objectType, $fieldHash) = @_;
2173          # Call the underlying method.          # Call the underlying method.
2174          $self->{_erdb}->InsertObject($objectType, $fieldHash);      $self->InsertObject($objectType, $fieldHash);
2175  }  }
2176    
2177  =head3 Annotate  =head3 Annotate
# Line 1747  Line 2330 
2330          return @retVal;          return @retVal;
2331  }  }
2332    
2333  =head3 Exists  =head3 FeatureTranslation
2334    
2335  C<< my $found = $sprout->Exists($entityName, $entityID); >>  C<< my $translation = $sprout->FeatureTranslation($featureID); >>
2336    
2337  Return TRUE if an entity exists, else FALSE.  Return the translation of a feature.
2338    
2339  =over 4  =over 4
2340    
2341  =item entityName  =item featureID
   
 Name of the entity type (e.g. C<Feature>) relevant to the existence check.  
   
 =item entityID  
2342    
2343  ID of the entity instance whose existence is to be checked.  ID of the feature whose translation is desired
   
 =item RETURN  
   
 Returns TRUE if the entity instance exists, else FALSE.  
   
 =back  
   
 =cut  
 #: Return Type $;  
 sub Exists {  
         # Get the parameters.  
         my ($self, $entityName, $entityID) = @_;  
         # Check for the entity instance.  
         my $testInstance = $self->GetEntity($entityName, $entityID);  
         # Return an existence indicator.  
         my $retVal = ($testInstance ? 1 : 0);  
         return $retVal;  
 }  
   
 =head3 FeatureTranslation  
   
 C<< my $translation = $sprout->FeatureTranslation($featureID); >>  
   
 Return the translation of a feature.  
   
 =over 4  
   
 =item featureID  
   
 ID of the feature whose translation is desired  
2344    
2345  =item RETURN  =item RETURN
2346    
# Line 1966  Line 2515 
2515          return @retVal;          return @retVal;
2516  }  }
2517    
2518    =head3 GetProperties
2519    
2520    C<< my @list = $sprout->GetProperties($fid, $key, $value, $url); >>
2521    
2522    Return a list of the properties with the specified characteristics.
2523    
2524    Properties are arbitrary key-value pairs associated with a feature. (At some point they
2525    will also be associated with genomes.) A property value is represented by a 4-tuple of
2526    the form B<($fid, $key, $value, $url)>. These exactly correspond to the parameter
2527    
2528    =over 4
2529    
2530    =item fid
2531    
2532    ID of the feature possessing the property.
2533    
2534    =item key
2535    
2536    Name or key of the property.
2537    
2538    =item value
2539    
2540    Value of the property.
2541    
2542    =item url
2543    
2544    URL of the document that indicated the property should have this particular value, or an
2545    empty string if no such document exists.
2546    
2547    =back
2548    
2549    The parameters act as a filter for the desired data. Any non-null parameter will
2550    automatically match all the tuples returned. So, specifying just the I<$fid> will
2551    return all the properties of the specified feature; similarly, specifying the I<$key>
2552    and I<$value> parameters will return all the features having the specified property
2553    value.
2554    
2555    A single property key can have many values, representing different ideas about the
2556    feature in question. For example, one paper may declare that a feature C<fig|83333.1.peg.10> is
2557    virulent, and another may declare that it is not virulent. A query about the virulence of
2558    C<fig|83333.1.peg.10> would be coded as
2559    
2560        my @list = $sprout->GetProperties('fig|83333.1.peg.10', 'virulence', '', '');
2561    
2562    Here the I<$value> and I<$url> fields are left blank, indicating that those fields are
2563    not to be filtered. The tuples returned would be
2564    
2565        ('fig|83333.1.peg.10', 'virulence', 'yes', 'http://www.somewhere.edu/first.paper.pdf')
2566        ('fig|83333.1.peg.10', 'virulence', 'no', 'http://www.somewhere.edu/second.paper.pdf')
2567    
2568    =cut
2569    #: Return Type @@;
2570    sub GetProperties {
2571        # Get the parameters.
2572        my ($self, @parms) = @_;
2573        # Declare the return variable.
2574        my @retVal = ();
2575        # Now we need to create a WHERE clause that will get us the data we want. First,
2576        # we create a list of the columns containing the data for each parameter.
2577        my @colNames = ('HasProperty(from-link)', 'Property(property-name)',
2578                        'Property(property-value)', 'HasProperty(evidence)');
2579        # Now we build the WHERE clause and the list of parameter values.
2580        my @where = ();
2581        my @values = ();
2582        for (my $i = 0; $i <= $#colNames; $i++) {
2583            my $parm = $parms[$i];
2584            if (defined $parm && ($parm ne '')) {
2585                push @where, "$colNames[$i] = ?";
2586                push @values, $parm;
2587            }
2588        }
2589        # Format the WHERE clause.
2590        my $filter = (@values > 0 ? (join " AND ", @where) : undef);
2591        # Ask for all the propertie values with the desired characteristics.
2592        my $query = $self->Get(['HasProperty', 'Property'], $filter, \@values);
2593        while (my $valueObject = $query->Fetch()) {
2594            my @tuple = $valueObject->Values(\@colNames);
2595            push @retVal, \@tuple;
2596        }
2597        # Return the result.
2598        return @retVal;
2599    }
2600    
2601  =head3 FeatureProperties  =head3 FeatureProperties
2602    
2603  C<< my @properties = $sprout->FeatureProperties($featureID); >>  C<< my @properties = $sprout->FeatureProperties($featureID); >>
# Line 2160  Line 2792 
2792  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>  C<< my %subsystems = $sprout->SubsystemsOf($featureID); >>
2793    
2794  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped  Return a hash describing all the subsystems in which a feature participates. Each subsystem is mapped
2795  to the role the feature performs.  to the roles the feature performs.
2796    
2797  =over 4  =over 4
2798    
# Line 2170  Line 2802 
2802    
2803  =item RETURN  =item RETURN
2804    
2805  Returns a hash mapping all the feature's subsystems to the feature's role.  Returns a hash mapping all the feature's subsystems to a list of the feature's roles.
2806    
2807  =back  =back
2808    
2809  =cut  =cut
2810  #: Return Type %;  #: Return Type %@;
2811  sub SubsystemsOf {  sub SubsystemsOf {
2812          # Get the parameters.          # Get the parameters.
2813          my ($self, $featureID) = @_;          my ($self, $featureID) = @_;
2814          # Use the SSCell to connect features to subsystems.      # Get the subsystem list.
2815          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],          my @subsystems = $self->GetAll(['ContainsFeature', 'HasSSCell', 'IsRoleOf'],
2816                                                                          "ContainsFeature(to-link) = ?", [$featureID],                                                                          "ContainsFeature(to-link) = ?", [$featureID],
2817                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);                                                                          ['HasSSCell(from-link)', 'IsRoleOf(from-link)']);
2818          # Create the return value.          # Create the return value.
2819          my %retVal = ();          my %retVal = ();
2820        # Build a hash to weed out duplicates. Sometimes the same PEG and role appears
2821        # in two spreadsheet cells.
2822        my %dupHash = ();
2823          # Loop through the results, adding them to the hash.          # Loop through the results, adding them to the hash.
2824          for my $record (@subsystems) {          for my $record (@subsystems) {
2825                  $retVal{$record->[0]} = $record->[1];          # Get this subsystem and role.
2826            my ($subsys, $role) = @{$record};
2827            # Insure it's the first time for both.
2828            my $dupKey = "$subsys\n$role";
2829            if (! exists $dupHash{"$subsys\n$role"}) {
2830                $dupHash{$dupKey} = 1;
2831                push @{$retVal{$subsys}}, $role;
2832            }
2833          }          }
2834          # Return the hash.          # Return the hash.
2835          return %retVal;          return %retVal;
2836  }  }
2837    
2838    =head3 SubsystemList
2839    
2840    C<< my @subsystems = $sprout->SubsystemList($featureID); >>
2841    
2842    Return a list containing the names of the subsystems in which the specified
2843    feature participates. Unlike L</SubsystemsOf>, this method only returns the
2844    subsystem names, not the roles.
2845    
2846    =over 4
2847    
2848    =item featureID
2849    
2850    ID of the feature whose subsystem names are desired.
2851    
2852    =item RETURN
2853    
2854    Returns a list of the names of the subsystems in which the feature participates.
2855    
2856    =back
2857    
2858    =cut
2859    #: Return Type @;
2860    sub SubsystemList {
2861        # Get the parameters.
2862        my ($self, $featureID) = @_;
2863        # Get the list of names.
2864        my @retVal = $self->GetFlat(['ContainsFeature', 'HasSSCell'], "ContainsFeature(to-link) = ?",
2865                                    [$featureID], 'HasSSCell(from-link)');
2866        # Return the result.
2867        return @retVal;
2868    }
2869    
2870    =head3 GenomeSubsystemData
2871    
2872    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2873    
2874    Return a hash mapping genome features to their subsystem roles.
2875    
2876    =over 4
2877    
2878    =item genomeID
2879    
2880    ID of the genome whose subsystem feature map is desired.
2881    
2882    =item RETURN
2883    
2884    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2885    2-tuple contains a subsystem name followed by a role ID.
2886    
2887    =back
2888    
2889    =cut
2890    
2891    sub GenomeSubsystemData {
2892        # Get the parameters.
2893        my ($self, $genomeID) = @_;
2894        # Declare the return variable.
2895        my %retVal = ();
2896        # Get a list of the genome features that participate in subsystems. For each
2897        # feature we get its spreadsheet cells and the corresponding roles.
2898        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2899                                 "HasFeature(from-link) = ?", [$genomeID],
2900                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2901        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2902        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2903        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2904        # again at the end to filter out participation in subsystems with a negative variant code.
2905        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2906                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2907                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2908        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2909        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2910        # link these two lists together to create the result. First, we want a hash mapping
2911        # spreadsheet cells to subsystem names.
2912        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2913        # We loop through @cellData to build the hash.
2914        for my $roleEntry (@roleData) {
2915            # Get the data for this feature and cell.
2916            my ($fid, $cellID, $role) = @{$roleEntry};
2917            # Check for a subsystem name.
2918            my $subsys = $subHash{$cellID};
2919            if ($subsys) {
2920                # Insure this feature has an entry in the return hash.
2921                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2922                # Merge in this new data.
2923                push @{$retVal{$fid}}, [$subsys, $role];
2924            }
2925        }
2926        # Return the result.
2927        return %retVal;
2928    }
2929    
2930  =head3 RelatedFeatures  =head3 RelatedFeatures
2931    
2932  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2294  Line 3028 
3028          return @retVal;          return @retVal;
3029  }  }
3030    
 =head3 GetAll  
   
 C<< my @list = $sprout->GetAll(\@objectNames, $filterClause, \@parameters, \@fields, $count); >>  
   
 Return a list of values taken from the objects returned by a query. The first three  
 parameters correspond to the parameters of the L</Get> method. The final parameter is  
 a list of the fields desired from each record found by the query. The field name  
 syntax is the standard syntax used for fields in the B<ERDB> system--  
 B<I<objectName>(I<fieldName>)>-- where I<objectName> is the name of the relevant entity  
 or relationship and I<fieldName> is the name of the field.  
   
 The list returned will be a list of lists. Each element of the list will contain  
 the values returned for the fields specified in the fourth parameter. If one of the  
 fields specified returns multiple values, they are flattened in with the rest. For  
 example, the following call will return a list of the features in a particular  
 spreadsheet cell, and each feature will be represented by a list containing the  
 feature ID followed by all of its aliases.  
   
 C<< $query = $sprout->Get(['ContainsFeature', 'Feature'], "ContainsFeature(from-link) = ?", [$ssCellID], ['Feature(id)', 'Feature(alias)']); >>  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item fields  
   
 List of the fields to be returned in each element of the list returned.  
   
 =item count  
   
 Maximum number of records to return. If omitted or 0, all available records will be returned.  
   
 =item RETURN  
   
 Returns a list of list references. Each element of the return list contains the values for the  
 fields specified in the B<fields> parameter.  
   
 =back  
   
 =cut  
 #: Return Type @@;  
 sub GetAll {  
         # Get the parameters.  
         my ($self, $objectNames, $filterClause, $parameterList, $fields, $count) = @_;  
         # Create the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Set up a counter of the number of records read.  
         my $fetched = 0;  
         # Insure the counter has a value.  
         if (!defined $count) {  
                 $count = 0;  
         }  
         # Loop through the records returned, extracting the fields. Note that if the  
         # counter is non-zero, we stop when the number of records read hits the count.  
         my @retVal = ();  
         while (($count == 0 || $fetched < $count) && (my $row = $query->Fetch())) {  
                 my @rowData = $row->Values($fields);  
                 push @retVal, \@rowData;  
                 $fetched++;  
         }  
         # Return the resulting list.  
         return @retVal;  
 }  
   
 =head3 GetFlat  
   
 C<< my @list = $sprout->GetFlat(\@objectNames, $filterClause, $parameterList, $field); >>  
   
 This is a variation of L</GetAll> that asks for only a single field per record and  
 returns a single flattened list.  
   
 =over 4  
   
 =item objectNames  
   
 List containing the names of the entity and relationship objects to be retrieved.  
   
 =item filterClause  
   
 WHERE/ORDER BY clause (without the WHERE) to be used to filter and sort the query. The WHERE clause can  
 be parameterized with parameter markers (C<?>). Each field used must be specified in the standard form  
 B<I<objectName>(I<fieldName>)>. Any parameters specified in the filter clause should be added to the  
 parameter list as additional parameters. The fields in a filter clause can come from primary  
 entity relations, relationship relations, or secondary entity relations; however, all of the  
 entities and relationships involved must be included in the list of object names.  
   
 =item parameterList  
   
 List of the parameters to be substituted in for the parameters marks in the filter clause.  
   
 =item field  
   
 Name of the field to be used to get the elements of the list returned.  
   
 =item RETURN  
   
 Returns a list of values.  
   
 =back  
   
 =cut  
 #: Return Type @;  
 sub GetFlat {  
         # Get the parameters.  
         my ($self, $objectNames, $filterClause, $parameterList, $field) = @_;  
         # Construct the query.  
         my $query = $self->Get($objectNames, $filterClause, $parameterList);  
         # Create the result list.  
         my @retVal = ();  
         # Loop through the records, adding the field values found to the result list.  
         while (my $row = $query->Fetch()) {  
                 push @retVal, $row->Value($field);  
         }  
         # Return the list created.  
         return @retVal;  
 }  
   
3031  =head3 Protein  =head3 Protein
3032    
3033  C<< my $protein = Sprout::Protein($sequence, $table); >>  C<< my $protein = Sprout::Protein($sequence, $table); >>
# Line 2527  Line 3129 
3129          # Create the return list, priming it with the name of the data directory.          # Create the return list, priming it with the name of the data directory.
3130          my @retVal = ($self->{_options}->{dataDir});          my @retVal = ($self->{_options}->{dataDir});
3131          # Concatenate the table names.          # Concatenate the table names.
3132          push @retVal, $self->{_erdb}->GetTableNames();      push @retVal, $self->GetTableNames();
3133          # Return the result.          # Return the result.
3134          return @retVal;          return @retVal;
3135  }  }
3136    
3137  =head3 LowBBHs  =head3 LowBBHs
3138    
3139  C<< my %bbhMap = $sprout->GoodBBHs($featureID, $cutoff); >>  C<< my %bbhMap = $sprout->LowBBHs($featureID, $cutoff); >>
3140    
3141  Return the bidirectional best hits of a feature whose score is no greater than a  Return the bidirectional best hits of a feature whose score is no greater than a
3142  specified cutoff value. A higher cutoff value will allow inclusion of hits with  specified cutoff value. A higher cutoff value will allow inclusion of hits with
# Line 2576  Line 3178 
3178          return %retVal;          return %retVal;
3179  }  }
3180    
3181    =head3 Sims
3182    
3183    C<< my $simList = $sprout->Sims($fid, $maxN, $maxP, $select, $max_expand, $filters); >>
3184    
3185    Get a list of similarities for a specified feature. Similarity information is not kept in the
3186    Sprout database; rather, they are retrieved from a network server. The similarities are
3187    returned as B<Sim> objects. A Sim object is actually a list reference that has been blessed
3188    so that its elements can be accessed by name.
3189    
3190    Similarities can be either raw or expanded. The raw similarities are basic
3191    hits between features with similar DNA. Expanding a raw similarity drags in any
3192    features considered substantially identical. So, for example, if features B<A1>,
3193    B<A2>, and B<A3> are all substatially identical to B<A>, then a raw similarity
3194    B<[C,A]> would be expanded to B<[C,A] [C,A1] [C,A2] [C,A3]>.
3195    
3196    =over 4
3197    
3198    =item fid
3199    
3200    ID of the feature whose similarities are desired.
3201    
3202    =item maxN
3203    
3204    Maximum number of similarities to return.
3205    
3206    =item maxP
3207    
3208    Minumum allowable similarity score.
3209    
3210    =item select
3211    
3212    Selection criterion: C<raw> means only raw similarities are returned; C<fig>
3213    means only similarities to FIG features are returned; C<all> means all expanded
3214    similarities are returned; and C<figx> means similarities are expanded until the
3215    number of FIG features equals the maximum.
3216    
3217    =item max_expand
3218    
3219    The maximum number of features to expand.
3220    
3221    =item filters
3222    
3223    Reference to a hash containing filter information, or a subroutine that can be
3224    used to filter the sims.
3225    
3226    =item RETURN
3227    
3228    Returns a reference to a list of similarity objects, or C<undef> if an error
3229    occurred.
3230    
3231    =back
3232    
3233    =cut
3234    
3235    sub Sims {
3236        # Get the parameters.
3237        my ($self, $fid, $maxN, $maxP, $select, $max_expand, $filters) = @_;
3238        # Create the shim object to test for deleted FIDs.
3239        my $shim = FidCheck->new($self);
3240        # Ask the network for sims.
3241        my $retVal = FIGRules::GetNetworkSims($shim, $fid, {}, $maxN, $maxP, $select, $max_expand, $filters);
3242        # Return the result.
3243        return $retVal;
3244    }
3245    
3246  =head3 GetGroups  =head3 GetGroups
3247    
3248  C<< my %groups = $sprout->GetGroups(\@groupList); >>  C<< my %groups = $sprout->GetGroups(\@groupList); >>
# Line 2624  Line 3291 
3291      return %retVal;      return %retVal;
3292  }  }
3293    
3294    =head3 MyGenomes
3295    
3296    C<< my @genomes = Sprout::MyGenomes($dataDir); >>
3297    
3298    Return a list of the genomes to be included in the Sprout.
3299    
3300    This method is provided for use during the Sprout load. It presumes the Genome load file has
3301    already been created. (It will be in the Sprout data directory and called either C<Genome>
3302    or C<Genome.dtx>.) Essentially, it reads in the Genome load file and strips out the genome
3303    IDs.
3304    
3305    =over 4
3306    
3307    =item dataDir
3308    
3309    Directory containing the Sprout load files.
3310    
3311    =back
3312    
3313    =cut
3314    #: Return Type @;
3315    sub MyGenomes {
3316        # Get the parameters.
3317        my ($dataDir) = @_;
3318        # Compute the genome file name.
3319        my $genomeFileName = LoadFileName($dataDir, "Genome");
3320        # Extract the genome IDs from the files.
3321        my @retVal = map { $_ =~ /^(\S+)/; $1 } Tracer::GetFile($genomeFileName);
3322        # Return the result.
3323        return @retVal;
3324    }
3325    
3326    =head3 LoadFileName
3327    
3328    C<< my $fileName = Sprout::LoadFileName($dataDir, $tableName); >>
3329    
3330    Return the name of the load file for the specified table in the specified data
3331    directory.
3332    
3333    =over 4
3334    
3335    =item dataDir
3336    
3337    Directory containing the Sprout load files.
3338    
3339    =item tableName
3340    
3341    Name of the table whose load file is desired.
3342    
3343    =item RETURN
3344    
3345    Returns the name of the file containing the load data for the specified table, or
3346    C<undef> if no load file is present.
3347    
3348    =back
3349    
3350    =cut
3351    #: Return Type $;
3352    sub LoadFileName {
3353        # Get the parameters.
3354        my ($dataDir, $tableName) = @_;
3355        # Declare the return variable.
3356        my $retVal;
3357        # Check for the various file names.
3358        if (-e "$dataDir/$tableName") {
3359            $retVal = "$dataDir/$tableName";
3360        } elsif (-e "$dataDir/$tableName.dtx") {
3361            $retVal = "$dataDir/$tableName.dtx";
3362        }
3363        # Return the result.
3364        return $retVal;
3365    }
3366    
3367    =head3 DeleteGenome
3368    
3369    C<< my $stats = $sprout->DeleteGenome($genomeID, $testFlag); >>
3370    
3371    Delete a genome from the database.
3372    
3373    =over 4
3374    
3375    =item genomeID
3376    
3377    ID of the genome to delete
3378    
3379    =item testFlag
3380    
3381    If TRUE, then the DELETE statements will be traced, but no deletions will occur.
3382    
3383    =item RETURN
3384    
3385    Returns a statistics object describing the rows deleted.
3386    
3387    =back
3388    
3389    =cut
3390    #: Return Type $%;
3391    sub DeleteGenome {
3392        # Get the parameters.
3393        my ($self, $genomeID, $testFlag) = @_;
3394        # Perform the delete for the genome's features.
3395        my $retVal = $self->Delete('Feature', "fig|$genomeID.%", $testFlag);
3396        # Perform the delete for the primary genome data.
3397        my $stats = $self->Delete('Genome', $genomeID, $testFlag);
3398        $retVal->Accumulate($stats);
3399        # Return the result.
3400        return $retVal;
3401    }
3402    
3403    =head3 Fix
3404    
3405    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3406    
3407    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3408    Groups with the same primary name will be combined. The primary name is the
3409    first capitalized word in the group name.
3410    
3411    =over 4
3412    
3413    =item groupHash
3414    
3415    Hash to be fixed up.
3416    
3417    =item RETURN
3418    
3419    Returns a fixed-up version of the hash.
3420    
3421    =back
3422    
3423    =cut
3424    
3425    sub Fix {
3426        # Get the parameters.
3427        my (%groupHash) = @_;
3428        # Create the result hash.
3429        my %retVal = ();
3430        # Copy over the genomes.
3431        for my $groupID (keys %groupHash) {
3432            # Make a safety copy of the group ID.
3433            my $realGroupID = $groupID;
3434            # Yank the primary name.
3435            if ($groupID =~ /([A-Z]\w+)/) {
3436                $realGroupID = $1;
3437            }
3438            # Append this group's genomes into the result hash.
3439            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3440        }
3441        # Return the result hash.
3442        return %retVal;
3443    }
3444    
3445    =head3 ReadGroupFile
3446    
3447    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3448    
3449    Read in the data from the specified group file. The group file contains information
3450    about each of the NMPDR groups.
3451    
3452    =over 4
3453    
3454    =item name
3455    
3456    Name of the group.
3457    
3458    =item page
3459    
3460    Name of the group's page on the web site (e.g. C<campy.php> for
3461    Campylobacter)
3462    
3463    =item genus
3464    
3465    Genus of the group
3466    
3467    =item species
3468    
3469    Species of the group, or an empty string if the group is for an entire
3470    genus. If the group contains more than one species, the species names
3471    should be separated by commas.
3472    
3473    =back
3474    
3475    The parameters to this method are as follows
3476    
3477    =over 4
3478    
3479    =item groupFile
3480    
3481    Name of the file containing the group data.
3482    
3483    =item RETURN
3484    
3485    Returns a hash keyed on group name. The value of each hash
3486    
3487    =back
3488    
3489    =cut
3490    
3491    sub ReadGroupFile {
3492        # Get the parameters.
3493        my ($groupFileName) = @_;
3494        # Declare the return variable.
3495        my %retVal;
3496        # Read the group file.
3497        my @groupLines = Tracer::GetFile($groupFileName);
3498        for my $groupLine (@groupLines) {
3499            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3500            $retVal{$name} = [$page, $genus, $species];
3501        }
3502        # Return the result.
3503        return %retVal;
3504    }
3505    
3506  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3507    
3508  =head3 ParseAssignment  =head3 ParseAssignment
# Line 2634  Line 3513 
3513    
3514  A functional assignment is always of the form  A functional assignment is always of the form
3515    
3516      I<XXXX>C<\nset >I<YYYY>C< function to\n>I<ZZZZZ>      C<set >I<YYYY>C< function to\n>I<ZZZZZ>
3517    
3518  where I<XXXX> is the B<assigning user>, I<YYYY> is the B<user>, and I<ZZZZ> is the  where I<YYYY> is the B<user>, and I<ZZZZ> is the actual functional role. In most cases,
3519  actual functional role. In most cases, the user and the assigning user will be the  the user and the assigning user (from MadeAnnotation) will be the same, but that is
3520  same, but that is not always the case.  not always the case.
3521    
3522    In addition, the functional role may contain extra data that is stripped, such as
3523    terminating spaces or a comment separated from the rest of the text by a tab.
3524    
3525  This is a static method.  This is a static method.
3526    
3527  =over 4  =over 4
3528    
3529    =item user
3530    
3531    Name of the assigning user.
3532    
3533  =item text  =item text
3534    
3535  Text of the annotation.  Text of the annotation.
# Line 2659  Line 3545 
3545    
3546  sub _ParseAssignment {  sub _ParseAssignment {
3547          # Get the parameters.          # Get the parameters.
3548          my ($text) = @_;      my ($user, $text) = @_;
3549          # Declare the return value.          # Declare the return value.
3550          my @retVal = ();          my @retVal = ();
3551          # Check to see if this is a functional assignment.          # Check to see if this is a functional assignment.
3552          my ($user, $type, $function) = split(/\n/, $text);      my ($type, $function) = split(/\n/, $text);
3553          if ($type =~ m/^set ([^ ]+) function to$/i) {      if ($type =~ m/^set function to$/i) {
3554                  # Here it is, so we return the user name (which is in $1), the functional role text,          # Here we have an assignment without a user, so we use the incoming user ID.
3555          # and the assigning user.          @retVal = ($user, $function);
3556                  @retVal = ($1, $function, $user);      } elsif ($type =~ m/^set (\S+) function to$/i) {
3557            # Here we have an assignment with a user that is passed back to the caller.
3558            @retVal = ($1, $function);
3559        }
3560        # If we have an assignment, we need to clean the function text. There may be
3561        # extra junk at the end added as a note from the user.
3562        if (defined( $retVal[1] )) {
3563            $retVal[1] =~ s/(\t\S)?\s*$//;
3564          }          }
3565          # Return the result list.          # Return the result list.
3566          return @retVal;          return @retVal;
# Line 2695  Line 3588 
3588    
3589  sub FriendlyTimestamp {  sub FriendlyTimestamp {
3590      my ($timeValue) = @_;      my ($timeValue) = @_;
3591      my $retVal = strftime("%a %b %e %H:%M:%S %Y", localtime($timeValue));      my $retVal = localtime($timeValue);
3592      return $retVal;      return $retVal;
3593  }  }
3594    
3595    =head3 AddProperty
3596    
3597    C<< my  = $sprout->AddProperty($featureID, $key, $value, $url); >>
3598    
3599    Add a new attribute value (Property) to a feature. In the SEED system, attributes can
3600    be added to almost any object. In Sprout, they can only be added to features. In
3601    Sprout, attributes are implemented using I<properties>. A property represents a key/value
3602    pair. If the particular key/value pair coming in is not already in the database, a new
3603    B<Property> record is created to hold it.
3604    
3605    =over 4
3606    
3607    =item peg
3608    
3609    ID of the feature to which the attribute is to be replied.
3610    
3611    =item key
3612    
3613    Name of the attribute (key).
3614    
3615    =item value
3616    
3617    Value of the attribute.
3618    
3619    =item url
3620    
3621    URL or text citation from which the property was obtained.
3622    
3623    =back
3624    
3625    =cut
3626    #: Return Type ;
3627    sub AddProperty {
3628        # Get the parameters.
3629        my ($self, $featureID, $key, $value, $url) = @_;
3630        # Declare the variable to hold the desired property ID.
3631        my $propID;
3632        # Attempt to find a property record for this key/value pair.
3633        my @properties = $self->GetFlat(['Property'],
3634                                       "Property(property-name) = ? AND Property(property-value) = ?",
3635                                       [$key, $value], 'Property(id)');
3636        if (@properties) {
3637            # Here the property is already in the database. We save its ID.
3638            $propID = $properties[0];
3639            # Here the property value does not exist. We need to generate an ID. It will be set
3640            # to a number one greater than the maximum value in the database. This call to
3641            # GetAll will stop after one record.
3642            my @maxProperty = $self->GetAll(['Property'], "ORDER BY Property(id) DESC", [], ['Property(id)'],
3643                                            1);
3644            $propID = $maxProperty[0]->[0] + 1;
3645            # Insert the new property value.
3646            $self->Insert('Property', { 'property-name' => $key, 'property-value' => $value, id => $propID });
3647        }
3648        # Now we connect the incoming feature to the property.
3649        $self->Insert('HasProperty', { 'from-link' => $featureID, 'to-link' => $propID, evidence => $url });
3650    }
3651    
3652    
3653  1;  1;

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.83

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3