[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.72, Sun Jun 25 02:37:04 2006 UTC revision 1.84, Thu Sep 14 14:11:09 2006 UTC
# Line 15  Line 15 
15      use FidCheck;      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 92  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({
# Line 99  Line 102 
102                                                          # database type                                                          # database type
103                         dataDir      => $FIG_Config::sproutData,                         dataDir      => $FIG_Config::sproutData,
104                                                          # data file directory                                                          # data file directory
105                         xmlFileName  => "$FIG_Config::fig/SproutDBD.xml",                         xmlFileName  => "$dbd_dir/SproutDBD.xml",
106                                                          # database definition file name                                                          # database definition file name
107                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",                         userData     => "$FIG_Config::dbuser/$FIG_Config::dbpass",
108                                                          # user name and password                                                          # user name and password
109                         port         => $FIG_Config::dbport,                         port         => $FIG_Config::dbport,
110                                                          # database connection port                                                          # database connection port
111                         sock         => $FIG_Config::dbsock,                         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                         noDBOpen     => 0,               # 1 to suppress the database open
# Line 119  Line 123 
123      my $dbh;      my $dbh;
124      if (! $optionTable->{noDBOpen}) {      if (! $optionTable->{noDBOpen}) {
125          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,          $dbh = DBKernel->new($optionTable->{dbType}, $dbName, $userName,
126                                  $password, $optionTable->{port}, undef, $optionTable->{sock});                                  $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}";
# Line 1472  Line 1476 
1476      my %retVal = ();      my %retVal = ();
1477      # Loop through the incoming features.      # Loop through the incoming features.
1478      for my $featureID (@{$featureList}) {      for my $featureID (@{$featureList}) {
1479          # Create a query to get the feature's best hit.          # Ask the server for the feature's best hit.
1480          my $query = $self->Get(['IsBidirectionalBestHitOf'],          my @bbhData = FIGRules::BBHData($featureID);
                                "IsBidirectionalBestHitOf(from-link) = ? AND IsBidirectionalBestHitOf(genome) = ?",  
                                [$featureID, $genomeID]);  
1481          # Peel off the BBHs found.          # Peel off the BBHs found.
1482          my @found = ();          my @found = ();
1483          while (my $bbh = $query->Fetch) {          for my $bbh (@bbhData) {
1484              push @found, $bbh->Value('IsBidirectionalBestHitOf(to-link)');              push @found, $bbh->[0];
1485          }          }
1486          $retVal{$featureID} = \@found;          $retVal{$featureID} = \@found;
1487      }      }
# Line 1493  Line 1495 
1495    
1496  Return a list of the similarities to the specified feature.  Return a list of the similarities to the specified feature.
1497    
1498  Sprout does not support real similarities, so this method just returns the bidirectional  This method just returns the bidirectional best hits for performance reasons.
 best hits.  
1499    
1500  =over 4  =over 4
1501    
# Line 1514  Line 1515 
1515      # Get the parameters.      # Get the parameters.
1516      my ($self, $featureID, $count) = @_;      my ($self, $featureID, $count) = @_;
1517      # Ask for the best hits.      # Ask for the best hits.
1518      my @lists = $self->GetAll(['IsBidirectionalBestHitOf'],      my @lists = FIGRules::BBHData($featureID);
                               "IsBidirectionalBestHitOf(from-link) = ? ORDER BY IsBidirectionalBestHitOf(score) DESC",  
                               [$featureID], ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(score)'],  
                               $count);  
1519      # Create the return value.      # Create the return value.
1520      my %retVal = ();      my %retVal = ();
1521      for my $tuple (@lists) {      for my $tuple (@lists) {
# Line 1527  Line 1525 
1525      return %retVal;      return %retVal;
1526  }  }
1527    
   
   
1528  =head3 IsComplete  =head3 IsComplete
1529    
1530  C<< my $flag = $sprout->IsComplete($genomeID); >>  C<< my $flag = $sprout->IsComplete($genomeID); >>
# Line 1656  Line 1652 
1652  sub CoupledFeatures {  sub CoupledFeatures {
1653      # Get the parameters.      # Get the parameters.
1654      my ($self, $featureID) = @_;      my ($self, $featureID) = @_;
1655        Trace("Looking for features coupled to $featureID.") if T(coupling => 3);
1656      # Create a query to retrieve the functionally-coupled features.      # Create a query to retrieve the functionally-coupled features.
1657      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],      my $query = $self->Get(['ParticipatesInCoupling', 'Coupling'],
1658                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);                             "ParticipatesInCoupling(from-link) = ?", [$featureID]);
# Line 1668  Line 1665 
1665          # Get the ID and score of the coupling.          # Get the ID and score of the coupling.
1666          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',          my ($couplingID, $score) = $clustering->Values(['Coupling(id)',
1667                                                          'Coupling(score)']);                                                          'Coupling(score)']);
1668            Trace("$featureID coupled with score $score to ID $couplingID.") if T(coupling => 4);
1669          # Get the other feature that participates in the coupling.          # Get the other feature that participates in the coupling.
1670          my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],          my ($otherFeatureID) = $self->GetFlat(['ParticipatesInCoupling'],
1671                                             "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",                                             "ParticipatesInCoupling(to-link) = ? AND ParticipatesInCoupling(from-link) <> ?",
1672                                             [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');                                             [$couplingID, $featureID], 'ParticipatesInCoupling(from-link)');
1673            Trace("$couplingID target feature is $otherFeatureID.") if T(coupling => 4);
1674          # Attach the other feature's score to its ID.          # Attach the other feature's score to its ID.
1675          $retVal{$otherFeatureID} = $score;          $retVal{$otherFeatureID} = $score;
1676          $found = 1;          $found = 1;
# Line 1804  Line 1803 
1803      my ($self, $peg1, $peg2) = @_;      my ($self, $peg1, $peg2) = @_;
1804      # Declare the return values. We'll start with the coupling ID and undefine the      # Declare the return values. We'll start with the coupling ID and undefine the
1805      # flag and score until we have more information.      # flag and score until we have more information.
1806      my ($retVal, $inverted, $score) = (CouplingID($peg1, $peg2), undef, undef);      my ($retVal, $inverted, $score) = ($self->CouplingID($peg1, $peg2), undef, undef);
1807      # Find the coupling data.      # Find the coupling data.
1808      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],      my @pegs = $self->GetAll(['Coupling', 'ParticipatesInCoupling'],
1809                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",                                   "Coupling(id) = ? ORDER BY ParticipatesInCoupling(pos)",
# Line 1825  Line 1824 
1824      return ($retVal, $inverted, $score);      return ($retVal, $inverted, $score);
1825  }  }
1826    
1827    =head3 GetSynonymGroup
1828    
1829    C<< my $id = $sprout->GetSynonymGroup($fid); >>
1830    
1831    Return the synonym group name for the specified feature.
1832    
1833    =over 4
1834    
1835    =item fid
1836    
1837    ID of the feature whose synonym group is desired.
1838    
1839    =item RETURN
1840    
1841    The name of the synonym group to which the feature belongs. If the feature does
1842    not belong to a synonym group, the feature ID itself is returned.
1843    
1844    =back
1845    
1846    =cut
1847    
1848    sub GetSynonymGroup {
1849        # Get the parameters.
1850        my ($self, $fid) = @_;
1851        # Declare the return variable.
1852        my $retVal;
1853        # Find the synonym group.
1854        my @groups = $self->GetFlat(['IsSynonymGroupFor'], "IsSynonymGroupFor(to-link) = ?",
1855                                       [$fid], 'IsSynonymGroupFor(from-link)');
1856        # Check to see if we found anything.
1857        if (@groups) {
1858            $retVal = $groups[0];
1859        } else {
1860            $retVal = $fid;
1861        }
1862        # Return the result.
1863        return $retVal;
1864    }
1865    
1866    =head3 GetBoundaries
1867    
1868    C<< my ($contig, $beg, $end) = $sprout->GetBoundaries(@locList); >>
1869    
1870    Determine the begin and end boundaries for the locations in a list. All of the
1871    locations must belong to the same contig and have mostly the same direction in
1872    order for this method to produce a meaningful result. The resulting
1873    begin/end pair will contain all of the bases in any of the locations.
1874    
1875    =over 4
1876    
1877    =item locList
1878    
1879    List of locations to process.
1880    
1881    =item RETURN
1882    
1883    Returns a 3-tuple consisting of the contig ID, the beginning boundary,
1884    and the ending boundary. The beginning boundary will be left of the
1885    end for mostly-forward locations and right of the end for mostly-backward
1886    locations.
1887    
1888    =back
1889    
1890    =cut
1891    
1892    sub GetBoundaries {
1893        # Get the parameters.
1894        my ($self, @locList) = @_;
1895        # Set up the counters used to determine the most popular direction.
1896        my %counts = ( '+' => 0, '-' => 0 );
1897        # Get the last location and parse it.
1898        my $locObject = BasicLocation->new(pop @locList);
1899        # Prime the loop with its data.
1900        my ($contig, $beg, $end) = ($locObject->Contig, $locObject->Left, $locObject->Right);
1901        # Count its direction.
1902        $counts{$locObject->Dir}++;
1903        # Loop through the remaining locations. Note that in most situations, this loop
1904        # will not iterate at all, because most of the time we will be dealing with a
1905        # singleton list.
1906        for my $loc (@locList) {
1907            # Create a location object.
1908            my $locObject = BasicLocation->new($loc);
1909            # Count the direction.
1910            $counts{$locObject->Dir}++;
1911            # Get the left end and the right end.
1912            my $left = $locObject->Left;
1913            my $right = $locObject->Right;
1914            # Merge them into the return variables.
1915            if ($left < $beg) {
1916                $beg = $left;
1917            }
1918            if ($right > $end) {
1919                $end = $right;
1920            }
1921        }
1922        # If the most common direction is reverse, flip the begin and end markers.
1923        if ($counts{'-'} > $counts{'+'}) {
1924            ($beg, $end) = ($end, $beg);
1925        }
1926        # Return the result.
1927        return ($contig, $beg, $end);
1928    }
1929    
1930  =head3 CouplingID  =head3 CouplingID
1931    
1932  C<< my $couplingID = Sprout::CouplingID($peg1, $peg2); >>  C<< my $couplingID = $sprout->CouplingID($peg1, $peg2); >>
1933    
1934  Return the coupling ID for a pair of feature IDs.  Return the coupling ID for a pair of feature IDs.
1935    
# Line 1860  Line 1962 
1962  =cut  =cut
1963  #: Return Type $;  #: Return Type $;
1964  sub CouplingID {  sub CouplingID {
1965      return DigestKey(join " ", sort @_);      my ($self, @pegs) = @_;
1966        return $self->DigestKey(join " ", sort @pegs);
1967  }  }
1968    
1969  =head3 ReadFasta  =head3 ReadFasta
# Line 2756  Line 2859 
2859      return @retVal;      return @retVal;
2860  }  }
2861    
2862    =head3 GenomeSubsystemData
2863    
2864    C<< my %featureData = $sprout->GenomeSubsystemData($genomeID); >>
2865    
2866    Return a hash mapping genome features to their subsystem roles.
2867    
2868    =over 4
2869    
2870    =item genomeID
2871    
2872    ID of the genome whose subsystem feature map is desired.
2873    
2874    =item RETURN
2875    
2876    Returns a hash mapping each feature of the genome to a list of 2-tuples. Eacb
2877    2-tuple contains a subsystem name followed by a role ID.
2878    
2879    =back
2880    
2881    =cut
2882    
2883    sub GenomeSubsystemData {
2884        # Get the parameters.
2885        my ($self, $genomeID) = @_;
2886        # Declare the return variable.
2887        my %retVal = ();
2888        # Get a list of the genome features that participate in subsystems. For each
2889        # feature we get its spreadsheet cells and the corresponding roles.
2890        my @roleData = $self->GetAll(['HasFeature', 'ContainsFeature', 'IsRoleOf'],
2891                                 "HasFeature(from-link) = ?", [$genomeID],
2892                                 ['HasFeature(to-link)', 'IsRoleOf(to-link)', 'IsRoleOf(from-link)']);
2893        # Now we get a list of the spreadsheet cells and their associated subsystems. Subsystems
2894        # with an unknown variant code (-1) are skipped. Note the genome ID is at both ends of the
2895        # list. We use it at the beginning to get all the spreadsheet cells for the genome and
2896        # again at the end to filter out participation in subsystems with a negative variant code.
2897        my @cellData = $self->GetAll(['IsGenomeOf', 'HasSSCell', 'ParticipatesIn'],
2898                                     "IsGenomeOf(from-link) = ? AND ParticipatesIn(variant-code) >= 0 AND ParticipatesIn(from-link) = ?",
2899                                     [$genomeID, $genomeID], ['HasSSCell(to-link)', 'HasSSCell(from-link)']);
2900        # Now "@roleData" lists the spreadsheet cell and role for each of the genome's features.
2901        # "@cellData" lists the subsystem name for each of the genome's spreadsheet cells. We
2902        # link these two lists together to create the result. First, we want a hash mapping
2903        # spreadsheet cells to subsystem names.
2904        my %subHash = map { $_->[0] => $_->[1] } @cellData;
2905        # We loop through @cellData to build the hash.
2906        for my $roleEntry (@roleData) {
2907            # Get the data for this feature and cell.
2908            my ($fid, $cellID, $role) = @{$roleEntry};
2909            # Check for a subsystem name.
2910            my $subsys = $subHash{$cellID};
2911            if ($subsys) {
2912                # Insure this feature has an entry in the return hash.
2913                if (! exists $retVal{$fid}) { $retVal{$fid} = []; }
2914                # Merge in this new data.
2915                push @{$retVal{$fid}}, [$subsys, $role];
2916            }
2917        }
2918        # Return the result.
2919        return %retVal;
2920    }
2921    
2922  =head3 RelatedFeatures  =head3 RelatedFeatures
2923    
2924  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>  C<< my @relatedList = $sprout->RelatedFeatures($featureID, $function, $userID); >>
# Line 2791  Line 2954 
2954      # Get the parameters.      # Get the parameters.
2955      my ($self, $featureID, $function, $userID) = @_;      my ($self, $featureID, $function, $userID) = @_;
2956      # Get a list of the features that are BBHs of the incoming feature.      # Get a list of the features that are BBHs of the incoming feature.
2957      my @bbhFeatures = $self->GetFlat(['IsBidirectionalBestHitOf'],      my @bbhFeatures = map { $_->[0] } FIGRules::BBHData($featureID);
                                      "IsBidirectionalBestHitOf(from-link) = ?", [$featureID],  
                                      'IsBidirectionalBestHitOf(to-link)');  
