###########################################################################
#                                                                         #
# 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.                                                               #
###########################################################################
#
# covariance.py
from cistematic.programs import Program
import time, Consensus
from cistematic.core.motif import Motif


class CisGreedy(Program):


    def getSettings(self):
        return (self.model, self.nmotifs, self.minwidth, self.maxwidth, self.bON,
                self.iterations, self.Founder, self.bfile, self.percID,
                self.founderPercID, self.MarkovSize)


    def setSettings(self, settings):
        self.motifs = []
        (self.model, self.nmotifs, self.minwidth, self.maxwidth, self.bON,
         self.iterations, self.Founder, self.bfile, self.percID,
        self.founderPercID, self.MarkovSize) = settings


    def setGenExpOptions(self, optionArray):
        self.setModel("zoops")
        self.setNumMotifs("10")
        self.setMaxWidth("15")
        self.setMinWidth("6")
        self.setBON(True)
        self.setIterations("100")
        self.bfile = "None"
        self.TCM="All"
        self.Founder = False
        self.setPercID("75")
        self.setMarkovSize("3")
        self.setFounderPID("75")
        self.setBackground("None")
        for option in optionArray:
            (optionTag, optionValue) = option.split(":")

            if optionTag == "Markov size":
                self.setMarkovSize(optionValue)

            if optionTag == "percentID":
                self.setPercID(optionValue)

            if optionTag == "founderPercentID":
                self.setFounderPID(optionValue)

            if optionTag == "model":
                self.setModel(optionValue)

            if optionTag == "nmotifs":
                self.setNumMotifs(optionValue)

            if optionTag == "width": 
                self.setMinWidth(optionValue)
                self.setMaxWidth(optionValue)

            if optionTag == "minWidth":
                self.setMinWidth(optionValue)

            if optionTag == "maxWidth":
                self.setMaxWidth(optionValue)

            if optionTag == "reverse":
                self.setBON(optionValue)

            if optionTag == "iterations":
                self.setIterations(optionValue)

            if optionTag == "background":
                self.setBackground(optionValue)

            if optionTag == "founder":
                self.setFounder(True)

        #if no background file is provided the default setting
        #is to use the input sequences as a background
        print self.bfile
        #set markovSize


    def setMarkovSize(self, val):
        self.MarkovSize = int(str(val))


    def setFounderPID(self, val):
        self.founderPercID = int(str(val))


    def setFounder(self, boolVal):
        if boolVal:
            self.Founder = True
        else:
            self.Founder = False


    def setPercID(self, percVal):
        self.percID = int(str(percVal))


    def setModel(self, modelType):
        if modelType in ["oops", "zoops", "tcm"]:
            self.model = str(modelType)
        else:
            self.model = "zoops";    


    def setIterations(self, number):
        self.iterations = int(str(number))


    def setNumMotifs(self, motifNum):
        self.nmotifs = int(str(motifNum))


    def setBON(self, B_val):
        if str(B_val) == "False": 
            self.bON = False 
        else: 
            self.bON = True 


    def getSequences(self, infile):
        seqFiles = open(infile, 'r')
        Lines = seqFiles.readlines()
        seqFiles.close()
        self.sequences = []
        for line in Lines:
            if line[0] != ">":
                self.sequences.append(line[:-1])


    def setMinWidth(self, width):
        self.minwidth = int(str(width))


    def setMaxWidth(self, width):
        self.maxwidth = int(str(width))
   

    def setBackground(self, backFile):
        self.bfile = str(backFile)


    def run(self):
        print self.nmotifs
        startTime = time.time()
        if self.bfile == "None":
            self.bfile = Consensus.Markov(self.sequences, self.bON,self.MarkovSize)

        print "geting consensus score" 
        self.contents = Consensus.MotifFinder(self.sequences, self.minwidth, self.maxwidth,
                                              self.nmotifs, self.iterations, self.bfile,
                                              self.bON, self.Founder,self.percID,
                                              self.founderPercID, self.MarkovSize, self.model,
                                              self.TCM) 	
        stopTime = time.time()
        print "\nThis run took %.3f - %.3f = %.3f seconds and produced %d motifs" % (startTime, stopTime, stopTime - startTime, len(self.contents[0]))


    def getMotifs(self):    
        self.motifs = []
        try:
            (PWMs, Seqs) = self.contents
            for index in range(len(Seqs)):
                self.motifs.append(Motif("%s-cisGreedy-%s" % (self.tagID, str(index + 1)), seqs=Seqs[index]))
        except:
            print "error returning motifs"
            pass

        return self.motifs