-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
80 lines (59 loc) · 2.41 KB
/
util.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
import sys
import string
import subprocess
import os
DNA_COMP = None
def comp(seq_str):
"""complements the provided DNA sequence and returns it"""
global DNA_COMP
#### MAHEETHA CHANGED STRING into STR before the maketrans
if DNA_COMP is None:
DNA_COMP = str.maketrans("ATCGMRWSYKNatcgmrwsykn",
"TAGCKYWSRMNtagckywsrmn")
return seq_str.translate(DNA_COMP)
def revcomp(seq_str):
"""returns reverse complement of provided DNA sequence"""
return comp(seq_str)[::-1]
def sort_bam(input_bam, output_prefix):
"""Calls samtools sort on input_bam filename and writes to
output_bam. Takes into account that the command line arguments
for samtools sort have changed between versions."""
output_bam = output_prefix + ".sort.bam"
# first try new way of using samtools sort
failed = False
cmd = "samtools sort -o " + output_bam + " " + input_bam
sys.stderr.write("running command: %s\n" % cmd)
try:
subprocess.check_call(cmd, shell=True)
except Exception as e:
sys.stderr.write("samtools sort command failed:\n%s\n" %
str(e))
failed = True
if not os.path.exists(output_bam):
sys.stderr.write("output file %s does not exist\n" % output_bam)
failed = True
if failed:
# OLD way of calling samtools (changed in newer versions)
sys.stderr.write("samtools sort command failed, trying old samtools "
"syntax\n")
cmd = "samtools sort " + input_bam + " " + output_prefix
sys.stderr.write("running command: %s\n" % cmd)
try:
subprocess.check_call(cmd, shell=True)
except Exception as e:
sys.stderr.write("samtools sort command failed:\n%s\n" %
str(e))
exit(1)
if not os.path.exists(paths.sorted_output_bam):
raise IOError("Failed to create sorted BAM file '%s'" %
paths.sorted_output_bam)
def is_gzipped(filename):
"""Checks first two bytes of provided filename and looks for
gzip magic number. Returns true if it is a gzipped file"""
f = open(filename, "rb")
# read first two bytes
byte1 = f.read(1)
byte2 = f.read(1)
f.close()
# check against gzip magic number 1f8b
return (byte1 == chr(0x1f)) and (byte2 == chr(0x8b))