<commit_before>#!/usr/bin/env python

"""
Detects gaps in PDB files (sequence/distance). Works only for
protein molecules and segments.

usage: python pdb_gap.py <pdb file>
example: python pdb_gap.py 1CTF.pdb

Author: {0} ({1})

This program is part of the PDB tools distributed with HADDOCK
or with the HADDOCK tutorial. The utilities in this package
can be used to quickly manipulate PDB files, with the benefit
of 'piping' several different commands. This is a rewrite of old
FORTRAN77 code that was taking too much effort to compile. RIP.
"""

from __future__ import print_function

import os
import re
import sys

__author__ = "<NAME>"
__email__ = "<EMAIL>"

USAGE = __doc__.format(__author__, __email__)

def check_input(args):
    """Checks whether to read from stdin/file and validates user input/options."""

    if not len(args):
        # Read from pipe
        if not sys.stdin.isatty():
            pdbfh = sys.stdin
        else:
            sys.stderr.write(USAGE)
            sys.exit(1)
    elif len(args) == 1:
        # File
        if not os.path.isfile(args[0]):
            sys.stderr.write('File not found: ' + args[0] + '\n')
            sys.stderr.write(USAGE)
            sys.exit(1)
        pdbfh = open(args[0], 'r')
    else:
        sys.stderr.write(USAGE)
        sys.exit(1)

    return pdbfh

def _check_structure_gaps(fhandle):
    """Enclosing logic in a function"""

    _centroid = ' CA ' # respect spacing. 'CA  ' != ' CA '
    _distance_threshold = 4.0

    def _calc_distance(i, j):
        return ( (j[0]-i[0])**2 + (j[1]-i[1])**2 + (j[2]-i[2])**2 )**0.5

    coord_re = re.compile('^(ATOM|HETATM)')
    prev_at = (None, None, None, None, (None, None, None))
    for line in fhandle:
        line = line.strip()
        if coord_re.match(line):
            aname = line[12:16]
            if aname != _centroid:
                continue

            resn = line[17:20]
            resi = int(line[22:26])
            chain = line[21]
            x, y, z = float(line[30:38]), \
                      float(line[38:46]), \
                      float(line[46:54])

            at_uid = (chain, resi, resn, aname, (x,y,z))
            if prev_at[0] == at_uid[0]:
                d = _calc_distance(at_uid[4], prev_at[4])
                if d > _distance_threshold:
                    print("{0[0]}:{0[2]}{0[1]} < {2:7.2f}A > {1[0]}:{1[2]}{1[1]}".format(prev_at, at_uid, d))
                elif prev_at[1] + 1 != at_uid[1]:
                    print("{0[0]}:{0[2]}{0[1]} < Seq. Gap > {1[0]}:{1[2]}{1[1]}".format(prev_at, at_uid, d))

            prev_at = at_uid

if __name__ == '__main__':

    # Check Input
    pdbfh = check_input(sys.argv[1:])

    # Do the job
    _check_structure_gaps(pdbfh)

    # last line of the script
    # We can close it even if it is sys.stdin
    pdbfh.close()
    sys.exit(0)<commit_msg>Corrected gap tracking over multi-model PDBS<commit_after>#!/usr/bin/env python

"""
Detects gaps in PDB files (sequence/distance). Works only for
protein molecules and segments.

usage: python pdb_gap.py <pdb file>
example: python pdb_gap.py 1CTF.pdb

Author: {0} ({1})

This program is part of the PDB tools distributed with HADDOCK
or with the HADDOCK tutorial. The utilities in this package
can be used to quickly manipulate PDB files, with the benefit
of 'piping' several different commands. This is a rewrite of old
FORTRAN77 code that was taking too much effort to compile. RIP.
"""

from __future__ import print_function

import os
import sys

__author__ = "<NAME>"
__email__ = "<EMAIL>"

USAGE = __doc__.format(__author__, __email__)

def check_input(args):
    """Checks whether to read from stdin/file and validates user input/options."""

    if not len(args):
        # Read from pipe
        if not sys.stdin.isatty():
            pdbfh = sys.stdin
        else:
            sys.stderr.write(USAGE)
            sys.exit(1)
    elif len(args) == 1:
        # File
        if not os.path.isfile(args[0]):
            sys.stderr.write('File not found: ' + args[0] + '\n')
            sys.stderr.write(USAGE)
            sys.exit(1)
        pdbfh = open(args[0], 'r')
    else:
        sys.stderr.write(USAGE)
        sys.exit(1)

    return pdbfh

def _check_structure_gaps(fhandle):
    """Enclosing logic in a function"""

    _centroid = ' CA ' # respect spacing. 'CA  ' != ' CA '
    _distance_threshold = 4.0

    def _calc_distance(i, j):
        return ( (j[0]-i[0])**2 + (j[1]-i[1])**2 + (j[2]-i[2])**2 )**0.5

    prev_at = (None, None, None, None, (None, None, None))
    for line in fhandle:
        line = line.strip()
        if line.startswith('MODEL'):
            model = int(line[10:14])

        elif line.startswith(('ATOM', 'HETATM')):
            aname = line[12:16]
            if aname != _centroid:
                continue

            resn = line[17:20]
            resi = int(line[22:26])
            chain = line[21]
            x, y, z = float(line[30:38]), \
                      float(line[38:46]), \
                      float(line[46:54])

            at_uid = (model, chain, resi, resn, aname, (x,y,z))
            if prev_at[0] == at_uid[0] and prev_at[1] == at_uid[1]:
                d = _calc_distance(at_uid[5], prev_at[5])
                if d > _distance_threshold:
                    print("{0[1]}:{0[3]}{0[2]} < {2:7.2f}A > {1[1]}:{1[3]}{1[2]}".format(prev_at, at_uid, d))
                elif prev_at[2] + 1 != at_uid[2]:
                    print("{0[1]}:{0[3]}{0[2]} < Seq. Gap > {1[1]}:{1[3]}{1[2]}".format(prev_at, at_uid, d))

            prev_at = at_uid

if __name__ == '__main__':

    # Check Input
    pdbfh = check_input(sys.argv[1:])

    # Do the job
    _check_structure_gaps(pdbfh)

    # last line of the script
    # We can close it even if it is sys.stdin
    pdbfh.close()
    sys.exit(0)
{'fraction_non_alphanumeric': 0.1433230346721593, 'fraction_numerical': 0.030895983522142123, 'mean_word_length': 2.226467331118494, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2422538', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2032, 'n_words': 537}