-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.py
502 lines (408 loc) · 15.7 KB
/
helper_functions.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#/usr/bin/python
# helper_functions.py
# Caleb Matthew Radens
# 2016_1_26
### A bunch of helper functions I think I'll use a lot
import os
from subprocess import call
import gzip
import time
def remove_all(array, element):
""" Remove all instances of element from array.
Arguments:
array = type: list.
element = object that you wish to remove all of from array.
"""
if not isinstance(array, list):
raise Exception("Please only give lists to this function.")
n = array.count(element)
while n > 0:
array.remove(element)
n = array.count(element)
def index_all(array, element):
"""Return all indeces of array that point to an element.
Arguments:
array = type: list.
element = object that you wish to get indeces for from array.
"""
if not isinstance(array, list):
raise Exception("Please only give lists to this function.")
matched_indices = [i for i, x in enumerate(array)if x == element]
return matched_indices
def make_scisub_job_command(
Script,
ScriptDir,
Queue = "voight_normal",
ErrOut=True,
ErrOutDir = "",
Extra="",
Language="python"):
"""Generate an appropriately formatted string for submitting a *python* job on pmacs.
Arguments:
Script: "script_to_execute.py"
ScriptDir: "/directory_with_script/"
Queue: Scisub -q command. Should be: "voight_normal" (default),
"voight_long", or "voight_priority"
ErrOut: Boolean. Should the job save log files?
ErrOutDir: "/directory_for_log_files/"
Extra: Optional string. If the script takes command line arguments,
add them here.
Language: programming language to execute job in (defaults to python)
Depends on: os
Returns a string formatted for scisub job submission.
Example usage:
make_scisub_job_command(
Script="my_script.py",
ScriptDir="/project/voight_subrate/.../scripts/",
Queue = "voight_normal",
ErrOut=True,
ErrOutDir = "/project/voight_subrate/.../logs/",
Extra="cowabung baby"
Language="python")
Example output :
(note, output is a list of 2 strings with no line breaks/newlines, I just broke them here
to ease visualization)
['bsub -e /project/voight_subrate/.../logs/my_script_year_month_day_hr_min_sec.err
-o /project/voight_subrate/.../logs/my_script_year_month_day_hr_min_sec.out
-q voight_normal
python /project/voight_subrate/.../scripts/my_script.py cowabunga baby',
'IS_COMMAND']
"""
# Languages I'm comfortable submitting jobs in:
ACCEPTABLE_LANGUAGES = ["python", "R", "bash"]
if Language not in ACCEPTABLE_LANGUAGES:
raise ValueError(str(Language)+" not one of the following: "+str(ACCEPTABLE_LANGUAGES))
elif Language == "python" or Language == "bash":
language = " "+Language+" "
else:
language = " Rscript "
if (type(Script) is not str
or type(ScriptDir) is not str
or type(Queue) is not str
or type(ErrOutDir) is not str
or type(Extra) is not str):
raise ValueError("All arguments (except ErrOut) need to be strings.")
if type(ErrOut) is not bool:
raise ValueError("ErrOut needs to be a boolean.")
if Script[-2:] != "py" and Script[-1:] != "R" and Script[-2:] != "sh":
raise ValueError("Expected a .py python, .sh shell, or .R R script, instead got: "+Script)
if not (os.path.isdir(ScriptDir)):
raise ValueError(ScriptDir+" not found.")
if ScriptDir[-1] != "/":
raise ValueError("ScriptDir needs to end with a forward slash.")
if not os.path.isfile(ScriptDir+Script):
raise ValueError(Script+" not found in "+ScriptDir)
if (Queue != "voight_normal"
and Queue != "voight_long"
and Queue != "voight_priority"):
raise ValueError(
"Expected voight_normal, voight_long, or voight_priority, instead got: "+Queue)
if len(ErrOutDir) > 0:
if not (os.path.isdir(ErrOutDir)):
raise ValueError(ErrOutDir+" not found.")
if ErrOutDir[-1] != "/":
raise ValueError("ErrOutDir needs to end with a forward slash.")
if len(Extra) > 0:
# Add space before the extra commands
if Extra[0] == " ":
raise ValueError("No space in the beginning of Extra, please.")
Extra = " "+Extra
if ErrOut:
time_stamp = time.strftime("%Y_%m_%d_%H_%M_%S")
command = "bsub -e "+ErrOutDir+Script[0:-2]+"_"+time_stamp+".err "
command = command + "-o "+ErrOutDir+Script[0:-2:]+"_"+time_stamp+".out "
command = command + "-q "+Queue
command = command + language +ScriptDir+Script+Extra
else:
command = "bsub "
command = command + "-q "+Queue
command = command + language +ScriptDir+Script+Extra
return [command, "IS_SCISUB_COMMAND"]
def submit_scisub_job(Command):
""" Given the output from make_scisub_job_command, submit a job.
"""
if type(Command) is not list:
raise ValueError("Command isn't from make_scisub_job_command...(not a list)")
if Command[1] != "IS_SCISUB_COMMAND":
raise ValueError("Command isn't from make_scisub_job_command...(where is 'IS_SCISUB_COMMAND'?)")
# Submit a system command
call([Command[0]],shell=True)
def make_consign_job_command(
Script,
ScriptDir,
ErrOut=True,
ErrOutDir = "",
Extra="",
Language = "python"):
"""Generate an appropriately formatted string for submitting a *python* job on consign.pmacs
Arguments:
Script: "script_to_execute.py"
ScriptDir: "/directory_with_script/"
ErrOut: Boolean. Should the job save log files?
ErrOutDir: "/directory_for_log_files/"
Extra: Optional string. If the script takes command line arguments,
add them here.
Language: programming language to execute job in (defaults to python)
Depends on: os
Returns a string formatted for consign job submission.
Example usage:
make_consign_job_command(
Script="my_script.py",
ScriptDir="/project/chrbrolab/.../scripts/",
ErrOut=True,
ErrOutDir = "/project/chrbrolab/.../logs/",
Extra="cowabung baby"
Language="python")
Example output :
(note, output is a list of 2 strings with no line breaks/newlines,
I just broke them here to ease visualization)
['bsub -e /project/chrbrolab/.../logs/my_script_year_month_day_hr_min_sec.err
-o /project/chrbrolab/.../logs/my_script_year_month_day_hr_min_sec.out
python /project/chrbrolab/.../scripts/my_script.py cowabunga baby',
'IS_CONSIGN_COMMAND']
"""
# Languages I'm comfortable submitting jobs in:
ACCEPTABLE_LANGUAGES = ["python", "R"]
if Language not in ACCEPTABLE_LANGUAGES:
raise ValueError(str(Language)+" not one of the following: "+str(ACCEPTABLE_LANGUAGES))
elif Language == "python":
language = " "+Language+" "
else:
language = " Rscript "
if (type(Script) is not str
or type(ScriptDir) is not str
or type(ErrOutDir) is not str
or type(Extra) is not str):
raise ValueError("All arguments (except ErrOut) need to be strings.")
if type(ErrOut) is not bool:
raise ValueError("ErrOut needs to be a boolean.")
if Script[-2:] != "py" and Script[-1:] != "R":
raise ValueError("Expected a .py python or .R R script, instead got: "+Script)
if not (os.path.isdir(ScriptDir)):
raise ValueError(ScriptDir+" not found.")
if ScriptDir[-1] != "/":
raise ValueError("ScriptDir needs to end with a forward slash.")
if not os.path.isfile(ScriptDir+Script):
raise ValueError(Script+" not found in "+ScriptDir)
if len(ErrOutDir) > 0:
if not (os.path.isdir(ErrOutDir)):
raise ValueError(ErrOutDir+" not found.")
if ErrOutDir[-1] != "/":
raise ValueError("ErrOutDir needs to end with a forward slash.")
if len(Extra) > 0:
# Add space before the extra commands
if Extra[0] == " ":
raise ValueError("No space in the beginning of Extra, please.")
Extra = " "+Extra
if ErrOut:
time_stamp = time.strftime("%Y_%m_%d_%H_%M_%S")
command = "bsub -e "+ErrOutDir+Script[0:-2]+"_"+time_stamp+".err "
command = command + " -e " + ErrOutDir+Script[0:-2]+"_"+time_stamp+".err "
command = command + "-o " + ErrOutDir+Script[0:-2:]+"_"+time_stamp+".out "
command = command + language + ScriptDir+Script+Extra
else:
command = "bsub "
command = command + language + ScriptDir+Script+Extra
return [command, "IS_CONSIGN_COMMAND"]
def submit_consign_job(Command):
""" Given the output from make_consign_job_command, submit a job.
"""
if type(Command) is not list:
raise ValueError("Command isn't from make_consign_job_command...(not a list)")
if Command[1] != "IS_CONSIGN_COMMAND":
raise ValueError("Command isn't from make_consign_job_command...(where is 'IS_CONSIGN_COMMAND'?)")
# Submit a system command
call([Command[0]],shell=True)
def gz_head(File, Dir="", Lines=10):
""" Preview top Lines of a file.gz
Arguments:
File: "my_fav_file.txt.gz" [needs to be a .gz file]
Dir: "/my_directory/" [optional, you may make File a the full filepath instead.]
Lines: Integer greater than 0.
Split: Optional string. If
"""
if type(File) is not str or type(Dir) is not str:
raise ValueError("All arguments need to be strings.")
if type(Lines) is not int or Lines < 1:
raise ValueError("Lines needs to be an integer > 0.")
if len(Dir) > 0:
if not (os.path.isdir(Dir)):
raise ValueError(Dir+" not found.")
if Dir[-1] != "/":
raise ValueError("Dir needs to end with a forward slash.")
if File[-2:] != "gz":
raise ValueError("File needs to ba a .gz file.")
if not os.path.isfile(Dir+File):
raise ValueError(File+" not found in directory\n"+Dir)
path = Dir+File
# 'rb' means read
f_IN = gzip.open(path, 'rb')
for line in f_IN:
if Lines == 0:
break
# Remove newline chars and split by tab
split_line = line.rstrip('\r\n').split('\t')
print split_line
print '\n'
Lines = Lines-1
f_IN.close()
def my_head(File, Dir="", Lines=10):
""" Preview top Lines of a (non gz) file
Arguments:
File: "my_fav_file.txt"
Dir: "/my_directory/" [optional, you may make File a the full filepath instead.]
Lines: Integer greater than 0.
"""
if type(File) is not str or type(Dir) is not str:
raise ValueError("All arguments need to be strings.")
if type(Lines) is not int or Lines < 1:
raise ValueError("Lines needs to be an integer > 0.")
if len(Dir) > 0:
if not (os.path.isdir(Dir)):
raise ValueError(Dir+" not found.")
if Dir[-1] != "/":
raise ValueError("Dir needs to end with a forward slash.")
if File[-2:] == "gz":
raise ValueError("Please do not use this function on .gz files.")
if not os.path.isfile(Dir+File):
raise ValueError(File+" not found in directory\n"+Dir)
path = Dir+File
with open(path, 'rb') as the_file:
content = the_file.readlines()
for line in content:
if Lines == 0:
break
# Remove newline chars and split by tab
# split_line = line.rstrip('\r\n').split('\t')
print line
Lines = Lines-1
def bash_sort(File, In_dir, Out_dir, Col, Delim = "\\t", Sort_style="", Header = True):
""" Bash sort a file, return location of sorted file.
Arguments:
File: "my_fav_file.txt"
In_dir: "/my_directory/" [optional, you may make File a the full filepath instead
Out_dir: "/some_dir" where sorted file is saved
Col: integer. Which column to sort by? [1 = first column]
Delim: string. If tab, must be '\\t'
Sort_style: string. 'n' or '' ['n' if column is numeric, empty if not]
Header: boolean. Does the file have a header?
Assumptions:
File is not gz-zipped (or compressed at all)
File ends in '.'txt'
File has a single lined header, or no header
Returns: filepath of the sorted file
"""
if type(File) is not str or type(In_dir) is not str or type(Out_dir) is not str:
raise TypeError("File, In_dir, and Out_dir need to be strings.")
if type(Col) is not int or Col <= 0:
raise ValueError("Col needs to be an integer > 0.")
if len(In_dir) > 0:
if not (os.path.isdir(In_dir)):
raise ValueError(In_dir+" not found.")
if In_dir[-1] != "/":
raise ValueError("In_dir needs to end with a forward slash.")
if len(Out_dir) > 0:
if not (os.path.isdir(Out_dir)):
raise ValueError(Out_dir+" not found.")
if Out_dir[-1] != "/":
raise ValueError("Out_dir needs to end with a forward slash.")
if File[-4:] != ".txt":
raise ValueError("Please only use this function on .txt files.")
if not os.path.isfile(In_dir+File):
raise ValueError(File+" not found in directory\n"+In_dir)
if type(Sort_style) is not str:
raise TypeError("Sort_style needs to be a string")
if Sort_style != "n" and Sort_style != "":
raise ValueError("Sort_style needs to be 'n' or '', not: "+Sort_style)
print "Passed bash_sort checks."
in_file_path = In_dir + File
out_file_path = Out_dir + File[:-4]+"_sorted.txt"
# Which column will be sorted by
sort_at = str(Col)+","+str(Col)
if Header:
# Save the header to out_file
command = "head " + in_file_path + " -n 1 > " + out_file_path
call([command], shell= True)
# This command is for checking if the column is already sorted.
check_if_sorted_command = "tail -n +2 " + in_file_path + " | sort -t "+Delim+" -"+Sort_style+"k " + sort_at + " -c"
test_sorted = call([check_if_sorted_command], shell = True)
# If not sorted:
if test_sorted == 1:
# This sorts the file, but skips the header when sorting it, and writes the result to file
command = "tail -n +2 " + in_file_path + " | sort -t "+Delim+" -"+Sort_style+"k " + sort_at + " >> " + out_file_path
call([command], shell = True)
# Else not sorted, just return original file path
else:
# Delete out_file that was going to be sorted
command = "rm "+out_file_path
call([command], shell = True)
return in_file_path
# Else no header
else:
# This command is for checking if the column is already sorted.
check_if_sorted_command = "sort "+in_file_path+" -t "+Delim+" -"+Sort_style+"k " + sort_at + " -c"
test_sorted = call([check_if_sorted_command], shell = True)
# If not sorted:
if test_sorted == 1:
command = "sort "+in_file_path+" -t "+Delim+" -"+Sort_style+"k " + sort_at + " > " + out_file_path
call([command], shell = True)
# Else file is already sorted
else:
return in_file_path
return out_file_path
def grep_for_files(Dir, Pattern, Lacks = ""):
"""Search a directory (recursively) for files that contain 'Pattern' in their name.
Arguments:
Dir: "/my_directory/"
Pattern:"pattern_to_match"
Lacks: A pattern file needs to lack
string
Assumptions:
Dir is extant
Pattern is non-empty
Returns:
A list of files that matched (full file path!) - will be [] if no matches.
"""
if type(Dir) is not str or type(Pattern) is not str:
raise ValueError("Dir and Pattern need to be strings.")
if not (os.path.isdir(Dir)):
raise ValueError(Dir+" not found.")
if len(Pattern) == 0:
raise ValueError("Pattern was an empty string. It should not be an empty string.")
if not isinstance(Lacks, str):
raise ValueError("Lacks needs to be a string (empty '' is fine)")
matched_files = list()
for root, subdirs, files in os.walk(Dir):
# ID files that contain the Pattern
for f in files:
if Pattern in f:
if len(Lacks)>0:
# Skip file if i contains the Lacks string
if Lacks in f:
continue
matched_files.append(os.path.join(root,f))
return matched_files
def new_sub_dir_file(File_path, Appendage):
"""Given a file path, return a new file path in the same directory with an appended text
to the parent diretcory of the file_path.
example:File_path = "/HOME/.../my_dir/hello_world.care_bears"
Appendage = ".SEQ"
returns: "/HOME/.../my_dir/my_dir.SEQ"
Arguments:
File_path: ".../my_directory/my_file"
Appendage:"string_to_append_to_new_file_path"
Assumptions:
File_path points to real file
Appendage is non-empty
Returns:
A string (the new file_path)
"""
if type(File_path) is not str or type(Appendage) is not str:
raise ValueError("File_path and Appendage need to be strings.")
if not (os.path.isfile(File_path)):
raise ValueError(File_path+" not found.")
if len(Appendage) == 0:
raise ValueError("Appendage was an empty string. It should not be an empty string.")
directory = os.path.dirname(File_path)
new_file = os.path.join(directory, os.path.basename(directory)+Appendage)
return new_file