Skip to content

Commit

Permalink
Merge pull request #32 from cmbi/post-get_metadomain_annotation
Browse files Browse the repository at this point in the history
use a post request to send the requested domain list, rather than a get request
  • Loading branch information
laurensvdwiel authored Sep 10, 2018
2 parents b782c2c + 1a419da commit a011b2b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 73 deletions.
51 changes: 23 additions & 28 deletions metadome/presentation/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from metadome.domain.repositories import GeneRepository
from metadome.domain.services.metadome import process_visualization_request

from flask import Blueprint, jsonify, render_template
from flask import Blueprint, jsonify, render_template, request
from builtins import Exception

import traceback
Expand Down Expand Up @@ -43,35 +43,30 @@ def get_transcript_ids_for_gene(gene_name):

return jsonify(trancript_ids=transcript_results, message=message)

@bp.route('/get_metadomain_annotation', methods=['GET'])
def get_metadomain_annotation_stub():
"""This endpoint is a stub, to ensure deeper endpoints may be used"""
pass
@bp.route('/get_metadomain_annotation/', methods=['POST'])
def get_metadomain_annotation():
data = request.get_json()

_log.debug("data is {}".format(data))

if not 'transcript_id' in data:
return jsonify({"error: no transcript id"}), 400
elif not 'protein_position' in data:
return jsonify({"error: no protein position"}), 400
elif not 'requested_domains' in data:
return jsonify({"error: no requested domains"}), 400

transcript_id = data['transcript_id']
protein_pos = data['protein_position']
requested_domains = data['requested_domains']

_log.debug("get_metadomain_annotation with transcript: {}, protein position: {}, requested_domains: {}"
.format(transcript_id, protein_pos, requested_domains))

@bp.route('/get_metadomain_annotation/<string:transcript_id>/<int:protein_pos>/<string:domain_request>')
def get_metadomain_annotation(transcript_id, protein_pos, domain_request):
_log.debug('get_metadomain_annotation with: transcript_id: '+str(transcript_id)+', protein_pos: '+str(protein_pos)+', domain_request: '+str(domain_request))

# Try to tokenize the domain request
try:
tokens = domain_request.split(':')

domain_request_as_json = "{"
for token in tokens:
if token.startswith('PF'):
domain_request_as_json+= '"'+token+'":'
else:
domain_request_as_json+=token
domain_request_as_json+="}"
_log.debug(domain_request_as_json)
domain_positions = json.loads(domain_request_as_json)
except Exception as e:
return jsonify({"error:"+str(e)})

# attempt to retrieve the response for a metadomain position
from metadome.tasks import retrieve_metadomain_annotation as rma
response = rma(transcript_id, protein_pos, domain_positions)
response = rma(transcript_id, protein_pos, requested_domains)

return jsonify(response)

@bp.route('/submit_gene_analysis', methods=['GET'])
Expand Down Expand Up @@ -139,4 +134,4 @@ def conditional_jsonify(_value, _jsonify=True):
"""Conditianally jsonifies a given value"""
if _jsonify:
return jsonify(_value)
return _value
return _value
96 changes: 51 additions & 45 deletions metadome/presentation/web/templates/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,50 +552,56 @@ function loadDoc() {
}

