###########################################################################
#                                                                         #
# 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.                                                               #
###########################################################################
#
# The locate class allows mapping of known motifs to sequences or genomes
# should only be used with the Locator program, for now
from experiment import Experiment
from analyzeMotifs import AnalyzeMotifs
from cistematic.core import getChromosomeNames, readChromosome

class Locate(Experiment, AnalyzeMotifs):
    experimentType = "locate"


    def getChromosomeList(self, genome, partition=1, slice=0, db=""):
        """ return a list of the chromosomes available for a given genome.
        """
        result = []
        try:
            chromList = getChromosomeNames(genome, db, partition, slice)
        except:
            chromList = []

        for entry in chromList:
            result.append((genome, entry))

        return result


    def initialize(self, listType, compoundList):
        """ must initialize with listType='chromosome' in order to scan chromosomes
        """
        self.genepool = {}
        genepoolKeys = []
        if listType == "chromosome":
            for (genome, chromName) in compoundList:
                self.genepool[(genome, chromName)] = readChromosome(genome, chromName)
                genepoolKeys.append("%s\t%s" % (genome, chromName))

            self.genepoolID = self.setSettings("chromolist", genepoolKeys)
        else:
            Experiment.initialize(self, compoundList)


    def run(self):
        Experiment.run(self)
        datasetID = self.genepoolID
        for (prog, settingsID) in self.programs:
            try:
                if prog.name() == "Locator":
                    (settingName, settingData) = self.getSettingsID(settingsID)
                    prog.setSettings(settingData)
                    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("Can only run Locate experiment with Locator program")
            except:
                self.mlog("Error running program %s with settings %s" % (prog, settingsID))