#!/usr/bin/env python3
import subprocess
import os
import numpy as np
import pandas as pd
from contextlib import suppress
import re
import csv

def getInteractions(filename):
    data = pd.read_csv(filename, index_col=0, header =0, sep="\t")
    contactList = getIndexes(data,1)
    print(contactList)
    number = len(contactList)
    return number

def formatMuts(mut_file,pdbname):  
    with open(mut_file) as csvfile:
        readCSV = csv.reader(csvfile)
        muts = []
        for row in readCSV:
                mut = row[0]
                muts.append(mut)
        
    mut_list = []
    outfile = "/home/tanu/git/LSHTM_analysis/foldx/test2/individual_list_"+pdbname+".txt"
    with open(outfile, "w") as output:
        for m in muts:
                print(m)
                mut = m[:1]+'A'+m[1:]
                mut_list.append(mut)
                mut = mut + ";"
                print(mut)
                output.write(mut)
                output.write("\n")
    return mut_list

def getIndexes(data, value):
    colnames = data.columns.values
    listOfPos = list()
    result = data.isin([value])
    result.columns=colnames
    seriesdata = result.any()
    columnNames = list(seriesdata[seriesdata==True].index)
    for col in columnNames:
        rows = list(result[col][result[col]==True].index)
        
        for row in rows:
            listOfPos.append((row,col))
    
    return listOfPos

def loadFiles(df):
    # load a text file in to np matrix
    resultList = []
    f = open(df,'r')
    for line in f:
        line = line.rstrip('\n')
        aVals = line.split("\t")
        fVals = list(map(np.float32, sVals))
        resultList.append(fVals)
    f.close()
    return np.asarray(resultList, dtype=np.float32)
    
