-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhelperFunctions.py
More file actions
251 lines (202 loc) · 6.32 KB
/
helperFunctions.py
File metadata and controls
251 lines (202 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/lab/64/bin/python
# helperFunctions.py
# Author: Angela Brooks
# Program Completion Date:
# Description:
# Modification Date(s):
# Copyright (c) 2011, Angela Brooks. anbrooks@gmail.com
# All rights reserved.
import sys
import optparse
import random
import math
import os
import time
from subprocess import Popen, PIPE
#############
# CONSTANTS #
#############
#################
# END CONSTANTS #
#################
###########
# CLASSES #
###########
class OptionParser(optparse.OptionParser):
"""
Adding a method for required arguments.
Taken from:
http://www.python.org/doc/2.3/lib/optparse-extending-examples.html
"""
def check_required(self, opt):
option = self.get_option(opt)
# Assumes the option's 'default' is set to None!
if getattr(self.values, option.dest) is None:
print "%s option not supplied" % option
self.print_help()
sys.exit(1)
class CommentedFile:
"""
Taken from:
http://www.mfasold.net/blog/2010/02/python-recipe-read-csvtsv-textfiles-and-ignore-comment-lines/
"""
def __init__(self, f, commentstring="#"):
self.f = f
self.commentstring = commentstring
def next(self):
line = self.f.next()
while line.startswith(self.commentstring):
line = self.f.next()
return line
def __iter__(self):
return self
def close(self):
self.f.close()
###############
# END CLASSES #
###############
########
# MAIN #
########
def main():
# opt_parser = OptionParser()
# Add Options. Required options should have default=None
# opt_parser.add_option("<opt>",
# dest=,
# type=,
# help=,)
# (options, args) = opt_parser.parse_args()
# validate the command line arguments
# opt_parser.check_required("<req_arg>")
sys.exit(0)
############
# END_MAIN #
############
#############
# FUNCTIONS #
#############
def formatLine(line):
line = line.replace("\r","")
line = line.replace("\n","")
return line
def coordsOverlap(start1, end1, start2, end2):
"""
Just check if the coordinates overlap.
"""
if start1 >= start2 and start1 <= end2:
return True
if end1 >= start2 and end1 <= end2:
return True
if start1 <= start2 and end1 >= end2:
return True
return False
def median(vals):
vals.sort()
val_len = len(vals)
if val_len % 2 != 0:
return vals[(val_len + 1)/2 - 1]
else:
lower = vals[val_len/2-1]
upper = vals[val_len/2]
return float(lower + upper)/2
def runCmd(cmd_str, which_shell, pCommand=False):
try:
if pCommand:
print cmd_str
p = Popen(cmd_str, shell=True, executable=which_shell)
sts = os.waitpid(p.pid,0)
except:
print "Something wrong with: %s" % cmd_str
sys.exit(1)
def getShannonIndex(pos2countDict, totalCount):
"""
Calculates a Shannon-Wiener Index for the positions and counts. It treats
each position like a unique species.
"""
if totalCount == 0:
return 0
summation = 0
for pos in pos2countDict:
p = float(pos2countDict[pos]) / totalCount
if p == 0.0:
continue
summation += p * math.log(p, 2)
if summation == 0:
return 0
return -summation
def runLSF(cmd, bsub_out, job_id, queue="week", mem=8, group="cancerfolk",
tmp_file_name = "tmp.bjob", job_group=None):
bsub_options = "#!/bin/tcsh\n"
bsub_options += "#BSUB -q %s\n" % queue
if queue == "hour":
bsub_options += "#BSUB -W 240\n"
bsub_options += "#BSUB -R \"rusage[mem=%d]\"\n" % mem
bsub_options += "#BSUB -P %s\n" % group
bsub_options += "#BSUB -o %s\n" % bsub_out
bsub_options += "#BSUB -J %s\n" % job_id
if job_group:
bsub_options += "#BSUB -g /%s\n" % job_group
# Make tmp file
tmp_file = open(tmp_file_name, "w")
tmp_file.write(bsub_options)
tmp_file.write(cmd)
tmp_file.close()
os.system("bsub < %s" % tmp_file_name)
# Remove tmp_file
os.remove(tmp_file_name)
def updateDictOfLists(d, key, item):
"""
I noticed that in many scripts I have a dictionary of lists. When I want
to add to the dictionary, I always have to first check if the key exists,
if it doesn't I have to map the new key to a list, but if it does, I just
append to the existing list. Doing this many times in separate pieces of
code makes this function useful.
"""
try:
d[key].append(item)
except KeyError:
d[key] = [item]
def updateDictOfLists_extend(d, key, item):
"""
I noticed that in many scripts I have a dictionary of lists. When I want
to add to the dictionary, I always have to first check if the key exists,
if it doesn't I have to map the new key to a list, but if it does, I just
append to the existing list. Doing this many times in separate pieces of
code makes this function useful.
In some cases, the item might be a list itself, which I want to extend to
the existing list instead of just append a list to a list.
This function would also work if you wanted to just add one item.
"""
try:
if type(item) == type([]):
d[key].extend(item)
else:
d[key].append(item)
except KeyError:
if type(item) == type([]):
d[key] = item
else:
d[key] = [item]
def updateDictOfSets(d, key, item):
"""
Similar functionality as updateDictOfLists
"""
try:
d[key].add(item)
except KeyError:
d[key] = set([item])
def waitForChildren(children_processes, sleeptime=5):
ctr = 0
num_children = len(children_processes)
while 1:
for child in children_processes:
if not child.poll() is None:
ctr += 1
sys.stdout.flush()
if ctr == num_children:
break
time.sleep(sleeptime)
#################
# END FUNCTIONS #
#################
if __name__ == "__main__": main()