你们都好吗?希望你做得很好!
所以,拿着这个。我需要将一些.CIF文件(在这里找到:https://www.ccdc.cam.ac.uk/support-and-resources/downloads/ - MOF )转换成我可以用于熊猫的格式,比如CSV或XLS。我正在研究如何使用MOF进行储氢,这个从Cambrigde的结构数据库收集的资料会给我带来奇迹。
到目前为止,我能够使用ToposPro转换它们,但不能转换成我可以与Pandas readTo一起使用的格式。
你们中有人知道怎么做的吗?我也读过有关化脓性物质和基质剂的文章,但我以前从未使用过。
另外,很抱歉我的写作出了任何问题,英语不是我的主要语言。谢谢你的帮助!
发布于 2022-09-06 10:22:30
要将.CIF文件读取为熊猫DataFrame,您可以使用biopython中的模块首先解析.CIF文件并返回一个字典。然后,您将需要从生物字典创建数据。最后,您必须使用将行作为列(因为我们将index
定义为dict处理“缺失”值的方向)。
您需要通过在(Windows)终端中执行这一行来安装:
pip install biopython
然后,可以使用下面的代码读取特定的.CIF文件:
import pandas as pd
from Bio.PDB.MMCIF2Dict import MMCIF2Dict
dico = MMCIF2Dict(r"path_to_the_MOF_collection\abavij_P1.cif")
df = pd.DataFrame.from_dict(dico, orient='index')
df = df.transpose()
>>> display(df)
现在,如果您需要将整个MOF集合(~10k文件)读取为一个数据文件,则可以使用以下内容:
from pathlib import Path
import pandas as pd
from Bio.PDB.MMCIF2Dict import MMCIF2Dict
from time import time
mof_collection = r"path_to_the_MOF_collection"
start = time()
list_of_cif = []
for file in Path(mof_collection).glob('*.cif'):
dico = MMCIF2Dict(file)
temp = pd.DataFrame.from_dict(dico, orient='index')
temp = temp.transpose()
temp.insert(0, 'Filename', Path(file).stem) #to get the .CIF filename
list_of_cif.append(temp)
df = pd.concat(list_of_cif)
end = time()
print(f'The DataFrame of the MOF Collection was created in {end-start} seconds.')
df
>>> output
我相信您知道.CIF文件可能有不同的列数。因此,可以自由地连接(或不) MOF集合。最后但并非最不重要的一点是,如果您想获取数据文件的.csv和/或.xlsx文件,可以使用或。
df.to_csv('your_output_filename.csv', index=False)
df.to_excel('your_output_filename.xlsx', index=False)
编辑:
要将.CIF文件的结构读取为DataFrame,可以通过使用使用方法:
from pymatgen.io.cif import CifParser
parser = CifParser("abavij_P1.cif")
structure = parser.get_structures()[0]
structure.as_dataframe()
>>> output
如果需要检查.CIF文件是否具有有效结构,可以使用:
if len(structure)==0:
print('The .CIF file has no structure')
或者:
try:
structure = parser.get_structures()[0]
except:
print('The .CIF file has no structure')
https://stackoverflow.com/questions/73612417
复制相似问题