gabcg
1/28/2019 - 5:20 PM

gistfile1.txt

def calculate_pdb_chain_mean_minimum_distances(pdb_file_path):
    """
    Returns a dictionary with residues as keys and mean minimum distances as
    values.
    """
    chain = ''                      # the current chain
    residue = ''                    # the current residue
    residue_coordinates = []        # all coordinates of a residue
    all_chain_coordinates = []      # all coordinates of a chain

    with open(pdb_file_path) as pdbfile:
        for line in pdbfile:
            # Split the line in pieces, store them to a list
            if line[:4] == 'ATOM':  # or line[:6] == "HETATM":
                splitted_line = [line[:6], line[6:11], line[12:16],
                                 line[17:20], line[21], line[22:26],
                                 line[30:38], line[38:46], line[46:54]]

                # if splitted_line[4] != chain:
                #     chain = splitted_line[4]
                # else:
                #     pass

                # If var residue is empty, assign it.
                # This happens only for 1st iteration
                if not residue:
                    residue = splitted_line[5]
                # (Pythonic way of checking that something is empty)

                # Get the coordinates as tuple, convert to float and append
                # to that residue's list.
                if splitted_line[5] == residue:
                    residue_coordinates.append((float(splitted_line[6]),
                                                float(splitted_line[7]),
                                                float(splitted_line[8])))
                    # This one adds tuples for each non-first atom.

                else:
                    all_chain_coordinates.append(residue_coordinates)
                    residue = splitted_line[5]
                    residue_coordinates = []
                    residue_coordinates.append((float(splitted_line[6]),
                                                float(splitted_line[7]),
                                                float(splitted_line[8])))
                    # If we are here, a new residue has appeared:
                    # 1. Last's residue list of coordinates is appended to the
                    #    chain's coordinates.
                    # 2. residue_coordinates list is reset.
                    # 3. The first atom is added.
        all_chain_coordinates.append(residue_coordinates)