Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions src/MGRAST/lib/resources/matrix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ sub new {
$self->{org2tid} = {};
$self->{max_mgs} = 100;
$self->{cutoffs} = { evalue => '5', identity => '60', length => '15' };
$self->{attributes} = { "id" => [ 'string', 'unique object identifier' ],
"url" => [ 'uri', 'resource location of this object instance' ],
"format" => [ 'string', 'format specification name' ],
"format_url" => [ 'string', 'url to the format specification' ],
"type" => [ 'string', 'type of the data in the return table (taxon, function or gene)' ],
"generated_by" => [ 'string', 'identifier of the data generator' ],
"date" => [ 'date', 'time the output data was generated' ],
"matrix_type" => [ 'string', 'type of the data encoding matrix (dense or sparse)' ],
"matrix_element_type" => [ 'string', 'data type of the elements in the return matrix' ],
"matrix_element_value" => [ 'string', 'result_type of the elements in the return matrix' ],
"shape" => [ 'list', ['integer', 'list of the dimension sizes of the return matrix'] ],
"rows" => [ 'list', ['object', [{'id' => ['string', 'unique annotation text'],
'metadata' => ['hash', 'key value pairs describing metadata']}, "rows object"]] ],
"columns" => [ 'list', ['object', [{'id' => ['string', 'unique metagenome identifier'],
'metadata' => ['hash', 'key value pairs describing metadata']}, "columns object"]] ],
"data" => [ 'list', ['list', ['float', 'the matrix values']] ]
};
$self->{sources} = { organism => [ $self->source->{m5nr}, $self->source->{m5rna} ],
ontology => $self->source->{ontology}
};
$self->{attributes} = {
"id" => [ 'string', 'unique object identifier' ],
"url" => [ 'uri', 'resource location of this object instance' ],
"format" => [ 'string', 'format specification name' ],
"format_url" => [ 'string', 'url to the format specification' ],
"type" => [ 'string', 'type of the data in the return table (taxon, function or gene)' ],
"generated_by" => [ 'string', 'identifier of the data generator' ],
"date" => [ 'date', 'time the output data was generated' ],
"matrix_type" => [ 'string', 'type of the data encoding matrix (dense or sparse)' ],
"matrix_element_type" => [ 'string', 'data type of the elements in the return matrix' ],
"matrix_element_value" => [ 'string', 'result_type of the elements in the return matrix' ],
"shape" => [ 'list', ['integer', 'list of the dimension sizes of the return matrix'] ],
"rows" => [ 'list', ['object', [{'id' => ['string', 'unique annotation text'],
'metadata' => ['hash', 'key value pairs describing metadata']}, "rows object"]]
],
"columns" => [ 'list', ['object', [{'id' => ['string', 'unique metagenome identifier'],
'metadata' => ['hash', 'key value pairs describing metadata']}, "columns object"]]
],
"data" => [ 'list', ['list', ['float', 'the matrix values']] ]
};
$self->{sources} = {
organism => [ $self->source->{m5nr}, $self->source->{m5rna} ],
ontology => $self->source->{ontology}
};
map { push @{$self->{sources}{organism}}, $_ } @{$self->source->{protein}};
map { push @{$self->{sources}{organism}}, $_ } @{$self->source->{rna}};
return $self;
Expand Down
159 changes: 101 additions & 58 deletions src/MGRAST/lib/resources/profile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,82 @@ sub new {
$self->{name} = "profile";
$self->{rights} = \%rights;
$self->{cutoffs} = { evalue => '5', identity => '60', length => '15' };
$self->{sources} = [ $self->source->{m5nr}[0], @{$self->source->{protein}}, $self->source->{m5rna}[0], @{$self->source->{rna}}, @{$self->source->{ontology}} ];
$self->{attributes} = { "id" => [ 'string', 'unique object identifier' ],
"format" => [ 'string', 'format specification name' ],
"format_url" => [ 'string', 'url to the format specification' ],
"type" => [ 'string', 'type of the data in the return table (taxon, function or gene)' ],
"generated_by" => [ 'string', 'identifier of the data generator' ],
"date" => [ 'date', 'time the output data was generated' ],
"matrix_type" => [ 'string', 'type of the data encoding matrix (dense or sparse)' ],
"matrix_element_type" => [ 'string', 'data type of the elements in the return matrix' ],
"shape" => [ 'list', [ 'integer', 'list of the dimension sizes of the return matrix' ] ],
"rows" => [ 'list', [ 'object', [ { 'id' => [ 'string', 'unique identifier' ],
'metadata' => [ 'hash', 'key value pairs describing metadata' ] }, "rows object" ] ] ],
"columns" => [ 'list', [ 'object', [ { 'id' => [ 'string', 'unique identifier' ],
'metadata' => [ 'hash', 'list of metadata, contains the metagenome' ] }, "columns object" ] ] ],
"data" => [ 'list', [ 'list', [ 'float', 'the matrix values' ] ] ]
};
$self->{sources} = [
$self->source->{m5nr}[0],
@{$self->source->{protein}},
$self->source->{m5rna}[0],
@{$self->source->{rna}},
@{$self->source->{ontology}}
];
$self->{attributes} = {
"id" => [ 'string', 'unique object identifier' ],
"format" => [ 'string', 'format specification name' ],
"format_url" => [ 'string', 'url to the format specification' ],
"type" => [ 'string', 'type of the data in the return table (taxon, function or gene)' ],
"generated_by" => [ 'string', 'identifier of the data generator' ],
"date" => [ 'date', 'time the output data was generated' ],
"matrix_type" => [ 'string', 'type of the data encoding matrix (dense or sparse)' ],
"matrix_element_type" => [ 'string', 'data type of the elements in the return matrix' ],
"shape" => [ 'list', [ 'integer', 'list of the dimension sizes of the return matrix' ] ],
"rows" => [ 'list', [ 'object', [ { 'id' => [ 'string', 'unique identifier' ],
'metadata' => [ 'hash', 'key value pairs describing metadata' ] }, "rows object" ] ]
],
"columns" => [ 'list', [ 'object', [ { 'id' => [ 'string', 'unique identifier' ],
'metadata' => [ 'hash', 'list of metadata, contains the metagenome' ] }, "columns object" ] ]
],
"data" => [ 'list', [ 'list', [ 'float', 'the matrix values' ] ] ]
};
return $self;
}

# resource is called without any parameters
# this method must return a description of the resource
sub info {
my ($self) = @_;
my $content = { 'name' => $self->name,
'url' => $self->cgi->url."/".$self->name,
'description' => "A profile in biom format that contains abundance and similarity values",
'type' => 'object',
'documentation' => $self->cgi->url.'/api.html#'.$self->name,
'requests' => [ { 'name' => "info",
'request' => $self->cgi->url."/".$self->name,
'description' => "Returns description of parameters and attributes.",
'method' => "GET" ,
'type' => "synchronous" ,
'attributes' => "self",
'parameters' => { 'options' => {},
'required' => {},
'body' => {} } },
{ 'name' => "instance",
'request' => $self->cgi->url."/".$self->name."/{ID}",
'description' => "Returns a single data object in BIOM format",
'method' => "GET" ,
'type' => "synchronous" ,
'attributes' => $self->attributes,
'parameters' => { 'options' => {
'evalue' => ['int', 'negative exponent value for maximum e-value cutoff: default is '.$self->{cutoffs}{evalue}],
'identity' => ['int', 'percent value for minimum % identity cutoff: default is '.$self->{cutoffs}{identity}],
'length' => ['int', 'value for minimum alignment length cutoff: default is '.$self->{cutoffs}{length}],
'type' => [ 'cv', [ ['organism', 'return organism data'],
['function', 'return functional data'],
['feature', 'return feature data'] ] ],
'source' => [ 'cv', $self->{sources} ]
},
'required' => { "id" => [ "string", "unique object identifier" ] },
'body' => {} } }
]
};
$self->return_data($content);
my ($self) = @_;
my $content = {
'name' => $self->name,
'url' => $self->cgi->url."/".$self->name,
'description' => "A profile in biom format that contains abundance and similarity values",
'type' => 'object',
'documentation' => $self->cgi->url.'/api.html#'.$self->name,
'requests' => [
{ 'name' => "info",
'request' => $self->cgi->url."/".$self->name,
'description' => "Returns description of parameters and attributes.",
'method' => "GET" ,
'type' => "synchronous" ,
'attributes' => "self",
'parameters' => {
'options' => {},
'required' => {},
'body' => {} }
},
{ 'name' => "instance",
'description' => "Returns a single data object in BIOM format",
'method' => "GET" ,
'type' => "synchronous" ,
'attributes' => $self->attributes,
'parameters' => {
'options' => {
'evalue' => ['int', 'negative exponent value for maximum e-value cutoff: default is '.$self->{cutoffs}{evalue}],
'identity' => ['int', 'percent value for minimum % identity cutoff: default is '.$self->{cutoffs}{identity}],
'length' => ['int', 'value for minimum alignment length cutoff: default is '.$self->{cutoffs}{length}],
'source' => ['cv', $self->{sources}],
'type' => ['cv', [ ['organism', 'return organism data'],
['function', 'return functional data'],
['feature', 'return feature data'] ]
],
'hit_type' => ['cv', [ ['all', 'returns results based on all organisms that map to top hit per read-feature'],
['single', 'returns results based on a single organism for top hit per read-feature'],
['lca', 'returns results based on the Least Common Ancestor for all organisms (M5NR+M5RNA only) that map to hits from a read-feature']]
]
},
'required' => { "id" => ["string", "unique object identifier"] },
'body' => {} }
}
]
};
$self->return_data($content);
}

# the resource is called with an id parameter
Expand Down Expand Up @@ -124,6 +143,7 @@ sub prepare_data {
my $params = {};
my $cgi = $self->cgi;
$params->{type} = $cgi->param('type') ? $cgi->param('type') : 'organism';
$params->{hit_type} = $cgi->param('hit_type') ? $cgi->param('hit_type') : 'all';
$params->{source} = $cgi->param('source') ? $cgi->param('source') : (($params->{type} eq 'organism') ? 'M5NR' : (($params->{type} eq 'function') ? 'Subsystems': 'RefSeq'));
$params->{evalue} = defined($cgi->param('evalue')) ? $cgi->param('evalue') : $self->{cutoffs}{evalue};
$params->{identity} = defined($cgi->param('identity')) ? $cgi->param('identity') : $self->{cutoffs}{identity};
Expand Down Expand Up @@ -180,20 +200,43 @@ sub prepare_data {
# get data
if ($params->{type} eq 'organism') {
$ttype = 'Taxon';
# my ($self, $sources, $eval, $ident, $alen, $with_taxid) = @_;
my ($md5_abund, $result) = $mgdb->get_organisms_for_sources([$params->{source}], int($params->{evalue}), int($params->{identity}), int($params->{length}), 1);
# mgid, source, tax_domain, tax_phylum, tax_class, tax_order, tax_family, tax_genus, tax_species, name, abundance, sub_abundance, exp_avg, exp_stdv, ident_avg, ident_stdv, len_avg, len_stdv, md5s, taxid
my $result = [];
if ($params->{hit_type} eq 'all') {
# my ($self, $sources, $eval, $ident, $alen, $with_taxid) = @_;
(undef, $result) = $mgdb->get_organisms_for_sources([$params->{source}], int($params->{evalue}), int($params->{identity}), int($params->{length}), 1);
# mgid, source, tax_domain, tax_phylum, tax_class, tax_order, tax_family, tax_genus, tax_species, name, abundance, sub_abundance, exp_avg, exp_stdv, ident_avg, ident_stdv, len_avg, len_stdv, md5s, taxid
} elsif ($params->{hit_type} eq 'single') {
# my ($self, $source, $eval, $ident, $alen, $with_taxid) = @_;
$result = $mgdb->get_organisms_unique_for_source($params->{source}, int($params->{evalue}), int($params->{identity}), int($params->{length}), 1);
# mgid, tax_domain, tax_phylum, tax_class, tax_order, tax_family, tax_genus, tax_species, name, abundance, exp_avg, exp_stdv, ident_avg, ident_stdv, len_avg, len_stdv, md5s, taxid
} elsif ($params->{hit_type} eq 'lca') {
# my ($self, $eval, $ident, $alen) = @_;
$result = $mgdb->get_lca_data(int($params->{evalue}), int($params->{identity}), int($params->{length}));
# mgid, tax_domain, tax_phylum, tax_class, tax_order, tax_family, tax_genus, tax_species, name, abundance, exp_avg, exp_stdv, ident_avg, ident_stdv, len_avg, len_stdv
}
foreach my $row (@$result) {
my $tax_str = [ $row->[2], $row->[3], $row->[4], $row->[5], $row->[6], $row->[7], $row->[9] ];
push(@$rows, { "id" => $row->[19], "metadata" => { "taxonomy" => $tax_str } });
push(@$values, [ $self->toFloat($row->[10]), $self->toFloat($row->[12]), $self->toFloat($row->[14]), $self->toFloat($row->[16]) ]);
if ($params->{hit_type} eq 'all') {
my $rmd = {
taxonomy => [$row->[2], $row->[3], $row->[4], $row->[5], $row->[6], $row->[7], $row->[8]],
tax_id => $row->[19]
};
push(@$rows, { "id" => $row->[9], "metadata" => $rmd });
push(@$values, [ $self->toFloat($row->[10]), $self->toFloat($row->[12]), $self->toFloat($row->[14]), $self->toFloat($row->[16]) ]);
} else {
my $rmd = { taxonomy => [$row->[1], $row->[2], $row->[3], $row->[4], $row->[5], $row->[6], $row->[7]] };
if ($params->{hit_type} eq 'single') {
$rmd->{tax_id} = $row->[17];
}
push(@$rows, { "id" => $row->[8], "metadata" => $rmd });
push(@$values, [ $self->toFloat($row->[9]), $self->toFloat($row->[10]), $self->toFloat($row->[12]), $self->toFloat($row->[14]) ]);
}
}
}
elsif ($params->{type} eq 'function') {
$ttype = 'Function';
my $function2ont = $mgdb->get_hierarchy('ontology', $params->{source});
# my ($self, $source, $eval, $ident, $alen) = @_;
my ($md5_abund, $result) = $mgdb->get_ontology_for_source($params->{source}, int($params->{evalue}), int($params->{identity}), int($params->{length}));
my (undef, $result) = $mgdb->get_ontology_for_source($params->{source}, int($params->{evalue}), int($params->{identity}), int($params->{length}));
# mgid, id, annotation, abundance, sub_abundance, exp_avg, exp_stdv, ident_avg, ident_stdv, len_avg, len_stdv, md5s
foreach my $row (@$result) {
next unless (exists $function2ont->{$row->[1]});
Expand Down
34 changes: 24 additions & 10 deletions src/MGRAST/lib/resources/resource.pm
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,22 @@ sub format {

# get cgi header
sub header {
my ($self, $status) = @_;
return $self->cgi->header( -type => $self->format,
-status => $status,
-Access_Control_Allow_Origin => '*' );
my ($self, $status, $text) = @_;
unless ($status) {
$status = 200;
}
my $header = $self->cgi->header(
-type => $self->format,
-status => $status,
-Access_Control_Allow_Origin => '*'
);
{
use bytes;
if ($text) {
$header->set('Content-Length' => length($text));
}
}
return $header
}

# method initially called from the api module
Expand Down Expand Up @@ -381,7 +393,7 @@ sub return_cached {
my $cached = $self->memd->get($self->url_id);
if ($cached) {
# do a runaround on ->return_data
print $self->header;
print $self->header(200, $cached);
print $cached;
exit 0;
}
Expand Down Expand Up @@ -438,8 +450,9 @@ sub return_data {
$self->memd->set($self->url_id, $self->json->encode($data), $self->{expire});
}
}
print $self->header($status);
print $self->json->encode($data);
my $data_text = $self->json->encode($data);
print $self->header($status, $data_text);
print $data_text;
exit 0;
}
else {
Expand All @@ -449,8 +462,9 @@ sub return_data {
$data = { 'data' => $data };
}
$self->format("application/json");
print $self->header($status);
print $self->cgi->param('callback')."(".$self->json->encode($data).");";
my $data_text = $self->cgi->param('callback')."(".$self->json->encode($data).");"
print $self->header($status, $data_text);
print $data_text;
exit 0;
}
# normal return
Expand All @@ -463,7 +477,7 @@ sub return_data {
$self->memd->set($self->url_id, $data, $self->{expire});
}
# send it
print $self->header($status);
print $self->header($status, $data);
print $data;
exit 0;
}
Expand Down