annotate tools/mira4_0/mira4_make_bam.py @ 2:4eb32a3d67d1 draft

v0.0.8 - renamed folder, added note about mirabait
author peterjc
date Wed, 02 Sep 2015 07:46:29 -0400
parents
children 1713289d9908
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
2 """Wrapper script using miraconvert & samtools to get BAM from MIRA.
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
3 """
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
4 import os
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
5 import sys
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
6 import shutil
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
7 import subprocess
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
8 import tempfile
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
9
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
10 def sys_exit(msg, err=1):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
11 sys.stderr.write(msg+"\n")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
12 sys.exit(err)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
13
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
14 def run(cmd, log_handle):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
15 try:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
16 child = subprocess.Popen(cmd, shell=True,
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
17 stdout=subprocess.PIPE,
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
18 stderr=subprocess.STDOUT)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
19 except Exception, err:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
20 sys.stderr.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
21 #TODO - call clean up?
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
22 log_handle.write("Error invoking command:\n%s\n\n%s\n" % (cmd, err))
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
23 sys.exit(1)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
24 #Use .communicate as can get deadlocks with .wait(),
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
25 stdout, stderr = child.communicate()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
26 assert not stderr #Should be empty as sent to stdout
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
27 if len(stdout) > 10000:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
28 #miraconvert can be very verbose (is holding stdout in RAM a problem?)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
29 stdout = stdout.split("\n")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
30 stdout = stdout[:10] + ["...", "<snip>", "..."] + stdout[-10:]
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
31 stdout = "\n".join(stdout)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
32 log_handle.write(stdout)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
33 return child.returncode
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
34
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
35 def depad(fasta_file, sam_file, bam_file, log_handle):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
36 log_handle.write("\n================= Converting MIRA assembly from SAM to BAM ===================\n")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
37 #Also doing SAM to (uncompressed) BAM during depad
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
38 bam_stem = bam_file + ".tmp" # Have write permissions and want final file in this folder
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
39 cmd = 'samtools depad -S -u -T "%s" "%s" | samtools sort - "%s"' % (fasta_file, sam_file, bam_stem)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
40 return_code = run(cmd, log_handle)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
41 if return_code:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
42 return "Error %i from command:\n%s" % (return_code, cmd)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
43 if not os.path.isfile(bam_stem + ".bam"):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
44 return "samtools depad or sort failed to produce BAM file"
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
45
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
46 log_handle.write("\n====================== Indexing MIRA assembly BAM file =======================\n")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
47 cmd = 'samtools index "%s.bam"' % bam_stem
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
48 return_code = run(cmd, log_handle)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
49 if return_code:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
50 return "Error %i from command:\n%s" % (return_code, cmd)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
51 if not os.path.isfile(bam_stem + ".bam.bai"):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
52 return "samtools indexing of BAM file failed to produce BAI file"
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
53
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
54 shutil.move(bam_stem + ".bam", bam_file)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
55 os.remove(bam_stem + ".bam.bai") #Let Galaxy handle that...
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
56
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
57
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
58 def make_bam(mira_convert, maf_file, fasta_file, bam_file, log_handle):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
59 if not os.path.isfile(mira_convert):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
60 return "Missing binary %r" % mira_convert
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
61 if not os.path.isfile(maf_file):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
62 return "Missing input MIRA file: %r" % maf_file
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
63 if not os.path.isfile(fasta_file):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
64 return "Missing padded FASTA file: %r" % fasta_file
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
65
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
66 log_handle.write("\n====================== Converting MIRA assembly to SAM =======================\n")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
67 tmp_dir = tempfile.mkdtemp()
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
68 sam_file = os.path.join(tmp_dir, "x.sam")
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
69
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
70 # Note add nbb to the template name, possible MIRA 4.0 RC4 bug
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
71 cmd = '"%s" -f maf -t samnbb "%s" "%snbb"' % (mira_convert, maf_file, sam_file)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
72 return_code = run(cmd, log_handle)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
73 if return_code:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
74 return "Error %i from command:\n%s" % (return_code, cmd)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
75 if not os.path.isfile(sam_file):
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
76 return "Conversion from MIRA to SAM failed"
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
77
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
78 #Also doing SAM to (uncompressed) BAM during depad
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
79 msg = depad(fasta_file, sam_file, bam_file, log_handle)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
80 if msg:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
81 return msg
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
82
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
83 os.remove(sam_file)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
84 os.rmdir(tmp_dir)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
85
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
86 return None #Good :)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
87
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
88 if __name__ == "__main__":
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
89 mira_convert, maf_file, fasta_file, bam_file = sys.argv[1:]
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
90 msg = make_bam(mira_convert, maf_file, fasta_file, bam_file, sys.stdout)
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
91 if msg:
4eb32a3d67d1 v0.0.8 - renamed folder, added note about mirabait
peterjc
parents:
diff changeset
92 sys_exit(msg)