Mercurial > repos > peterjc > samtools_idxstats
annotate 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 |
rev | line source |
---|---|
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
2 """Wrapper for "samtools idxstats" for use in Galaxy. |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
3 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
4 This script takes exactly three command line arguments: |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
5 * Input BAM filename |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
6 * Input BAI filename (via Galaxy metadata) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
7 * Output tabular filename |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
8 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
9 This messes about with the filenames to make samtools happy, then |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
10 runs "samtools idxstats" and captures the output to the desired |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
11 tabular file. |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
12 """ |
2 | 13 |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
14 import os |
2 | 15 import sys |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
16 import tempfile |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
17 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
18 if "-v" in sys.argv or "--version" in sys.argv: |
2 | 19 # Galaxy seems to invert the order of the two lines |
1 | 20 print "(Galaxy wrapper v0.0.2)" |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
21 cmd = "samtools 2>&1 | grep -i ^Version" |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
22 sys.exit(os.system(cmd)) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
23 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
24 if len(sys.argv) != 4: |
2 | 25 sys.exit("Require three arguments: BAM, BAI, tabular filenames") |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
26 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
27 bam_filename, bai_filename, tabular_filename = sys.argv[1:] |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
28 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
29 if not os.path.isfile(bam_filename): |
2 | 30 sys.exit("Input BAM file not found: %s" % bam_filename) |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
31 if not os.path.isfile(bai_filename): |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
32 if bai_filename == "None": |
2 | 33 sys.exit("Error: Galaxy did not index your BAM file") |
34 sys.exit("Input BAI file not found: %s" % bai_filename) | |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
35 |
2 | 36 # Assign sensible names with real extensions, and setup symlinks: |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
37 tmp_dir = tempfile.mkdtemp() |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
38 bam_file = os.path.join(tmp_dir, "temp.bam") |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
39 bai_file = os.path.join(tmp_dir, "temp.bam.bai") |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
40 os.symlink(os.path.abspath(bam_filename), bam_file) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
41 os.symlink(os.path.abspath(bai_filename), bai_file) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
42 assert os.path.isfile(bam_file), bam_file |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
43 assert os.path.isfile(bai_file), bai_file |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
44 assert os.path.isfile(bam_file + ".bai"), bam_file |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
45 |
2 | 46 # Run samtools idxstats: |
1 | 47 cmd = 'samtools idxstats "%s" > "%s"' % (bam_file, tabular_filename) |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
48 return_code = os.system(cmd) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
49 |
2 | 50 # Remove the temp symlinks: |
0
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
51 os.remove(bam_file) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
52 os.remove(bai_file) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
53 os.rmdir(tmp_dir) |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
54 |
d4412c04d7b1
Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff
changeset
|
55 if return_code: |
2 | 56 sys.exit("Return code %i from command:\n%s" % (return_code, cmd)) |