-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
257 lines (200 loc) · 6.73 KB
/
Copy pathutils.py
File metadata and controls
257 lines (200 loc) · 6.73 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
252
253
254
255
256
257
import time,sys,os,mmap,gzip,subprocess
import numpy as np
from functools import partial
import multiprocessing,csv
cpus = multiprocessing.cpu_count()
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
mem_mib = mem_bytes/(1024.**2)
proc_mem = mem_mib / (cpus +1)
def return_open_func(f):
'''
Detects file extension and return proper open_func
'''
file_path,file_root,file_extension = get_path_info(f)
if 'bgz' in file_extension:
#print('gzip.open with rb mode')
open_func = partial(gzip.open, mode = 'rb')
elif 'gz' in file_extension:
#print('gzip.open with rt mode')
open_func = partial(gzip.open, mode = 'rt')
else:
#print('regular open')
open_func = open
return open_func
def progressBar(value, endvalue, bar_length=20):
'''
Writes progress bar, given value (eg.current row) and endvalue(eg. total number of rows)
'''
percent = float(value) / endvalue
arrow = '-' * int(round(percent * bar_length)-1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write("\rPercent: [{0}] {1}%".format(arrow + spaces, int(round(percent * 100))))
sys.stdout.flush()
def identify_separator(f):
open_func = return_open_func(f)
with open_func(f) as i:header = i.readline().strip()
sniffer = csv.Sniffer()
dialect = sniffer.sniff(header)
return dialect.delimiter
def timing_function(some_function):
"""
Outputs the time a function takes to execute.
"""
def wrapper(*args,**kwargs):
t1 = time.time()
some_function(*args)
t2 = time.time()
print("Time it took to run the function: " + str((t2 - t1)))
return wrapper
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
def get_path_info(path):
file_path = os.path.dirname(path)
basename = os.path.basename(path)
file_root, file_extension = os.path.splitext(basename)
return file_path,file_root,file_extension
def file_exists(fname):
'''
Function to pass to type in argparse
'''
if os.path.isfile(fname):
return str(fname)
else:
print(fname + ' does not exist')
sys.exit(1)
def pretty_print(string,l = 30):
l = l-int(len(string)/2)
print('-'*l + '> ' + string + ' <' + '-'*l)
def mapcount(filename):
if not os.path.isfile(filename):
raise ValueError("File doesn't exist")
try:
return count_lines(filename)
except:
return 0
def count_lines(filename):
'''
Counts line in file
'''
f = open(filename, "r+")
buf = mmap.mmap(f.fileno(), 0)
lines = 0
readline = buf.readline
while readline():
lines += 1
return lines
class SliceMaker(object):
'''
allows to pass around slices
'''
def __getitem__(self, item):
return item
def line_iterator(f,separator=None,count = False,columns = SliceMaker()[:],dtype = str,skiprows = 0):
'''
Function that iterates through a file and returns each line as a list with separator being used to split.
N.B. it requires that all elements are the same type
'''
if not separator:
separator = identify_separator(f)
open_func = return_open_func(f)
i = open_func(f)
for x in range(skiprows):next(i)
if count is False:
for line in i:
yield np.array(line.strip().split(separator),dtype = str)[columns].astype(dtype)
else:
row = 0
for line in i:
row +=1
yield row,np.array(line.strip().split(separator),dtype = str)[columns].astype(dtype)
def basic_iterator(f,separator =None,skiprows = 0,count = False,columns = 'all'):
'''
Function that iterates through a file and returns each line as a list with separator being used to split.
'''
if not separator:
separator = identify_separator(f)
open_func = return_open_func(f)
i = open_func(f)
for x in range(skiprows):next(i)
if count is False:
for line in i:
line =line.strip().split(separator)
line = return_columns(line,columns)
yield line
else:
row = 0
for line in i:
line =line.strip().split(separator)
line = return_columns(line,columns)
row += 1
yield row,line
def return_columns(l,columns):
'''
Returns all columns, or rather the elements, provided the columns
'''
if columns == 'all':
return l
elif type(columns) == int:
return l[columns]
elif type(columns) == list:
return list(map(l.__getitem__,columns))
def tmp_bash(cmd,check = False):
from tempfile import NamedTemporaryFile
scriptFile = NamedTemporaryFile(delete=True)
with open(scriptFile.name, 'w') as f:
f.write("#!/bin/bash\n")
f.write(cmd + "\n")
os.chmod(scriptFile.name,0o777)
scriptFile.file.close()
if check:
subprocess.check_call(scriptFile.name)
else:
subprocess.call(scriptFile.name,stderr = subprocess.DEVNULL)
def natural_sort(l):
import re
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
def pad(s):
'''
Prepend/append an empty space to the input.
'''
return ' ' + str(s) + ' '
def return_header(f):
open_func = return_open_func(f)
with open_func(f) as i:header = i.readline().strip()
delimiter = identify_separator(f)
header = header.split(delimiter)
return header
def valid_string(s):
if s:
return s
else:
print('Invalid String')
sys.exit(1)
def merge_files(o_file,file_list):
with open(o_file,'wt') as o:
for f in file_list:
with open(f,'rt') as i:
for line in i:
o.write(line)
def make_sure_path_exists(path):
import errno
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise