Skip to content

Commit

Permalink
version 0.1.0 finalizada
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri101-Ar committed Jun 22, 2022
1 parent c98cd0c commit 9861aee
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ docker-compose.yml
bio-api/secretkey.txt
*.log
databases/hgnc/hgnc_output.json
databases/hgnc/hgnc_dataset.tsv
databases/hgnc/hgnc_dataset.tsv
databases/cpdb/
32 changes: 13 additions & 19 deletions bio-api/bioapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from flask import request
import urllib.parse
import pymongo
from pymongo.database import Database
import re
import os
import configparser
import logging
from pymongo.database import Database


# Gets production flag
IS_DEBUG: bool = os.environ.get('DEBUG', 'true') == 'true'
Expand Down Expand Up @@ -60,7 +61,7 @@ def get_mongo_connection() -> Database:
mongo_client = pymongo.MongoClient(f"mongodb://{user}:{password}@{host}:{port}/?authSource=admin")
return mongo_client[db]
except Exception as e:
logging.error("BioAPI no se pudo conectar a la base de datos MongoDB configurada." + str(e), exc_info=True)
logging.error("Database connection error." + str(e), exc_info=True)
exit(-1)


Expand Down Expand Up @@ -151,8 +152,14 @@ def genSymbol(gene_id):
respuesta = {gene_id: []}
try:
gv = mapear_gen(gene_id)
if len(gv) == 0:
abort(404, "invalid gene identifier")
respuesta[gene_id] = gv
except Exception as e:
except TypeError as e:
abort(400, e)
except ValueError as e:
abort(400, e)
except KeyError as e:
abort(400, e)
return make_response(respuesta, 200, headers)

Expand All @@ -178,10 +185,9 @@ def genSymbols():
def genes_of_the_same_family(gene_id):
respuesta = {"gene_id": None, "groups": [], "locus_group": None, "locus_type": None}
try:

mapped_gene = mapear_gen(gene_id)
if len(mapped_gene) == 0:
abort(400, "invalid gene identifier")
abort(404, "invalid gene identifier")
elif len(mapped_gene) >= 2:
abort(400, "ambiguous gene identifier. The identifier may refer to more than one HGNC-approved gene (" + ",".join(mapped_gene) + ")")
approved_symbol = mapped_gene[0]
Expand All @@ -208,25 +214,13 @@ def genes_of_the_same_family(gene_id):

# Manejo de errores
@flask_app.errorhandler(400)
def resource_not_found(e):
def bad_request(e):
return jsonify(error=str(e)), 400

@flask_app.errorhandler(405)
def resource_not_found(e):
return jsonify(error=str(e)), 405

@flask_app.errorhandler(404)
def resource_not_found(e):
def not_found(e):
return jsonify(error=str(e)), 404

@flask_app.errorhandler(409)
def resource_not_found(e):
return jsonify(error=str(e)), 409

@flask_app.errorhandler(500)
def resource_not_found(e):
return jsonify(error=str(e)), 500

return flask_app


Expand Down
9 changes: 2 additions & 7 deletions bio-api/tests/test_gene_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def test_gene_id_valid(client):
response = client.get(f'{URL_BASE}/{valid_id}')
res = json.loads(response.data)
assert response.status_code == 200
assert valid_id in list(res.keys())
assert type(res[valid_id]) == list
assert "TP53" in res[valid_id]

Expand All @@ -18,7 +17,6 @@ def test_gene_alias_two_genes(client):
response = client.get(f'{URL_BASE}/{alias_id}')
res = json.loads(response.data)
assert response.status_code == 200
assert alias_id in list(res.keys())
assert type(res[alias_id]) == list
assert "BRCA1" in res[alias_id]
assert "ICE2" in res[alias_id]
Expand All @@ -29,15 +27,12 @@ def test_gene_id_invalid(client):
invalid_id="XXX" #Invalid id
response = client.get(f'{URL_BASE}/{invalid_id}')
res = json.loads(response.data)
assert response.status_code == 200
assert invalid_id in list(res.keys())
assert type(res[invalid_id]) == list
assert res[invalid_id] == []
assert response.status_code == 404
assert "error" in list(res.keys())

def test_missing_gene_id_param(client):
"""Tests missing mandatory parameter"""
response = client.get(f'{URL_BASE}/')
res = json.loads(response.data)
assert response.status_code == 404
assert "error" in list(res.keys())
assert type(res["error"]) == str
27 changes: 4 additions & 23 deletions bio-api/tests/test_genes_same_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@ def test_gene_in_group(client):
response = client.get(f'{URL_BASE}/{valid_id}')
res = json.loads(response.data)
assert response.status_code == 200
assert "gene_id" in list(res.keys())
assert "groups" in list(res.keys())
assert "locus_group" in list(res.keys())
assert "locus_type" in list(res.keys())
assert type(res["gene_id"]) == str
assert type(res["groups"]) == list
assert type(res["locus_group"]) == str
assert type(res["locus_type"]) == str
assert res["gene_id"] == "EGFR"
assert res["locus_group"] == "protein-coding gene"
assert res["locus_type"] == "gene with protein product"
assert len(res["groups"]) == 1
assert res["groups"][0]["gene_group"] == "Erb-b2 receptor tyrosine kinases"
assert res["groups"][0]["gene_group_id"] == "1096"
assert sorted(res["groups"][0]["genes"]) == sorted(["ERBB3","EGFR","ERBB2","ERBB4"])
assert res["gene_id"] == "EGFR"
assert res["locus_group"] == "protein-coding gene"
assert res["locus_type"] == "gene with protein product"


