我试图在我的文件中的某一行之后添加一个单词。我正在使用infile和一个引用,并试图创建outfile。infile和reference是相同的类型,但reference在特定位置包含TER单词。我想把terword添加到outfile中(基本上创建一个infle的副本并从引用中添加terword)。 我试图按一个数字(代码中的resnum)搜索,但会有一个问题,因为许多连续的行都有相同的数字。 有人能帮忙吗?你知道吗
from sys import argv
import argparse
script,infile,outfile, reference = argv
Ter = []
res = []
def get_Ter(reference):
reference_1 = open(reference,"r")
for line in reference_1:
contents = line.split(" ")
if contents[0] == "TER":
resnum = line[22:27]
resname = line[17:20]
chain = line[21]
Ter.append(resnum)
def find_TER(infile,outfile):
with open(infile, "r") as infile_1:
content = infile_1.readlines()
with open(outfile, "w+") as outfile_1:
outfile_1.write(content)
if line[0:6] == "ATOM ":
resnum_1 = line[22:27]
res.append(resnum_1)
if resnum_1 in res == resnum in Ter:
outfile_1.write(line + "\nTER")
find_TER(infile,outfile)
get_Ter(reference)
文件的示例(这是引用,infile是相同的,但缺少TER)。它们都很好地排列在一起(这里的格式):
原子992 SG CYX D 452 23.296 45.745 28.572 1.00 0.00
原子993 C CYX D 452 20.742 42.431 27.841 1.00 0.00
原子994 O CYX D 452 20.689 41.447 28.565 1.00 0.00
原子995 OXT CYX D 452 19.788 42.822 27.185 1.00 0.00
TER 995 CYS D 452
原子996 N ARG D 492 27.510 26.357 34.041 1.00 0.00
原子997 H1 ARG D 492 26.590 26.591 33.694 1.00 0.00
原子998 H2 ARG D 492 28.138 27.135 34.182 1.00 0.00
原子999 H3 ARG D 492 27.422 26.030 34.993 1.00 0.00
原子1000 CA ARG D 492 28.179 25.410 33.192 1.00 0.00
现在我有了这个:
from sys import argv
import argparse
script,infile,outfile, reference = argv
Ter = []
res = []
def get_Ter(reference):
reference_1 = open(reference,"r")
for line in reference_1:
contents = line.split(" ")
if contents[0] == "TER":
ternum = line[22:27]
def find_TER(infile,outfile):
with open(infile, "r") as infile_1:
content = infile_1.readlines()
with open(outfile, "w+") as outfile_1:
for line in content:
outfile_1.write(line)
line = line.split(" ")
if line[0] == "ATOM":
resnum = line[22:27]
if ternum == resnum:
find_TER(infile,outfile)
get_Ter(reference)
基本逻辑有两个方面:
对于第二部分,您真正需要做的就是识别resnum452(或任何数字)有一个挂起的TER输出。您可以使用一个简单的变量来实现这一点:将它保持在-1,直到您有一个有效的resnum。你知道吗
当你读的时候,你不断地检查resnum。如果它是正数并且与最近的输入行不同,那么您必须在执行其他操作之前打印TER行。像这样:
您可能还需要在文件末尾进行检查,以防最后一个resnum有一行要打印出来。你知道吗
这足以让你前进吗?你知道吗
相关问题 更多 >
编程相关推荐