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