-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitbam.py
More file actions
37 lines (30 loc) · 970 Bytes
/
splitbam.py
File metadata and controls
37 lines (30 loc) · 970 Bytes
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
import getopt, sys, os
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:", ["help", "input="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
infile = None
for opt, arg in opts:
if opt in ("-h","--help"):
usage()
sys.exit()
elif opt in ("-i", "--input"):
if os.path.isfile(arg):
infile = arg
else:
assert False, "Unhandled option"
if infile is not None:
splitbam(infile)
def splitbam(infile):
acceptable_chr = ["chr" + str(x + 1) for x in range(22)]
acceptable_chr.append("chrX")
acceptable_chr.append("chrY")
for chr in acceptable_chr:
samtools_call = f"samtools view {infile} {chr} -b > {chr}_{infile}"
print(samtools_call)
os.system(samtools_call)
if __name__ == "__main__":
main()