from os import chdir
from glob import glob
from sklearn.model_selection import train_test_split
from numpy import append
from scipy.sparse import vstack

from sklearn.datasets import load_svmlight_file
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import RandomizedLogisticRegression
from sklearn.model_selection import LeaveOneGroupOut, RandomizedSearchCV
from scipy.stats import expon
from sklearn.svm import SVC, LinearSVC

from math import copysign
import numpy as np
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score, matthews_corrcoef, confusion_matrix, roc_curve, auc
from scipy import interp
import matplotlib.pyplot as plt
from itertools import cycle

def import_descriptors(path, file_wildcard, size=None):
    chdir(path)
    X = y = None
    labels = []
    files = glob(file_wildcard)
    for f in sorted(files):
        X_f, y_f = load_svmlight_file(f, zero_based=False)
        if size is not None :
            X_f, _, y_f, _ = train_test_split(X_f, y_f, train_size=size, stratify = y_f)
        if X is None:
            X = X_f
            y = y_f
        else :
            X = vstack([X, X_f], "csr")
            y = append(y, y_f)
        current_label = f[:4]
        labels += [current_label] * y_f.size
    return (X, y, labels)


class ModelSVM:
    kernel = "" 
    features = []
    C = -1
    gamma = -1
    degree = -1
    coef0 = -1

    def __init__(self, kernel, features, C = 0, gamma = 0, coef0 = 0, degree = 0):
        self.kernel = kernel
        self.features = features
        self.C = C
        self.gamma = gamma
        self.degree = degree
        self.coef0 = coef0

def test_interface_prediction_svm(training_set_path, test_set_path, cat, it, bu, svm_model):
    print "Importing descriptors from the training set."
    X, y, _ = import_descriptors(training_set_path, "*_" + it + "_" + bu + "_train_descriptors_N20.txt")
    
    print "Scaling data."
    from sklearn.preprocessing import MinMaxScaler
    min_max_scaler = MinMaxScaler()
    X_scale = min_max_scaler.fit_transform(X.todense())

    print "Performing feature selection."
    print ("Total number of features: %d." % X.shape[-1])
    X_scale = X_scale[:, [x - 1 for x in svm_model.features]]
    print ("Reduced number of features: %d." % X_scale.shape[-1])

    print "Building SVM."
    if (svm_model.kernel == "linear") :
        svm_best = LinearSVC(C=svm_model.C)
    else :
        svm_best = SVC(kernel=svm_model.kernel, C=svm_model.C, gamma=svm_model.gamma, degree=svm_model.degree, coef0=svm_model.coef0)

    svm_best.fit(X_scale, y)
    
    print "Loading test set."
    X_test, y_test, labels_test = import_descriptors(test_set_path, "*_" + it + "_" + bu + "_test_descriptors_N20.txt")
    print "Scaling data."
    X_test_scale = min_max_scaler.transform(X_test.todense())
    print "Feature selection."
    X_test_scale = X_test_scale[:, [x - 1 for x in svm_model.features]]
    
    print "Predicting the test set."
    y_score = svm_best.decision_function(X_test_scale)
    y_pred =  np.array([copysign(1, ys) for ys in y_score])

    outname = ("%stest_predictions_%s_%s_%s_N20.csv" % (test_set_path, cat, it, bu))
    test_scores = output_prediction_scores(y_test, y_pred, y_score, labels_test, outname)

    print "\n"
    print ("scores_pred[(\"%s\", \"%s\", \"%s\")] = \"%s\"" % (cat, it, bu, test_scores))
    with open("/media/sebastian/Storage/3DZD_interface_prediction_db5/benchmark5/fpr_tpr_scores.txt", "a") as output_file :
        output_file.write("scores_pred[(\"%s\", \"%s\", \"%s\")] = \"%s\"\n" % (cat, it, bu, test_scores))

