forked from glennhickey/teHmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
308 lines (271 loc) · 11.2 KB
/
common.py
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
#Copyright (C) 2013 by Glenn Hickey
#
#Released under the MIT license, see LICENSE.txt
import sys
import os
import argparse
import subprocess
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
import logging
import resource
import logging.handlers
import collections
import numpy as np
import string
import random
from argparse import ArgumentParser
from optparse import OptionParser, OptionContainer, OptionGroup
import pybedtools
from pkg_resources import parse_version
LOGZERO = -1e100
EPSILON = np.finfo(float).eps
def __myLogFloat(x, logZeroVal = LOGZERO, epsilonVal = EPSILON):
if np.abs(x) < epsilonVal:
return logZeroVal
return np.log(x)
""" Replace np.log to accept zero """
myLog = np.vectorize(__myLogFloat)
def runShellCommand(command):
try:
logger.info("Running %s" % command)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=sys.stderr, bufsize=-1)
output, nothing = process.communicate()
sts = process.wait()
if sts != 0:
raise RuntimeError("Command: %s exited with non-zero status %i" %
(command, sts))
return output
except KeyboardInterrupt:
raise RuntimeError("Aborting %s" % command)
def runParallelShellCommands(argList, numProc, execFunction=runShellCommand,
useThreads = False):
""" run some commands in parallel, either as processes or threads.
argList should be a list of arguments, one for each instance of
execFunction (ie this function should take a single parameter
"""
if useThreads is False:
poolType = Pool
else:
poolType = ThreadPool
output = None
if numProc == 1 or len(argList) == 1:
output = map(execFunction, argList)
elif len(argList) > 0:
mpPool = poolType(processes=min(numProc, len(argList)))
result = mpPool.map_async(execFunction, argList)
# specifying a timeout allows keyboard interrupts to work?!
# http://stackoverflow.com/questions/1408356/keyboard-interrupts-with-pythons-multiprocessing-pool
try:
output = result.get(sys.maxint)
except KeyboardInterrupt:
mpPool.terminate()
raise RuntimeError("Keyboard interrupt")
if not result.successful():
raise "One or more of commands %s failed" % str(argList)
return output
def getLocalTempPath(prefix="", extension="", tagLen=5):
S = string.ascii_uppercase + string.digits
tag = ''.join(random.choice(S) for x in range(tagLen))
tempPath = os.path.join(os.getcwd(), "%s%s%s" % (prefix, tag, extension))
return tempPath
def initBedTool(tempPrefix=""):
# keep temporary files in current directory, to make it a little harder to
# lose track of them and clog up the system....
S = string.ascii_uppercase + string.digits
tag = ''.join(random.choice(S) for x in range(5))
tempPath = os.path.join(os.getcwd(), "%sTempBedTool_%s" % (tempPrefix, tag))
logger.info("Temporary directory for BedTools (you may need to manually"
" erase in event of crash): %s" % tempPath)
try:
os.makedirs(tempPath)
except:
pass
pybedtools.set_tempdir(tempPath)
return tempPath
def cleanBedTool(tempPath):
# do best to erase temporary bedtool files if necessary
# (tempPath argument must have been created with initBedTool())
assert "TempBedTool_" in tempPath
pybedtools.cleanup(remove_all=True)
runShellCommand("rm -rf %s" % tempPath)
def binSearch(items, val, idx = [0,1], first = None, last = None):
""" binary search list of items for val, where each item is a tuple
and we are searching for val in a subtuple of that tuple. items of
course must be sorted along the specified index"""
assert len(val) == len(idx)
if first is None:
first = 0
if last is None:
last = len(items) - 1
pivot = (first + last) / 2
pv = tuple([items[pivot][i] for i in idx])
if first == last:
if pv == val:
return first
return None
assert pivot != last
if pv == val:
return pivot
elif pv > val:
return binSearch(items, val, idx, first, pivot)
else:
return binSearch(items, val, idx, pivot + 1, last)
def intersectSize(interval1, interval2):
""" return the size of the overlap between two intervals of format
(chrom, start, end) """
overlap = 0
if interval1[0] == interval2[0]:
i1 = interval1
i2 = interval2
if i1[1] > i2[1]:
i1, i2 = i2, i1
overlap = min(i1[2], i2[2]) - i2[1]
overlap = max(0, overlap)
return overlap
def distance(interval1, interval2):
""" return the distance between two intervals """
if intersectSize(interval1, interval2) > 0:
return 0
elif interval1[0] != interval2[0]:
return sys.maxint
elif interval1[2] <= interval2[1]:
return interval2[1] - interval1[2]
else:
assert interval2[2] <= interval1[1]
return interval1[1] - interval2[2]
def checkRequirements():
""" check some version numbers of pyhton libraries to see if
things ought to run -- should be kept consistent with readme"""
try:
from Cython.Compiler.Version import version
except:
raise RuntimeError("Cython not installed")
if parse_version(version) < parse_version("0.19.2"):
raise RuntimeError("Cython version >= 0.19.2 required (%s detected)" %
version)
if sys.version_info < (2, 7):
raise RuntimeError("Python version >= 2.7 required (%s detected)" %
"%d.%d" % (sys.version_info[0], sys.version_info[1]))
if parse_version(np.version.version) < parse_version("1.7"):
raise RuntimeError("Numpy version >= 1.7 required (%s detected)" %
np.version.version)
try:
from pybedtools import __version__ as pbt_version
except:
raise RuntimeError("Unable to detect pybedtools version")
if parse_version(pbt_version) < parse_version("0.6.7"):
raise RuntimeError("Pybedtools version >= 0.6.7 required (%s detected)" %
pbt_version)
#########################################################
#########################################################
#########################################################
#global logging settings / log functions
#########################################################
#########################################################
#########################################################
# Logging stuff copied from bioio.py from https://github.com/benedictpaten/sonLib
#
#Copyright (C) 2006-2012 by Benedict Paten ([email protected])
#
#Released under the MIT license, see LICENSE.txt
loggingFormatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
def __setDefaultLogger():
l = logging.getLogger()
for handler in l.handlers: #Do not add a duplicate handler unless needed
if handler.stream == sys.stderr:
return l
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(loggingFormatter)
l.addHandler(handler)
l.setLevel(logging.WARNING)
return l
logger = __setDefaultLogger()
logLevelString = "CRITICAL"
def redirectLoggerStreamHandlers(oldStream, newStream):
"""Redirect the stream of a stream handler to a different stream
"""
for handler in list(logger.handlers): #Remove old handlers
if handler.stream == oldStream:
handler.close()
logger.removeHandler(handler)
for handler in logger.handlers: #Do not add a duplicate handler
if handler.stream == newStream:
return
logger.addHandler(logging.StreamHandler(newStream))
def getLogLevelString():
return logLevelString
__loggingFiles = []
def addLoggingFileHandler(fileName, rotatingLogging=False):
if fileName in __loggingFiles:
return
__loggingFiles.append(fileName)
if rotatingLogging:
handler = logging.handlers.RotatingFileHandler(fileName, maxBytes=1000000, backupCount=1)
else:
handler = logging.FileHandler(fileName)
handler.setFormatter(loggingFormatter)
logger.addHandler(handler)
return handler
def setLogLevel(logLevel):
logLevel = logLevel.upper()
assert logLevel in [ "OFF", "CRITICAL", "WARNING", "INFO", "DEBUG" ] #Log level must be one of these strings.
global logLevelString
logLevelString = logLevel
if logLevel == "OFF":
logger.setLevel(logging.FATAL)
elif logLevel == "INFO":
logger.setLevel(logging.INFO)
elif logLevel == "DEBUG":
logger.setLevel(logging.DEBUG)
elif logLevel == "WARNING":
logger.setLevel(logging.WARNING)
elif logLevel == "CRITICAL":
logger.setLevel(logging.CRITICAL)
def logFile(fileName, printFunction=logger.info):
"""Writes out a formatted version of the given log file
"""
printFunction("Reporting file: %s" % fileName)
shortName = fileName.split("/")[-1]
fileHandle = open(fileName, 'r')
line = fileHandle.readline()
while line != '':
if line[-1] == '\n':
line = line[:-1]
printFunction("%s:\t%s" % (shortName, line))
line = fileHandle.readline()
fileHandle.close()
def addLoggingOptions(parser):
##################################################
# BEFORE YOU ADD OR REMOVE OPTIONS TO THIS FUNCTION, BE SURE TO MAKE THE SAME CHANGES TO
# addLoggingOptions_argparse() OTHERWISE YOU WILL BREAK THINGS
##################################################
parser.add_argument("--logOff", action="store_true", default=False,
help="Turn off logging. (default is CRITICAL)")
parser.add_argument("--logInfo", action="store_true", default=False,
help="Turn on logging at INFO level. (default is CRITICAL)")
parser.add_argument("--logDebug", action="store_true", default=False,
help="Turn on logging at DEBUG level. (default is CRITICAL)")
parser.add_argument("--logLevel", default='WARNING',
help="Log at level (may be either OFF/INFO/DEBUG/CRITICAL). default=CRITICAL")
parser.add_argument("--logFile", help="File to log in")
parser.add_argument("--rotatingLogging", action="store_true", default=False,
help="Turn on rotating logging, which prevents log files getting too big. default=False")
def setLoggingFromOptions(options):
"""Sets the logging from a dictionary of name/value options.
"""
#We can now set up the logging info.
if options.logLevel is not None:
setLogLevel(options.logLevel) #Use log level, unless flags are set..
if options.logOff:
setLogLevel("OFF")
elif options.logInfo:
setLogLevel("INFO")
elif options.logDebug:
setLogLevel("DEBUG")
logger.info("Logging set at level: %s" % logLevelString)
if options.logFile is not None:
addLoggingFileHandler(options.logFile, options.rotatingLogging)
logger.info("Logging to file: %s" % options.logFile)