Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,286 +1,287 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
-
from Bio import Entrez, SeqIO
|
| 3 |
-
import primer3
|
| 4 |
-
import ssl
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import io
|
| 7 |
-
import json
|
| 8 |
-
from datetime import datetime
|
| 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 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
"
|
| 95 |
-
"
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
"
|
| 151 |
-
"
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
"
|
| 157 |
-
"
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
'
|
| 190 |
-
'
|
| 191 |
-
'
|
| 192 |
-
'
|
| 193 |
-
'
|
| 194 |
-
'
|
| 195 |
-
'
|
| 196 |
-
'
|
| 197 |
-
'
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
'
|
| 203 |
-
'
|
| 204 |
-
'
|
| 205 |
-
'
|
| 206 |
-
'
|
| 207 |
-
'
|
| 208 |
-
'
|
| 209 |
-
'
|
| 210 |
-
'
|
| 211 |
-
'
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
'
|
| 244 |
-
'
|
| 245 |
-
'
|
| 246 |
-
'
|
| 247 |
-
'
|
| 248 |
-
'
|
| 249 |
-
'
|
| 250 |
-
'
|
| 251 |
-
'
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
"
|
| 273 |
-
"
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
| 286 |
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
+
from Bio import Entrez, SeqIO
|
| 3 |
+
import primer3
|
| 4 |
+
import ssl
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import io
|
| 7 |
+
import json
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
| 13 |
+
def get_ready_for_primers(gene_symbol, species="human"):
|
| 14 |
+
"""获取基因序列和外显子交界点信息"""
|
| 15 |
+
Entrez.email = "your_email@example.com"
|
| 16 |
+
|
| 17 |
+
# 1. 搜索基因并获取 NCBI 内部 ID
|
| 18 |
+
search_term = f"{gene_symbol}[Gene Name] AND {species}[Organism]"
|
| 19 |
+
handle = Entrez.esearch(db="gene", term=search_term)
|
| 20 |
+
record = Entrez.read(handle)
|
| 21 |
+
if not record["IdList"]:
|
| 22 |
+
return {"error": "未找到该基因"}
|
| 23 |
+
gene_id = record["IdList"][0]
|
| 24 |
+
|
| 25 |
+
# 2. 获取该基因关联的 NM_ 编号
|
| 26 |
+
link_handle = Entrez.elink(dbfrom="gene", db="nucleotide", id=gene_id, term="srcdb_refseq[prop] AND mRNA[filter]")
|
| 27 |
+
link_record = Entrez.read(link_handle)
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# 获取第一个关联的核苷酸 UID
|
| 31 |
+
nucl_id = link_record[0]["LinkSetDb"][0]["Link"][0]["Id"]
|
| 32 |
+
|
| 33 |
+
# 3. 下载完整的 GenBank 格式数据
|
| 34 |
+
handle = Entrez.efetch(db="nucleotide", id=nucl_id, rettype="gb", retmode="text")
|
| 35 |
+
seq_record = SeqIO.read(handle, "genbank")
|
| 36 |
+
|
| 37 |
+
# 提取外显子分界点
|
| 38 |
+
junctions = []
|
| 39 |
+
current_pos = 0
|
| 40 |
+
for feature in seq_record.features:
|
| 41 |
+
if feature.type == "exon":
|
| 42 |
+
start, end = feature.location.start, feature.location.end
|
| 43 |
+
current_pos += (end - start)
|
| 44 |
+
junctions.append(int(current_pos))
|
| 45 |
+
|
| 46 |
+
if junctions:
|
| 47 |
+
junctions.pop() # 移除最后一个边界
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"symbol": gene_symbol,
|
| 51 |
+
"nm_id": seq_record.id,
|
| 52 |
+
"sequence": str(seq_record.seq),
|
| 53 |
+
"junctions": junctions
|
| 54 |
+
}
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return {"error": f"获取基因信息失败: {str(e)}"}
|
| 57 |
+
|
| 58 |
+
def design_qpcr_primers(gene_data):
|
| 59 |
+
"""设计qPCR引物"""
|
| 60 |
+
if "error" in gene_data:
|
| 61 |
+
return gene_data
|
| 62 |
+
|
| 63 |
+
seq_args = {
|
| 64 |
+
'SEQUENCE_ID': gene_data['nm_id'],
|
| 65 |
+
'SEQUENCE_TEMPLATE': gene_data['sequence'],
|
| 66 |
+
'SEQUENCE_OVERLAP_JUNCTION_LIST': gene_data['junctions'],
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
global_args = {
|
| 70 |
+
'PRIMER_OPT_SIZE': 20,
|
| 71 |
+
'PRIMER_MIN_SIZE': 18,
|
| 72 |
+
'PRIMER_MAX_SIZE': 25,
|
| 73 |
+
'PRIMER_OPT_TM': 60.0,
|
| 74 |
+
'PRIMER_MIN_TM': 57.0,
|
| 75 |
+
'PRIMER_MAX_TM': 63.0,
|
| 76 |
+
'PRIMER_TM_MAX_DIFF': 1.0,
|
| 77 |
+
'PRIMER_MIN_GC': 40.0,
|
| 78 |
+
'PRIMER_MAX_GC': 60.0,
|
| 79 |
+
'PRIMER_PRODUCT_SIZE_RANGE': [120, 300],
|
| 80 |
+
'PRIMER_NUM_RETURN': 5,
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
results = primer3.bindings.design_primers(seq_args, global_args)
|
| 85 |
+
|
| 86 |
+
primer_pairs = []
|
| 87 |
+
for i in range(global_args['PRIMER_NUM_RETURN']):
|
| 88 |
+
try:
|
| 89 |
+
pair = {
|
| 90 |
+
"id": i + 1,
|
| 91 |
+
"forward": results[f'PRIMER_LEFT_{i}_SEQUENCE'],
|
| 92 |
+
"reverse": results[f'PRIMER_RIGHT_{i}_SEQUENCE'],
|
| 93 |
+
"f_tm": f"{results[f'PRIMER_LEFT_{i}_TM']:.2f}",
|
| 94 |
+
"r_tm": f"{results[f'PRIMER_RIGHT_{i}_TM']:.2f}",
|
| 95 |
+
"product_size": results[f'PRIMER_PAIR_{i}_PRODUCT_SIZE'],
|
| 96 |
+
"junction_info": "跨越外显子交界点"
|
| 97 |
+
}
|
| 98 |
+
primer_pairs.append(pair)
|
| 99 |
+
except KeyError:
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
return {"primers": primer_pairs, "gene_info": gene_data}
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return {"error": f"引物设计失败: {str(e)}"}
|
| 105 |
+
|
| 106 |
+
@app.route('/')
|
| 107 |
+
def index():
|
| 108 |
+
return render_template('index.html')
|
| 109 |
+
|
| 110 |
+
@app.route('/design_primers', methods=['POST'])
|
| 111 |
+
def design_primers_api():
|
| 112 |
+
data = request.json
|
| 113 |
+
gene_symbol = data.get('gene_symbol', '').strip()
|
| 114 |
+
species = data.get('species', 'human')
|
| 115 |
+
|
| 116 |
+
if not gene_symbol:
|
| 117 |
+
return jsonify({"error": "请输入基因名称"})
|
| 118 |
+
|
| 119 |
+
# 获取基因信息
|
| 120 |
+
gene_data = get_ready_for_primers(gene_symbol, species)
|
| 121 |
+
|
| 122 |
+
# 设计引物
|
| 123 |
+
result = design_qpcr_primers(gene_data)
|
| 124 |
+
|
| 125 |
+
return jsonify(result)
|
| 126 |
+
|
| 127 |
+
@app.route('/batch_design_primers', methods=['POST'])
|
| 128 |
+
def batch_design_primers_api():
|
| 129 |
+
data = request.json
|
| 130 |
+
gene_list = data.get('gene_list', [])
|
| 131 |
+
species = data.get('species', 'human')
|
| 132 |
+
|
| 133 |
+
if not gene_list:
|
| 134 |
+
return jsonify({"error": "请输入基因列表"})
|
| 135 |
+
|
| 136 |
+
results = []
|
| 137 |
+
for gene_symbol in gene_list:
|
| 138 |
+
gene_symbol = gene_symbol.strip()
|
| 139 |
+
if not gene_symbol:
|
| 140 |
+
continue
|
| 141 |
+
time.sleep(0.5)
|
| 142 |
+
# 获取基因信息
|
| 143 |
+
gene_data = get_ready_for_primers(gene_symbol, species)
|
| 144 |
+
|
| 145 |
+
# 设计引物
|
| 146 |
+
result = design_qpcr_primers(gene_data)
|
| 147 |
+
|
| 148 |
+
if "error" in result:
|
| 149 |
+
results.append({
|
| 150 |
+
"gene": gene_symbol,
|
| 151 |
+
"status": "failed",
|
| 152 |
+
"error": result["error"]
|
| 153 |
+
})
|
| 154 |
+
else:
|
| 155 |
+
results.append({
|
| 156 |
+
"gene": gene_symbol,
|
| 157 |
+
"status": "success",
|
| 158 |
+
"data": result
|
| 159 |
+
})
|
| 160 |
+
|
| 161 |
+
return jsonify({"results": results})
|
| 162 |
+
|
| 163 |
+
@app.route('/export_primers', methods=['POST'])
|
| 164 |
+
def export_primers():
|
| 165 |
+
data = request.json
|
| 166 |
+
export_format = data.get('format', 'excel') # excel, csv, json
|
| 167 |
+
results_data = data.get('data', [])
|
| 168 |
+
|
| 169 |
+
if export_format == 'excel':
|
| 170 |
+
return export_to_excel(results_data)
|
| 171 |
+
elif export_format == 'csv':
|
| 172 |
+
return export_to_csv(results_data)
|
| 173 |
+
elif export_format == 'json':
|
| 174 |
+
return export_to_json(results_data)
|
| 175 |
+
else:
|
| 176 |
+
return jsonify({"error": "不支持的导出格式"})
|
| 177 |
+
|
| 178 |
+
def export_to_excel(results_data):
|
| 179 |
+
"""导出为Excel格式"""
|
| 180 |
+
rows = []
|
| 181 |
+
|
| 182 |
+
for result in results_data:
|
| 183 |
+
if result.get('status') == 'success':
|
| 184 |
+
gene_info = result['data']['gene_info']
|
| 185 |
+
primers = result['data']['primers']
|
| 186 |
+
|
| 187 |
+
for primer in primers:
|
| 188 |
+
rows.append({
|
| 189 |
+
'基因名称': gene_info['symbol'],
|
| 190 |
+
'RefSeq ID': gene_info['nm_id'],
|
| 191 |
+
'引物对编号': primer['id'],
|
| 192 |
+
'正向引物序列': primer['forward'],
|
| 193 |
+
'反向引物序列': primer['reverse'],
|
| 194 |
+
'正向引物Tm(°C)': primer['f_tm'],
|
| 195 |
+
'反向引物Tm(°C)': primer['r_tm'],
|
| 196 |
+
'产物长度(bp)': primer['product_size'],
|
| 197 |
+
'外显子交界点': ', '.join(map(str, gene_info['junctions'])),
|
| 198 |
+
'设计时间': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 199 |
+
})
|
| 200 |
+
else:
|
| 201 |
+
rows.append({
|
| 202 |
+
'基因名称': result['gene'],
|
| 203 |
+
'RefSeq ID': 'N/A',
|
| 204 |
+
'引物对编号': 'N/A',
|
| 205 |
+
'正向引物序列': 'N/A',
|
| 206 |
+
'反向引物序列': 'N/A',
|
| 207 |
+
'正向引物Tm(°C)': 'N/A',
|
| 208 |
+
'反向引物Tm(°C)': 'N/A',
|
| 209 |
+
'产物长度(bp)': 'N/A',
|
| 210 |
+
'外显子交界点': 'N/A',
|
| 211 |
+
'设计时间': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
| 212 |
+
'错误信息': result.get('error', '未知错误')
|
| 213 |
+
})
|
| 214 |
+
|
| 215 |
+
df = pd.DataFrame(rows)
|
| 216 |
+
|
| 217 |
+
# 创建Excel文件
|
| 218 |
+
output = io.BytesIO()
|
| 219 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 220 |
+
df.to_excel(writer, sheet_name='引物设计结果', index=False)
|
| 221 |
+
|
| 222 |
+
output.seek(0)
|
| 223 |
+
filename = f"qPCR_primers_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 224 |
+
|
| 225 |
+
return send_file(
|
| 226 |
+
output,
|
| 227 |
+
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
| 228 |
+
as_attachment=True,
|
| 229 |
+
download_name=filename
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
def export_to_csv(results_data):
|
| 233 |
+
"""导出为CSV格式"""
|
| 234 |
+
rows = []
|
| 235 |
+
|
| 236 |
+
for result in results_data:
|
| 237 |
+
if result.get('status') == 'success':
|
| 238 |
+
gene_info = result['data']['gene_info']
|
| 239 |
+
primers = result['data']['primers']
|
| 240 |
+
|
| 241 |
+
for primer in primers:
|
| 242 |
+
rows.append({
|
| 243 |
+
'基因名称': gene_info['symbol'],
|
| 244 |
+
'RefSeq ID': gene_info['nm_id'],
|
| 245 |
+
'引物对编号': primer['id'],
|
| 246 |
+
'正向引物序列': primer['forward'],
|
| 247 |
+
'反向引物序列': primer['reverse'],
|
| 248 |
+
'正向引物Tm(°C)': primer['f_tm'],
|
| 249 |
+
'反向引物Tm(°C)': primer['r_tm'],
|
| 250 |
+
'产物长度(bp)': primer['product_size'],
|
| 251 |
+
'外显子交界点': ', '.join(map(str, gene_info['junctions'])),
|
| 252 |
+
'设计时间': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 253 |
+
})
|
| 254 |
+
|
| 255 |
+
df = pd.DataFrame(rows)
|
| 256 |
+
|
| 257 |
+
output = io.StringIO()
|
| 258 |
+
df.to_csv(output, index=False, encoding='utf-8-sig')
|
| 259 |
+
|
| 260 |
+
filename = f"qPCR_primers_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
| 261 |
+
|
| 262 |
+
return send_file(
|
| 263 |
+
io.BytesIO(output.getvalue().encode('utf-8-sig')),
|
| 264 |
+
mimetype='text/csv',
|
| 265 |
+
as_attachment=True,
|
| 266 |
+
download_name=filename
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
def export_to_json(results_data):
|
| 270 |
+
"""导出为JSON格式"""
|
| 271 |
+
export_data = {
|
| 272 |
+
"export_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
| 273 |
+
"total_genes": len(results_data),
|
| 274 |
+
"results": results_data
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
filename = f"qPCR_primers_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 278 |
+
|
| 279 |
+
return send_file(
|
| 280 |
+
io.BytesIO(json.dumps(export_data, ensure_ascii=False, indent=2).encode('utf-8')),
|
| 281 |
+
mimetype='application/json',
|
| 282 |
+
as_attachment=True,
|
| 283 |
+
download_name=filename
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
if __name__ == '__main__':
|
| 287 |
app.run(debug=True, host='0.0.0.0', port=5000)
|