def output_prediction_scores(y, y_pred, y_score, labels, outname):
    tp = {}
    tn = {}
    fp = {}
    fn = {}
    matthews_scores = {}
    accuracy_scores = {}
    f1_scores = {}
    precisions = {}
    recalls = {}
    
    fpr = {}
    tpr = {}
    thresholds = {}
    roc_auc = {}
    get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]

    pdb_ids = sorted(set(labels))
    for l in pdb_ids :
        l_indices = get_indexes(l, labels)
        f1_scores[l] = f1_score(y[l_indices], y_pred[l_indices])
        precisions[l] = precision_score(y[l_indices], y_pred[l_indices])
        recalls[l] = recall_score(y[l_indices], y_pred[l_indices])
        accuracy_scores[l] = accuracy_score(y[l_indices], y_pred[l_indices])
        matthews_scores[l] = matthews_corrcoef(y[l_indices], y_pred[l_indices])
        cm = confusion_matrix(y[l_indices], y_pred[l_indices])
        tn[l] = cm[0][0]
        fn[l] = cm[1][0]
        tp[l] = cm[1][1]
        fp[l] = cm[0][1]
        
        fpr[l], tpr[l], thresholds[l] = roc_curve(y[l_indices], y_score[l_indices])
        roc_auc[l] = auc(fpr[l], tpr[l])
        
    print "Prediction results:"
    print ("-"*255)
    with open(outname, "w") as outfile :
        outfile.write("PDB-ID;F1;precision;recall;accuracy;MCC;AUC_ROC;TP;TN;FP;FN\n")
        print "PDB-ID\tF1\tprecision\trecall\taccuracy\tMCC\tAUC_ROC\tTP\tTN\tFP\tFN"
        for l in pdb_ids :
            outfile.write("%s;%f;%f;%f;%f;%f;%f;%d;%d;%d;%d\n" % (l, f1_scores[l], precisions[l], recalls[l], accuracy_scores[l], matthews_scores[l], roc_auc[l], tp[l], tn[l], fp[l], fn[l]))
            print ("%s\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5d\t%5d\t%5d\t%5d" % (l, f1_scores[l], precisions[l], recalls[l], accuracy_scores[l], matthews_scores[l], roc_auc[l], tp[l], tn[l], fp[l], fn[l]))
 
    from numpy import mean, std
    mean_f1_score = mean(f1_scores.values())
    stdev_f1_score = std(f1_scores.values())
    mean_accuracy = mean(accuracy_scores.values())
    stdev_accuracy = std(accuracy_scores.values())        
    mean_precision = mean(precisions.values())
    stdev_precision = std(precisions.values())
    mean_recall = mean(recalls.values())
    stdev_recall = std(recalls.values())
    mean_matthews_score = mean(matthews_scores.values())
    stdev_matthews_score = mean(matthews_scores.values())
    mean_roc_auc = mean(roc_auc.values())
    stdev_roc_auc = std(roc_auc.values())
    return ("%5.5f (%5.5f) & %5.5f (%5.5f) & %5.5f (%5.5f) & %5.5f (%5.5f) & %5.5f (%5.5f) & %5.5f (%5.5f)" % (mean_f1_score, stdev_f1_score, mean_accuracy, stdev_accuracy, mean_precision, stdev_precision, mean_recall, stdev_recall, mean_matthews_score, stdev_matthews_score, mean_roc_auc, stdev_roc_auc))
 
 

kernel = {}
features = {}
C = {}
gamma ={}
degree = {}
coef0 = {}    
def make_svm_model(cat, it, bu):
    if (cat, it, bu) in degree :
        return ModelSVM(kernel[(cat, it, bu)], features[(cat, it, bu)], C[(cat, it, bu)], gamma[(cat, it, bu)], coef0[(cat, it, bu)], degree[(cat, it, bu)])
    elif (cat, it, bu) in coef0 :
        return ModelSVM(kernel[(cat, it, bu)], features[(cat, it, bu)], C[(cat, it, bu)], gamma[(cat, it, bu)], coef0[(cat, it, bu)])
    elif (cat, it, bu) in gamma :
        return ModelSVM(kernel[(cat, it, bu)], features[(cat, it, bu)], C[(cat, it, bu)], gamma[(cat, it, bu)])
    else :
        return ModelSVM(kernel[(cat, it, bu)], features[(cat, it, bu)], C[(cat, it, bu)])

#################################################################################################################################


kernel[("A", "r", "u")] = "rbf"
C[("A", "r", "u")] = 1365.135008
gamma[("A", "r", "u")] = 0.000390
features[("A", "r", "u")] = [5, 10, 17, 21, 26, 37, 46, 50, 66, 130, 131, 147, 170, 211, 221, 222, 231, 242, 248, 252, 259, 266, 268, 271, 486, 491, 495, 497, 627, 630, 637, 677, 679, 685, 689, 694, 704, 705, 714, 725, 726, 730, 731, 732, 735, 736, 747, 756, 762, 763, 768, 774, 775, 781, 790, 807, 816, 825, 835, 836, 847, 849, 869, 879, 1000, 1042, 1098, 1100, 1101, 1105, 1138, 1145, 1153, 1161, 1189, 1199, 1210, 1212, 1213, 1214, 1240, 1242, 1246, 1248, 1252, 1257, 1259, 1266, 1274, 1282, 1291, 1300, 1310, 1320, 1330, 1331]

kernel[("A", "r", "b")] = "sigmoid"
C[("A", "r", "b")] = 495.329488
gamma[("A", "r", "b")] = 0.000537
coef0[("A", "r", "b")] = 1.444700
features[("A", "r", "b")] = [23, 28, 34, 37, 40, 77, 122, 147, 170, 177, 185, 193, 203, 204, 222, 485, 494, 497, 501, 505, 512, 518, 521, 534, 550, 556, 566, 586, 608, 611, 627, 637, 649, 660, 672, 676, 679, 715, 725, 736, 746, 751, 755, 756, 762, 768, 775, 776, 782, 789, 790, 797, 798, 806, 807, 815, 816, 825, 826, 835, 836, 846, 847, 850, 857, 1106, 1119, 1125, 1131, 1138, 1145, 1153, 1161, 1170, 1179, 1189, 1198, 1199, 1210, 1212, 1215, 1226, 1228, 1230, 1233, 1237, 1241, 1242, 1246, 1248, 1252, 1254, 1259, 1262, 1265, 1266, 1274, 1282, 1291, 1300, 1309, 1310, 1313, 1314, 1319, 1320, 1324, 1330, 1331]

