def parseVerbose(vf, mut):
    # parse the verbose file
    vfh = open(vf)

    contacts = {}
    cons_aa = {}
    con = None
    rotamerpairs = []

    for vfh_l in vfh:
        if vfh_l.startswith("crwdnes"):
            break
            # looking for contacts involving the central residue
        if vfh_l.startswith("contact"):

            if con != None:
                # first, dump the last contact
                if len(rotamerpairs) > 0:
                    contacts[con] = rotamerpairs
                con = None
                rotamerpairs = []

            items = vfh_l.strip().split()
            if mut.c + "," + str(mut.n) in items:
                cenfirst = 0
                if items[1] == mut.c + "," + str(mut.n):
                    cenfirst = 1
                contmp, con_aa = items[1 + cenfirst], items[4 + cenfirst]
                # only look at contact in the same chain
                if not contmp.startswith(mut.c):
                    continue
                else:
                    con = contmp
                    cons_aa[con] = con_aa

        elif con != None:  # reading rotamer pairs under the potential contacts
            items = vfh_l.strip().split()
            if items[0 + cenfirst * 3] == con_aa:
                if items[3 - cenfirst * 3] == PDB.s2t(mut.w):
                    rotamerpairs.append(mut.w + " " + vfh_l.strip().split()[-1])
                if items[3 - cenfirst * 3] == PDB.s2t(mut.m):
                    rotamerpairs.append(mut.m + " " + vfh_l.strip().split()[-1])
                # the last one can be left
    if len(rotamerpairs) > 0:
        contacts[con] = rotamerpairs

        # now go back to the beginning of file and read the denominator
        # first look at the denominator of the central residue
    vfh.seek(0)
    denominator_cen = []
    cen = False
    for vfh_l in vfh:
        if vfh_l.startswith("end of rotamer filtering"):
            break
        if vfh_l.startswith("position"):
            if cen == True:
                break
            if vfh_l.strip().split()[-1] == mut.c + "," + str(mut.n):
                cen = True
        elif vfh_l.startswith(PDB.s2t(mut.w)) or vfh_l.startswith(PDB.s2t(mut.m)):
            if cen == True:
                info = (
                    vfh_l.strip().split(":")[0]
                    + " "
                    + " ".join([x.strip().split()[1] for x in vfh_l.strip().split(":")[1].split(";")[:-1]])
                )
                denominator_cen.append(info)
    vfh.seek(0)

    denominator_cons = {}
    con = None
    for vfh_l in vfh:
        if vfh_l.startswith("end of rotamer filtering"):
            break
        if vfh_l.startswith("position"):
            if vfh_l.strip().split()[-1] in contacts:
                con = vfh_l.strip().split()[-1]
        elif con != None:
            if vfh_l.startswith(cons_aa[con]):
                info = (
                    vfh_l.strip().split(":")[0]
                    + " "
                    + " ".join([x.strip().split()[1] for x in vfh_l.strip().split(":")[1].split(";")[:-1]])
                )
                denominator_cons[con] = info
                con = None

    return contacts, denominator_cons, denominator_cen
Example #2
0
File: fpd.py Project: vancalory/PDZ
# start and end residue for the peptide chain
pepstart = p.numResidues() + 1
pepend = pepstart + pepchain.numResidues() -1

# add peptide backbone from template to receptor
rec = p + pepchain.copy()
writePDB('_start.pdb', rec)

if args.flip:
    # flip the structure
    os.system('perl -w '+ SELFBIN + '/flipPeptideChirality.pl _start.pdb _startf.pdb 0')

    # mutate the chirality of the residues on the domain
    domainseq = []
    for dres in p.iterResidues():
        dresname = PDB.s2t(PDB.t2s(dres.getResname()))
        if dresname != 'GLY':
            domainseq.append('D'+ dresname)
        else:
            domainseq.append(dresname)
    pdz.normalMut('_startf.pdb', range(1, pepstart), domainseq, '_startf1.pdb')

    # mutate the residue on peptide
    pepseq = []
    for pres in args.pseq:
        if len(pres) == 4: # if should be a D-residue, make the name to be a L-residue (since the domain has been flipped)
            pepseq.append(pres[1:])
        if len(pres) == 3:
            if pres != 'GLY':
                pepseq.append('D' + pres)
            else:
