根据化合物的英文名称批量获取其对应smiles与CID编号

话不多说,python代码奉上:

import os
import requests

names = ["aspirin", "acetaminophen", "ibuprofen"]           # 需要检索的化合物英文
base_url = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{}/property/CanonicalSMILES,title/CSV'
headers = {"content-type":"application/x-www-form-urlencoded"}
file_path = 'result_by_name.csv'                            # 输出文件,每次运行前需要删除上一次运行存在的输出

# 检查文件是否存在且为空,如果是,则只在文件开头写入标题行
if not os.path.exists(file_path) or os.stat(file_path).st_size == 0:
    with open(file_path, 'w', encoding='UTF8') as f:
        f.write('"CID","CanonicalSMILES","Title"\n')

for name in names:
    url = base_url.format(name)
    res = requests.get(url, headers=headers)
    # 分割响应文本以跳过标题行,然后将剩余部分写入文件,不添加额外的空行
    data_lines = res.text.strip().split('\n')[1:]  # 移除首尾空白字符,并分割文本以跳过标题行
    if data_lines:  # 确保data_lines不为空
        with open(file_path, 'a', encoding='UTF8') as f:
            f.write('\n'.join(data_lines) + '\n')  # 写入数据行,每行之后直接跟换行符

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你写一个这样的代码。你可以使用PubChemPy库来进行这个任务。首先,你需要在PubChem中搜索每个化合物,然后从搜索结果中提取CID和Isomeric SMILES。以下是代码示例: ``` import pubchempy as pcp import pandas as pd # 假设化合物名称的列表为compounds_list compounds_list = ['水', '氢氧化钠', '氯化钠'] results = [] for compound in compounds_list: try: # 在PubChem中搜索化合物 search_results = pcp.get_compounds(compound, 'name') # 从搜索结果中提取CID和Isomeric SMILES cid = search_results[0].cid isomeric_smiles = search_results[0].isomeric_smiles # 将结果追加到结果列表中 results.append({'Compound': compound, 'CID': cid, 'Isomeric SMILES': isomeric_smiles}) except: # 如果搜索失败,则将结果追加到结果列表中,但CID和Isomeric SMILES都为空值 results.append({'Compound': compound, 'CID': None, 'Isomeric SMILES': None}) # 将结果转换为DataFrame results_df = pd.DataFrame(results) # 将结果保存到CSV文件中 results_df.to_csv('compounds_info.csv', index=False) ``` 这个代码将在PubChem中搜索你提供的每个化合物,并从每个搜索结果中提取CID和Isomeric SMILES。然后,它将结果存储到一个CSV文件中,其中包含化合物名称、CID和Isomeric SMILES。注意,有些化合物可能无法在PubChem中找到,如果这样的话,代码将不会崩溃,而是在结果列表中将CID和Isomeric SMILES设置为null。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值