kernel[("A", "l", "u")] = "linear"
C[("A", "l", "u")] = 221.638636
features[("A", "l", "u")] = [3, 13, 21, 32, 53, 74, 82, 112, 128, 130, 131, 133, 137, 138, 158, 249, 252, 365, 367, 368, 370, 380, 400, 485, 497, 499, 504, 509, 513, 537, 566, 580, 600, 608, 609, 611, 621, 627, 637, 643, 647, 649, 653, 654, 660, 663, 668, 679, 685, 695, 697, 705, 715, 717, 727, 749, 776, 848, 850, 860, 864, 865, 868, 870, 875, 881, 884, 890, 891, 905, 929, 942, 943, 959, 963, 971, 989, 1016, 1023, 1093, 1103, 1106, 1126, 1212, 1217]
 
kernel[("A", "l", "b")] = "linear"
C[("A", "l", "b")] = 46.052133
features[("A", "l", "b")] = [21, 32, 44, 63, 109, 131, 133, 138, 147, 158, 163, 170, 184, 220, 230, 241, 252, 365, 370, 389, 454, 485, 497, 499, 544, 545, 552, 571, 608, 609, 626, 627, 637, 646, 647, 649, 654, 660, 661, 663, 668, 676, 687, 704, 708, 714, 715, 717, 726, 727, 736, 739, 744, 749, 848, 849, 850, 860, 864, 867, 868, 870, 872, 879, 884, 890, 891, 917, 925, 928, 929, 934, 938, 950, 959, 971, 985, 989, 991, 1000, 1005, 1050, 1093, 1096, 1126, 1171, 1180, 1214, 1220]
 
kernel[("AB", "r", "u")] = "poly"
C[("AB", "r", "u")] = 426.469407
gamma[("AB", "r", "u")] = 0.011103
degree[("AB", "r", "u")] = 3
coef0[("AB", "r", "u")] = 0.015390
features[("AB", "r", "u")] = [15, 19, 21, 29, 33, 34, 35, 48, 75, 78, 80, 86, 87, 95, 96, 97, 98, 99, 102, 106, 107, 109, 110, 117, 118, 120, 365, 367, 393, 444, 453, 462, 472, 473, 494, 521, 566, 606, 611, 616, 637, 663, 672, 679, 681, 695, 708, 717, 735, 738, 741, 775, 781, 790, 807, 825, 826, 835, 836, 846, 847, 880, 881, 884, 897, 929, 933, 934, 937, 942, 943, 947, 949, 953, 956, 957, 968, 1214, 1215, 1216, 1217, 1219, 1220, 1222, 1244, 1246, 1250, 1252, 1259, 1262, 1264, 1266, 1270, 1272, 1274, 1278, 1280, 1281, 1282, 1286, 1291, 1295, 1300, 1310, 1313, 1319, 1320, 1331]
   
kernel[("AB", "r", "b")] = "poly"
C[("AB", "r", "b")] = 23.865514
gamma[("AB", "r", "b")] = 0.030064
degree[("AB", "r", "b")] = 2
coef0[("AB", "r", "b")] = 1.734644 
features[("AB", "r", "b")] = [2, 13, 15, 17, 19, 21, 24, 29, 33, 34, 69, 75, 78, 79, 80, 82, 83, 86, 96, 97, 98, 99, 102, 106, 107, 117, 118, 120, 393, 435, 444, 453, 463, 472, 473, 484, 521, 566, 586, 594, 611, 616, 636, 637, 649, 663, 665, 672, 708, 717, 735, 738, 741, 742, 745, 750, 775, 789, 790, 806, 807, 825, 826, 835, 836, 846, 847, 863, 864, 866, 876, 884, 911, 917, 928, 929, 933, 934, 937, 943, 947, 949, 953, 956, 957, 967, 968, 1096, 1214, 1216, 1217, 1219, 1220, 1221, 1222, 1235, 1244, 1246, 1248, 1250, 1252, 1255, 1257, 1258, 1259, 1262, 1264, 1266, 1270, 1272, 1273, 1281, 1282, 1300, 1313, 1319, 1320]
  
kernel[("AB", "l", "u")] = "poly"
C[("AB", "l", "u")] = 4362.448176
gamma[("AB", "l", "u")] = 0.034699
degree[("AB", "l", "u")] = 10
coef0[("AB", "l", "u")] = -0.036130
features[("AB", "l", "u")] = [6, 20, 37, 82, 83, 86, 88, 90, 96, 97, 102, 106, 108, 127, 130, 131, 139, 144, 147, 149, 151, 157, 193, 202, 211, 244, 262, 267, 290, 297, 305, 313, 365, 370, 410, 412, 445, 473, 505, 521, 528, 542, 596, 619, 637, 649, 663, 707, 717, 728, 877, 879, 883, 928, 933, 937, 944, 945, 949, 970, 975, 982, 1000, 1012, 1091, 1099, 1106, 1110, 1112, 1123, 1136, 1171, 1220, 1311, 1312]

kernel[("AB", "l", "b")] = "poly"
C[("AB", "l", "b")] = 2157.879900
gamma[("AB", "l", "b")] = 0.019062
degree[("AB", "l", "b")] = 7
coef0[("AB", "l", "b")] = 0.176144
features[("AB", "l", "b")] = [6, 9, 11, 12, 13, 26, 37, 82, 102, 127, 130, 131, 144, 147, 149, 154, 163, 244, 262, 267, 372, 375, 379, 380, 419, 427, 431, 486, 506, 516, 528, 532, 539, 596, 626, 627, 637, 649, 708, 717, 728, 749, 758, 849, 853, 856, 857, 860, 868, 873, 879, 884, 903, 913, 918, 919, 925, 928, 929, 933, 947, 968, 970, 975, 978, 1000, 1080, 1091, 1095, 1098, 1099, 1110, 1216, 1217, 1219, 1223, 1292, 1312]

