erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / experiments / phyloTest.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 # a simple use of the experiment class
31 from experiment import Experiment
32 from analyzeMotifs import AnalyzeMotifs
33 from conservation import Conservation
34 from draw import Draw
35 from cistematic.core.homology import homologyDB
36
37 class PhyloTest(Experiment, AnalyzeMotifs, Conservation, Draw):
38     experimentType = "phyloTest"
39     phyloTestMap = {}
40     useConservation = True
41
42
43     def initialize(self, sGenome="", rGenes=[], tGenomes=[], analysisDB="", conservationDB="", maskNonConserved=True):
44         wholeDataset = []
45         try:
46             self.createAnalysis(dbName=analysisDB)
47             self.createConservation(dbName=conservationDB)
48         except:
49             pass
50
51         if sGenome == "":
52             sGenome = self.startingGenome
53         else:
54             self.startingGenome = sGenome
55
56         if len(rGenes) == 0:
57             rGenes = self.refGenes
58         else:
59             self.refGenes = rGenes
60
61         if len(tGenomes) == 0:
62             tGenomes = self.targetGenomes
63         else:
64             self.targetGenomes = tGenomes
65
66         self.useConservation = maskNonConserved
67         self.phyloTestMap[sGenome] = []
68         for gene in rGenes:
69             self.phyloTestMap[sGenome].append((sGenome, gene))
70
71         for genome in self.targetGenomes:
72             self.phyloTestMap[genome] = []
73
74         self.targetGenomes.append(sGenome)
75         try:
76             for gene in rGenes:
77                 wholeDataset.append((sGenome, [gene]))
78                 hGenes = self.returnHomologs((sGenome, gene))
79                 for oneGene in hGenes:
80                     wholeDataset.append((oneGene[0], [oneGene[1]]))
81                     self.phyloTestMap[oneGene[0]].append(oneGene)
82         except:
83             self.mlog("could not load from homology for (%s,%s,%s)" % (sGenome, str(rGenes), str(tGenomes)))
84
85         Experiment.initialize(self, wholeDataset)
86
87         
88     def run(self):
89         for genome in self.targetGenomes:
90             dset = self.phyloTestMap[genome]
91             datasetID = self.setSettingsID("dataset-%s" % genome, dset)
92             if self.useConservation:
93                 fastaFile = self.createDataFile(datasetID, geneDict = self.maskNonConservedSequence(datasetID))
94             else:
95                 fastaFile = self.createDataFile(datasetID)
96
97             for (prog, settingsID) in self.programs:
98                 prog.inputFile(fastaFile)
99                 runID = self.setRun(prog.name(), datasetID, settingsID)
100                 tag = str(runID) 
101                 prog.setTagID(tag)
102                 prog.run()
103                 theMotifs = prog.getMotifs()
104                 for mot in theMotifs:
105                     self.appendResults(mot)