#=======================================================================
def main():
    pdbname = '3pl1'
    mut_filename = "pnca_muts_sample.csv"
    mutlist = formatMuts(mut_filename, pdbname)

    print(mutlist)
    nmuts = len(mutlist)+1
    print(nmuts)
    print(mutlist)
    print("start")

    output = subprocess.check_output(['bash', 'runfoldx.sh', pdbname])
    print("end")
    for n in range(1,nmuts):
        print(n)
        with suppress(Exception):
            subprocess.check_output(['bash', 'runPrintNetworks.sh', pdbname,str(n)])
        
    for n in range(1,nmuts):
        print(n)
        with suppress(Exception):
            subprocess.check_output(['bash', 'mutrenamefiles.sh', pdbname,str(n)])

            
    out = subprocess.check_output(['bash','renamefiles.sh',pdbname])
    
    dGdatafile = "/home/tanu/git/LSHTM_analysis/foldx/test2/Dif_"+pdbname+"_Repair.txt"
    dGdata = pd.read_csv(dGdatafile, sep="\t")
    print(dGdata)
    ddG=[]
    for i in range(0,len(dGdata)):
        ddG.append(dGdata['total energy'].loc[i])
    print(ddG)
    distfile = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Distances_"+pdbname+"_Repair_PN.txt"
    wt_nc = getInteractions(distfile)
    
    elecfileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Electro_RR_"+pdbname+"_Repair_PN.txt"
    wt_neRR = getInteractions(elecfileRR)

    elecfileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Electro_MM_"+pdbname+"_Repair_PN.txt"
    wt_neMM = getInteractions(elecfileMM)
    
    elecfileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Electro_SM_"+pdbname+"_Repair_PN.txt"
    wt_neSM = getInteractions(elecfileSM)

    elecfileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Electro_SS_"+pdbname+"_Repair_PN.txt"
    wt_neSS = getInteractions(elecfileSS)

    disufileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Disulfide_RR_"+pdbname+"_Repair_PN.txt"
    wt_ndRR = getInteractions(disufileRR)

    disufileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Disulfide_MM_"+pdbname+"_Repair_PN.txt"
    wt_ndMM = getInteractions(disufileMM)

    disufileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Disulfide_SM_"+pdbname+"_Repair_PN.txt"
    wt_ndSM = getInteractions(disufileSM)

    disufileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Disulfide_SS_"+pdbname+"_Repair_PN.txt"
    wt_ndSS = getInteractions(disufileSS)

    hbndfileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Hbonds_RR_"+pdbname+"_Repair_PN.txt"
    wt_nhRR = getInteractions(hbndfileRR)

    hbndfileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Hbonds_MM_"+pdbname+"_Repair_PN.txt"
    wt_nhMM = getInteractions(hbndfileMM)

    hbndfileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Hbonds_SM_"+pdbname+"_Repair_PN.txt"
    wt_nhSM = getInteractions(hbndfileSM)

    hbndfileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Hbonds_SS_"+pdbname+"_Repair_PN.txt"
    wt_nhSS = getInteractions(hbndfileSS)

    partfileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Partcov_RR_"+pdbname+"_Repair_PN.txt"
    wt_npRR = getInteractions(partfileRR)

    partfileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Partcov_MM_"+pdbname+"_Repair_PN.txt"
    wt_npMM = getInteractions(partfileMM)

    partfileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Partcov_SM_"+pdbname+"_Repair_PN.txt"
    wt_npSM = getInteractions(partfileSM)

    partfileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Partcov_SS_"+pdbname+"_Repair_PN.txt"
    wt_npSS = getInteractions(partfileSS)

    vdwcfileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_VdWClashes_RR_"+pdbname+"_Repair_PN.txt"
    wt_nvRR = getInteractions(vdwcfileRR)
  
    vdwcfileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_VdWClashes_MM_"+pdbname+"_Repair_PN.txt"
    wt_nvMM = getInteractions(vdwcfileMM)

    vdwcfileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_VdWClashes_SM_"+pdbname+"_Repair_PN.txt"
    wt_nvSM = getInteractions(vdwcfileSM)

    vdwcfileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_VdWClashes_SS_"+pdbname+"_Repair_PN.txt"
    wt_nvSS = getInteractions(vdwcfileSS)

    volufileRR = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Volumetric_RR_"+pdbname+"_Repair_PN.txt"
    wt_nvoRR = getInteractions(volufileRR)

    volufileMM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Volumetric_MM_"+pdbname+"_Repair_PN.txt"
    wt_nvoMM = getInteractions(volufileMM)

    volufileSM = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Volumetric_SM_"+pdbname+"_Repair_PN.txt"
    wt_nvoSM = getInteractions(volufileSM)

    volufileSS = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Volumetric_SS_"+pdbname+"_Repair_PN.txt"
    wt_nvoSS = getInteractions(volufileSS)

    dnc = []
    dneRR = []
    dneMM = []
    dneSM = []
    dneSS = [] 
    dndRR = []
    dndMM = []
    dndSM = []
    dndSS = []
    dnhRR = []
    dnhMM = []
    dnhSM = []
    dnhSS = []
    dnpRR = []
    dnpMM = []
    dnpSM = []
    dnpSS = []
    dnvRR = []
    dnvMM = []
    dnvSM = []
    dnvSS = []
    dnvoRR = []
    dnvoMM = []
    dnvoSM = []
    dnvoSS = []
    for n in range(1, nmuts):
        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Distances_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_nc = getInteractions(filename)
        diffc = wt_nc - mut_nc
        dnc.append(diffc)

        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Electro_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_neRR = getInteractions(filename)
        diffeRR = wt_neRR - mut_neRR
        dneRR.append(diffeRR)

        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Disulfide_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_ndRR = getInteractions(filename)
        diffdRR = wt_ndRR - mut_ndRR
        dndRR.append(diffdRR)
 
        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Hbonds_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_nhRR = getInteractions(filename)
        diffhRR = wt_nhRR - mut_nhRR
        dnhRR.append(diffhRR)
        
        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Partcov_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_npRR = getInteractions(filename)
        diffpRR = wt_npRR - mut_npRR
        dnpRR.append(diffpRR)

        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_VdWClashes_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_nvRR = getInteractions(filename)
        diffvRR = wt_nvRR - mut_nvRR
        dnvRR.append(diffvRR)

        filename = "/home/tanu/git/LSHTM_analysis/foldx/test2/Matrix_Volumetric_RR_"+pdbname+"_Repair_" + str(n)+"_PN.txt"
        mut_nvoRR = getInteractions(filename)
        diffvoRR = wt_nvoRR - mut_nvoRR
        dnvoRR.append(diffvoRR)
    print(dnc)
    print(dneRR)
    print(dndRR)
    print(dnhRR)
    print(dnpRR)
    print(dnvRR)
    print(dnvoRR)

    results = pd.DataFrame([(ddG),(dnc),(dneRR),(dndRR),(dnhRR),(dnpRR),(dnvRR),(dnvoRR)], columns=mutlist, index=["ddG","contacts","electro","disulfide","hbonds","partcov","VdWClashes","volumetric"])
    results.append(ddG)
    print(results)
    results2 = results.T # transpose df
    outputfilename = "foldx_results_"+pdbname+".csv"
#    results.to_csv(outputfilename)
    results2.to_csv(outputfilename)
if __name__ == "__main__":
    main()
