-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (74 loc) · 2.68 KB
/
app.py
File metadata and controls
87 lines (74 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import Flask, jsonify, Response
from flask_cors import CORS
import os
from dotenv import load_dotenv
from utils.sto_parser import parse_stockholm_file
from utils.s3 import get_seed_file_from_s3
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
@app.route('/', methods=['GET'])
def health_check():
"""
Health check endpoint for Kubernetes liveness/readiness probes.
Returns: JSON with service status
"""
return jsonify({
'status': 'healthy',
'service': 'RNA Alignment API',
'environment': os.getenv('ENVIRONMENT', 'unknown')
}), 200
@app.route('/<identifier>', methods=['GET'])
def get_msa_data(identifier):
"""
Get MSA data by identifier from S3, parsed and formatted for the MSA viewer.
URL pattern: /{identifier}
Returns: JSON data structure ready for MSA viewer consumption
"""
# Prevent health check route from being treated as an identifier
if identifier in ['health', 'favicon.ico']:
return jsonify({
'status': 'error',
'message': 'Invalid identifier',
'data': None
}), 400
try:
# Get .sto file content from S3
sto_content = get_seed_file_from_s3(identifier)
if not sto_content.strip():
return jsonify({
'status': 'error',
'message': f'No content found for identifier {identifier}',
'data': None
}), 404
# Parse Stockholm content and format for MSA viewer
msa_data = parse_stockholm_file(sto_content)
# Add metadata
response_data = {
'status': 'success',
'message': f'MSA data retrieved successfully for {identifier}',
'data': {
'identifier': identifier,
'consensus': msa_data['consensus'],
'notation': msa_data.get('notation'),
'sequences': msa_data.get('sequences'),
'sequenceCount': len(msa_data.get('sequences', []))
}
}
return jsonify(response_data)
except ValueError as e:
return jsonify({
'status': 'error',
'message': f'Invalid Stockholm format for {identifier}: {str(e)}',
'data': None
}), 400
except Exception as e:
return jsonify({
'status': 'error',
'message': f'Failed to retrieve and parse Stockholm file for {identifier}: {str(e)}',
'data': None
}), 500
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=False)