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