Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
127 lines (99 loc) · 3.25 KB
/
Copy pathutil.py
File metadata and controls
127 lines (99 loc) · 3.25 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
import re, string, sys, time
rctab = string.maketrans('ACGTacgt','TGCAtgca')
def message(text, indent=2):
# print message to stderr
space = ' ' * indent
text = re.sub('\n', '\n%s' %(space), text)
sys.stderr.write('%s%s\n' %(space, text))
def error(text, indent=2):
# print message to stderr and quit
space = ' ' * indent
text = re.sub('\n', '\n%s' %(space), text)
sys.stderr.write('%s%s\n' %(space, text))
quit()
def read_list(fn, dtype=str):
# read file as list
x = [dtype(line.rstrip()) for line in open(fn)]
return x
def read_dataframe(fn, index_dtype=str, columns_dtype=str):
import pandas as pd
# read file as pandas dataframe
x = pd.read_table(fn, sep='\t', header=0, index_col=0)
#x.index = x.index.astype(index_dtype)
#x.columns = x.columns.astype(columns_dtype)
return x
def read_tseries(fn):
import pandas as pd
# read file as pandas time series
return read_dataframe(fn, index_dtype=float, columns_dtype=str)
def iter_fst(fn):
# generator that iterates through [sid, seq] pairs in a fasta file
seq = ''
for line in open(fn):
line = line.rstrip()
if line.startswith('>'):
if seq != '':
yield [sid, seq]
sid = line
seq = ''
else:
seq += line
yield [sid, seq]
def iter_fsq(fn):
# generator that iterates through records in a fastq file
record = []
i = 0
for line in open(fn):
i += 1
if i % 4 == 1:
if len(record) > 0:
yield record
record = []
record.append(line.rstrip())
yield record
def read_fst(fn, reverse=False):
# read fasta file as dictionary
fst = {}
for [sid, seq] in iter_fst(fst):
if reverse == False:
fst[sid] = seq
elif reverse == True:
fst[seq] = sid
return fst
def cycle(x):
# an efficient way to cycle through a list (similar to itertools.cycle)
while True:
for xi in x:
yield xi
class timer():
# generator that measures elapsed time
def __init__(self):
self.t = [time.time()]
def __iter__(self):
return self
def next(self):
self.t.append(time.time())
return self.t[-1] - self.t[-2]
def mean(self):
return np.mean(self.t)
def median(self):
return np.median(self.t)
def reverse_complement(x):
return x[::-1].translate(rctab)
def pretty_colors(format='hex'):
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
colors = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
if format == 'rgb':
return colors
if format == 'hex':
return [rgb_to_hex(color) for color in colors]
if format == 'dec':
for i in range(len(colors)):
r, g, b = colors[i]
colors[i] = (r / 255., g / 255., b / 255.)
return colors