Mercurial > repos > peterjc > samtools_idxstats
comparison tools/samtools_idxstats/samtools_idxstats.py @ 2:71afa65f444a draft default tip
v0.0.5 Internal changes to command line handling
author | peterjc |
---|---|
date | Tue, 16 May 2017 09:24:18 -0400 |
parents | 8945bad80f4a |
children |
comparison
equal
deleted
inserted
replaced
1:8945bad80f4a | 2:71afa65f444a |
---|---|
8 | 8 |
9 This messes about with the filenames to make samtools happy, then | 9 This messes about with the filenames to make samtools happy, then |
10 runs "samtools idxstats" and captures the output to the desired | 10 runs "samtools idxstats" and captures the output to the desired |
11 tabular file. | 11 tabular file. |
12 """ | 12 """ |
13 | |
14 import os | |
13 import sys | 15 import sys |
14 import os | |
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.2)" | 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 sys_exit(msg, error_level=1): | |
25 """Print error message to stdout and quit with given error level.""" | |
26 sys.stderr.write("%s\n" % msg) | |
27 sys.exit(error_level) | |
28 | |
29 if len(sys.argv) != 4: | 24 if len(sys.argv) != 4: |
30 sys_exit("Require three arguments: BAM, BAI, tabular filenames") | 25 sys.exit("Require three arguments: BAM, BAI, tabular filenames") |
31 | 26 |
32 bam_filename, bai_filename, tabular_filename = sys.argv[1:] | 27 bam_filename, bai_filename, tabular_filename = sys.argv[1:] |
33 | 28 |
34 if not os.path.isfile(bam_filename): | 29 if not os.path.isfile(bam_filename): |
35 sys_exit("Input BAM file not found: %s" % bam_filename) | 30 sys.exit("Input BAM file not found: %s" % bam_filename) |
36 if not os.path.isfile(bai_filename): | 31 if not os.path.isfile(bai_filename): |
37 if bai_filename == "None": | 32 if bai_filename == "None": |
38 sys_exit("Error: Galaxy did not index your BAM file") | 33 sys.exit("Error: Galaxy did not index your BAM file") |
39 sys_exit("Input BAI file not found: %s" % bai_filename) | 34 sys.exit("Input BAI file not found: %s" % bai_filename) |
40 | 35 |
41 #Assign sensible names with real extensions, and setup symlinks: | 36 # Assign sensible names with real extensions, and setup symlinks: |
42 tmp_dir = tempfile.mkdtemp() | 37 tmp_dir = tempfile.mkdtemp() |
43 bam_file = os.path.join(tmp_dir, "temp.bam") | 38 bam_file = os.path.join(tmp_dir, "temp.bam") |
44 bai_file = os.path.join(tmp_dir, "temp.bam.bai") | 39 bai_file = os.path.join(tmp_dir, "temp.bam.bai") |
45 os.symlink(os.path.abspath(bam_filename), bam_file) | 40 os.symlink(os.path.abspath(bam_filename), bam_file) |
46 os.symlink(os.path.abspath(bai_filename), bai_file) | 41 os.symlink(os.path.abspath(bai_filename), bai_file) |
47 assert os.path.isfile(bam_file), bam_file | 42 assert os.path.isfile(bam_file), bam_file |
48 assert os.path.isfile(bai_file), bai_file | 43 assert os.path.isfile(bai_file), bai_file |
49 assert os.path.isfile(bam_file + ".bai"), bam_file | 44 assert os.path.isfile(bam_file + ".bai"), bam_file |
50 | 45 |
51 #Run samtools idxstats: | 46 # Run samtools idxstats: |
52 cmd = 'samtools idxstats "%s" > "%s"' % (bam_file, tabular_filename) | 47 cmd = 'samtools idxstats "%s" > "%s"' % (bam_file, tabular_filename) |
53 return_code = os.system(cmd) | 48 return_code = os.system(cmd) |
54 | 49 |
55 #Remove the temp symlinks: | 50 # Remove the temp symlinks: |
56 os.remove(bam_file) | 51 os.remove(bam_file) |
57 os.remove(bai_file) | 52 os.remove(bai_file) |
58 os.rmdir(tmp_dir) | 53 os.rmdir(tmp_dir) |
59 | 54 |
60 if return_code: | 55 if return_code: |
61 sys_exit("Return code %i from command:\n%s" % (return_code, cmd)) | 56 sys.exit("Return code %i from command:\n%s" % (return_code, cmd)) |