1

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  
5
  • Sow us the input file content (or a simplified version), same thing for the reference file and a desired output (different from what the above code produces). On the other hand I see that you are writing in the output file while iterating it. I think that might confuse the file pointer. Commented Mar 24, 2017 at 13:12
  • Unfortunately, I can't figure out which of the 4 lines belong to which file :( Commented Mar 24, 2017 at 13:48
  • Sorry, they all look the same just the reference file only contains the TER records. The other two dont have them. Commented Mar 24, 2017 at 13:51
  • @Loki Did my answer help you? If you provide more information, I might be able to assist further. Commented Mar 24, 2017 at 20:57
  • Hi, pingul. I dont think the split is an issue as i have been using that for a while and it worked. I am trying to open f file and write its context into the outfile (of) and then add TER records into the outfile from the reference (rf). The TER records need to go after the last residue number (there are few of them underneath each other - no set number). Commented Mar 26, 2017 at 13:07

1 Answer 1

1

A couple of points/guesses:

  • You open of as w+, but then you do for line in of:. Did you mean f? (I would suggest some more descriptive names)

  • You seem to check against "ATOM " (note 2 spaces). From your file it looks like it should only be 1 space.

To get away with the problems of spaces, I suggest you use split:

 "one two three".split()
 ['one', 'two', 'three']

So, for your case, use as:

for line in rf:
    contents = line.split()
    if contents[0] == "TER":
        ...


for line in f:
    contents = line.split()
    if contents[0] == "ATOM":
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.