annotate tools/samtools_idxstats/samtools_idxstats.py @ 0:d4412c04d7b1 draft

Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
author peterjc
date Wed, 20 Nov 2013 12:27:33 -0500
parents
children 8945bad80f4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 """
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
13 import sys
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
14 import os
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
15 import subprocess
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:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
19 #Galaxy seems to invert the order of the two lines
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
20 print "(Galaxy wrapper v0.0.1)"
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 def stop_err(msg, error_level=1):
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
25 """Print error message to stdout and quit with given error level."""
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
26 sys.stderr.write("%s\n" % msg)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
27 sys.exit(error_level)
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 len(sys.argv) != 4:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
30 stop_err("Require three arguments: BAM, BAI, tabular filenames")
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
31
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
32 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
33
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
34 if not os.path.isfile(bam_filename):
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
35 stop_err("Input BAM file not found: %s" % bam_filename)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
36 if not os.path.isfile(bai_filename):
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
37 if bai_filename == "None":
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
38 stop_err("Error: Galaxy did not index your BAM file")
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
39 stop_err("Input BAI file not found: %s" % bai_filename)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
40
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
41 #Assign sensible names with real extensions, and setup symlinks:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
42 tmp_dir = tempfile.mkdtemp()
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
51 #Run samtools idxstats:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
52 cmd = "samtools idxstats %s > %s" % (bam_file, tabular_filename)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
53 return_code = os.system(cmd)
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 #Remove the temp symlinks:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
56 os.remove(bam_file)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
57 os.remove(bai_file)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
58 os.rmdir(tmp_dir)
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
59
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
60 if return_code:
d4412c04d7b1 Uploaded v0.0.1 (as tested previously on the Test Tool Shed)
peterjc
parents:
diff changeset
61 stop_err("Return code %i from command:\n%s" % (return_code, cmd))