comparison tools/samtools_idxstats/samtools_idxstats.py @ 1:8945bad80f4a draft

v0.0.4; internal changes for packaging
author peterjc
date Wed, 13 May 2015 10:35:50 -0400
parents d4412c04d7b1
children 71afa65f444a
comparison
equal deleted inserted replaced
0:d4412c04d7b1 1:8945bad80f4a
15 import subprocess 15 import subprocess
16 import tempfile 16 import tempfile
17 17
18 if "-v" in sys.argv or "--version" in sys.argv: 18 if "-v" in sys.argv or "--version" in sys.argv:
19 #Galaxy seems to invert the order of the two lines 19 #Galaxy seems to invert the order of the two lines
20 print "(Galaxy wrapper v0.0.1)" 20 print "(Galaxy wrapper v0.0.2)"
21 cmd = "samtools 2>&1 | grep -i ^Version" 21 cmd = "samtools 2>&1 | grep -i ^Version"
22 sys.exit(os.system(cmd)) 22 sys.exit(os.system(cmd))
23 23
24 def stop_err(msg, error_level=1): 24 def sys_exit(msg, error_level=1):
25 """Print error message to stdout and quit with given error level.""" 25 """Print error message to stdout and quit with given error level."""
26 sys.stderr.write("%s\n" % msg) 26 sys.stderr.write("%s\n" % msg)
27 sys.exit(error_level) 27 sys.exit(error_level)
28 28
29 if len(sys.argv) != 4: 29 if len(sys.argv) != 4:
30 stop_err("Require three arguments: BAM, BAI, tabular filenames") 30 sys_exit("Require three arguments: BAM, BAI, tabular filenames")
31 31
32 bam_filename, bai_filename, tabular_filename = sys.argv[1:] 32 bam_filename, bai_filename, tabular_filename = sys.argv[1:]
33 33
34 if not os.path.isfile(bam_filename): 34 if not os.path.isfile(bam_filename):
35 stop_err("Input BAM file not found: %s" % bam_filename) 35 sys_exit("Input BAM file not found: %s" % bam_filename)
36 if not os.path.isfile(bai_filename): 36 if not os.path.isfile(bai_filename):
37 if bai_filename == "None": 37 if bai_filename == "None":
38 stop_err("Error: Galaxy did not index your BAM file") 38 sys_exit("Error: Galaxy did not index your BAM file")
39 stop_err("Input BAI file not found: %s" % bai_filename) 39 sys_exit("Input BAI file not found: %s" % bai_filename)
40 40
41 #Assign sensible names with real extensions, and setup symlinks: 41 #Assign sensible names with real extensions, and setup symlinks:
42 tmp_dir = tempfile.mkdtemp() 42 tmp_dir = tempfile.mkdtemp()
43 bam_file = os.path.join(tmp_dir, "temp.bam") 43 bam_file = os.path.join(tmp_dir, "temp.bam")
44 bai_file = os.path.join(tmp_dir, "temp.bam.bai") 44 bai_file = os.path.join(tmp_dir, "temp.bam.bai")
47 assert os.path.isfile(bam_file), bam_file 47 assert os.path.isfile(bam_file), bam_file
48 assert os.path.isfile(bai_file), bai_file 48 assert os.path.isfile(bai_file), bai_file
49 assert os.path.isfile(bam_file + ".bai"), bam_file 49 assert os.path.isfile(bam_file + ".bai"), bam_file
50 50
51 #Run samtools idxstats: 51 #Run samtools idxstats:
52 cmd = "samtools idxstats %s > %s" % (bam_file, tabular_filename) 52 cmd = 'samtools idxstats "%s" > "%s"' % (bam_file, tabular_filename)
53 return_code = os.system(cmd) 53 return_code = os.system(cmd)
54 54
55 #Remove the temp symlinks: 55 #Remove the temp symlinks:
56 os.remove(bam_file) 56 os.remove(bam_file)
57 os.remove(bai_file) 57 os.remove(bai_file)
58 os.rmdir(tmp_dir) 58 os.rmdir(tmp_dir)
59 59
60 if return_code: 60 if return_code:
61 stop_err("Return code %i from command:\n%s" % (return_code, cmd)) 61 sys_exit("Return code %i from command:\n%s" % (return_code, cmd))