def test_gene_not_in_group(client):
Expand All @@ -31,19 +24,12 @@ def test_gene_not_in_group(client):
response = client.get(f'{URL_BASE}/{valid_id}')
res = json.loads(response.data)
assert response.status_code == 200
assert "gene_id" in list(res.keys())
assert "groups" in list(res.keys())
assert "locus_group" in list(res.keys())
assert "locus_type" in list(res.keys())
assert type(res["gene_id"]) == str
assert type(res["groups"]) == list
assert type(res["locus_group"]) == str
assert type(res["locus_type"]) == str
assert res["gene_id"] == "TP53"
assert res["locus_group"] == "protein-coding gene"
assert res["locus_type"] == "gene with protein product"
assert len(res["groups"]) == 0


def test_gene_alias(client):
"""test with gene symbol that is alias of two valid symbols"""
Expand All @@ -52,7 +38,6 @@ def test_gene_alias(client):
res = json.loads(response.data)
assert response.status_code == 400
assert "error" in list(res.keys())
assert type(res["error"]) == str
assert res["error"] == "400 Bad Request: ambiguous gene identifier. The identifier may refer to more than one HGNC-approved gene (BRCA1,ICE2)"


Expand All @@ -62,8 +47,6 @@ def test_invalid_gene_symbol(client):
response = client.get(f'{URL_BASE}/{invalid_id}')
res = json.loads(response.data)
assert response.status_code == 400
assert "error" in list(res.keys())
assert type(res["error"]) == str
assert res["error"] =="400 Bad Request: invalid gene identifier"


Expand All @@ -72,6 +55,4 @@ def test_invalid_gene_symbol(client):
response = client.get(f'{URL_BASE}')
res = json.loads(response.data)
assert response.status_code == 404
assert "error" in list(res.keys())
assert type(res["error"]) == str
assert res["error"] == "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."
15 changes: 2 additions & 13 deletions bio-api/tests/test_genes_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ def test_gene_id_valid(client):
}
response = client.post(f'{URL_BASE}', data=json.dumps(data), headers=headers)
res = json.loads(response.data)
assert response.content_type == 'application/json'
assert response.status_code == 200
assert valid_id in list(res.keys())
assert type(res[valid_id]) == list
assert "BRAF" in res[valid_id]

def test_valid_and_invalid_genes(client):
""""test for one valid and one invalid gene"""
"""test for one valid and one invalid gene"""
valid_id="HGNC:3236" #hgnc_id
invalid_id="blcdtm" #invalid id
data = {
"genes_ids" : [ valid_id, invalid_id ]
}
response = client.post(f'{URL_BASE}', data=json.dumps(data), headers=headers)
res = json.loads(response.data)
assert response.content_type == 'application/json'
assert response.status_code == 200
assert valid_id in list(res.keys())
assert invalid_id in list(res.keys())
assert type(res[valid_id]) == list
assert type(res[invalid_id]) == list
assert res[valid_id][0] == "EGFR"
Expand All @@ -47,10 +42,7 @@ def test_gene_alias_two_genes_and_invalid_id(client):
}
response = client.post(f'{URL_BASE}', data=json.dumps(data), headers=headers)
res = json.loads(response.data)
assert response.content_type == 'application/json'
assert response.status_code == 200
assert alias_id in list(res.keys())
assert invalid_id in list(res.keys())
assert type(res[alias_id]) == list
assert type(res[invalid_id]) == list
assert res[invalid_id] == []
Expand All @@ -68,7 +60,6 @@ def test_empty_gene_id(client):
}
response = client.post(f'{URL_BASE}', data=json.dumps(data), headers=headers)
res = json.loads(response.data)
assert response.content_type == 'application/json'
assert response.status_code == 200
assert len(list(res.keys())) == 0
assert res == {}
Expand All @@ -81,7 +72,5 @@ def test_corrupted_structure(client):
}
response = client.post(f'{URL_BASE}', data=json.dumps(data), headers=headers)
res = json.loads(response.data)
assert response.content_type == 'application/json'
assert response.status_code == 400
assert "error" in list(res.keys())
assert res["error"] == "400 Bad Request: genes_ids is mandatory"
assert "error" in list(res.keys())

0 comments on commit 9861aee

Please sign in to comment.