1
votes

I have 3 output files containing x, y, z coordinates respectively

file1.txt (contains x only) | file2 (Y) | file3 (Z)

2.113
3.023
-7.234
...

and a parent pdb file which contains coordinates data. I want to extract only those lines from pdb file matching with x,y,z coordinates from file1,file2,file3.The pdb file lines are;

ATOM 1 O5' G A 93 -12.706 19.299 0.129 1.00 0.00 O

The bold values would be my matching criteria to copy the whole line.

1- How I can merge the three output files to make a single file which can give me x,y,z coordinates in a line to work with my script.

final_file = [file1, file2, file3] ?

2- How I can extract points on matching criteria;

def read_convex_points(final_file,parent_pdb):
    with open("output.txt","w") as f1:
        with open(final_file) as f2:
            with open(parent_pdb) as f3:
                for line1 in f2:
                    for line2 in f3: 
                        if line1 in line2:
                            f1.write(line2)
    return               

final_file = "file1.txt"
parent_pdb = "original.pdb"
read_convex_points(final_file,parent_pdb)

I wrote function something like that but if condition is not working.

2
Your description is a little unclear. Are the coordinates in the separate files? Eg, the X coordinates in file1, Y coordinates in file2, Z coordinates in file3. - PM 2Ring
You have 2 questions here, they probably should be in separate posts. But anyway, if you're on Unix or Linux, the simplest way to join the 3 files into 1 is to use paste. It's easy enough to do in Python, as Peter's answer shows. Do you want to do the pasting in the same program that does the match testing? I think it might be a good idea to do it in a separate program, especially if you need to make multiple passes through the data. - PM 2Ring
yes, paste function works. i could not pass line1 in if statement. could you please give any clue? - Muhammad Sajid Iqbal
Roughly how many sets of coordinates are in final_file and how many sets of coordinates are in the PDB file? You can use a double for loop to do your search, but it's not very efficient if you have a lot of data. And at the end of the inner loop you need to rewind the file back to the start using seek(). Eg, f3.seek(0). - PM 2Ring
final_file will have 62 while PDB has 1400. The 62 points were extracted from PDB file. I can run the program over 1400 in loop. It is giving me 1400 lines of single value. I guess I am not giving the right input which helps to copy the whole line. - Muhammad Sajid Iqbal

2 Answers

0
votes

You can merge files like this:

def merge(paths):
    with open(paths[0]) as f1, open(paths[1]) as f2, open(paths[2]) as f3:
        try:
            yield next(f1), next(f2), next(f3)
        except StopIteration:
            return

for x, y, z in merge((file1, file2, file3)):
    # check if matching

The caveat is this assumes files are of equal length, so it will stop the moment it encounters shortest file. This is probably accetable.

0
votes

Here's one way to paste together multiple files in Python. It handles any number of input files, but like Peter's solution if the files have different numbers of lines it stops when the shortest file runs out of lines.

Fields in the output file are separated by the delimiter string, which is a space by default.

def paste(sources, dest, delimiter=' '):
    fsrc = [open(fname, 'r') for fname in sources]
    fdest = open(dest, 'w')
    for t in zip(*fsrc):
        outline = delimiter.join([line.strip() for line in t]) + '\n'
        fdest.write(outline)
    for f in fsrc:
        f.close()
    fdest.close()


paste(('file1', 'file2', 'file3'), 'final_file')