[SOLVED] Get atomic coordinates from specific residues and compute distances

Issue

I’m trying to extract the exact coordinates for an atom in specific residues in a PDB file.

The idea is to use a function that finds the coordinates of all oxygens of the cysteines in a file, find the nitrogen coordinates on the histidines in the same file, and calculate the distances between them.

import sys
import os
os.getcwd()
filename = sys.argv[1]


def cys_ox_coord_extr():
    with open(filename, "r") as fileobj:
        content = fileobj.readlines()
        print("Cysteine's oxygen coordinates\n")
        for line in content:
            if line.startswith("ATOM"):
                if str(line.split()[3]) == "CYS" and str(line.split()[2]) == "O":
                    cys_ox = [line.split()[5], [line.split()[6], line.split()[7], line.split()[8]]]
                    print(cys_ox)

The function works fine, and I can get the coordinates for this atom type, however, I can’t use these results elsewhere.

I can’t recall "cys_ox" anywhere outside the function itself, even using `return cys_ox.
Yet, if I print it, the results come out fine.

My goal is to get the coordinates for another atom type in another residue and compute the distances, but it is impossible if I can’t use the results outside the function which generates them.

What can I do?

Thank you!!!

Solution

as Giacomo suggested,

define a list outside the function, and append cys_ox to the list. So the list has global scope and you can access outside the function. Else you should define global cys_ox at start of function, so python know that such variable should have global scope (and not the default function scope), but you may have many cys_ox, so a list is better. –
Giacomo Catenazzi

Answered By – Ludovico Pipitò

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published.