kernel[("EI", "r", "u")] = "sigmoid"
C[("EI", "r", "u")] = 33.321428
gamma[("EI", "r", "u")] = 0.000291
coef0[("EI", "r", "u")] = -1.619529
features[("EI", "r", "u")] = [6, 13, 17, 69, 78, 82, 86, 96, 102, 117, 122, 138, 144, 147, 151, 158, 185, 193, 202, 211, 221, 242, 243, 364, 367, 369, 372, 376, 384, 386, 388, 393, 399, 405, 412, 424, 427, 432, 440, 444, 453, 463, 469, 471, 472, 473, 484, 490, 492, 497, 498, 505, 516, 522, 541, 606, 612, 619, 621, 623, 625, 627, 629, 630, 635, 636, 637, 643, 661, 663, 669, 672, 677, 679, 686, 687, 694, 695, 705, 706, 707, 708, 715, 717, 726, 730, 731, 734, 736, 743, 747, 755, 760, 767, 781, 792, 825, 828, 846, 851, 853, 860, 861, 868, 879, 904, 921, 949, 959, 964, 975, 982, 985, 989, 999, 1000, 1025, 1042, 1050, 1070, 1093, 1110, 1115, 1226, 1230, 1235, 1246, 1300, 1310]

kernel[("EI", "r", "b")] = "poly"
C[("EI", "r", "b")] = 1514.496836
gamma[("EI", "r", "b")] = 0.000032
degree[("EI", "r", "b")] = 3
coef0[("EI", "r", "b")] = -0.159223
features[("EI", "r", "b")] = [32, 38, 364, 367, 376, 412, 419, 424, 427, 431, 432, 435, 440, 441, 444, 445, 449, 453, 463, 473, 484, 490, 497, 501, 526, 538, 541, 553, 556, 566, 584, 586, 606, 607, 619, 621, 624, 625, 627, 630, 634, 635, 636, 637, 640, 641, 643, 653, 663, 669, 679, 686, 694, 695, 704, 705, 708, 713, 714, 715, 717, 725, 726, 730, 732, 739, 741, 743, 767, 768, 781, 809, 826, 828, 832, 835, 846, 851, 854, 861, 879, 905, 921, 929, 930, 949, 959, 972, 974, 985, 1000, 1034, 1070, 1090, 1093, 1110, 1112, 1171, 1219, 1226, 1230, 1235, 1246, 1282, 1291]

kernel[("EI", "l", "u")] = "poly"
C[("EI", "l", "u")] = 1916.019443
gamma[("EI", "l", "u")] = 0.015310
degree[("EI", "l", "u")] = 4
coef0[("EI", "l", "u")] = 0.138396
features[("EI", "l", "u")] = [2, 32, 38, 112, 138, 147, 186, 370, 375, 379, 419, 427, 490, 493, 494, 495, 497, 501, 516, 521, 538, 566, 586, 607, 608, 629, 630, 634, 637, 640, 663, 679, 704, 717, 725, 732, 733, 735, 736, 737, 849, 850, 851, 857, 860, 868, 879, 905, 921, 950, 959, 970, 978, 1000, 1096, 1115, 1171, 1211, 1230, 1231, 1232, 1235, 1240, 1241, 1252, 1259, 1266, 1274, 1282, 1284, 1286, 1290, 1291, 1299, 1300, 1309, 1310, 1320, 1322, 1331]

kernel[("EI", "l", "b")] = "sigmoid"
C[("EI", "l", "b")] = 213.154771
gamma[("EI", "l", "b")] = 0.000655
coef0[("EI", "l", "b")] = 0.472938
features[("EI", "l", "b")] = [2, 6, 11, 32, 38, 74, 230, 241, 375, 419, 427, 435, 487, 490, 494, 496, 497, 501, 516, 521, 526, 538, 540, 542, 566, 586, 605, 607, 625, 627, 629, 630, 634, 637, 638, 640, 643, 646, 663, 679, 685, 694, 704, 708, 714, 717, 720, 725, 732, 860, 879, 884, 949, 970, 988, 990, 1000, 1057, 1070, 1093, 1101, 1115, 1126, 1145, 1150, 1151, 1153, 1154, 1158, 1161, 1170, 1171, 1179, 1189, 1199, 1210, 1224, 1226, 1230, 1240, 1259, 1266, 1274, 1281, 1282, 1290, 1291, 1299, 1300, 1309, 1320]

kernel[("ER", "r", "u")] = "rbf"
C[("ER", "r", "u")] = 298.468355
gamma[("ER", "r", "u")] = 0.002222
features[("ER", "r", "u")] = [3, 17, 21, 32, 34, 37, 43, 44, 58, 81, 112, 223, 246, 406, 407, 487, 497, 516, 608, 611, 618, 626, 628, 633, 637, 638, 642, 648, 649, 663, 687, 717, 730, 850, 857, 859, 860, 862, 864, 868, 872, 875, 877, 879, 880, 881, 883, 884, 889, 919, 928, 929, 934, 937, 945, 953, 957, 964, 966, 968, 970, 973, 978, 981, 985, 987, 989, 992, 993, 996, 997, 998, 1001, 1002, 1005, 1045, 1046, 1047, 1049, 1050, 1054, 1055, 1056, 1064, 1066, 1091, 1093, 1099, 1101, 1106, 1114, 1117, 1122, 1123, 1125, 1126, 1128, 1129, 1131, 1136, 1154, 1171, 1179, 1199, 1210, 1212, 1214, 1215, 1216, 1222, 1228, 1230, 1233, 1252, 1259, 1263, 1264, 1265, 1266, 1274, 1277, 1294, 1310, 1320, 1329, 1331]