// annotates meta domain information for a position
function createPositionalInformation(domain_metadomain_coverage, transcript_id, position_json){
// Retrieve the needed information for the GET request
protein_position = position_json.values[0].protein_pos;

// Construct the request for this domain and the aligned positions
domain_request = "";
domain_ids = Object.keys(position_json.values[0].domains);
for (i = 0; i < domain_ids.length; i++){
domain_id = domain_ids[i];

// Check if this position is meta domain suitable
if (!(position_json.values[0].domains[domain_id] == null)){
domain_request += domain_id + ":[";

// append the consensus positions
for (j = 0; j < position_json.values[0].domains[domain_id].consensus_pos.length; j ++) {
domain_request += position_json.values[0].domains[domain_id].consensus_pos[j];
if (j+1 < position_json.values[0].domains[domain_id].consensus_pos.length) {
domain_request += ",";
}
}
domain_request += "]";
if (i+1 < domain_ids.length) {
domain_request += ",";
}
}
}

if (domain_request != ""){
// Activate the loading overlay
$("#loading_overlay").addClass('is-active');

// Execute the GET request
$.get("{{ url_for('api.get_metadomain_annotation_stub') }}" + "/" + transcript_id + "/" + protein_position + "/" + domain_request, function(data){
$("#loading_overlay").removeClass('is-active');
FillPositionalInformation(domain_metadomain_coverage, position_json, data);
$("#positional_information_overlay").addClass('is-active');
});
}
else{
// No domain request, so we can fill in the information without performing a GET request
FillPositionalInformation(domain_metadomain_coverage, position_json, {});
$("#positional_information_overlay").addClass('is-active');
}
function createPositionalInformation(domain_metadomain_coverage, transcript_id, position_json) {
// Retrieve the needed information for the GET request
protein_position = position_json.values[0].protein_pos;

// Construct the request for this domain and the aligned positions
var requested_domains = {};
domain_ids = Object.keys(position_json.values[0].domains);
for (i = 0; i < domain_ids.length; i++){
domain_id = domain_ids[i];

// Check if this position is meta domain suitable
if (!(position_json.values[0].domains[domain_id] == null)){
requested_domains[domain_id] = [];

// append the consensus positions
for (j = 0; j < position_json.values[0].domains[domain_id].consensus_pos.length; j ++) {
requested_domains[domain_id].push(position_json.values[0].domains[domain_id].consensus_pos[j])
}
}
}

if (Object.keys(requested_domains).length > 0) {
// Activate the loading overlay
$("#loading_overlay").addClass('is-active');

var input = {"requested_domains": requested_domains,
"transcript_id": transcript_id,
"protein_position": protein_position};

// Execute the GET request
$.ajax(
{
type: 'POST',
url: "{{ url_for('api.get_metadomain_annotation') }}",
data: JSON.stringify(input),
success:function(data) {
$("#loading_overlay").removeClass('is-active');
FillPositionalInformation(domain_metadomain_coverage, position_json, data);
$("#positional_information_overlay").addClass('is-active');
},
contentType: "application/json",
dataType: 'json'
}
);
}
else {
// No domains requested, so we can fill in the information without performing a GET request
FillPositionalInformation(domain_metadomain_coverage, position_json, {});
$("#positional_information_overlay").addClass('is-active');
}
}

//Adds positional information for a selected position
Expand Down Expand Up @@ -861,4 +867,4 @@ function sortTable(){
$.each(rows, function(index, row) {
$('#position_information_tbody').append(row);
});
}
}
56 changes: 56 additions & 0 deletions tests/unit/presentation/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import logging

from nose.tools import with_setup, eq_
from mock import patch
from flask import url_for

from metadome.factory import create_app


_log = logging.getLogger(__name__)


@patch("metadome.database.db")
def setup(mock_db):
global server
global client

class FakeDB:
def init_app(self, app):
pass

def create_all(self):
pass
mock_db.return_value = FakeDB()

server = create_app({'SERVER_NAME': "test-server",
'TESTING': True,
'SECRET_KEY': 'testing'})
client = server.test_client()


def teardown():
pass


@with_setup(setup, teardown)
@patch("metadome.tasks.retrieve_metadomain_annotation")
def test_get_metadomain_annotation(mock_retrieve):
global server
global client

mock_retrieve.return_value = {}

input_ = {
'transcript_id': 'test',
'protein_position': 1,
'requested_domains': {}
}

with server.app_context():
r = client.post(url_for('api.get_metadomain_annotation'),
data=json.dumps(input_),
content_type="application/json")

eq_(r.status_code, 200)

0 comments on commit a011b2b

Please sign in to comment.