批量生成子网页
import pandas as pd
import os
import shutil
# 你的数据表
df = pd.read_csv("species_info.tsv", sep="\\t")
# 模板网页所在文件夹(包含 index.html + static)
template_dir = r"./362614"
template_html = os.path.join(template_dir, "index.html")
output_root = r"./species"
# 读取模板 HTML
with open(template_html, "r", encoding="utf-8") as f:
template_content = f.read()
# 遍历每个物种
for idx, row in df.iterrows():
taxid = str(row['taxid'])
organism_name = str(row['Organism name'])
target_dir = os.path.join(output_root, taxid)
# 复制整个模板文件夹到新目录
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
shutil.copytree(template_dir, target_dir)
# 替换网页内容
html_file = os.path.join(target_dir, "index.html")
html_content = template_content
# 先替换三处物种名
html_content = html_content.replace(
'<title>Usnea hakonensis | Bryophyte</title>',
f'<title>{organism_name} | Bryophyte</title>'
)
html_content = html_content.replace(
'» Usnea hakonensis </nav>',
f'» {organism_name} </nav>'
)
html_content = html_content.replace(
'<h1 class="title" id="page-title">Usnea hakonensis </h1>',
f'<h1 class="title" id="page-title">{organism_name}</h1>'
)
# 替换其他占位符
replacements = {
"{{Organism name}}": organism_name,
"{{taxid}}": taxid,
"{{total length}}": str(row['total length(Mb)']),
"{{contig N50}}": str(row['contig N50(Mb)']),
"{{gc perc}}": str(row['gc perc']),
"{{Sequencing technology}}": str(row['Sequencing technology']),
"{{GenBank accession}}": str(row['GenBank accession']),
"{{Assembly level}}": str(row['Assembly level'])
}
for k, v in replacements.items():
html_content = html_content.replace(k, v)
# 写入修改后的 HTML
with open(html_file, "w", encoding="utf-8") as f:
f.write(html_content)
print("✅ 所有网页已生成完毕。")
import pandas as pd
df = pd.read_csv("lichen.csv", sep=None, engine="python", on_bad_lines="skip")
print(df.head())
# 生成 HTML 表格代码
html_table = df.to_html(
index=False,
escape=False, # 允许HTML格式
justify='center',
border=1
)
# 输出到文件
with open("lichen_table.html", "w", encoding="utf-8") as f:
f.write(html_table)
print("✅ 表格已生成为 lichen_table.html")
from fastapi import FastAPI, Form
import subprocess, uuid, os
app = FastAPI()
@app.post("/blastn")
def blastn(seq: str = Form(...)):
query_file = f"/tmp/{uuid.uuid4()}.fna"
with open(query_file, "w") as f:
f.write(f">query\\n{seq}")
result = subprocess.check_output([
"blastn",
"-query", query_file,
"-db", "db/genomic_db",
"-outfmt", "6"
]).decode()
os.remove(query_file)
return {"result": result}
@app.post("/blastp")
def blastp(seq: str = Form(...)):
query_file = f"/tmp/{uuid.uuid4()}.faa"
with open(query_file, "w") as f:
f.write(f">query\\n{seq}")
result = subprocess.check_output([
"blastp",
"-query", query_file,
"-db", "db/protein_db",
"-outfmt", "6"
]).decode()
os.remove(query_file)
return {"result": result}