kernel[("ER", "r", "b")] = "rbf"
C[("ER", "r", "b")] = 9.216232 
gamma[("ER", "r", "b")] = 0.003664 
features[("ER", "r", "b")] = [13, 32, 37, 67, 74, 76, 112, 246, 366, 389, 406, 407, 421, 487, 497, 501, 505, 510, 514, 517, 518, 520, 521, 523, 525, 526, 534, 556, 558, 565, 566, 574, 583, 584, 594, 605, 618, 637, 642, 649, 663, 672, 679, 681, 687, 689, 707, 717, 736, 850, 857, 860, 864, 868, 873, 877, 879, 880, 881, 883, 884, 886, 889, 897, 905, 908, 921, 934, 949, 953, 957, 959, 978, 981, 1000, 1018, 1050, 1070, 1093, 1114, 1115, 1117, 1119, 1125, 1126, 1170, 1171, 1189, 1199, 1210, 1212, 1213, 1214, 1216, 1228, 1230, 1232, 1237, 1238, 1240, 1242, 1246, 1248, 1252, 1266, 1274, 1282, 1286, 1291, 1294, 1300, 1310, 1313, 1320, 1331]

kernel[("ER", "l", "u")] = "poly"
C[("ER", "l", "u")] = 1001.441519
gamma[("ER", "l", "u")] = 0.005968
degree[("ER", "l", "u")] = 5
coef0[("ER", "l", "u")] = 0.000386
features[("ER", "l", "u")] = [2, 14, 16, 21, 32, 38, 64, 74, 80, 177, 185, 193, 231, 242, 243, 244, 267, 291, 298, 306, 314, 323, 332, 342, 352, 363, 367, 369, 375, 376, 389, 400, 405, 409, 483, 484, 488, 490, 492, 505, 516, 518, 523, 531, 558, 586, 611, 614, 618, 620, 637, 642, 645, 652, 662, 671, 679, 682, 683, 687, 707, 713, 722, 726, 733, 782, 790, 851, 861, 879, 880, 881, 884, 921, 929, 949, 959, 969, 973, 975, 982, 989, 1000, 1080, 1095, 1101, 1126, 1131, 1135, 1171, 1209, 1210, 1211, 1212, 1266, 1274, 1282, 1300, 1310, 1331]

kernel[("ER", "l", "b")] = "sigmoid"
C[("ER", "l", "b")] = 157.317848
gamma[("ER", "l", "b")] = 0.000237
coef0[("ER", "l", "b")] = -0.242721
features[("ER", "l", "b")] = [3, 31, 32, 44, 92, 112, 130, 131, 147, 151, 157, 163, 170, 211, 244, 246, 262, 267, 272, 306, 314, 323, 352, 363, 367, 369, 372, 375, 389, 397, 399, 400, 403, 405, 419, 445, 459, 473, 489, 496, 497, 500, 501, 503, 505, 518, 521, 524, 526, 539, 550, 553, 556, 562, 563, 564, 566, 570, 571, 573, 580, 590, 618, 622, 637, 642, 748, 857, 864, 868, 879, 884, 913, 929, 959, 989, 990, 1000, 1095, 1098, 1099, 1101, 1115, 1123, 1129, 1131, 1145, 1161, 1167, 1171, 1175, 1185, 1212, 1214, 1240, 1252, 1282, 1291, 1320, 1331]

kernel[("ES", "r", "u")] = "linear"
C[("ES", "r", "u")] = 7010.356119
features[("ES", "r", "u")] = [47, 62, 90, 121, 130, 135, 221, 241, 244, 331, 341, 342, 351, 352, 362, 369, 372, 373, 376, 400, 410, 412, 415, 416, 417, 419, 424, 445, 487, 506, 516, 558, 576, 606, 615, 618, 621, 623, 637, 652, 659, 661, 666, 667, 674, 675, 677, 683, 687, 688, 695, 707, 726, 728, 735, 736, 743, 747, 763, 857, 864, 884, 892, 897, 901, 929, 949, 973, 1018, 1050, 1102, 1212, 1214, 1246, 1299, 1309, 1319, 1320, 1330]
 
kernel[("ES", "r", "b")] = "linear"
C[("ES", "r", "b")] = 196.846360
features[("ES", "r", "b")] = [10, 17, 46, 47, 49, 54, 61, 64, 69, 72, 79, 81, 86, 121, 372, 374, 375, 376, 378, 379, 416, 484, 566, 618, 622, 627, 637, 642, 666, 687, 688, 697, 707, 728, 790, 850, 858, 860, 879, 884, 887, 894, 900, 903, 908, 909, 911, 919, 925, 928, 929, 939, 968, 981, 985, 990, 1000, 1005, 1015, 1030, 1090, 1098, 1102, 1104, 1212, 1213, 1214, 1216, 1235, 1241, 1242, 1246, 1248, 1252, 1253, 1254, 1259, 1266, 1282, 1300, 1310, 1320, 1330, 1331]
 
