Python脚本:将mol2分子库文件拆分为单个mol2文件
如题:将mol2分子库文件拆分为单个mol2文件
用法:
python split_multimol2.py multi-mol2.mol2 out_dir
注释:python 脚本文件 mol2分子库 输出目录
split_multimol2.py:
#Python2 or Python3
#AspirinCode 2018
#Script that splits a multi-mol2 file into individual mol2 files.
#python split_multimol2.py multi-mol2.mol2 out_dirimport sys
import osdef split_multimol2(multimol2):"""Splits a multi-mol2 file.Parameters----------multimol2 : strPath to the multi-mol2 file.Returns----------A generator object for lists for every extracted mol2-file. Lists containthe molecule ID and the mol2 file contents.e.g., ['ID1234', '@<TRIPOS>MOLECULE...'"""with open(multimol2, 'r') as mol2file:line = mol2file.readline()while not mol2file.tell() == os.fstat(mol2file.fileno()).st_size:if line.startswith("@<TRIPOS>MOLECULE"):mol2cont = []mol2cont.append(line)line = mol2file.readline()molecule_id = line.strip()while not line.startswith("@<TRIPOS>MOLECULE"):mol2cont.append(line)line = mol2file.readline()if mol2file.tell() == os.fstat(mol2file.fileno()).st_size:mol2cont.append(line)breakmol2cont[-1] = mol2cont[-1].rstrip() # removes blank line at file endyield [molecule_id, "".join(mol2cont)]def write_multimol2(multimol2, out_dir):"""Splits a multi-mol2 file into smaller multi-mol2 files.Parameters-----------multimol2 : strPath to the multi-mol2 file.out_dir : str:Output directory. New files will be named<molecule_name_1>.mol2, ... <molecule_name_n>.mol2Returns-----------chunks : intNumber of files written."""if not out_dir:os.mkdir(out_dir)single_mol2s = split_multimol2(args.MOL2_FILE)for mol2 in single_mol2s:out_mol2 = os.path.join(args.OUT_DIR, mol2[0]) + '.mol2'with open(out_mol2, 'w') as out_file:for line in mol2[1]:out_file.write(line)out_file.write('\n')def write_multimol2_chunks(multimol2, chunk_size, out_dir):"""Splits a multi-mol2 file into smaller multi-mol2 files.Parameters-----------multimol2 : strPath to the multi-mol2 file.chunksize : intNumber of mol2 files per chunk.out_dir : str:Output directory. New files will be named<multimol2>_1.mol2, ... <multimol2>_n.mol2Returns-----------chunks : intNumber of files written."""if not os.path.exists(out_dir):os.mkdir(out_dir)out_path_stem = os.path.dirname(multimol2)out_file_stem = os.path.basename(multimol2).split('.mol2')[0]cnt = 0chunks = 1out_file = open(os.path.join(out_dir, out_file_stem)+'_%d.mol2' % chunks, 'w')for mol2 in split_multimol2(multimol2):cnt += 1if cnt == chunk_size:cnt = 0chunks += 1out_file.close()out_file = open(os.path.join(out_dir, out_file_stem)+'_%d.mol2' % chunks, 'w')out_file.write(mol2[1] + '\n')out_file.close()return chunksif __name__ == '__main__':import argparseparser = argparse.ArgumentParser(description='Splits a multi-mol2 file into individual mol2 files',formatter_class=argparse.RawTextHelpFormatter)parser.add_argument('MOL2_FILE')parser.add_argument('OUT_DIR')parser.add_argument('-c', '--chunksize', help='Number of MOL2 structures per file (1 by default)', type=int)parser.add_argument('-v', '--version', action='version', version='split_multimol2 v. 1.1')args = parser.parse_args()if args.chunksize:write_multimol2_chunks(multimol2=args.MOL2_FILE, chunk_size=args.chunksize, out_dir=args.OUT_DIR)else:write_multimol2(multimol2=args.MOL2_FILE, out_dir=args.OUT_DIR)
Python脚本:将mol2分子库文件拆分为单个mol2文件相关推荐
- 使用awk处理多行fasta文件拆分为单个fasta文件,并去掉后缀^M
大文件:31万个序列(我看的文章里面说一小时处理100条序列,这里我用了半个小时) 代码如下:把mulfa.fasta文件拆分成单个fasta文件 awk '{if($0~/^>/)a=$0; ...
- python ipaddr库_用Python脚本查询纯真IP库QQWry.dat(Demon修改版)
#!/usr/bin/env python # coding: utf-8 # from: http://linuxtoy.org/files/pyip.py # Blog: http://linux ...
- 用 python 脚本,把当前目录及子目录下的 wav 音频文件转换为 flac 格式
用 python 脚本,把当前目录及子目录下的 wav 音频文件转换为 flac 格式 import os, subprocessfor d,sd,files in os.walk('.'):for ...
- 怎么将PDF文件拆分成多个文件?2个方法帮你搞定
PDF格式在日常办公学习中经常被使用,因为它不仅易于阅读和浏览,而且文件大小相对较小,兼容性也很强.然而,有时候当PDF文件内容较多时,找到文件中重要内容可能会变得困难.为了解决这个问题,我们可以尝试 ...
- python读取txt文件并分割成列表_在python中读取文本文件并将其拆分为单个单词
给定此文件:$ cat words.txt line1 word1 word2 line2 word3 word4 line3 word5 word6 如果一次只需要一个单词(忽略文件中空格和换行符的 ...
- 将PDF文件拆分成多个文件的教程
毕业的大学生都要写论文,这是很重要的一个环节,但是论文不像我们平时编辑资料一样,只要差不多就可以交,但是论文不一样.我们到网上采集资料的时候,都是都是PDF格式的文件偏多,所以我们的就很苦恼,下载之后 ...
- linux 文件拆分 合并,Linux下文件的切分与合并的简单方法
linux下文件分割可以通过split命令来实现,可以将一个大文件拆分成指定大小的多个文件,并且拆分速度非常的快,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现, ...
- 批量将多个 PPT 幻灯片文件合并成单个 PPT 文件
概要:我们经常处理 PPT 幻灯片文件,有时候我们有很多个 PPT 幻灯片文件需要整理.最常见的需求就是我们需要批量的将多个 PPT 幻灯片文件合并成一个独立的 PPT 文件,这个时候该怎么办呢?如果 ...
- idea打开java文件_IDEA运行单个Java文件
对于某些Java示例可能是只有单个文件,并不是完整的Java工程,那么要如何运行单个Java文件呢,以IDEA为例. 我的环境: IDEA 2017.3.2 jdk 1.8.0.73 操作步骤: 1. ...
最新文章
- 全面分析再动手的习惯:链表的反转问题(递归和非递归方式)
- 如何评价软件写的好还是坏?
- vm ububtu突然没网
- 程序员面试题精选100题(22)-整数二进制表示中1的个数[算法]
- Fabric--configtxlator转换配置添加组织到channel
- 对多个button触发enter操作,执行不同的操作
- 如何深拷贝一个对象数组?
- Appnium安装-Mac平台
- 02Framelayout:帧布局
- Socket通信之TCP拆包和封包
- 旅游预订平台Travala.com将与物联网区块链平台IoTeX合作
- PHP错误处理注册机制
- vue-app之H5打开第三方app
- 冷藏温度范围_冷藏和冷冻的温度标准
- 惠普Elite Mini 800 G9 评测
- blendere删除标注
- dnf剑魂buff等级上限_DNF95全新版本,剑魂之玩法攻略
- 按键android手机排行榜,【直板全键盘手机推荐】直板键盘手机排行榜
- 实验一 利用Excel表格进行掷硬币模拟实验
- 专升本笔记记载-第七章-无穷级数