#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@copyright: Copyright 2019, HITS gGmbH
@license: EUPL v. 1.2
@author: richtesn

"""

from multipipsa.multipipsa import PipsaRun
from multipipsa.multipipsa import ApbsRun
from multipipsa.multipipsa import ResultProcessor #Optional
from multipipsa.clusterpipsa import ClusterPipsa
from multipipsa.pipsatypes import *
import numpy as np
import os
import sys

# Location of distribution PIPSA executables and examples directory
#  - assumes that user has write access to the package directory and
#    that it is writable by the user.
localPIPSA = os.path.join(os.getcwd(), 'data', 'pipsa')
examplePDBs = os.path.join(os.getcwd(), 'data', 'example', 'pdbs')
print(localPIPSA)
print(examplePDBs)

ingrp = ["AC1", "AC5", "AC6"]
outgrp = ["AC2", "AC3", "AC4", "AC7", "AC8", "AC9"]
apbs = ApbsRun(dataDir=examplePDBs, pipsaRoot=localPIPSA,
               temp=298.15, pH="7.4", ios=0.100, structures=ingrp+outgrp)
apbs.runPdb2Pqr()
apbs.runApbs()

pr = PipsaRun(pipsaRoot=localPIPSA,
              dataDir=examplePDBs,
              pointsTemplate="AC5")

pr.setUseWeights(False)  # Optional
# rp = ResultProcessor()  #Optional
# rp.setNormalize(True)  #Optional
# rp.setNormalize(True, useAbsoluteValues=True) #Optional
# pr.getResultObject().setResultProcessor(rp)    #Optional

print("\n\nRun ingroup and outgroup comparision over all C-Alpha Atoms for binding analysis")
print("Warnings about IEEE_INVALID_FLAG can be ignored, they are due to running the comparision")
print("also inside the protein, where no overlap on the surface can be generated.\n\n")
print("This results in scores according to Fig 5 of the reference:")
print("Tong, Wade and Bruce, Proteins 2016; 84:1844-1858\n\n\n")
pr.runBindingScorePipsa(ingrp, outgrp)
pr.savePDBResult(filename="SimHodgkin.pdb",
                 scoreType=ScoreType.MS,
                 similarityType=SimilarityType.HODGKIN)
pr.savePDBResult(filename="SimCarbo.pdb",
                 scoreType=ScoreType.MS,
                 similarityType=SimilarityType.CARBO)
pr.savePDBResult(filename="CompactHodgkin.pdb",
                 scoreType=ScoreType.CS,
                 similarityType=SimilarityType.HODGKIN)
pr.savePDBResult(filename="CompactCarbo.pdb",
                 scoreType=ScoreType.CS,
                 similarityType=SimilarityType.CARBO)

print("\n\nRun plain PIPSA over total protein")
pr.runPipsa(ingrp+outgrp)
print("\n\nRun PIPSA, centered at the position of the C-Alpha of the 11th residue")
cl0 = ClusterPipsa(structures=ingrp+outgrp, pipsaRoot=localPIPSA,
                   dataDir=examplePDBs,
                   distanceType=DistanceType.PIPSA,
                   graphicsFileRoot="clust0")
pr.runClusterPipsa(ingrp+outgrp, points=[], cluster=cl0)
cl1 = ClusterPipsa(structures=ingrp+outgrp, pipsaRoot=localPIPSA,
                   dataDir=examplePDBs,
                   distanceType=DistanceType.PIPSA,
                   graphicsFileRoot="clust1")
print("\n\nRun plain pipsa over a given list of points (C-Alpha positions)")
pr.runClusterPipsa(
    ingrp+outgrp, points=pr.getStructure().getCAAtoms(), cluster=cl1)
cl2 = ClusterPipsa(structures=ingrp+outgrp, pipsaRoot=localPIPSA,
                   dataDir=examplePDBs,
                   distanceType=DistanceType.PIPSA,
                   graphicsFileRoot="clust2")
print("\n\nRun plain pipsa and cluster on the tenth residue of the pdbfile")
pr.runClusterPipsa(
    ingrp+outgrp, points=pr.getStructure().getCAAtoms()[10], cluster=cl2)

# This referencecluster corresponds
referenceCluster = np.array([1, 1, 1, 2, 2, 2, 2, 2, 2])
# to the ingrp+outgrp list.
cl = ClusterPipsa(structures=ingrp+outgrp, pipsaRoot=localPIPSA,
                  dataDir=examplePDBs,
                  referenceCluster=referenceCluster,
                  distanceType=DistanceType.PIPSA,
                  graphicsFileRoot="clust")
print("\n\nRun PIPSA over all C-Alpha Atoms and cluster each and compare to reference cluster")
print("Generates a pdb file with b scores indicating similarity to the reference cluster (CORRECTED_RAND)")
print("or a score indicating the cluster information content (CLUSTER_INFORMATION).")
# runClusterPipsa also accepts the points argument
pr.runClusterPipsa(ingrp+outgrp, cluster=cl)
#pr.savePDBResult(filename="ClusterCorrectedRand.pdb",scoreType=ScoreType.CORRECTED_RAND, clusterObject=cl)
#pr.savePDBResult(filename="ClusterClusterInfo.pdb",scoreType=ScoreType.CLUSTER_INFORMATION, clusterObject=cl)