kernel[("ES", "l", "u")] = "poly"
C[("ES", "l", "u")] = 721.430043
gamma[("ES", "l", "u")] = 0.026924
degree[("ES", "l", "u")] = 6
coef0[("ES", "l", "u")] = 0.000223
features[("ES", "l", "u")] = [12, 15, 40, 42, 48, 49, 56, 62, 82, 109, 121, 163, 176, 220, 230, 241, 243, 305, 306, 313, 314, 322, 331, 332, 341, 342, 351, 352, 362, 363, 372, 375, 376, 378, 379, 419, 427, 506, 516, 542, 611, 614, 616, 620, 621, 647, 652, 653, 654, 705, 714, 715, 725, 731, 736, 776, 808, 862, 863, 864, 867, 894, 896, 901, 903, 911, 929, 973, 974, 985, 1002, 1008, 1015, 1051, 1090, 1101, 1102, 1104, 1105, 1109, 1126, 1139, 1141, 1145, 1149, 1153]
 
kernel[("ES", "l", "b")] = "poly"
C[("ES", "l", "b")] = 954.759391
gamma[("ES", "l", "b")] = 0.005810
degree[("ES", "l", "b")] = 6
coef0[("ES", "l", "b")] = 1.001039
features[("ES", "l", "b")] = [250, 284, 290, 297, 305, 341, 351, 362, 363, 372, 375, 376, 378, 379, 419, 427, 435, 453, 484, 493, 494, 499, 500, 506, 516, 520, 526, 531, 533, 538, 540, 546, 584, 605, 615, 622, 641, 644, 645, 652, 659, 860, 869, 879, 896, 910, 970, 973, 985, 991, 994, 996, 1001, 1005, 1008, 1013, 1015, 1018, 1022, 1030, 1070, 1099, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1115, 1149, 1150, 1153, 1154, 1166, 1179, 1252, 1259, 1265, 1273, 1319, 1330, 1331]
 
kernel[("OG", "r", "u")] = "rbf"
C[("OG", "r", "u")] = 12.420779
gamma[("OG", "r", "u")] = 0.000617
features[("OG", "r", "u")] = [6, 8, 9, 12, 37, 50, 57, 77, 78, 82, 96, 122, 134, 138, 139, 144, 147, 175, 182, 183, 191, 223, 225, 236, 243, 252, 367, 384, 407, 421, 475, 490, 491, 493, 494, 496, 497, 499, 504, 533, 537, 538, 540, 541, 548, 553, 556, 561, 565, 566, 570, 574, 584, 605, 609, 632, 637, 640, 643, 646, 648, 649, 656, 663, 679, 687, 708, 717, 719, 725, 730, 756, 790, 797, 806, 825, 850, 864, 868, 879, 884, 900, 905, 921, 924, 929, 959, 972, 978, 985, 989, 1003, 1005, 1025, 1077, 1088, 1093, 1099, 1105, 1106, 1110, 1138, 1145, 1153, 1161, 1170, 1211]

kernel[("OG", "r", "b")] = "poly"
C[("OG", "r", "b")] = 8543.275419
gamma[("OG", "r", "b")] = 0.016824
degree[("OG", "r", "b")] = 6
coef0[("OG", "r", "b")] = 0.000039
features[("OG", "r", "b")] = [10, 21, 32, 38, 44, 82, 122, 134, 138, 154, 155, 161, 168, 227, 238, 243, 244, 246, 272, 366, 384, 395, 407, 421, 486, 493, 496, 497, 499, 504, 507, 518, 521, 540, 545, 546, 548, 552, 553, 554, 556, 559, 561, 562, 563, 565, 566, 570, 574, 584, 586, 594, 605, 637, 640, 641, 649, 663, 713, 717, 736, 849, 864, 870, 878, 879, 891, 921, 959, 969, 978, 989, 993, 999, 1000, 1003, 1004, 1077, 1078, 1088, 1089, 1091, 1099, 1101, 1105, 1106, 1108, 1109, 1110, 1112, 1126, 1138, 1143, 1145, 1153, 1161, 1170, 1199, 1210, 1211, 1214, 1220]
 
kernel[("OG", "l", "u")] = "poly"
C[("OG", "l", "u")] = 3421.900304
gamma[("OG", "l", "u")] = 0.016589
degree[("OG", "l", "u")] = 8
coef0[("OG", "l", "u")] = 0.000135
features[("OG", "l", "u")] = [3, 12, 13, 17, 19, 32, 38, 58, 82, 90, 130, 160, 161, 252, 268, 367, 488, 490, 497, 505, 516, 566, 570, 586, 612, 614, 618, 637, 642, 663, 687, 691, 707, 732, 735, 736, 738, 762, 816, 826, 851, 852, 853, 856, 860, 886, 933, 949, 969, 988, 1000, 1006, 1026, 1031, 1032, 1048, 1067, 1078, 1088, 1089, 1093, 1095, 1119, 1125, 1179, 1212, 1214, 1240, 1246, 1251, 1252, 1299, 1309, 1310, 1319, 1320, 1330, 1331]
 
