Permalink
Browse files

m5nr api: added more options to retrieve taxonomy/ontology slices

  • Loading branch information...
1 parent 5752edb commit 874f9b6efafb39a54dc6567d639d33fbb937f7a8 Travis Harrison committed Dec 21, 2012
Showing with 100 additions and 25 deletions.
  1. +22 −0 src/MGRAST/lib/Analysis.pm
  2. +78 −25 src/MGRAST/lib/resources2/m5nr.pm
View
@@ -563,6 +563,28 @@ sub get_hierarchy {
# { end_node => [ hierachy of node ] }
}
+sub get_hierarchy_slice {
+ my ($self, $type, $src, $pname, $plevel) = @_;
+
+ my $tbl = exists($self->_atbl->{$type}) ? $self->_atbl->{$type} : '';
+ my $col = $self->_get_table_cols($tbl);
+ unless ($tbl && @$col && $pname && $plevel && grep(/^$plevel$/, @$col)) {
+ return [];
+ }
+ my $data = [];
+ my $index = first { $col->[$_] eq $plevel } 0..$#{$col};
+ if ($plevel eq 'tax_domain') { $index += 1; } # ncbi hack
+ if ((! $index) || ($index == $#{$col})) {
+ return [];
+ }
+ my $sql = "SELECT DISTINCT ".$col->[$index+1]." FROM ".$self->_atbl->{$type}." WHERE ".$plevel." = ".$self->_dbh->quote($pname);
+ if (($type eq 'ontology') && $src) {
+ $sql .= " AND source = ".$self->_src_id->{$src};
+ }
+ my $cols = $self->_dbh->selectcol_arrayref($sql);
+ return ($cols && @$cols) ? $cols : [];
+}
+
sub _get_annotation_map {
my ($self, $type, $anns, $src) = @_;
@@ -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,36 @@ 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_level' => ['string', 'level of ontology group to retrieve children of'],
+ '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_level' => ['string', 'level of taxanomy group to retrieve children of'],
+ '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 +112,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 +129,40 @@ 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') || '';
+ my $plvl = $self->cgi->param('parent_level') || '';
+
+ 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);
+ }
+ unless ($pname && $plvl && grep(/^$plvl$/, @ont_hier)) {
+ @$data = values %{ $mgdb->get_hierarchy('ontology', $source, undef, undef, $min_lvl) };
+ } else {
+ @$data = $mgdb->get_hierarchy_slice('ontology', $source, $pname, $plvl);
+ }
+ } 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);
+ }
+ unless ($pname && $plvl && grep(/^$plvl$/, @tax_hier)) {
+ @$data = values %{ $mgdb->get_hierarchy('organism', undef, undef, undef, $min_lvl) };
+ } else {
+ $plvl = 'tax_'.$plvl;
+ @$data = $mgdb->get_hierarchy_slice('organism', undef, $pname, $plvl);
+ }
} elsif ($type eq 'sources') {
$data = $mgdb->_sources();
delete $data->{GO};

0 comments on commit 874f9b6

Please sign in to comment.