def computeContacts(mut, contacts, denominator_cons, denominator_cen, outf):
    # for each contact, compute two separated values for wild type and mutant
    contacts_keys = [x for x in contacts.keys() if x.split(",")[1].isdigit()]
    contacts_keys = sorted(contacts_keys, key=lambda x: int(x.split(",")[1]))
    idx = 1
    outfh = open(outf, "w")
    normal_cmap = args.sdir + "/" + mut.p.lower() + ".clean.cmap"

    for con in contacts_keys:
        numerator_w = sum([float(x.split()[1]) for x in contacts[con] if x.split()[0] == mut.w])
        numerator_m = sum([float(x.split()[1]) for x in contacts[con] if x.split()[0] == mut.m])
        if numerator_w == 0:
            contactdegree_w = 0
        else:
            denominator_cen_w = [x for x in denominator_cen if x.split()[0] == PDB.s2t(mut.w)][0].split()[1:]
            denominator_con = denominator_cons[con].split()[1:]
            denominator_w = 0
            for p1, p2 in itertools.product(denominator_cen_w[1:], denominator_con[1:]):
                denominator_w += float(p1) * float(p2)
            denominator_w *= float(denominator_cen_w[0]) * float(denominator_con[0])
            contactdegree_w = numerator_w / denominator_w
        if numerator_m == 0:
            contactdegree_m = 0
        else:
            denominator_cen_m = [x for x in denominator_cen if x.split()[0] == PDB.s2t(mut.m)][0].split()[1:]
            denominator_con = denominator_cons[con].split()[1:]
            denominator_m = 0
            for p1, p2 in itertools.product(denominator_cen_m[1:], denominator_con[1:]):
                denominator_m += float(p1) * float(p2)
            denominator_m *= float(denominator_cen_m[0]) * float(denominator_con[0])
            contactdegree_m = numerator_m / denominator_m
        contactdegree_w, contactdegree_m = format(contactdegree_w, ".4f"), format(contactdegree_m, ".4f")

        # also add normal contact degree between these two position:
        normal_cond = Fragment.getConD(normal_cmap, mut.c + "," + str(mut.n), con)
        normal_cond = format(normal_cond, ".4f")

        outstring = (
            "\t".join(
                map(
                    str,
                    [
                        idx,
                        mut.p,
                        mut.c + "," + str(mut.n),
                        con,
                        contactdegree_w,
                        contactdegree_m,
                        normal_cond,
                        denominator_cons[con].split()[0],
                    ],
                )
            )
            + "\n"
        )
        idx += 1
        outfh.write(outstring)

        # there could be some contacts which are not detected by two side chains
    normal_cons, normal_conress = Fragment.contactList(normal_cmap, mut.c, mut.n, dcut=0.01)
    for k in range(len(normal_cons)):
        if normal_cons[k] in contacts_keys:  # already indexed contact
            continue
        else:
            normal_cond = Fragment.getConD(normal_cmap, mut.c + "," + str(mut.n), normal_cons[k])
            normal_cond = format(normal_cond, ".4f")
            outstring = (
                "\t".join(
                    map(
                        str,
                        [
                            idx,
                            mut.p,
                            mut.c + "," + str(mut.n),
                            normal_cons[k],
                            0.0000,
                            0.0000,
                            normal_cond,
                            normal_conress[k],
                        ],
                    )
                )
                + "\n"
            )
            idx += 1
            outfh.write(outstring)

            # it would be also nice to have permanent contact, but they are rare and probably captured by environment

    outfh.close()