[Bio] / SeedViewer / MetagenomeAnalysis.pm Repository:
ViewVC logotype

Diff of /SeedViewer/MetagenomeAnalysis.pm

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

revision 1.4, Tue Apr 22 19:56:18 2008 UTC revision 1.12, Wed Jun 11 20:52:32 2008 UTC
# Line 10  Line 10 
10    
11  1;  1;
12    
   
   
13  use constant QUERY_DEFAULTS =>  use constant QUERY_DEFAULTS =>
14    { 1 => { evalue => '1e-05', align_len => 50 }, # RDP    { 1 => { evalue => '1e-05', align_len => 50 }, # RDP
15      2 => { evalue => '0.01' }, # SEED      2 => { evalue => '0.01' }, # SEED
# Line 21  Line 19 
19      6 => { evalue => '0.01' }, # Subsystem      6 => { evalue => '0.01' }, # Subsystem
20    };    };
21    
   
   
   
22  sub new {  sub new {
23    my ($class, $job) = @_;    my ($class, $job) = @_;
24    
# Line 36  Line 31 
31    my $dbh;    my $dbh;
32    eval {    eval {
33    
34        my $dbms     = $FIG_Config::mgrast_dbms;
35      my $host     = $FIG_Config::mgrast_dbhost;      my $host     = $FIG_Config::mgrast_dbhost;
36      my $database = $FIG_Config::mgrast_db;      my $database = $FIG_Config::mgrast_db;
37      my $user     = $FIG_Config::mgrast_dbuser;      my $user     = $FIG_Config::mgrast_dbuser;
38      my $password = '';      my $password = '';
39    
40        if ($dbms eq 'Pg')
41        {
42            $dbh = DBI->connect("DBI:Pg:dbname=$database;host=$host", $user, $password,
43                            { RaiseError => 1, AutoCommit => 0, PrintError => 0 }) ||
44                                die "database connect error.";
45        }
46        elsif ($dbms eq 'mysql' or $dbms eq '') # Default to mysql
47        {
48      $dbh = DBI->connect("DBI:mysql:database=$database;host=$host", $user, $password,      $dbh = DBI->connect("DBI:mysql:database=$database;host=$host", $user, $password,
49                          { RaiseError => 1, AutoCommit => 0, PrintError => 0 }) ||                          { RaiseError => 1, AutoCommit => 0, PrintError => 0 }) ||
50                                            die "database connect error.";                                            die "database connect error.";
51        }
52        else
53        {
54            die "MetagenomeAnalysis: unknown dbms '$dbms'";
55        }
56    
57    };    };
58    if ($@) {    if ($@) {
59      warn "Unable to connect to metagenomics database: $@\n";      warn "Unable to connect to metagenomics database: $@\n";
# Line 65  Line 75 
75    
76  }  }
77    
   
   
   
78  sub job {  sub job {
79    return $_[0]->{job};    return $_[0]->{job};
80  }  }
# Line 85  Line 92 
92    return $_[0]->{dbtable};    return $_[0]->{dbtable};
93  }  }
94    
95    sub dbtable_best_psc {
96      unless (defined $_[0]->{dbtable}) {
97        $_[0]->{dbtable} = 'tax_sim_best_by_psc_'.$_[0]->job->id;
98      }
99      return $_[0]->{dbtable};
100    }
101    
102    sub dbtable_best_iden {
103      unless (defined $_[0]->{dbtable}) {
104        $_[0]->{dbtable} = 'tax_sim_best_by_iden_'.$_[0]->job->id;
105      }
106      return $_[0]->{dbtable};
107    }
108    
109  sub get_key2taxa_mapping {  sub get_key2taxa_mapping {
110    
# Line 149  Line 169 
169    
170  =pod  =pod
171    
172    =over 4
173    
174  =item * B<query_evalue> (I<evalue>)  =item * B<query_evalue> (I<evalue>)
175    
176  Set/get the expectation value which is currently used to query the database.  Set/get the expectation value which is currently used to query the database.
# Line 315  Line 337 
337  #******************************************************************************  #******************************************************************************
338    
339    
   
   
   
340  =pod  =pod
341    
342  =item * B<get_sequence> (I<sequence_id>)  =item * B<get_sequence> (I<sequence_id>)
# Line 360  Line 379 
379  sub get_hits_count {  sub get_hits_count {
380    my ($self, $dataset) = @_;    my ($self, $dataset) = @_;
381    
382    my $table = $self->dbtable;    my $table = $self->dbtable_best_psc;
383    my $dbid  = $self->get_dataset_id($dataset);    my $dbid  = $self->get_dataset_id($dataset);
384    my $where = $self->get_where_clause();    my $where = $self->get_where_clause();
385    $where = ($where) ? "and $where" : '';    $where = ($where) ? "and $where" : '';
386    
387    my $sth = $self->dbh->prepare("select count(*) from ( select id1, min(rank_psc) from $table where dbid=$dbid $where group by id1) as b");    my $sth = $self->dbh->prepare("select count(distinct id1) from  $table where dbid=$dbid $where");
388    $sth->execute;    $sth->execute;
389    my ($result) = $sth->fetchrow_array;    my ($result) = $sth->fetchrow_array;
390    
# Line 390  Line 409 
409  sub get_group_counts {  sub get_group_counts {
410    my ($self, $dataset, $group, $filter1, $filter2) = @_;    my ($self, $dataset, $group, $filter1, $filter2) = @_;
411    
412    my $table = $self->dbtable;    my $table = $self->dbtable_best_psc;
413    my $dbid  = $self->get_dataset_id($dataset);    my $dbid  = $self->get_dataset_id($dataset);
414    my $where = $self->get_where_clause();    my $where = $self->get_where_clause();
415    $where = ($where) ? "and $where" : '';    $where = ($where) ? "and $where" : '';
# Line 401  Line 420 
420    push @filters, "tax_group_2='$filter2'" if($filter2);    push @filters, "tax_group_2='$filter2'" if($filter2);
421    my $filter = (scalar(@filters)) ? 'and '.join(' and ', @filters) : '';    my $filter = (scalar(@filters)) ? 'and '.join(' and ', @filters) : '';
422    
423    my $sth = $self->dbh->prepare("select s.$group as tax, count(*) as num from ( select id1, min(rank_psc) as rank from $table where dbid=$dbid $where group by id1) as b inner join $table as s on b.id1=s.id1 and b.rank=s.rank_psc where dbid=$dbid $filter group by s.$group");    my $sth = $self->dbh->prepare("select $group as tax, count(*) as num from $table where dbid=$dbid $where $filter group by tax");
424    $sth->execute;    $sth->execute;
425    my $result = $sth->fetchall_arrayref();    my $result = $sth->fetchall_arrayref();
426    
# Line 422  Line 441 
441  sub get_taxa_counts {  sub get_taxa_counts {
442    my ($self, $dataset) = @_;    my ($self, $dataset) = @_;
443    
444    my $table = $self->dbtable;    my $table = $self->dbtable_best_psc;
445    my $dbid  = $self->get_dataset_id($dataset);    my $dbid  = $self->get_dataset_id($dataset);
446    my $where = $self->get_where_clause();    my $where = $self->get_where_clause();
447    $where = ($where) ? "and $where" : '';    $where = ($where) ? "and $where" : '';
448    
449    my $sth = $self->dbh->prepare("select s.tax_str as tax, count(*) as num from ( select id1, min(rank_psc) as rank from $table where dbid=$dbid $where group by id1) as b inner join $table as s on b.id1=s.id1 and b.rank=s.rank_psc where dbid=$dbid group by s.tax_str");    my $sth = $self->dbh->prepare("select tax_str as tax, count(*) from $table where dbid=$dbid group by tax");
450    
451    $sth->execute;    $sth->execute;
452    my $result = $sth->fetchall_arrayref();    my $result = $sth->fetchall_arrayref();
# Line 449  Line 468 
468  sub get_subsystem_counts {  sub get_subsystem_counts {
469    my ($self, $dataset) = @_;    my ($self, $dataset) = @_;
470    
471    my $table = $self->dbtable;    my $table = $self->dbtable_best_psc;
472    my $dbid  = $self->get_dataset_id($dataset);    my $dbid  = $self->get_dataset_id($dataset);
473    my $where = $self->get_where_clause();    my $where = $self->get_where_clause();
474    $where = ($where) ? "and $where" : '';    $where = ($where) ? "and $where" : '';
475    
476    #print STDERR "select s.tax_group_1, s.tax_group_2, s.tax_group_3, s.tax_str, count(*) as num from ( select id1, min(rank_psc) as rank from $table where dbid=$dbid $where group by id1) as b inner join $table as s on b.id1=s.id1 and b.rank=s.rank_psc where dbid=$dbid group by s.tax_group_1, s.tax_group_2, s.tax_group_3";    my $sth = $self->dbh->prepare("select tax_group_1, tax_group_2, tax_group_3, tax_str, count(*) as num from $table where dbid=$dbid group by tax_group_1, tax_group_2, tax_group_3, tax_str");
  #die;  
   my $sth = $self->dbh->prepare("select s.tax_group_1, s.tax_group_2, s.tax_group_3, s.tax_str, count(*) as num from ( select id1, min(rank_psc) as rank from $table where dbid=$dbid $where group by id1) as b inner join $table as s on b.id1=s.id1 and b.rank=s.rank_psc where dbid=$dbid group by s.tax_group_1, s.tax_group_2, s.tax_group_3");  
477    
478    $sth->execute;    $sth->execute;
479    my $result = $sth->fetchall_arrayref();    my $result = $sth->fetchall_arrayref();
# Line 480  Line 497 
497  sub get_sequence_subset {  sub get_sequence_subset {
498    my ($self, $dataset, $filter) = @_;    my ($self, $dataset, $filter) = @_;
499    
500    my $table = $self->dbtable;    my $table = $self->dbtable_best_psc;
501    my $dbid  = $self->get_dataset_id($dataset);    my $dbid  = $self->get_dataset_id($dataset);
502    my $where = $self->get_where_clause();    my $where = $self->get_where_clause();
503    $where = ($where) ? "and $where" : '';    $where = ($where) ? "and $where" : '';
504    
505    my $sth = $self->dbh->prepare("select s.id1, s.ali_ln, s.id2, s.tax_str from ( select id1, min(rank_psc) as rank from $table where dbid=$dbid $where group by id1) as b inner join $table as s on b.id1=s.id1 and b.rank=s.rank_psc where dbid=$dbid and s.tax_str like '$filter%'");    my $sth = $self->dbh->prepare("select id1, ali_ln, id2, tax_str from $table where dbid=$dbid $where and tax_str like '$filter%'");
506      $sth->execute;
507      my $result = $sth->fetchall_arrayref();
508    
509      return $result;
510    
511    }
512    
513    
514    =pod
515    
516    =item * B<get_recruitment_plot_data> (I<genome>)
517    
518    Given a genome id (83333.1), this method returns all sequence ids,
519    the alignment length, the match id and the taxonomy string for all
520    sequences which match the criteria and have their tax_str start equal
521    the genome tax string I<filter>.
522    
523    =cut
524    
525    sub get_recruitment_plot_data {
526      my ($self, $genome) = @_;
527    
528      my $table = $self->dbtable_best_psc;
529      my $dbid  = $self->get_dataset_id("SEED");
530      my $where = $self->get_where_clause();
531      $where = ($where) ? "and $where" : '';
532    
533      my ($tax_id) = $self->dbh->selectrow_array("select tax_str from rdp_to_tax where seq_num='". $genome . "'");
534    
535      if($tax_id =~ /(\S+)\s/){
536        $tax_id = $1;
537      }
538      my $sth = $self->dbh->prepare("select id1, id2, b2, e2, logpsc from $table where dbid=$dbid $where and tax_str='$tax_id'");
539    
540    $sth->execute;    $sth->execute;
541    my $result = $sth->fetchall_arrayref();    my $result = $sth->fetchall_arrayref();
# Line 495  Line 545 
545  }  }
546    
547    
548    
549    
550  =pod  =pod
551    
552  =item * B<get_hits_for_sequence> (I<seq_id>, I<dataset>, I<limit>)  =item * B<get_hits_for_sequence> (I<seq_id>, I<dataset>, I<limit>)
# Line 546  Line 598 
598    
599  }  }
600    
601    =pod
602    
603    =item * B<get_genome_id> (I<tax_str>)
604    
605    =cut
606    
607    sub get_genome_id {
608      my ($self, $tax_str) = @_;
609      return $self->dbh->selectrow_array("select seq_num from rdp_to_tax where tax_str='". $tax_str . "'")->[0];
610    }
611    
612    =pod
613    
614    =back
615    
616    =cut

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3