<commit_before>import tempfile, os, sys, commands, re, math

"""
Routines to rename atoms and residues from a 'labeled' step2_out.pdb file  ("0","+", or "-" tacked on the end) 

REVISION LOG
- 8-21-2007: VAV; spun these functions into their own python file 
- 10-10-2007: DLM: re-incorporating the proper handling of cysteine residues renamed CYD by MCCE which need to be renamed CYS2.
- 6-30-2009: DLM, additional bugfix relating to naming of CYS2, in this case it was messing up pdb formatting.
- 6-30-2009: DLM, additional bugfix relating to hydrogen and CD naming for NILE/CILE
- 7-1-2009: DLM, bugfix relating to handling of insertion ids; in cases where two consecutive residues (one of which had an insertion code but the same residue number) were the same amino acid, one residue would be omitted from the output, resulting in a consecutive sequence but a missing residue in the structure. Also fixed problem with naming of a hydrogen on GLH. 
- 8-19-2009: DLM, fixed a bug introduced at the last revision, wherein some N-terminal residues, when capped, would be split in two due to having two atoms with the same name. This had been introduced when fixing the insertion code bug.


TO DO:
- See description of disulfide_search function -- need to fix naming for CYD residues that are not disulfide bonded.
"""


def rename_residues(pdbarr, renameTermini=True):
    """Convert residue names (and some heavy atom and hydrogen names) to AMBER (ffamber) format.
    
    ARGUMENTS
        pdbarr - array of PDB atoms
        
    RETURNS
        pdbarr - updated array of PDB atoms with AMBER appropriate residue names
    """

    
    # DEBUG check.
    print "At start of renaming, pdbarr has",len(pdbarr),"items"
     
    # Transform the PDB file into a nested format that is easier to manipulate.
    npdb = nest_pdb(pdbarr)
    
    # DEBUG check.
    print "Nest/unnest leads to",len(unnest_pdb(npdb)),"items"

    # Convert residue and atom names to be appropriate for AMBER.
    npdb = rename_charged(npdb)
    npdb = histidine_search(npdb)
    npdb = disulfide_search(npdb)
    if (renameTermini):
        npdb = rename_termini(npdb)

    # Un-nest PDB
    pdbarr = unnest_pdb(npdb)
    
    # DEBUG check.
    #print "At end of renaming, pdbarr has ", len(pdbarr), "items"

    # Return the PDB file.
    return pdbarr

    


def rename_charged(npdb):
    """Generate AMBER-specific residue names for charged residues from MCCE residue names. Also fix some problems with atom naming (specifically hydrogens) for charged residues.
        
    ARGUMENTS
        npdb - "nested" PDB data structure generated by nest_pdb. Modified to reflect AMBER names.

    CHANGE LOG:
    - DLM 7-1-2009: Modified to fix naming of HE1 in GLH to HE2 to conform to ffamber rtp.
    """
        
    resname_and_state = map(lambda x:x[-1][17:20]+x[-1][-1],npdb)
    #print resname_and_state
    for i in range(len(npdb)):
        if (resname_and_state[i]=='HIS+'):
            npdb[i]=map(lambda x:x.replace('HIS','HIP'),npdb[i])
        elif (resname_and_state[i]=='LYS0'):
            npdb[i]=map(lambda x:x.replace('LYS','LYN'),npdb[i])
        elif (resname_and_state[i]=='LYS+'):
            npdb[i]=map(lambda x:x.replace('LYS','LYP'),npdb[i])
        elif (resname_and_state[i]=='CYS0'):
            npdb[i]=map(lambda x:x.replace('CYS','CYN'),npdb[i])
        elif (resname_and_state[i]=='CYS-'):
            npdb[i]=map(lambda x:x.replace('CYS','CYM'),npdb[i])
        elif (resname_and_state[i]=='ASP0'):
            npdb[i]=map(lambda x:x.replace('ASP','ASH'),npdb[i])
        #Aspartate requires no sub
        elif (resname_and_state[i]=='GLU0'):
            npdb[i]=map(lambda x:x.replace('GLU','GLH'),npdb[i])
            #DLM 7-1-2009: Fix GLY HE1 to HE2
            npdb[i] = map(lambda x:x.replace('HE1 GLH','HE2 GLH'),npdb[i])
        # Glutamate requires no sub


    return npdb


def labeledPDB_to_AmberPDB(labeledPDBfile, outPDBfile, renameResidues=True):
    """Reads a 'labeled' PDB file written by MCCE (with "0","+", or "-" indicating the protonation state - see mcce.py)
    Example:
    ATOM      1  CA  LEU _0001_000  -8.847   7.195  16.727   1.700       0.000      BK____M000
    0
    ATOM      2  C   LEU _0001_000  -9.019   7.821  15.386   1.700       0.000      BK____M000
    0
    ....

    Writes an AMBER-residue-formatted PDB file, outPDBfile.
    """

    # Read in the pdb lines
    fin = open(labeledPDBfile,'r')
    lines = fin.readlines()
    fin.close()
    # Format so the even-numbered lines, "0", "+", "-", etc are supposed to be tacked onto the ends of each atom's line<commit_msg>Fix to hydrogen naming for ASH<commit_after>import tempfile, os, sys, commands, re, math

