Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'develop' of github.com:MG-RAST/MG-RAST into develop

  • Loading branch information...
commit 971edf2562fe654ee19ec9c48d933d0e3ca5b1ff 2 parents a7f87b4 + ff9cb0e
@jaredbischof jaredbischof authored
View
107 src/MGRAST/lib/Analysis.pm
@@ -65,6 +65,7 @@ sub new {
jobs => [], # array: job_id
job_map => {}, # hash: mg_id => job_id
mg_map => {}, # hash: job_id => mg_id
+ name_map => {}, # hash: mg_id => job_name
sources => $srcs, # hash: source_name => { col => value }
id_src => \%idsrc, # hash: source_id => source_name
src_id => \%srcid, # hash: source_name => source_id
@@ -127,6 +128,10 @@ sub _mg_map {
my ($self) = @_;
return $self->{mg_map};
}
+sub _name_map {
+ my ($self) = @_;
+ return $self->{name_map};
+}
sub _id_src {
my ($self) = @_;
return $self->{id_src};
@@ -178,6 +183,7 @@ sub add_jobs {
# set values for $self->{jobs} and $self->{jtbl} based on metagenome_id list
sub set_jobs {
my ($self, $mgids, $jids) = @_;
+ $self->{name_map} = {};
if (defined($jids)) {
$self->{job_map} = $self->_get_jobid_map($mgids, 1);
} else {
@@ -203,7 +209,7 @@ sub _set_data {
}
sub _get_jobid_map {
- my ($self, $mgids, $jids) = @_;
+ my ($self, $mgids, $jids, $no_names) = @_;
unless ($mgids && scalar(@$mgids)) {
return {};
}
@@ -211,9 +217,12 @@ sub _get_jobid_map {
my $list = join(",", map {"'$_'"} @$mgids);
my $rows;
if ($jids) {
- $rows = $self->_jcache->selectall_arrayref("SELECT metagenome_id, job_id FROM Job WHERE job_id IN ($list) AND viewable = 1");
+ $rows = $self->_jcache->selectall_arrayref("SELECT metagenome_id, job_id, name FROM Job WHERE job_id IN ($list) AND viewable = 1");
} else {
- $rows = $self->_jcache->selectall_arrayref("SELECT metagenome_id, job_id FROM Job WHERE metagenome_id IN ($list) AND viewable = 1");
+ $rows = $self->_jcache->selectall_arrayref("SELECT metagenome_id, job_id, name FROM Job WHERE metagenome_id IN ($list) AND viewable = 1");
+ }
+ unless ($no_names) {
+ map { $self->{name_map}->{$_->[0]} = $_->[2] } @$rows;
}
if ($rows && (@$rows > 0)) {
%$hash = map { $_->[0], $_->[1] } @$rows;
@@ -520,40 +529,78 @@ sub _gammaln {
####################
sub get_hierarchy {
- my ($self, $type, $src, $use_taxid, $get_ids) = @_;
+ my ($self, $type, $src, $use_taxid, $get_ids, $max_lvl) = @_;
my $tbl = exists($self->_atbl->{$type}) ? $self->_atbl->{$type} : '';
- unless ($tbl) { return {}; }
+ my $col = $self->_get_table_cols($tbl);
+ unless ($tbl && @$col) { return {}; }
+ unless ($max_lvl) { $max_lvl = ''; }
+ if (($max_lvl eq 'level4') && ($src =~ /^[NC]OG$/)) { $max_lvl = 'level3'; }
+ my @cols = ();
my $hier = {};
- my $sql = "";
- if ($type eq 'ontology') {
- my $key = $get_ids ? '_id' : 'name';
- $sql = "SELECT $key,level1,level2,level3,level4 FROM ".$self->_atbl->{$type};
- if ($src) {
- $sql .= " WHERE source = ".$self->_src_id->{$src};
- }
- } else {
- $sql = "SELECT ncbi_tax_id,tax_domain,tax_phylum,tax_class,tax_order,tax_family,tax_genus,tax_species,name,_id FROM ".$self->_atbl->{$type};
- }
- my $tmp = $self->_dbh->selectall_arrayref($sql);
- unless ($tmp && @$tmp) { return {}; }
- if ($type eq 'ontology') {
- if ($tmp->[0][4]) {
- map { $hier->{$_->[0]} = [ @$_[1..4] ] } @$tmp;
- } else {
- map { $hier->{$_->[0]} = [ @$_[1..3] ] } @$tmp;
+ my $key = $get_ids ? '_id' : 'name';
+ my $pref = ($type eq 'ontology') ? 'level' : 'tax_';
+
+ foreach my $c ( grep {$_ =~ /^$pref/} @$col ) {
+ next if ($c eq 'tax_kingdom'); # ncbi hack
+ next if (($c eq 'level4') && ($src =~ /^[NC]OG$/)); # n|cog hack
+ if ($c ne $max_lvl) {
+ push @cols, $c;
}
- } else {
- if ($use_taxid) {
- map { $hier->{$_->[0]} = [ @$_[1..8] ] } grep { $_->[0] } @$tmp;
- } elsif ($get_ids) {
- map { $hier->{$_->[9]} = [ @$_[1..7] ] } @$tmp;
- } else {
- map { $hier->{$_->[8]} = [ @$_[1..7] ] } @$tmp;
+ else {
+ push @cols, $c;
+ $key = $c;
+ last;
}
}
+ if (($type eq 'organism') && $use_taxid && (! $max_lvl)) {
+ $key = 'ncbi_tax_id';
+ }
+ push @cols, $key;
+
+ my $sql = "SELECT DISTINCT ".join(", ", @cols)." FROM ".$self->_atbl->{$type};
+ if (($type eq 'ontology') && $src) {
+ $sql .= " WHERE source = ".$self->_src_id->{$src};
+ }
+ foreach my $row ( @{$self->_dbh->selectall_arrayref($sql)} ) {
+ my $id = pop @$row;
+ next unless ($id && ($id =~ /\S/));
+ $hier->{$id} = $row;
+ }
return $hier;
+ # { end_node => [ hierachy of node ] }
+}
+
+sub get_hierarchy_slice {
+ my ($self, $type, $source, $parent_name, $child_level) = @_;
+
+ my $tbl = exists($self->_atbl->{$type}) ? $self->_atbl->{$type} : '';
+ my $col = $self->_get_table_cols($tbl);
+ unless ($tbl && @$col && $parent_name && $child_level && grep(/^$child_level$/, @$col)) {
+ return [];
+ }
+ my $data = [];
+ my $child_index = first { $col->[$_] eq $child_level } 0..$#{$col};
+ # level does not exist
+ unless ($child_index) {
+ return [];
+ }
+ # no parent available
+ if (($child_level eq 'tax_domain') || ($child_level eq 'level1')) {
+ return [];
+ }
+ my $parent_index = $child_index - 1;
+ # ncbi hack
+ if ($child_level eq 'tax_phylum') {
+ $parent_index -= 1;
+ }
+ my $sql = "SELECT DISTINCT ".$col->[$child_index]." FROM ".$self->_atbl->{$type}." WHERE ".$col->[$parent_index]." = ".$self->_dbh->quote($parent_name);
+ if (($type eq 'ontology') && $source) {
+ $sql .= " AND source = ".$self->_src_id->{$source};
+ }
+ my $cols = $self->_dbh->selectcol_arrayref($sql);
+ return ($cols && @$cols) ? $cols : [];
}
sub _get_annotation_map {
@@ -871,7 +918,7 @@ sub metagenome_search {
return [];
}
# [ mgid ]
- return [ keys %{$self->_get_jobid_map($rows, 1)} ];
+ return [ keys %{$self->_get_jobid_map($rows, 1, 1)} ];
}
####################
View
99 src/MGRAST/lib/resources2/m5nr.pm
@@ -17,17 +17,26 @@ sub new {
# Add name / attributes
$self->{name} = "m5nr";
- $self->{attributes} = { 'hierarchy' => { 'NCBI' => [ 'hash', '7 level NCBI taxonomy' ],
- 'COG' => [ 'hash', '3 level COG ontology' ],
- 'NOG' => [ 'hash', '3 level NOG ontology' ],
- 'KO' => [ 'hash', '4 level KEGG-KO ontology' ],
- 'Subsystems' => [ 'hash', '4 level SEED-Subsystems ontology' ] },
- 'sources' => [ 'hash', [['key', ['string', 'source name']],
- ['value', ['object', [ { 'name' => ['string', 'source name'],
- 'description' => ['string', 'description of source'],
- 'type' => ['string', 'type of source'],
- 'link' => ['string', 'link for source id'] },
- 'information about source' ]]]
+ $self->{hierarchy} = { taxonomy => [ ['species', 'taxonomy level'],
+ ['genus', 'taxonomy level'],
+ ['family', 'taxonomy level'],
+ ['order', ' taxonomy level'],
+ ['class', ' taxonomy level'],
+ ['phylum', 'taxonomy level'],
+ ['domain', 'top taxonomy level'] ],
+ ontology => [ ['function', 'bottom ontology level'],
+ ['level3', 'ontology level' ],
+ ['level2', 'ontology level' ],
+ ['level1', 'top ontology level'] ]
+ };
+ $self->{attributes} = { taxonomy => [ 'list', ['list', 'requested taxonomy levels, from highest to lowest'] ],
+ ontology => [ 'list', ['list', 'requested ontology levels, from highest to lowest'] ],
+ sources => [ 'hash', [['key', ['string', 'source name']],
+ ['value', ['object', [ { 'name' => ['string', 'source name'],
+ 'description' => ['string', 'description of source'],
+ 'type' => ['string', 'type of source'],
+ 'link' => ['string', 'link for source id'] },
+ 'information about source' ]]]
]]
};
return $self;
@@ -53,21 +62,34 @@ sub info {
'required' => {},
'body' => {} }
},
- { 'name' => "hierarchy",
- 'request' => $self->cgi->url."/".$self->name."/hierarchy",
+ { 'name' => "ontology",
+ 'request' => $self->cgi->url."/".$self->name."/ontology",
'description' => "",
'method' => "GET",
'type' => "synchronous",
- 'attributes' => $self->attributes->{hierarchy},
- 'parameters' => { 'options' => { 'source' => ['cv', ['NCBI', 'returns 7 level NCBI taxonomy'],
- ['COG', 'returns 3 level COG ontology'],
- ['NOG', 'returns 3 level NOG ontology'],
- ['KO', 'returns 4 level KEGG-KO ontology' ],
- ['Subsystems', 'returns 4 level SEED-Subsystems ontology' ]]
+ 'attributes' => $self->attributes->{ontology},
+ 'parameters' => { 'options' => { 'source' => ['cv', ['Subsystems', 'returns 4 level SEED-Subsystems ontology' ],
+ ['COG', 'returns 3 level COG ontology'],
+ ['NOG', 'returns 3 level NOG ontology'],
+ ['KO', 'returns 4 level KEGG-KO ontology' ]],
+ 'min_level' => ['cv', $self->{hierarchy}{ontology}],
+ 'parent_name' => ['string', 'name of ontology group to retrieve children of']
},
'required' => {},
'body' => {} }
},
+ { 'name' => "taxonomy",
+ 'request' => $self->cgi->url."/".$self->name."/taxonomy",
+ 'description' => "",
+ 'method' => "GET",
+ 'type' => "synchronous",
+ 'attributes' => $self->attributes->{taxonomy},
+ 'parameters' => { 'options' => { 'min_level' => ['cv', $self->{hierarchy}{taxonomy}],
+ 'parent_name' => ['string', 'name of taxanomy group to retrieve children of']
+ },
+ 'required' => {},
+ 'body' => {} }
+ },
{ 'name' => "sources",
'request' => $self->cgi->url."/".$self->name."/sources",
'description' => "",
@@ -88,7 +110,7 @@ sub request {
# determine sub-module to use
if (scalar(@{$self->rest}) == 0) {
$self->info();
- } elsif (($self->rest->[0] eq 'hierarchy') || ($self->rest->[0] eq 'sources')) {
+ } elsif (($self->rest->[0] eq 'taxonomy') || ($self->rest->[0] eq 'ontology') || ($self->rest->[0] eq 'sources')) {
$self->static($self->rest->[0]);
} else {
$self->info();
@@ -105,11 +127,38 @@ sub static {
unless (ref($mgdb)) {
$self->return_data({"ERROR" => "could not connect to analysis database"}, 500);
}
-
- my $data = {};
- if ($type eq 'hierarchy') {
- my $source = $self->cgi->param('source') || 'NCBI';
- $data = ($source eq 'NCBI') ? $mgdb->get_hierarchy('organism') : $mgdb->get_hierarchy('ontology', $source);
+ my $data = [];
+ my $pname = $self->cgi->param('parent_name') || '';
+
+ if ($type eq 'ontology') {
+ my @ont_hier = map { $_->[0] } @{$self->{hierarchy}{ontology}};
+ my $source = $self->cgi->param('source') || 'Subsystems';
+ my $min_lvl = $self->cgi->param('min_level') || 'function';
+ if ( grep(/^$min_lvl$/, @ont_hier) ) {
+ if ($min_lvl eq 'function') {
+ $min_lvl = ($source =~ /^[NC]OG$/) ? 'level3' : 'level4';
+ }
+ } else {
+ $self->return_data({"ERROR" => "invalid min_level for m5nr/ontology: ".$min_lvl." - valid types are [".join(", ", @ont_hier)."]"}, 500);
+ }
+ if ($pname && ($min_lvl ne 'level1')) {
+ @$data = $mgdb->get_hierarchy_slice('ontology', $source, $pname, $min_lvl);
+ } else {
+ @$data = values %{ $mgdb->get_hierarchy('ontology', $source, undef, undef, $min_lvl) };
+ }
+ } elsif ($type eq 'taxonomy') {
+ my @tax_hier = map { $_->[0] } @{$self->{hierarchy}{taxonomy}};
+ my $min_lvl = $self->cgi->param('min_level') || 'species';
+ if ( grep(/^$min_lvl$/, @tax_hier) ) {
+ $min_lvl = 'tax_'.$min_lvl;
+ } else {
+ $self->return_data({"ERROR" => "invalid min_level for m5nr/taxonomy: ".$min_lvl." - valid types are [".join(", ", @tax_hier)."]"}, 500);
+ }
+ if ($pname && ($min_lvl ne 'tax_domain')) {
+ @$data = $mgdb->get_hierarchy_slice('organism', undef, $pname, $min_lvl);
+ } else {
+ @$data = values %{ $mgdb->get_hierarchy('organism', undef, undef, undef, $min_lvl) };
+ }
} elsif ($type eq 'sources') {
$data = $mgdb->_sources();
delete $data->{GO};
View
16 src/MGRAST/lib/resources2/matrix.pm
@@ -30,10 +30,10 @@ sub new {
['class', 'organism taxanomic level'],
['phylum', 'organism taxanomic level'],
['domain', 'top organism taxanomic level'] ],
- ontology => [ ['function', 'bottom ontology level (function:default)'],
- ['level3', 'function type level (function)' ],
- ['level2', 'function type level (function)' ],
- ['level1', 'top function type level (function)'] ]
+ ontology => [ ['function', 'bottom function ontology level'],
+ ['level3', 'function ontology level' ],
+ ['level2', 'function ontology level' ],
+ ['level1', 'top function ontology level'] ]
};
$self->{attributes} = { "id" => [ 'string', 'unique object identifier' ],
"format" => [ 'string', 'format specification name' ],
@@ -341,9 +341,11 @@ sub prepare_data {
}
my $mddb = MGRAST::Metadata->new();
my $meta = $mddb->get_jobs_metadata_fast([keys %$col_ids], 1);
+ my $name = $mgdb->_name_map();
foreach my $cid (sort {$col_ids->{$a} <=> $col_ids->{$b}} keys %$col_ids) {
my $cmd = exists($meta->{$cid}) ? $meta->{$cid} : undef;
- push @$bcols, { id => 'mgm'.$cid, metadata => $cmd };
+ my $cnm = exists($name->{$cid}) ? $name->{$cid} : undef;
+ push @$bcols, { id => 'mgm'.$cid, name => $cnm, metadata => $cmd };
}
my $obj = { "id" => join(";", sort map { $_->{id} } @$bcols).'_'.$glvl.'_'.$source.'_'.$rtype,
@@ -367,9 +369,9 @@ sub prepare_data {
sub get_hierarchy {
my ($self, $mgdb, $type, $level, $src, $leaf_node) = @_;
if ($type eq 'organism') {
- return $leaf_node ? $self->{org2tax} : $mgdb->ach->get_taxonomy4level_full($level, 1);
+ return $leaf_node ? $self->{org2tax} : $mgdb->get_hierarchy('organism', undef, undef, undef, $level);
} elsif ($type eq 'function') {
- return $leaf_node ? $mgdb->ach->get_all_ontology4source_hash($src) : $mgdb->ach->get_level4ontology_full($src, $level, 1);
+ return $leaf_node ? $mgdb->get_hierarchy('ontology', $src) : $mgdb->get_hierarchy('ontology', $src, undef, undef, $level);
} else {
return {};
}
Please sign in to comment.
Something went wrong with that request. Please try again.