Permalink
Browse files

changed m5nr resource call for returning children of parent from hier…

…archy
  • Loading branch information...
Travis Harrison
Travis Harrison committed Jan 3, 2013
1 parent 874f9b6 commit 1773a1ad2111b56d5f44c7653690e3155c0b5bdb
Showing with 25 additions and 20 deletions.
  1. +18 −9 src/MGRAST/lib/Analysis.pm
  2. +7 −11 src/MGRAST/lib/resources2/m5nr.pm
View
@@ -564,22 +564,31 @@ sub get_hierarchy {
}
sub get_hierarchy_slice {
- my ($self, $type, $src, $pname, $plevel) = @_;
+ 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 && $pname && $plevel && grep(/^$plevel$/, @$col)) {
+ unless ($tbl && @$col && $parent_name && $child_level && grep(/^$child_level$/, @$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})) {
+ my $data = [];
+ my $child_index = first { $col->[$_] eq $child_level } 0..$#{$col};
+ # level does not exist
+ unless ($child_index) {
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};
+ # 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 : [];
@@ -73,7 +73,6 @@ sub info {
['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' => {},
@@ -86,7 +85,6 @@ sub info {
'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' => {},
@@ -131,7 +129,6 @@ sub static {
}
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}};
@@ -144,24 +141,23 @@ sub static {
} 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) };
+ if ($pname && ($min_lvl ne 'level1')) {
+ @$data = $mgdb->get_hierarchy_slice('ontology', $source, $pname, $min_lvl);
} else {
- @$data = $mgdb->get_hierarchy_slice('ontology', $source, $pname, $plvl);
+ @$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';
+ 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) };
+ if ($pname && ($min_lvl ne 'tax_domain')) {
+ @$data = $mgdb->get_hierarchy_slice('organism', undef, $pname, $min_lvl);
} else {
- $plvl = 'tax_'.$plvl;
- @$data = $mgdb->get_hierarchy_slice('organism', undef, $pname, $plvl);
+ @$data = values %{ $mgdb->get_hierarchy('organism', undef, undef, undef, $min_lvl) };
}
} elsif ($type eq 'sources') {
$data = $mgdb->_sources();

0 comments on commit 1773a1a

Please sign in to comment.