erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / programs / cisGreedy.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 # covariance.py
31 from cistematic.programs import Program
32 import time, Consensus
33 from cistematic.core.motif import Motif
34
35
36 class CisGreedy(Program):
37
38
39     def getSettings(self):
40         return (self.model, self.nmotifs, self.minwidth, self.maxwidth, self.bON,
41                 self.iterations, self.Founder, self.bfile, self.percID,
42                 self.founderPercID, self.MarkovSize)
43
44
45     def setSettings(self, settings):
46         self.motifs = []
47         (self.model, self.nmotifs, self.minwidth, self.maxwidth, self.bON,
48          self.iterations, self.Founder, self.bfile, self.percID,
49         self.founderPercID, self.MarkovSize) = settings
50
51
52     def setGenExpOptions(self, optionArray):
53         self.setModel("zoops")
54         self.setNumMotifs("10")
55         self.setMaxWidth("15")
56         self.setMinWidth("6")
57         self.setBON(True)
58         self.setIterations("100")
59         self.bfile = "None"
60         self.TCM="All"
61         self.Founder = False
62         self.setPercID("75")
63         self.setMarkovSize("3")
64         self.setFounderPID("75")
65         self.setBackground("None")
66         for option in optionArray:
67             (optionTag, optionValue) = option.split(":")
68
69             if optionTag == "Markov size":
70                 self.setMarkovSize(optionValue)
71
72             if optionTag == "percentID":
73                 self.setPercID(optionValue)
74
75             if optionTag == "founderPercentID":
76                 self.setFounderPID(optionValue)
77
78             if optionTag == "model":
79                 self.setModel(optionValue)
80
81             if optionTag == "nmotifs":
82                 self.setNumMotifs(optionValue)
83
84             if optionTag == "width": 
85                 self.setMinWidth(optionValue)
86                 self.setMaxWidth(optionValue)
87
88             if optionTag == "minWidth":
89                 self.setMinWidth(optionValue)
90
91             if optionTag == "maxWidth":
92                 self.setMaxWidth(optionValue)
93
94             if optionTag == "reverse":
95                 self.setBON(optionValue)
96
97             if optionTag == "iterations":
98                 self.setIterations(optionValue)
99
100             if optionTag == "background":
101                 self.setBackground(optionValue)
102
103             if optionTag == "founder":
104                 self.setFounder(True)
105
106         #if no background file is provided the default setting
107         #is to use the input sequences as a background
108         print self.bfile
109         #set markovSize
110
111
112     def setMarkovSize(self, val):
113         self.MarkovSize = int(str(val))
114
115
116     def setFounderPID(self, val):
117         self.founderPercID = int(str(val))
118
119
120     def setFounder(self, boolVal):
121         if boolVal:
122             self.Founder = True
123         else:
124             self.Founder = False
125
126
127     def setPercID(self, percVal):
128         self.percID = int(str(percVal))
129
130
131     def setModel(self, modelType):
132         if modelType in ["oops", "zoops", "tcm"]:
133             self.model = str(modelType)
134         else:
135             self.model = "zoops";    
136
137
138     def setIterations(self, number):
139         self.iterations = int(str(number))
140
141
142     def setNumMotifs(self, motifNum):
143         self.nmotifs = int(str(motifNum))
144
145
146     def setBON(self, B_val):
147         if str(B_val) == "False": 
148             self.bON = False 
149         else: 
150             self.bON = True 
151
152
153     def getSequences(self, infile):
154         seqFiles = open(infile, 'r')
155         Lines = seqFiles.readlines()
156         seqFiles.close()
157         self.sequences = []
158         for line in Lines:
159             if line[0] != ">":
160                 self.sequences.append(line[:-1])
161
162
163     def setMinWidth(self, width):
164         self.minwidth = int(str(width))
165
166
167     def setMaxWidth(self, width):
168         self.maxwidth = int(str(width))
169    
170
171     def setBackground(self, backFile):
172         self.bfile = str(backFile)
173
174
175     def run(self):
176         print self.nmotifs
177         startTime = time.time()
178         if self.bfile == "None":
179             self.bfile = Consensus.Markov(self.sequences, self.bON,self.MarkovSize)
180
181         print "geting consensus score" 
182         self.contents = Consensus.MotifFinder(self.sequences, self.minwidth, self.maxwidth,
183                                               self.nmotifs, self.iterations, self.bfile,
184                                               self.bON, self.Founder,self.percID,
185                                               self.founderPercID, self.MarkovSize, self.model,
186                                               self.TCM)         
187         stopTime = time.time()
188         print "\nThis run took %.3f - %.3f = %.3f seconds and produced %d motifs" % (startTime, stopTime, stopTime - startTime, len(self.contents[0]))
189
190
191     def getMotifs(self):    
192         self.motifs = []
193         try:
194             (PWMs, Seqs) = self.contents
195             for index in range(len(Seqs)):
196                 self.motifs.append(Motif("%s-cisGreedy-%s" % (self.tagID, str(index + 1)), seqs=Seqs[index]))
197         except:
198             print "error returning motifs"
199             pass
200
201         return self.motifs