annotate tools/mummer/mummer.py @ 0:b0551f2a5986 draft

Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
author peterjc
date Mon, 27 Oct 2014 12:56:55 -0400
parents
children 8f93c1b7609e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
2 """MUMmer wrapper calling nucmer/promer/mummerplot etc,
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
3
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
4 Takes the following command line options,
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
5 1. FASTA filename of species A
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
6 2. FASTA filename of species B
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
7 3. Algorithm, nucmer or promer
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
8 4. PNG output filename
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
9 5. PDF output filename
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
10 """
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
11
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
12 import os
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
13 import sys
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
14 import tempfile
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
15 import shutil
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
16
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
17 def stop_err( msg ):
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
18 sys.stderr.write("%s\n" % msg)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
19 sys.exit(1)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
20
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
21 def run(cmd):
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
22 print(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
23 return_code = os.system(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
24 if return_code:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
25 stop_err("Error %i from: %s" % (return_code, cmd))
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
26
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
27 if "-v" in sys.argv [1:]or "--version" in sys.argv[1:]:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
28 print("MUMmer wrapper v0.0.1\n")
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
29 os.system("nucmer --version")
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
30 os.system("promer --version")
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
31 os.system("mummerplot --version")
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
32 sys.exit(0)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
33
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
34 #Parse Command Line
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
35 #TODO - optparse
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
36 try:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
37 fasta_a, fasta_b, algorithm, png_out, pdf_out = sys.argv[1:]
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
38 except:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
39 stop_err("Expect 5 arguments, got %i" % (len(sys.argv) - 1))
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
40
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
41
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
42 valid_algo = ["mummer", "nucmer", "promer"]
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
43 if algorithm not in valid_algo:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
44 stop_err("Invalid algorithm argument %r, should be: %s" % (algorithm, ", ".join(valid_algo)))
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
45
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
46 base_path = tempfile.mkdtemp()
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
47 prefix = os.path.join(base_path, "ref_qry")
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
48 coords = prefix + ".mums"
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
49 #gnuplot = prefix + ".gp"
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
50 ps_image = prefix + ".ps"
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
51 png_image = prefix + ".png"
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
52
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
53 if algorithm == "mummer":
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
54 #Add -mum as per example to find maximal unique matches between ref and query.
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
55 #Add the -b -c options to search both strands and report relative to forward strand
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
56 #which then matches the default dual-strand approach in nucmer and promer
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
57 cmd = '%s -mum -b -c "%s" "%s" > %s' % (algorithm, fasta_a, fasta_b, coords)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
58 else:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
59 coords = "out.delta"
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
60 cmd = '%s "%s" "%s"' % (algorithm, fasta_a, fasta_b)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
61 run(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
62
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
63 # PNG
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
64 # ===
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
65 cmd = 'mummerplot -R "%s" -Q "%s" --png --large --prefix=%s %s' % (fasta_a, fasta_b, prefix, coords)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
66 run(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
67 shutil.move(png_image, png_out)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
68
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
69 # PS --> PDF
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
70 # ==========
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
71 # Using --large, puts "set size 3,3" in the gnuplot - which seems to mess up.
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
72 # Problem here is the default bbox (BoundingBox) in the PS output is letter page size,
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
73 # and even if we override that, at least when view the PS output in Adobe Illustrator
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
74 # things don't seem to be lined up properly :(
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
75 #
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
76 # Using "set size 1,1" works better - which is what --small gives:
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
77 cmd = 'mummerplot -R "%s" -Q "%s" --postscript --small --prefix=%s %s' % (fasta_a, fasta_b, prefix, coords)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
78 run(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
79 cmd = 'ps2pdf -dEPSCrop "%s" "%s"' % (ps_image, pdf_out)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
80 run(cmd)
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
81
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
82 #Remove temp files...
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
83 os.remove(coords) # Might not be under the temp directory...
b0551f2a5986 Uploaded v0.0.1, essentially a preview (previously only on the TestToolShed). No tests yet, no gnuplot or ps2pdf dependency yet.
peterjc
parents:
diff changeset
84 shutil.rmtree(base_path)