# Path: PyContact/core/DataHandler.py
# class DataHandler:
#     """Handles the import or export of a session."""
# 
#     @staticmethod
#     def importSessionFromFile(fileName):
#         """Imports a saved session from 'filename'."""
#         importDict = pickle.load(open(fileName, "rb"))
#         contacts = importDict["contacts"]
#         arguments = importDict["analyzer"][0:-1]
#         trajArgs = importDict["trajectory"]
#         maps = importDict["maps"]
#         contactResults = importDict["analyzer"][-1]
#         del importDict
#         return [contacts, arguments, trajArgs, maps, contactResults]
# 
#     @staticmethod
#     def writeSessionToFile(fileName, analysis):
#         """Saves the current Session (analysis) at 'filename'."""
#         analyzerArgs = [analysis.psf, analysis.dcd, analysis.cutoff, analysis.hbondcutoff,
#                         analysis.hbondcutangle, analysis.sel1text, analysis.sel2text,
#                         analysis.contactResults]
#         trajArgs = analysis.getTrajectoryData()
#         exportDict = {"contacts": analysis.finalAccumulatedContacts, "analyzer": analyzerArgs, "trajectory": trajArgs,
#                       "maps": [analysis.lastMap1, analysis.lastMap2]}
#         pickle.dump(exportDict, open(fileName, "wb"))


from PyContact.core.ContactAnalyzer import *
from PyContact.core.DataHandler import DataHandler
    def __init__(self, cutoff, hbondcutoff, hbondcutangle, map1, map2, sel1, sel2):
        self.cutoff = cutoff
        self.hbondcutoff = hbondcutoff
        self.hbondcutangle = hbondcutangle
        self.map1 = map1
        self.map2 = map2
        self.sel1 = sel1
        self.sel2 = sel2


class PyContactJob:
    """Job class that is given a JobConfig and input files and handles
    running/analyzing the job as such."""
    def __init__(self, topo, traj, name, configuration):
        self.topo = topo
        self.traj = traj
        self.name = name
        self.configuration = configuration
        self.analyzer = None

    def runJob(self, ncores=1):
        """Runs contact analysis and accumulation with ncores threads."""
        print("Running job: " + self.name + " on " + str(ncores) + " cores.")
        self.analyzer = Analyzer(self.topo,self.traj, self.configuration.cutoff, self.configuration.hbondcutoff, self.configuration.hbondcutangle, self.configuration.sel1, self.configuration.sel2)
        self.analyzer.runFrameScan(ncores)
        self.analyzer.runContactAnalysis(self.configuration.map1, self.configuration.map2, ncores)

    def writeSessionToFile(self, fname=""):
        """Writes the current analysis session to a file with either self.name + .session or fname as output filename"""
        if fname != "":

# Path: PyContact/core/DataHandler.py
# class DataHandler:
#     """Handles the import or export of a session."""
# 
#     @staticmethod
#     def importSessionFromFile(fileName):
#         """Imports a saved session from 'filename'."""
#         importDict = pickle.load(open(fileName, "rb"))
#         contacts = importDict["contacts"]
#         arguments = importDict["analyzer"][0:-1]
#         trajArgs = importDict["trajectory"]
#         maps = importDict["maps"]
#         contactResults = importDict["analyzer"][-1]
#         del importDict
#         return [contacts, arguments, trajArgs, maps, contactResults]
# 
#     @staticmethod
#     def writeSessionToFile(fileName, analysis):
#         """Saves the current Session (analysis) at 'filename'."""
#         analyzerArgs = [analysis.psf, analysis.dcd, analysis.cutoff, analysis.hbondcutoff,
#                         analysis.hbondcutangle, analysis.sel1text, analysis.sel2text,
#                         analysis.contactResults]
#         trajArgs = analysis.getTrajectoryData()
#         exportDict = {"contacts": analysis.finalAccumulatedContacts, "analyzer": analyzerArgs, "trajectory": trajArgs,
#                       "maps": [analysis.lastMap1, analysis.lastMap2]}
#         pickle.dump(exportDict, open(fileName, "wb"))


# Path: PyContact/core/Scripting.py
from PyContact.core.ContactAnalyzer import *
from PyContact.core.DataHandler import DataHandler

    def __init__(self, cutoff, hbondcutoff, hbondcutangle, map1, map2, sel1, sel2):
        self.cutoff = cutoff
        self.hbondcutoff = hbondcutoff
        self.hbondcutangle = hbondcutangle
        self.map1 = map1
        self.map2 = map2
        self.sel1 = sel1
        self.sel2 = sel2


class PyContactJob:
    """Job class that is given a JobConfig and input files and handles
    running/analyzing the job as such."""
    def __init__(self, topo, traj, name, configuration):
        self.topo = topo
        self.traj = traj
        self.name = name
        self.configuration = configuration
        self.analyzer = None

    def runJob(self, ncores=1):
        """Runs contact analysis and accumulation with ncores threads."""
        print("Running job: " + self.name + " on " + str(ncores) + " cores.")
        self.analyzer = Analyzer(self.topo,self.traj, self.configuration.cutoff, self.configuration.hbondcutoff, self.configuration.hbondcutangle, self.configuration.sel1, self.configuration.sel2)
        self.analyzer.runFrameScan(ncores)
        self.analyzer.runContactAnalysis(self.configuration.map1, self.configuration.map2, ncores)

    def writeSessionToFile(self, fname=""):
        """Writes the current analysis session to a file with either self.name + .session or fname as output filename"""
        if fname != "":

            DataHandler.writeSessionToFile(fname, self.analyzer)