I am trying to write "TER" after a line in the outfile after particular residue number (matching that with the residue number in the reference file). The code doesnt give me any error but only copies from the infile and doesnt add the word. When trying to print after some bits of code I think it breaks when I am trying to search in the outfile.
So thats one part. The other is how do I then make sure the word "TER" is added only after last residue number (there are various number of them in lines underneath each other and I only want to add them after the last one). Here is my code:
import sys
import argparse
def main(argv):
parser = argparse.ArgumentParser(description='Read SSBOND directives from a PDB, and generate corresponding CONECT records')
parser.add_argument('infile', help='input file (PDB format)')
parser.add_argument('outfile', help='output file (PDB format)')
parser.add_argument('reference', help =' ref')
args = parser.parse_args()
resnum_1 =[]
res = []
with open(args.infile, "r") as f, open(args.outfile, "w+") as of, open(args.reference,"r") as rf:
for line in rf:
if line[0:4] == "TER ":
resnum = line[22:27]
resname = line[17:20]
chain = line[21]
resnum_1.append(resnum)
for line in f:
of.write(line)
for line in of:
if line[0:6] == "ATOM ":
resnum_fo = line[22:27]
resname_fo = line[17:19]
chain_fo = line[21]
res.append(resnum_fo)
if resnum in resnum_1 and resnum_fo in res:
of.write("TER\n")
if __name__ == "__main__":
main(sys.argv)
Thank you very much!
The files look like this:
ATOM 0 HB2 CYX D 452 45.962 -2.641 -17.575 1.00 0.00
ATOM 0 HB3 CYX D 452 46.188 -2.186 -19.050 1.00 0.00
TER 995 CYX D 452
ATOM 995 N ARG D 492 42.476 10.547 -39.562 1.00 0.00