0
|
1 #!/usr/bin/env python
|
|
2 """Wrapper for "samtools depad" for use in Galaxy.
|
|
3
|
|
4 This script takes exactly four command line arguments:
|
|
5 * Input padded reference FASTA file
|
|
6 * Input SAM/BAM filename
|
|
7 * Input format ("sam" or "bam")
|
|
8 * Output BAM filename
|
|
9
|
|
10 Runs "samtools depad" and captures the output to the desired BAM file.
|
|
11 """
|
|
12 import sys
|
|
13 import os
|
|
14 import subprocess
|
|
15 import tempfile
|
|
16
|
|
17 if "-v" in sys.argv or "--version" in sys.argv:
|
|
18 #Galaxy seems to invert the order of the two lines
|
|
19 print "(Galaxy wrapper v0.0.1)"
|
|
20 cmd = "samtools 2>&1 | grep -i ^Version"
|
|
21 sys.exit(os.system(cmd))
|
|
22
|
|
23 def stop_err(msg, error_level=1):
|
|
24 """Print error message to stdout and quit with given error level."""
|
|
25 sys.stderr.write("%s\n" % msg)
|
|
26 sys.exit(error_level)
|
|
27
|
|
28 if len(sys.argv) != 5:
|
|
29 stop_err("Require four arguments: padded FASTA, SAM/BAM file, format (SAM or BAM), output BAM filenames")
|
|
30
|
|
31 padded_ref, bam_filename, input_format, output_filename = sys.argv[1:]
|
|
32
|
|
33 if not os.path.isfile(padded_ref):
|
|
34 stop_err("Input padded reference FASTA file not found: %s" % padded_ref)
|
|
35 if not os.path.isfile(bam_filename):
|
|
36 stop_err("Input BAM file not found: %s" % bam_filename)
|
|
37 if input_format.lower() not in ["sam", "bam"]:
|
|
38 stop_err("Input format should be SAM or BAM, not %r" % input_format)
|
|
39
|
|
40 #Run samtools depad:
|
|
41 if input_format.lower() == "sam":
|
|
42 cmd = "samtools depad -S -T %s %s > %s" % (padded_ref, bam_filename, output_filename)
|
|
43 else:
|
|
44 cmd = "samtools depad -T %s %s > %s" % (padded_ref, bam_filename, output_filename)
|
|
45 return_code = os.system(cmd)
|
|
46
|
|
47 if return_code:
|
|
48 stop_err("Return code %i from command:\n%s" % (return_code, cmd))
|