栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用python批量下载需要的分子的SDF文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python批量下载需要的分子的SDF文件

使用python批量下载需要的分子的SDF文件

这里需要借助python的第三方库pubchempy
(可以使用pip安装:pip install pubchempy)
首先定义文件的路径(这里的文件指的是你需要下载的化合物由smile码构成的txt文件),建议使用绝对路径:

filePath = "D:\1000.txt";
f = open(filePath, "r",encoding='utf-8')

读取文件然后将其命名为列表

file_open = f.readlines()

然后定义两个空的列表;

file_strip = []
list_3 = []

如果在txt里面的文本是换行写的,先将其换行符号去掉;

for line in file_open:
    line = line.strip()
    file_strip.append(str(line)) # 去掉每行头尾空白

下面需要将里面的文本转变成cid唯一标识

i = 0
for i in range(len(file_strip)):
    list_1 = pcp.get_compounds(file_strip[i],'smiles')
    str_list_1 = str(list_1)#将列表转为字符串
    str_list_2 = re.findall(r'd+',str_list_1)#提取字符串中的数字

部分并赋值给列表str_list_2

```python
  str_list_3.extend( str_list_2)

print(list_3)

list_int = list(map(int,str_list_3))#将list_3字符串数组改为整形数组list_int
得到的list_int就是由cid构成的数组,下面只需将其用一个循环从网上下载即可。

```python

```python
for cid in list_int:
    sdfpath = os.path.join('D:\', "{}.sdf".format(cid))#这里要设置路径
    try:
        pcp.download('SDF',sdfpath, overwrite=True, identifier=cid, record_type='3d')        
    except pcp.NotFoundError as e:
        print('No 3d Conformer for {}.'.format(cid))
f.close()#最后别忘记关闭文档
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/931812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号