kernel[("OG", "l", "b")] = "poly"
C[("OG", "l", "b")] = 257.513031
gamma[("OG", "l", "b")] = 0.005750
degree[("OG", "l", "b")] = 3
coef0[("OG", "l", "b")] = -0.001906
features[("OG", "l", "b")] = [17, 32, 37, 44, 58, 82, 103, 112, 130, 169, 176, 251, 252, 258, 259, 268, 296, 367, 374, 379, 417, 488, 490, 493, 494, 495, 496, 497, 501, 518, 524, 531, 537, 538, 540, 541, 566, 586, 590, 601, 626, 637, 639, 642, 687, 707, 717, 731, 732, 735, 736, 738, 743, 762, 798, 860, 868, 879, 894, 969, 988, 989, 993, 1000, 1057, 1071, 1078, 1093, 1095, 1098, 1131, 1136, 1138, 1143, 1211, 1212, 1214, 1216, 1235, 1240, 1246, 1252, 1259, 1291, 1300, 1302, 1309, 1310, 1319, 1320, 1330, 1331]
 
kernel[("OR", "r", "u")] = "linear"
C[("OR", "r", "u")] = 1804.585041
features[("OR", "r", "u")] = [3, 7, 10, 17, 21, 30, 32, 37, 61, 74, 98, 268, 276, 281, 366, 372, 374, 400, 417, 445, 486, 497, 505, 516, 558, 608, 618, 637, 642, 649, 663, 679, 687, 717, 754, 850, 857, 860, 864, 868, 870, 875, 879, 881, 887, 900, 901, 908, 917, 929, 959, 963, 964, 996, 1002, 1046, 1070, 1211, 1226, 1228, 1259, 1266, 1274, 1282, 1291, 1300, 1320, 1331]

kernel[("OR", "r", "b")] = "linear"
C[("OR", "r", "b")] = 281.556690
features[("OR", "r", "b")] = [3, 16, 21, 22, 24, 32, 58, 64, 67, 72, 74, 80, 89, 99, 252, 365, 369, 371, 379, 485, 490, 493, 497, 499, 502, 505, 518, 521, 523, 524, 526, 530, 531, 533, 537, 566, 567, 586, 590, 594, 600, 601, 607, 608, 621, 627, 629, 634, 637, 663, 672, 679, 685, 694, 728, 849, 850, 868, 879, 905, 978, 980, 981, 983, 985, 994, 1002, 1005, 1033, 1050, 1051, 1070, 1074, 1091, 1093, 1095, 1097, 1115, 1126, 1135, 1211, 1212, 1213, 1214, 1232, 1241, 1246, 1254, 1266, 1268, 1274, 1282, 1291, 1300, 1310, 1320, 1331]

kernel[("OR", "l", "u")] = "sigmoid"
C[("OR", "l", "u")] = 63.939880
gamma[("OR", "l", "u")] = 0.002607
coef0[("OR", "l", "u")] = -1.903775
features[("OR", "l", "u")] = [6, 36, 37, 39, 40, 46, 47, 54, 82, 102, 106, 122, 125, 141, 154, 186, 196, 221, 231, 241, 365, 369, 371, 372, 381, 390, 421, 437, 493, 496, 497, 521, 523, 524, 526, 530, 531, 532, 533, 538, 540, 584, 586, 593, 594, 604, 605, 625, 634, 637, 681, 683, 687, 689, 707, 736, 763, 808, 850, 853, 869, 879, 884, 894, 905, 921, 957, 971, 990, 1000, 1012, 1026, 1042, 1044, 1050, 1052, 1071, 1095, 1097, 1098, 1111, 1116, 1147, 1163, 1211, 1212, 1214, 1216, 1230, 1235, 1240, 1246, 1252, 1266, 1282, 1291, 1299, 1310, 1318, 1320]

kernel[("OR", "l", "b")] = "poly"
C[("OR", "l", "b")] = 5502.260798
gamma[("OR", "l", "b")] = 0.019081
degree[("OR", "l", "b")] = 9
coef0[("OR", "l", "b")] = 0.001135
features[("OR", "l", "b")] = [32, 38, 74, 374, 453, 494, 496, 497, 499, 500, 501, 516, 521, 540, 555, 556, 566, 574, 601, 627, 637, 642, 679, 687, 708, 731, 857, 860, 862, 864, 879, 880, 884, 897, 900, 901, 908, 916, 929, 943, 949, 953, 964, 972, 988, 990, 998, 999, 1000, 1004, 1010, 1017, 1031, 1032, 1042, 1068, 1071, 1078, 1087, 1088, 1093, 1095, 1098, 1105, 1123, 1125, 1131, 1136, 1142, 1145, 1150, 1153, 1161, 1170, 1179, 1199, 1210, 1219, 1300]
 
kernel[("OX", "r", "u")] = "rbf"
C[("OX", "r", "u")] = 747.388915
gamma[("OX", "r", "u")] = 0.000062
features[("OX", "r", "u")] = [1, 2, 21, 22, 25, 27, 29, 32, 35, 38, 44, 58, 71, 74, 80, 94, 99, 112, 114, 124, 179, 202, 211, 243, 244, 368, 372, 375, 400, 413, 419, 444, 453, 486, 490, 493, 495, 496, 497, 499, 505, 516, 522, 541, 542, 607, 613, 618, 637, 642, 662, 663, 679, 687, 707, 717, 719, 736, 768, 816, 825, 826, 835, 836, 846, 847, 850, 856, 859, 860, 862, 863, 864, 868, 879, 884, 887, 893, 894, 900, 901, 905, 908, 921, 925, 929, 933, 934, 953, 959, 975, 976, 978, 981, 1005, 1008, 1015, 1025, 1050, 1070, 1092, 1093, 1098, 1121, 1126, 1163, 1211, 1212, 1230, 1232, 1235, 1237, 1240, 1241, 1242, 1266, 1274, 1290, 1291, 1300, 1320, 1331]