2958      # Now we loop through the features, pulling out the ones that have the correct      # Now we loop through the features, pulling out the ones that have the correct
2959      # functional assignment.      # functional assignment.
2960      my @retVal = ();      my @retVal = ();
# Line 2994  Line 3155 
3155      my ($self, $featureID, $cutoff) = @_;      my ($self, $featureID, $cutoff) = @_;
3156      # Create the return hash.      # Create the return hash.
3157      my %retVal = ();      my %retVal = ();
3158      # Create a query to get the desired BBHs.      # Query for the desired BBHs.
3159      my @bbhList = $self->GetAll(['IsBidirectionalBestHitOf'],      my @bbhList = FIGRules::BBHData($featureID, $cutoff);
                                 'IsBidirectionalBestHitOf(sc) <= ? AND IsBidirectionalBestHitOf(from-link) = ?',  
                                 [$cutoff, $featureID],  
                                 ['IsBidirectionalBestHitOf(to-link)', 'IsBidirectionalBestHitOf(sc)']);  
3160      # Form the results into the return hash.      # Form the results into the return hash.
3161      for my $pair (@bbhList) {      for my $pair (@bbhList) {
3162          $retVal{$pair->[0]} = $pair->[1];          $retVal{$pair->[0]} = $pair->[1];
# Line 3229  Line 3387 
3387      return $retVal;      return $retVal;
3388  }  }
3389    
3390    =head3 Fix
3391    
3392    C<< my %fixedHash = Sprout::Fix(%groupHash); >>
3393    
3394    Prepare a genome group hash (like that returned by L</GetGroups> for processing.
3395    Groups with the same primary name will be combined. The primary name is the
3396    first capitalized word in the group name.
3397    
3398    =over 4
3399    
3400    =item groupHash
3401    
3402    Hash to be fixed up.
3403    
3404    =item RETURN
3405    
3406    Returns a fixed-up version of the hash.
3407    
3408    =back
3409    
3410    =cut
3411    
3412    sub Fix {
3413        # Get the parameters.
3414        my (%groupHash) = @_;
3415        # Create the result hash.
3416        my %retVal = ();
3417        # Copy over the genomes.
3418        for my $groupID (keys %groupHash) {
3419            # Make a safety copy of the group ID.
3420            my $realGroupID = $groupID;
3421            # Yank the primary name.
3422            if ($groupID =~ /([A-Z]\w+)/) {
3423                $realGroupID = $1;
3424            }
3425            # Append this group's genomes into the result hash.
3426            Tracer::AddToListMap(\%retVal, $realGroupID, @{$groupHash{$groupID}});
3427        }
3428        # Return the result hash.
3429        return %retVal;
3430    }
3431    
3432    =head3 ReadGroupFile
3433    
3434    C<< my %groupData = Sprout::ReadGroupFile($groupFileName); >>
3435    
3436    Read in the data from the specified group file. The group file contains information
3437    about each of the NMPDR groups.
3438    
3439    =over 4
3440    
3441    =item name
3442    
3443    Name of the group.
3444    
3445    =item page
3446    
3447    Name of the group's page on the web site (e.g. C<campy.php> for
3448    Campylobacter)
3449    
3450    =item genus
3451    
3452    Genus of the group
3453    
3454    =item species
3455    
3456    Species of the group, or an empty string if the group is for an entire
3457    genus. If the group contains more than one species, the species names
3458    should be separated by commas.
3459    
3460    =back
3461    
3462    The parameters to this method are as follows
3463    
3464    =over 4
3465    
3466    =item groupFile
3467    
3468    Name of the file containing the group data.
3469    
3470    =item RETURN
3471    
3472    Returns a hash keyed on group name. The value of each hash
3473    
3474    =back
3475    
3476    =cut
3477    
3478    sub ReadGroupFile {
3479        # Get the parameters.
3480        my ($groupFileName) = @_;
3481        # Declare the return variable.
3482        my %retVal;
3483        # Read the group file.
3484        my @groupLines = Tracer::GetFile($groupFileName);
3485        for my $groupLine (@groupLines) {
3486            my ($name, $page, $genus, $species) = split(/\t/, $groupLine);
3487            $retVal{$name} = [$page, $genus, $species];
3488        }
3489        # Return the result.
3490        return %retVal;
3491    }
3492    
3493  =head2 Internal Utility Methods  =head2 Internal Utility Methods
3494    
3495  =head3 ParseAssignment  =head3 ParseAssignment
# Line 3285  Line 3546 
3546      }      }
3547      # If we have an assignment, we need to clean the function text. There may be      # If we have an assignment, we need to clean the function text. There may be
3548      # extra junk at the end added as a note from the user.      # extra junk at the end added as a note from the user.
3549      if (@retVal) {      if (defined( $retVal[1] )) {
3550          $retVal[1] =~ s/(\t\S)?\s*$//;          $retVal[1] =~ s/(\t\S)?\s*$//;
3551      }      }
3552      # Return the result list.      # Return the result list.

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.84

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3