###########################################################################
#                                                                         #
# C O P Y R I G H T   N O T I C E                                         #
#  Copyright (c) 2003-10 by:                                              #
#    * California Institute of Technology                                 #
#                                                                         #
#    All Rights Reserved.                                                 #
#                                                                         #
# Permission is hereby granted, free of charge, to any person             #
# obtaining a copy of this software and associated documentation files    #
# (the "Software"), to deal in the Software without restriction,          #
# including without limitation the rights to use, copy, modify, merge,    #
# publish, distribute, sublicense, and/or sell copies of the Software,    #
# and to permit persons to whom the Software is furnished to do so,       #
# subject to the following conditions:                                    #
#                                                                         #
# The above copyright notice and this permission notice shall be          #
# included in all copies or substantial portions of the Software.         #
#                                                                         #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      #
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                   #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS     #
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN      #
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN       #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE        #
# SOFTWARE.                                                               #
###########################################################################
#
#  a simple use of the experiment class
from experiment import Experiment
from analyzeMotifs import AnalyzeMotifs
from conservation import Conservation
from draw import Draw
from cistematic.core.homology import homologyDB

class Orthology(Experiment, AnalyzeMotifs, Conservation, Draw):
    experimentType = "orthology"
    useConservation = False


    def initialize(self, sGenome="", rGenes=[], tGenomes=[], analysisDB="", conservationDB="", maskNonConserved=True):
        wholeDataset = []
        try:
            self.createAnalysis(dbName=analysisDB)
            if self.consDBName == "":
                self.createConservation(dbName=conservationDB)
        except:
            pass

        if sGenome == "":
            sGenome = self.startingGenome
        else:
            self.startingGenome = sGenome

        if len(rGenes) == 0:
            rGenes = self.refGenes
        else:
            self.refGenes = rGenes

        if len(tGenomes) == 0:
            tGenomes = self.targetGenomes
        else:
            self.targetGenomes = tGenomes

        self.useConservation = maskNonConserved
        try:
            for gene in rGenes:
                wholeDataset.append((sGenome, [gene]))
                hGenes = self.returnHomologs((sGenome, gene))
                for oneGene in hGenes:
                    wholeDataset.append((oneGene[0], [oneGene[1]]))
        except:
            self.mlog("could not load homologs for (%s,%s,%s)" % (sGenome, str(rGenes), (tGenomes)))

        Experiment.initialize(self, wholeDataset)

        
    def run(self):
        for gene in self.refGenes:
            dset = self.returnHomologs((self.startingGenome, gene))
            dset.append((self.startingGenome, gene))

        datasetID = self.genepoolID
        if self.checkForConservedSequence():
            fastaFile = self.createDataFile(geneDict = self.maskNonConservedSequence())
            for (prog, settingsID) in self.programs:
                prog.inputFile(fastaFile)
                runID = self.setRun(prog.name(), datasetID, settingsID)
                tag = str(runID) 
                prog.setTagID(tag)
                prog.run()
                theMotifs = prog.getMotifs()
                for mot in theMotifs:
                    self.appendResults(mot)
        elif self.useConservation == False:
            fastaFile = self.createDataFile()
            for (prog, settingsID) in self.programs:
                prog.inputFile(fastaFile)
                runID = self.setRun(prog.name(), datasetID, settingsID)
                tag = str(runID) 
                prog.setTagID(tag)
                prog.run()
                theMotifs = prog.getMotifs()
                for mot in theMotifs:
                    self.appendResults(mot)
        else:
            self.mlog("no conserved sequences to run motif finder on")