kernel[("OX", "r", "b")] = "rbf"
C[("OX", "r", "b")] = 60.285023
gamma[("OX", "r", "b")] = 0.000290
features[("OX", "r", "b")] = [7, 21, 32, 37, 38, 44, 112, 122, 124, 131, 152, 163, 194, 195, 211, 213, 221, 231, 232, 233, 242, 244, 369, 497, 516, 522, 528, 542, 558, 587, 596, 598, 611, 613, 615, 618, 620, 622, 626, 637, 642, 644, 655, 666, 667, 679, 682, 683, 687, 691, 707, 717, 721, 722, 727, 730, 742, 746, 756, 758, 762, 768, 775, 782, 790, 798, 807, 816, 826, 829, 836, 846, 847, 853, 854, 855, 856, 857, 860, 862, 864, 868, 872, 879, 884, 896, 901, 903, 908, 909, 911, 916, 917, 918, 919, 921, 924, 925, 928, 929, 933, 937, 943, 950, 957, 964, 968, 972, 978, 984, 989, 1000, 1006, 1010, 1012, 1023, 1026, 1042, 1067, 1071, 1080, 1082, 1093, 1095, 1097, 1098, 1099, 1115, 1125, 1142, 1170, 1179, 1210, 1211, 1212, 1214, 1241, 1248, 1259, 1291, 1320]

kernel[("OX", "l", "u")] = "poly"
C[("OX", "l", "u")] = 779.956075
gamma[("OX", "l", "u")] = 0.029331
degree[("OX", "l", "u")] = 9
coef0[("OX", "l", "u")] = 0.052136
features[("OX", "l", "u")] = [6, 7, 10, 11, 21, 32, 37, 38, 47, 61, 62, 68, 82, 130, 131, 138, 147, 157, 163, 170, 177, 185, 193, 202, 211, 221, 231, 242, 252, 367, 368, 380, 384, 389, 400, 403, 416, 419, 425, 431, 432, 497, 499, 516, 538, 542, 545, 546, 552, 566, 596, 608, 615, 617, 618, 627, 637, 642, 643, 649, 663, 666, 673, 679, 687, 707, 708, 717, 731, 733, 851, 860, 864, 868, 870, 879, 881, 887, 891, 909, 915, 917, 921, 925, 926, 929, 943, 959, 970, 975, 978, 989, 1000, 1025, 1050, 1070, 1092, 1094, 1106, 1110, 1115, 1126, 1133, 1181, 1200, 1201, 1211, 1212, 1252, 1259, 1265, 1266, 1274, 1291, 1300, 1310, 1320, 1330]
 
kernel[("OX", "l", "b")] = "poly"
C[("OX", "l", "b")] = 383.621959
gamma[("OX", "l", "b")] = 0.021458
degree[("OX", "l", "b")] = 8
coef0[("OX", "l", "b")] = 0.042587
features[("OX", "l", "b")] = [3, 4, 20, 21, 22, 30, 31, 32, 37, 44, 58, 67, 71, 74, 80, 89, 99, 112, 120, 127, 130, 131, 177, 185, 193, 202, 231, 242, 244, 252, 367, 380, 381, 384, 386, 389, 400, 417, 419, 425, 427, 435, 442, 445, 485, 490, 494, 496, 497, 499, 501, 505, 507, 521, 523, 530, 531, 533, 534, 537, 538, 545, 546, 561, 563, 566, 570, 589, 594, 600, 601, 608, 625, 627, 634, 637, 643, 649, 663, 672, 679, 708, 717, 728, 744, 758, 850, 853, 854, 856, 857, 860, 868, 879, 885, 921, 975, 978, 981, 985, 989, 1005, 1018, 1050, 1051, 1070, 1095, 1099, 1106, 1112, 1115, 1117, 1123, 1129, 1136, 1143, 1145, 1151, 1153, 1161, 1170, 1212, 1214, 1226, 1235, 1252, 1259, 1266, 1291, 1300, 1310, 1331]

#################################################################################################################################

for cat in ["A", "AB", "EI", "ER", "ES", "OG", "OR", "OX"]:
    for it in ["l", "r"] :
        for bu in ["u", "b"] :
            print ("Running interface prediction for configuration: (%s, %s, %s)\n\n" % (cat, it, bu))
            svm_model = make_svm_model(cat, it, bu)
            training_set_path   = "/media/sebastian/Storage/3DZD_interface_prediction_db5/benchmark5/" + cat + "/train_" + it + "/descriptors_balanced/"
            test_set_path = "/media/sebastian/Storage/3DZD_interface_prediction_db5/benchmark5/" + cat + "/test_" + it + "/descriptors/"
            test_interface_prediction_svm(training_set_path, test_set_path, cat, it, bu, svm_model)
            print ("*" * 255)
