1 ###########################################################################
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 #
7 # All Rights Reserved. #
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: #
17 # The above copyright notice and this permission notice shall be #
18 # included in all copies or substantial portions of the Software. #
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 #
28 ###########################################################################
32 from cistematic.programs import supportedPrograms
33 from cistematic.experiments import experimentTypes, loadExperiment
34 from os import environ
36 if environ.get("CISTEMATIC_ROOT"):
37 cisRoot = environ.get("CISTEMATIC_ROOT")
39 cisRoot = "/proj/genome"
54 def __init__(self, path, dbPath, newexp, type="", dset="", progs="", analysis="", createExp=False):
56 self.dbFilePath = dbPath
58 self.expFilePath = string.replace(dbPath, ".db", "-db.py")
62 self.analysisName = newexp + analysis
67 if len(self.expProgs) == 0:
68 exp = loadExperiment( self.expName , self.dbFilePath)
69 self.expType = exp.experimentType
70 self.expProgs = exp.getSetting("expProgs")
73 exp = loadExperiment( self.expName , self.dbFilePath)
74 self.expType = exp.experimentType
75 self.dataset = exp.getDataset()
77 self.createExpRunFile()
81 exp = loadExperiment( self.expName , self.dbFilePath)
82 exp.setExternalStatus("QUEUED")
84 os.system("cd %s ; nohup /usr/bin/python %s & " % (self.dataPath, self.expFilePath))
88 strexec = 'from cistematic.experiments.' + modName + ' import ' + self.expType + '\n'
90 strexec = 'exp = ' + self.expType + '("' + self.expName + '", "' + self.dbFilePath + '")\n'
94 def createExpRunFile(self):
95 outFile = open(self.expFilePath, "w")
96 outFile.write(self.expHead())
97 outFile.write(self.expInit())
98 outFile.write(self.expPrograms())
99 outFile.write(self.expRun())
100 outFile.write(self.expAnalysis())
101 outFile.write(self.expStatus())
106 strHead = ["# Experiment: %s autogenerated by Cistematic GenExp 0.9.9b\n" % self.expName]
107 strHead.append("from sys import path\n")
108 strHead.append("cisPath = '%s'\n" % cisRoot)
109 strHead.append("if cisPath not in path:\n\tpath.append(cisPath)\n\n")
110 for oneProg in supportedPrograms:
111 strHead.append("from cistematic.programs.%s import %s\n" % (oneProg[0], oneProg[1]))
115 return string.join(strHead, "")
126 if self.expType in ["orthology", "phyloFoot", "phyloTest"]:
127 exp = loadExperiment( self.expName , self.dbFilePath)
128 consSettings = exp.getSetting("expConsConfig")
129 (window, threshold, numseq, homologs) = consSettings[0].split(":")
130 self.dataset.reverse()
131 if homologs != "1" and exp.settingsHasKey("gTargets"):
132 gTargets = exp.getSetting("gTargets")
133 if len(gTargets) > 0:
134 targetGenomes = gTargets[0].split(":")
137 for geneID in self.dataset:
138 (genome, gid) = geneID
139 if (genome not in targetGenomes) and homologs == "1":
140 targetGenomes.append(genome)
142 if startingGenome == "":
143 startingGenome = genome
146 # we are assuming that all of these genes will be from the same
147 # genome - this is only true (or necessary) if these are not
148 # explicitely maked as "homologs" by the user
149 if gid not in consGene:
152 geneIDList.append((genome, gid))
156 preInitializeList = ["exp.createConservation()\n"]
157 preInitializeList.append("exp.loadConservation()\n")
158 preInitializeList.append("paralogs = %s\n" % str(geneIDList))
159 preInitializeList.append('exp.insertHomologs(paralogs, "genExp")\n\n')
160 preInitialize = string.join(preInitializeList, "")
168 initializeArguments = str(self.dataset)
170 strInit = ["# Initialize Experiment\n"]
171 strInit.append("from cistematic.experiments import loadExperiment\n\n")
172 strInit.append('exp = loadExperiment("%s", "%s")\n' % (self.expName, self.dbFilePath))
173 strInit.append('exp.setExternalStatus("INITIALIZING")\n')
174 strInit.append(preInitialize)
175 if useMussa or useSeqcomp:
176 if len(targetGenomes) > 0:
177 strInit.append('exp.initialize("%s", %s, %s)\n\n' % (startingGenome, str(consGene), str(targetGenomes)))
179 strInit.append('exp.initialize("%s", %s)\n\n' % (startingGenome, str(consGene)))
180 strInit.append('exp.setExternalStatus("CONSERVATION")\n')
181 strInit.append("exp.computeAlignments()\n")
183 strInit.append("exp.initialize(%s)\n\n" % initializeArguments)
186 strInit.append("exp.mapMussaConservation(window=%s, threshold=%s)\n\n" % (window, threshold))
189 strInit.append("exp.mapSeqcompConservation(window=%s, threshold=%s, minSequences=%s)\n\n" % (window, threshold, numseq))
191 return string.join(strInit, "")
194 def expPrograms(self):
197 for (genome, geneID) in self.dataset:
198 if genome not in genomes:
199 genomes.append(genome)
201 strProg = ["# Load individual programs and settings\n"]
202 for entry in self.expProgs:
203 progArray = entry[:-1].split(";")
204 (amod, aProg) = progArray[0].split(":")
205 strProg.append("prog%s = %s()\n" % (str(index), aProg))
206 if len(genomes) == 1 and self.expType == "Simple":
207 strProg.apend('prog%s.setGenome("%s")\n' % (str(index), genomes[0]))
208 if len(progArray) > 1:
209 strProg.append("prog%s.setGenExpOptions(%s)\n" % (str(index), str(progArray[1:])))
210 strProg.append("exp.appendProgram(prog%s)\n" % (str(index)))
215 return string.join(strProg, "")
219 strRun = ["# Run experiment\n",
220 'exp.setExternalStatus("MOTIF FINDING")\n',
225 return string.join(strRun, "")
228 def expAnalysis(self):
229 strAnalysis = ["# Analysis Section\n",
230 'exp.setExternalStatus("ANALYZING")\n',
231 'exp.loadAnalysis("consensus")\n',
232 "exp.annotateConsensus()\n",
233 "exp.mapConsensus()\n",
234 "exp.buildMotifSize()\n",
236 'exp.loadAnalysis("1 mismatch")\n',
237 "exp.annotateConsensus(numMismatches=1)\n",
238 "exp.mapConsensus(numMismatches=1)\n",
239 "exp.buildMotifSize()\n",
241 'exp.loadAnalysis("PWM - 90% threshold")\n',
242 "exp.annotateConsensus()\n",
243 "exp.mapMotifs(90.0)\n",
244 "exp.buildMotifSize()\n\n",
248 return string.join(strAnalysis, "")
252 return "exp.resetExternalStatus()\n\n"