erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / experiments / locate.py
1 ###########################################################################
2 #                                                                         #
3 # C O P Y R I G H T   N O T I C E                                         #
4 #  Copyright (c) 2003-10 by:                                              #
5 #    * California Institute of Technology                                 #
6 #                                                                         #
7 #    All Rights Reserved.                                                 #
8 #                                                                         #
9 # Permission is hereby granted, free of charge, to any person             #
10 # obtaining a copy of this software and associated documentation files    #
11 # (the "Software"), to deal in the Software without restriction,          #
12 # including without limitation the rights to use, copy, modify, merge,    #
13 # publish, distribute, sublicense, and/or sell copies of the Software,    #
14 # and to permit persons to whom the Software is furnished to do so,       #
15 # subject to the following conditions:                                    #
16 #                                                                         #
17 # The above copyright notice and this permission notice shall be          #
18 # included in all copies or substantial portions of the Software.         #
19 #                                                                         #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         #
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      #
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                   #
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS     #
24 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN      #
25 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN       #
26 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE        #
27 # SOFTWARE.                                                               #
28 ###########################################################################
29 #
30 # The locate class allows mapping of known motifs to sequences or genomes
31 # should only be used with the Locator program, for now
32 from experiment import Experiment
33 from analyzeMotifs import AnalyzeMotifs
34 from cistematic.core import getChromosomeNames, readChromosome
35
36 class Locate(Experiment, AnalyzeMotifs):
37     experimentType = "locate"
38
39
40     def getChromosomeList(self, genome, partition=1, slice=0, db=""):
41         """ return a list of the chromosomes available for a given genome.
42         """
43         result = []
44         try:
45             chromList = getChromosomeNames(genome, db, partition, slice)
46         except:
47             chromList = []
48
49         for entry in chromList:
50             result.append((genome, entry))
51
52         return result
53
54
55     def initialize(self, listType, compoundList):
56         """ must initialize with listType='chromosome' in order to scan chromosomes
57         """
58         self.genepool = {}
59         genepoolKeys = []
60         if listType == "chromosome":
61             for (genome, chromName) in compoundList:
62                 self.genepool[(genome, chromName)] = readChromosome(genome, chromName)
63                 genepoolKeys.append("%s\t%s" % (genome, chromName))
64
65             self.genepoolID = self.setSettings("chromolist", genepoolKeys)
66         else:
67             Experiment.initialize(self, compoundList)
68
69
70     def run(self):
71         Experiment.run(self)
72         datasetID = self.genepoolID
73         for (prog, settingsID) in self.programs:
74             try:
75                 if prog.name() == "Locator":
76                     (settingName, settingData) = self.getSettingsID(settingsID)
77                     prog.setSettings(settingData)
78                     runID = self.setRun(prog.name(), datasetID, settingsID)
79                     tag = str(runID)
80                     prog.setTagID(tag)
81                     prog.run()
82                     theMotifs = prog.getMotifs()
83                     for mot in theMotifs:
84                         self.appendResults(mot)
85                 else:
86                     self.mlog("Can only run Locate experiment with Locator program")
87             except:
88                 self.mlog("Error running program %s with settings %s" % (prog, settingsID))