"""
Routines to rename atoms and residues from a 'labeled' step2_out.pdb file  ("0","+", or "-" tacked on the end) 

REVISION LOG
- 8-21-2007: VAV; spun these functions into their own python file 
- 10-10-2007: DLM: re-incorporating the proper handling of cysteine residues renamed CYD by MCCE which need to be renamed CYS2.
- 6-30-2009: DLM, additional bugfix relating to naming of CYS2, in this case it was messing up pdb formatting.
- 6-30-2009: DLM, additional bugfix relating to hydrogen and CD naming for NILE/CILE
- 7-1-2009: DLM, bugfix relating to handling of insertion ids; in cases where two consecutive residues (one of which had an insertion code but the same residue number) were the same amino acid, one residue would be omitted from the output, resulting in a consecutive sequence but a missing residue in the structure. Also fixed problem with naming of a hydrogen on GLH. 
- 8-19-2009: DLM, fixed a bug introduced at the last revision, wherein some N-terminal residues, when capped, would be split in two due to having two atoms with the same name. This had been introduced when fixing the insertion code bug.
- 8/25-2009: DLM, fixed hydrogen naming problem for ASH, wherein hydrogen needed to be named HD2, not HD1.

TO DO:
- See description of disulfide_search function -- need to fix naming for CYD residues that are not disulfide bonded.
"""


def rename_residues(pdbarr, renameTermini=True):
    """Convert residue names (and some heavy atom and hydrogen names) to AMBER (ffamber) format.
    
    ARGUMENTS
        pdbarr - array of PDB atoms
        
    RETURNS
        pdbarr - updated array of PDB atoms with AMBER appropriate residue names
    """

    
    # DEBUG check.
    print "At start of renaming, pdbarr has",len(pdbarr),"items"
     
    # Transform the PDB file into a nested format that is easier to manipulate.
    npdb = nest_pdb(pdbarr)
    
    # DEBUG check.
    print "Nest/unnest leads to",len(unnest_pdb(npdb)),"items"

    # Convert residue and atom names to be appropriate for AMBER.
    npdb = rename_charged(npdb)
    npdb = histidine_search(npdb)
    npdb = disulfide_search(npdb)
    if (renameTermini):
        npdb = rename_termini(npdb)

    # Un-nest PDB
    pdbarr = unnest_pdb(npdb)
    
    # DEBUG check.
    #print "At end of renaming, pdbarr has ", len(pdbarr), "items"

    # Return the PDB file.
    return pdbarr

    


def rename_charged(npdb):
    """Generate AMBER-specific residue names for charged residues from MCCE residue names. Also fix some problems with atom naming (specifically hydrogens) for charged residues.
        
    ARGUMENTS
        npdb - "nested" PDB data structure generated by nest_pdb. Modified to reflect AMBER names.

    CHANGE LOG:
    - DLM 7-1-2009: Modified to fix naming of HE1 in GLH to HE2 to conform to ffamber rtp.
    """
        
    resname_and_state = map(lambda x:x[-1][17:20]+x[-1][-1],npdb)
    #print resname_and_state
    for i in range(len(npdb)):
        if (resname_and_state[i]=='HIS+'):
            npdb[i]=map(lambda x:x.replace('HIS','HIP'),npdb[i])
        elif (resname_and_state[i]=='LYS0'):
            npdb[i]=map(lambda x:x.replace('LYS','LYN'),npdb[i])
        elif (resname_and_state[i]=='LYS+'):
            npdb[i]=map(lambda x:x.replace('LYS','LYP'),npdb[i])
        elif (resname_and_state[i]=='CYS0'):
            npdb[i]=map(lambda x:x.replace('CYS','CYN'),npdb[i])
        elif (resname_and_state[i]=='CYS-'):
            npdb[i]=map(lambda x:x.replace('CYS','CYM'),npdb[i])
        elif (resname_and_state[i]=='ASP0'):
            npdb[i]=map(lambda x:x.replace('ASP','ASH'),npdb[i])
            #DLM 8/25/2009: ASH needs to have HD2, not HD1, for some reason
            npdb[i]=map(lambda x:x.replace('HD1 ASH','HD2 ASH'),npdb[i])
        #Aspartate requires no sub
        elif (resname_and_state[i]=='GLU0'):
            npdb[i]=map(lambda x:x.replace('GLU','GLH'),npdb[i])
            #DLM 7-1-2009: Fix GLY HE1 to HE2
            npdb[i] = map(lambda x:x.replace('HE1 GLH','HE2 GLH'),npdb[i])
        # Glutamate requires no sub


    return npdb


def labeledPDB_to_AmberPDB(labeledPDBfile, outPDBfile, renameResidues=True):
    """Reads a 'labeled' PDB file written by MCCE (with "0","+", or "-" indicating the protonation state - see mcce.py)
    Example:
    ATOM      1  CA  LEU _0001_000  -8.847   7.195  16.727   1.700       0.000      BK____M000
    0
    ATOM      2  C   LEU _0001_000  -9.019   7.821  15.386   1.700       0.000      BK____M000
    0
    ....

    Writes an AMBER-residue-formatted PDB file, outPDBfile.
    """

    # Read in the pdb lines
    fin = open(labeledPDBfile,'r')
    lines = fin.readlines()
    fin.close()
    # Format so the even-numbered lines, "0", "+", "-", etc are supposed to be tacked onto the ends of each atom's line
{'fraction_non_alphanumeric': 0.10816198069660092, 'fraction_numerical': 0.03210239194292908, 'mean_word_length': 2.968776019983347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1635553', 'n_tokens_mistral': 3486, 'n_tokens_neox': 3117, 'n_words': 1057}