annotate cpt_mist3/mist3.py @ 0:1a9603d09814 draft default tip

Uploaded
author cpt
date Fri, 17 Jun 2022 02:58:50 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
1 #!/usr/bin/env python
1a9603d09814 Uploaded
cpt
parents:
diff changeset
2 import argparse
1a9603d09814 Uploaded
cpt
parents:
diff changeset
3 import time
1a9603d09814 Uploaded
cpt
parents:
diff changeset
4 import tempfile
1a9603d09814 Uploaded
cpt
parents:
diff changeset
5 import pprint
1a9603d09814 Uploaded
cpt
parents:
diff changeset
6 import shutil
1a9603d09814 Uploaded
cpt
parents:
diff changeset
7 import os
1a9603d09814 Uploaded
cpt
parents:
diff changeset
8 import math
1a9603d09814 Uploaded
cpt
parents:
diff changeset
9 from string import Template
1a9603d09814 Uploaded
cpt
parents:
diff changeset
10 from Bio import SeqIO
1a9603d09814 Uploaded
cpt
parents:
diff changeset
11 import subprocess
1a9603d09814 Uploaded
cpt
parents:
diff changeset
12 import sys
1a9603d09814 Uploaded
cpt
parents:
diff changeset
13 import logging
1a9603d09814 Uploaded
cpt
parents:
diff changeset
14
1a9603d09814 Uploaded
cpt
parents:
diff changeset
15 FORMAT = "[%(levelname)s:%(filename)s:%(lineno)s:%(funcName)s()] %(message)s"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
16 logging.basicConfig(level=logging.INFO, format=FORMAT)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
17 log = logging.getLogger("mist")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
18 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
19
1a9603d09814 Uploaded
cpt
parents:
diff changeset
20 MONTAGE_BORDER = 80
1a9603d09814 Uploaded
cpt
parents:
diff changeset
21 IMAGE_BORDER = 2
1a9603d09814 Uploaded
cpt
parents:
diff changeset
22
1a9603d09814 Uploaded
cpt
parents:
diff changeset
23 MONTAGE_BORDER_COORD = "%sx%s" % (MONTAGE_BORDER, MONTAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
24 IMAGE_BORDER_COORD = "%sx%s" % (IMAGE_BORDER, IMAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
25
1a9603d09814 Uploaded
cpt
parents:
diff changeset
26 DOUBLE_IMAGE_BORDER_COORD = "%sx%s" % (2 * IMAGE_BORDER, 2 * IMAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
27
1a9603d09814 Uploaded
cpt
parents:
diff changeset
28 MONTAGE_BORDER_COLOUR = "grey70"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
29 IMAGE_BORDER_COLOUR = "purple"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
30 LABEL_COLOUR = "grey22"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
31 TICK_LENGTH = 0.2 * MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
32
1a9603d09814 Uploaded
cpt
parents:
diff changeset
33 TYPEFONT = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
34
1a9603d09814 Uploaded
cpt
parents:
diff changeset
35 CREDITS = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
36 "CPTs MISTv3\\n"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
37 "GPLv3 (C) 2015 Helena Rasche <esr\@tamu.edu>\\n"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
38 "Dot plots by Gepard Dot Plotter by Dr. Jan Krumsiek"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
39 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
40
1a9603d09814 Uploaded
cpt
parents:
diff changeset
41
1a9603d09814 Uploaded
cpt
parents:
diff changeset
42 class FancyRecord(object):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
43 def __init__(self, record, tmpdir):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
44 self.temp = tempfile.NamedTemporaryFile(mode='w', dir=tmpdir, delete=False, suffix=".fa")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
45 self.temp_path = self.temp.name
1a9603d09814 Uploaded
cpt
parents:
diff changeset
46 self.id = self.temp_path.rsplit("/")[-1]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
47 self.record = record
1a9603d09814 Uploaded
cpt
parents:
diff changeset
48 self.header = record.id
1a9603d09814 Uploaded
cpt
parents:
diff changeset
49
1a9603d09814 Uploaded
cpt
parents:
diff changeset
50 # Description include record.id
1a9603d09814 Uploaded
cpt
parents:
diff changeset
51 sd = record.description.strip().split(" ")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
52 if len(sd) > 1:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
53 self.description = " ".join(sd[1:])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
54 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
55 self.description = ""
1a9603d09814 Uploaded
cpt
parents:
diff changeset
56
1a9603d09814 Uploaded
cpt
parents:
diff changeset
57 self.length = len(record.seq)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
58
1a9603d09814 Uploaded
cpt
parents:
diff changeset
59 # Serialize to disk
1a9603d09814 Uploaded
cpt
parents:
diff changeset
60 self._write(self.temp)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
61 self.temp.flush()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
62 self.temp.close()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
63
1a9603d09814 Uploaded
cpt
parents:
diff changeset
64 def _write(self, handle):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
65 SeqIO.write([self.record], handle, "fasta")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
66
1a9603d09814 Uploaded
cpt
parents:
diff changeset
67
1a9603d09814 Uploaded
cpt
parents:
diff changeset
68 class Subplot(object):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
69 def __init__(self, i, j, tmpdir, zoom):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
70 self.i = i
1a9603d09814 Uploaded
cpt
parents:
diff changeset
71 self.j = j
1a9603d09814 Uploaded
cpt
parents:
diff changeset
72 # Mist information
1a9603d09814 Uploaded
cpt
parents:
diff changeset
73 self.tmpdir = tmpdir
1a9603d09814 Uploaded
cpt
parents:
diff changeset
74 self.zoom = zoom
1a9603d09814 Uploaded
cpt
parents:
diff changeset
75
1a9603d09814 Uploaded
cpt
parents:
diff changeset
76 self.original_path = None
1a9603d09814 Uploaded
cpt
parents:
diff changeset
77 self.thumb_path = None
1a9603d09814 Uploaded
cpt
parents:
diff changeset
78 self.annotated_original_path = None
1a9603d09814 Uploaded
cpt
parents:
diff changeset
79
1a9603d09814 Uploaded
cpt
parents:
diff changeset
80 def safe(self, string):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
81 return "".join(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
82 [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
83 c
1a9603d09814 Uploaded
cpt
parents:
diff changeset
84 for c in string.strip()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
85 if c.isalpha() or c.isdigit() or c == " " or c in ("[", "]", "-", "_")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
86 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
87 ).rstrip()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
88
1a9603d09814 Uploaded
cpt
parents:
diff changeset
89 def move_to_dir(self, files_path):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
90 """Move a specific image that's part of a subplot to an output
1a9603d09814 Uploaded
cpt
parents:
diff changeset
91 directory and return the name
1a9603d09814 Uploaded
cpt
parents:
diff changeset
92 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
93 destination_fn = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
94 self.safe("%s_vs_%s_[annotated]" % (self.i.header, self.j.header)) + ".png"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
95 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
96 destination = os.path.join(files_path, destination_fn)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
97 log.debug("cp %s %s", self.annotated_original_path, destination)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
98 if (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
99 self.annotated_original_path is not None
1a9603d09814 Uploaded
cpt
parents:
diff changeset
100 and os.path.exists(self.annotated_original_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
101 and not os.path.exists(destination)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
102 ):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
103 shutil.move(self.annotated_original_path, destination)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
104 return destination_fn
1a9603d09814 Uploaded
cpt
parents:
diff changeset
105
1a9603d09814 Uploaded
cpt
parents:
diff changeset
106 def get_description(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
107 return "%s v %s" % (self.i.header, self.j.header)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
108
1a9603d09814 Uploaded
cpt
parents:
diff changeset
109 def __repr__(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
110 return "Subplot [%s]" % self.get_description()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
111
1a9603d09814 Uploaded
cpt
parents:
diff changeset
112 def run_gepard(self, matrix, window, global_rescale="35%"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
113 """Run gepard on two sequences, with a specified output file
1a9603d09814 Uploaded
cpt
parents:
diff changeset
114 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
115 log.info("Running Gepard on %s", self.get_description())
1a9603d09814 Uploaded
cpt
parents:
diff changeset
116
1a9603d09814 Uploaded
cpt
parents:
diff changeset
117 destination_fn = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
118 self.safe("%s_vs_%s_orig" % (self.i.header, self.j.header)) + ".png"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
119 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
120 self.original_path = os.path.join(self.tmpdir, destination_fn)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
121
1a9603d09814 Uploaded
cpt
parents:
diff changeset
122 cmd = [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
123 "java",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
124 "-jar",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
125 os.path.join(SCRIPT_DIR, "gepard.jar"),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
126 "--seq1",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
127 self.j.temp_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
128 "--seq2",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
129 self.i.temp_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
130 "--matrix",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
131 matrix + ".mat",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
132 "--outfile",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
133 self.original_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
134 "--word",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
135 str(window),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
136 "--zoom",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
137 str(self.zoom),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
138 "--silent",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
139 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
140 log.debug(subprocess.list2cmdline(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
141 #log.info(subprocess.check_output("convert -list type"))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
142 #exit(2)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
143 failure_count = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
144 while True:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
145 try:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
146 subprocess.check_call(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
147 break
1a9603d09814 Uploaded
cpt
parents:
diff changeset
148 except subprocess.CalledProcessError:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
149 failure_count += 1
1a9603d09814 Uploaded
cpt
parents:
diff changeset
150 log.warn("sp.CPE FC %s", failure_count)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
151 if failure_count > 3:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
152 break
1a9603d09814 Uploaded
cpt
parents:
diff changeset
153 time.sleep(1)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
154
1a9603d09814 Uploaded
cpt
parents:
diff changeset
155 # Generate border/individualised images
1a9603d09814 Uploaded
cpt
parents:
diff changeset
156 log.debug(" Annotating")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
157 destination_fn = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
158 self.safe("%s_vs_%s_annotated" % (self.i.header, self.j.header)) + ".png"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
159 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
160 self.annotated_original_path = os.path.join(self.tmpdir, destination_fn)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
161 self.annotate_image(self.original_path, self.annotated_original_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
162
1a9603d09814 Uploaded
cpt
parents:
diff changeset
163 # Generate thumbnail version of base image
1a9603d09814 Uploaded
cpt
parents:
diff changeset
164 log.debug(" Resizing")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
165 destination_fn = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
166 self.safe("%s_vs_%s_thumb" % (self.i.header, self.j.header)) + ".png"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
167 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
168 self.thumb_path = os.path.join(self.tmpdir, destination_fn)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
169 Misty.resize_image(global_rescale, self.original_path, self.thumb_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
170
1a9603d09814 Uploaded
cpt
parents:
diff changeset
171 def get_thumb_dims(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
172 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
173 For NxN sized images, this avoids running `identify` when it won't be
1a9603d09814 Uploaded
cpt
parents:
diff changeset
174 used (e.g i=3,j=4)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
175 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
176 if not hasattr(self, "thumb_dims") or self.thumb_dims is None:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
177 self.thumb_dims = Misty.obtain_image_dimensions(self.thumb_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
178 return self.thumb_dims
1a9603d09814 Uploaded
cpt
parents:
diff changeset
179
1a9603d09814 Uploaded
cpt
parents:
diff changeset
180 def label_formatter(self, bases):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
181 if bases > 1000000:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
182 label = "%s Mbp" % int(bases / 1000000)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
183 elif bases > 1000:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
184 label = "%s kbp" % int(bases / 1000)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
185 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
186 label = "%s b" % bases
1a9603d09814 Uploaded
cpt
parents:
diff changeset
187 return label
1a9603d09814 Uploaded
cpt
parents:
diff changeset
188
1a9603d09814 Uploaded
cpt
parents:
diff changeset
189 def annotate_image(self, infile, outfile):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
190 original_dims = Misty.obtain_image_dimensions(infile)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
191
1a9603d09814 Uploaded
cpt
parents:
diff changeset
192 half_height = (original_dims[1] / 2) + MONTAGE_BORDER + IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
193 half_width = (original_dims[0] / 2) + MONTAGE_BORDER + IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
194
1a9603d09814 Uploaded
cpt
parents:
diff changeset
195 restoreBorder = MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
196
1a9603d09814 Uploaded
cpt
parents:
diff changeset
197 def char_width(font_size):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
198 """approximate pixel width of a single character at Xpt font
1a9603d09814 Uploaded
cpt
parents:
diff changeset
199
1a9603d09814 Uploaded
cpt
parents:
diff changeset
200 Assumes that 40pt font results in 20px characters
1a9603d09814 Uploaded
cpt
parents:
diff changeset
201 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
202 return int(float(font_size) / 2)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
203
1a9603d09814 Uploaded
cpt
parents:
diff changeset
204 def char_height(font_size):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
205 """approximate pixel height of a single character at Xpt font
1a9603d09814 Uploaded
cpt
parents:
diff changeset
206
1a9603d09814 Uploaded
cpt
parents:
diff changeset
207 Assumes that 40pt font results in 30px characters
1a9603d09814 Uploaded
cpt
parents:
diff changeset
208 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
209 return int(float(font_size) * 30 / 40)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
210
1a9603d09814 Uploaded
cpt
parents:
diff changeset
211 def est_pixels(string, font_size):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
212 """guess pixel width of a string at a given font size
1a9603d09814 Uploaded
cpt
parents:
diff changeset
213 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
214 return char_width(font_size) * len(string)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
215
1a9603d09814 Uploaded
cpt
parents:
diff changeset
216 j_ticks = int(Misty.BestTick(self.j.length, 5))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
217 i_ticks = int(Misty.BestTick(self.i.length, 5))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
218
1a9603d09814 Uploaded
cpt
parents:
diff changeset
219 # Convert definitions
1a9603d09814 Uploaded
cpt
parents:
diff changeset
220 GREY_FILL = ["-fill", LABEL_COLOUR]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
221 NO_FILL = ["-fill", "none"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
222 NO_STROKE = ["-stroke", "none"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
223 GREY_STROKE = ["-stroke", LABEL_COLOUR, "-strokewidth", "2"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
224 GFNS = GREY_FILL + NO_STROKE
1a9603d09814 Uploaded
cpt
parents:
diff changeset
225 NFGS = NO_FILL + GREY_STROKE
1a9603d09814 Uploaded
cpt
parents:
diff changeset
226 # Font for labels
1a9603d09814 Uploaded
cpt
parents:
diff changeset
227 FONT_SPEC = GFNS + ["-font", TYPEFONT]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
228 FONT_10pt = FONT_SPEC + ["-pointsize", "10"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
229 FONT_20pt = FONT_SPEC + ["-pointsize", "20"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
230 FONT_30pt = FONT_SPEC + ["-pointsize", "30"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
231
1a9603d09814 Uploaded
cpt
parents:
diff changeset
232 cmd = [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
233 "convert",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
234 infile,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
235 "-bordercolor",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
236 IMAGE_BORDER_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
237 "-border",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
238 IMAGE_BORDER_COORD,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
239 "-bordercolor",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
240 MONTAGE_BORDER_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
241 "-border",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
242 MONTAGE_BORDER_COORD,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
243 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
244
1a9603d09814 Uploaded
cpt
parents:
diff changeset
245 # Rotate -90, apply row header at bottom.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
246 primary_header = self.i.header
1a9603d09814 Uploaded
cpt
parents:
diff changeset
247 secondary_head = self.i.description
1a9603d09814 Uploaded
cpt
parents:
diff changeset
248 cmd += (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
249 ["-rotate", "-90",]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
250 + FONT_30pt
1a9603d09814 Uploaded
cpt
parents:
diff changeset
251 + [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
252 # Side label (i/row)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
253 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
254 "+%s+%s"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
255 % (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
256 half_height - 0.5 * est_pixels(self.i.header, 30),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
257 original_dims[0]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
258 + MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
259 + 2 * IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
260 + char_height(30 + 20)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
261 + 10
1a9603d09814 Uploaded
cpt
parents:
diff changeset
262 # 30 = primary header
1a9603d09814 Uploaded
cpt
parents:
diff changeset
263 # 20 = tick labels
1a9603d09814 Uploaded
cpt
parents:
diff changeset
264 ),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
265 primary_header,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
266 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
267 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
268
1a9603d09814 Uploaded
cpt
parents:
diff changeset
269 if est_pixels(self.i.description, 10) < original_dims[1] and secondary_head != "":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
270 cmd += FONT_10pt + [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
271 # Side label (i/row)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
272 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
273 "+%s+%s"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
274 % (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
275 half_height - 0.5 * est_pixels(self.i.description, 10),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
276 original_dims[0]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
277 + MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
278 + 2 * IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
279 + char_height(30 + 20 + 10 + 4)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
280 # 30 = primary header
1a9603d09814 Uploaded
cpt
parents:
diff changeset
281 # 20 = tick labels
1a9603d09814 Uploaded
cpt
parents:
diff changeset
282 # 10 = secondary header height
1a9603d09814 Uploaded
cpt
parents:
diff changeset
283 # 4 = line spacing
1a9603d09814 Uploaded
cpt
parents:
diff changeset
284 ),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
285 secondary_head,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
286 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
287
1a9603d09814 Uploaded
cpt
parents:
diff changeset
288 # Apply row ticks labels at bottom
1a9603d09814 Uploaded
cpt
parents:
diff changeset
289 for z in range(0, self.i.length, i_ticks):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
290
1a9603d09814 Uploaded
cpt
parents:
diff changeset
291 image_side_percentage = float(z) / self.i.length
1a9603d09814 Uploaded
cpt
parents:
diff changeset
292 x = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
293 (image_side_percentage * original_dims[1])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
294 + MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
295 + IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
296 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
297 y = MONTAGE_BORDER + original_dims[0] + (2 * IMAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
298
1a9603d09814 Uploaded
cpt
parents:
diff changeset
299 # Apply ticks
1a9603d09814 Uploaded
cpt
parents:
diff changeset
300 cmd += NFGS
1a9603d09814 Uploaded
cpt
parents:
diff changeset
301 cmd += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
302 "-draw",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
303 "line %s,%s %s,%s" % (x, y, x, y + TICK_LENGTH),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
304 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
305
1a9603d09814 Uploaded
cpt
parents:
diff changeset
306 # Keep text from running off the edge.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
307 space_to_end_of_image = (1 - image_side_percentage) * original_dims[1]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
308 if space_to_end_of_image - est_pixels(self.label_formatter(z), 20) < 0:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
309 continue
1a9603d09814 Uploaded
cpt
parents:
diff changeset
310
1a9603d09814 Uploaded
cpt
parents:
diff changeset
311 # Text label
1a9603d09814 Uploaded
cpt
parents:
diff changeset
312 cmd += FONT_20pt
1a9603d09814 Uploaded
cpt
parents:
diff changeset
313 cmd += ["-annotate", "+%s+%s" % (x + 5, y + 15), self.label_formatter(z)]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
314
1a9603d09814 Uploaded
cpt
parents:
diff changeset
315 # Rotate back to final rotation
1a9603d09814 Uploaded
cpt
parents:
diff changeset
316 primary_header = self.j.header
1a9603d09814 Uploaded
cpt
parents:
diff changeset
317 secondary_head = self.j.description
1a9603d09814 Uploaded
cpt
parents:
diff changeset
318 cmd += (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
319 [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
320 "-rotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
321 "90",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
322 # Top label (j/column)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
323 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
324 + FONT_30pt
1a9603d09814 Uploaded
cpt
parents:
diff changeset
325 + [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
326 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
327 "+%s+%s"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
328 % (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
329 half_width - 0.5 * est_pixels(self.j.header, 30),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
330 MONTAGE_BORDER - char_height(20 + 10 + 8),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
331 ),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
332 primary_header,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
333 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
334 + FONT_10pt
1a9603d09814 Uploaded
cpt
parents:
diff changeset
335 + [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
336 # Credits
1a9603d09814 Uploaded
cpt
parents:
diff changeset
337 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
338 "+%s+%s" % (2, MONTAGE_BORDER + original_dims[1] + 2 * IMAGE_BORDER),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
339 "\\n" + CREDITS,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
340 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
341 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
342
1a9603d09814 Uploaded
cpt
parents:
diff changeset
343 if est_pixels(self.j.description, 10) < original_dims[0] and secondary_head != "":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
344 cmd += FONT_10pt + [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
345 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
346 "+%s+%s"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
347 % (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
348 half_width - 0.5 * est_pixels(self.j.description, 10),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
349 MONTAGE_BORDER - char_height(20 + 4)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
350 # 4 = line spacing
1a9603d09814 Uploaded
cpt
parents:
diff changeset
351 ),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
352 secondary_head,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
353 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
354
1a9603d09814 Uploaded
cpt
parents:
diff changeset
355 # Apply col ticks along top
1a9603d09814 Uploaded
cpt
parents:
diff changeset
356 for z in range(0, self.j.length, j_ticks):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
357 image_side_percentage = float(z) / self.j.length
1a9603d09814 Uploaded
cpt
parents:
diff changeset
358 x = (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
359 (image_side_percentage * original_dims[0])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
360 + MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
361 + IMAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
362 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
363 y = MONTAGE_BORDER - 1
1a9603d09814 Uploaded
cpt
parents:
diff changeset
364
1a9603d09814 Uploaded
cpt
parents:
diff changeset
365 # Ticks
1a9603d09814 Uploaded
cpt
parents:
diff changeset
366 cmd += NFGS
1a9603d09814 Uploaded
cpt
parents:
diff changeset
367 cmd += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
368 "-draw",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
369 "line %s,%s %s,%s" % (x, y, x, y - TICK_LENGTH),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
370 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
371
1a9603d09814 Uploaded
cpt
parents:
diff changeset
372 # Keep text from running off the edge.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
373 space_to_end_of_image = (1 - image_side_percentage) * original_dims[0]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
374 if space_to_end_of_image - est_pixels(self.label_formatter(z), 20) < 0:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
375 continue
1a9603d09814 Uploaded
cpt
parents:
diff changeset
376
1a9603d09814 Uploaded
cpt
parents:
diff changeset
377 # Text labels
1a9603d09814 Uploaded
cpt
parents:
diff changeset
378 cmd += FONT_20pt
1a9603d09814 Uploaded
cpt
parents:
diff changeset
379 cmd += ["-annotate", "+%s+%s" % (x + 5, y), self.label_formatter(z)]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
380
1a9603d09814 Uploaded
cpt
parents:
diff changeset
381 cmd.append(outfile)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
382 #tmpFile = open(outfile, "w")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
383 #tmpFile.close()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
384 log.info(subprocess.check_output( ["cp", infile, outfile] ))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
385 log.info(subprocess.list2cmdline(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
386 log.info(subprocess.check_output( "ls" ))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
387 log.info(self.tmpdir)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
388 log.info(subprocess.check_output( ["ls", self.tmpdir]))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
389 log.info(outfile[2:])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
390 log.info("Above was ls\n")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
391 try:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
392 subprocess.check_output(cmd)# + [" 2>&1"])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
393 except:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
394 log.info("Excepted")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
395
1a9603d09814 Uploaded
cpt
parents:
diff changeset
396
1a9603d09814 Uploaded
cpt
parents:
diff changeset
397
1a9603d09814 Uploaded
cpt
parents:
diff changeset
398 class Misty(object):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
399 """MIST Class for building MIST Plots
1a9603d09814 Uploaded
cpt
parents:
diff changeset
400 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
401
1a9603d09814 Uploaded
cpt
parents:
diff changeset
402 def __init__(self, window=10, zoom=50, matrix="edna", files_path="mist_images"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
403 self.tmpdir = tempfile.mkdtemp(prefix="cpt.mist3.", dir=".")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
404 self.window = str(window)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
405 self.zoom = zoom
1a9603d09814 Uploaded
cpt
parents:
diff changeset
406 self.matrix = matrix
1a9603d09814 Uploaded
cpt
parents:
diff changeset
407 self.records = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
408 self.matrix_data = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
409
1a9603d09814 Uploaded
cpt
parents:
diff changeset
410 # Image storage
1a9603d09814 Uploaded
cpt
parents:
diff changeset
411 self.files_path = files_path
1a9603d09814 Uploaded
cpt
parents:
diff changeset
412 if not os.path.exists(self.files_path):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
413 os.makedirs(self.files_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
414
1a9603d09814 Uploaded
cpt
parents:
diff changeset
415 def _get_record(self, record_id):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
416 for i, record in enumerate(self.records):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
417 if record.id == record_id:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
418 return record
1a9603d09814 Uploaded
cpt
parents:
diff changeset
419
1a9603d09814 Uploaded
cpt
parents:
diff changeset
420 def _get_record_idx(self, record_id):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
421 for i, record in enumerate(self.records):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
422 if record.id == record_id:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
423 return i
1a9603d09814 Uploaded
cpt
parents:
diff changeset
424
1a9603d09814 Uploaded
cpt
parents:
diff changeset
425 raise RuntimeError("Could not find record ID=%s" % record_id)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
426
1a9603d09814 Uploaded
cpt
parents:
diff changeset
427 def register_all_files(self, file_list):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
428 for fasta_file in file_list:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
429 for record in SeqIO.parse(fasta_file, "fasta"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
430 self.register_record(record)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
431
1a9603d09814 Uploaded
cpt
parents:
diff changeset
432 def register_record(self, record):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
433 self.records.append(FancyRecord(record, self.tmpdir))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
434
1a9603d09814 Uploaded
cpt
parents:
diff changeset
435 def set_matrix(self, matrix):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
436 self.matrix_data = matrix
1a9603d09814 Uploaded
cpt
parents:
diff changeset
437 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
438 record_i = self._get_record(self.matrix_data[i][0]["i"])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
439 for j in range(len(self.matrix_data[i])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
440 record_j = self._get_record(self.matrix_data[i][j]["j"])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
441 self.matrix_data[i][j]["subplot"] = Subplot(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
442 record_i, record_j, self.tmpdir, self.zoom
1a9603d09814 Uploaded
cpt
parents:
diff changeset
443 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
444
1a9603d09814 Uploaded
cpt
parents:
diff changeset
445 # More processing?
1a9603d09814 Uploaded
cpt
parents:
diff changeset
446 logging.debug("\n" + pprint.pformat(matrix))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
447
1a9603d09814 Uploaded
cpt
parents:
diff changeset
448 def generate_matrix(self, mtype="complete"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
449 matrix = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
450 if mtype == "complete":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
451 for i in self.records:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
452 row = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
453 for j in self.records:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
454 row.append({"i": i.id, "j": j.id})
1a9603d09814 Uploaded
cpt
parents:
diff changeset
455 matrix.append(row)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
456 elif mtype == "1vn":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
457 if len(self.records) < 2:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
458 raise RuntimeError("1vN not available for fewer than two sequences")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
459 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
460 row = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
461 for j in self.records[1:]:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
462 row.append({"i": self.records[0].id, "j": j.id})
1a9603d09814 Uploaded
cpt
parents:
diff changeset
463 matrix.append(row)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
464 return matrix
1a9603d09814 Uploaded
cpt
parents:
diff changeset
465
1a9603d09814 Uploaded
cpt
parents:
diff changeset
466 @classmethod
1a9603d09814 Uploaded
cpt
parents:
diff changeset
467 def obtain_image_dimensions(cls, path):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
468 cmd = ["identify", path]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
469 output = subprocess.check_output(cmd, universal_newlines=True)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
470 size = output.split(" ")[3]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
471 (w, h) = size[0 : size.index("+")].split("x")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
472 return (int(w), int(h))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
473
1a9603d09814 Uploaded
cpt
parents:
diff changeset
474 @classmethod
1a9603d09814 Uploaded
cpt
parents:
diff changeset
475 def BestTick(cls, largest, mostticks):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
476 # http://stackoverflow.com/a/361687
1a9603d09814 Uploaded
cpt
parents:
diff changeset
477 minimum = largest / mostticks
1a9603d09814 Uploaded
cpt
parents:
diff changeset
478 magnitude = 10 ** math.floor(math.log(minimum) / math.log(10))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
479 residual = minimum / magnitude
1a9603d09814 Uploaded
cpt
parents:
diff changeset
480 if residual > 5:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
481 tick = 10 * magnitude
1a9603d09814 Uploaded
cpt
parents:
diff changeset
482 elif residual > 2:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
483 tick = 5 * magnitude
1a9603d09814 Uploaded
cpt
parents:
diff changeset
484 elif residual > 1:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
485 tick = 2 * magnitude
1a9603d09814 Uploaded
cpt
parents:
diff changeset
486 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
487 tick = magnitude
1a9603d09814 Uploaded
cpt
parents:
diff changeset
488 return tick
1a9603d09814 Uploaded
cpt
parents:
diff changeset
489
1a9603d09814 Uploaded
cpt
parents:
diff changeset
490 @classmethod
1a9603d09814 Uploaded
cpt
parents:
diff changeset
491 def resize_image(cls, scale, from_file, to_file):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
492 cmd = ["convert", "-resize", scale, from_file, to_file]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
493 log.debug(" ".join(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
494 subprocess.check_call(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
495
1a9603d09814 Uploaded
cpt
parents:
diff changeset
496 def get_image_map(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
497 image_template = Template(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
498 '<area shape="rect" coords="${x1},${y1},${x2},${y2}" alt="${alt}" href="${href}" />'
1a9603d09814 Uploaded
cpt
parents:
diff changeset
499 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
500 imagemap = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
501
1a9603d09814 Uploaded
cpt
parents:
diff changeset
502 j_widths = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
503 i_height = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
504
1a9603d09814 Uploaded
cpt
parents:
diff changeset
505 for j in range(len(self.matrix_data[0])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
506 j_widths.append(self.matrix_data[0][j]["subplot"].get_thumb_dims()[0])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
507
1a9603d09814 Uploaded
cpt
parents:
diff changeset
508 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
509 i_height.append(self.matrix_data[i][0]["subplot"].get_thumb_dims()[1])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
510
1a9603d09814 Uploaded
cpt
parents:
diff changeset
511 log.debug(pprint.pformat(j_widths))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
512 log.debug(pprint.pformat(i_height))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
513
1a9603d09814 Uploaded
cpt
parents:
diff changeset
514 def cur_y(i_idx):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
515 return (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
516 MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
517 + sum(i_height[0:i_idx])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
518 + (2 * IMAGE_BORDER * (1 + i_idx))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
519 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
520
1a9603d09814 Uploaded
cpt
parents:
diff changeset
521 def cur_x(j_idx):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
522 return (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
523 MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
524 + sum(j_widths[0:j_idx])
1a9603d09814 Uploaded
cpt
parents:
diff changeset
525 + (2 * IMAGE_BORDER * (1 + j_idx))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
526 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
527
1a9603d09814 Uploaded
cpt
parents:
diff changeset
528 for j in range(len(self.matrix_data[0])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
529 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
530 current = self.matrix_data[i][j]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
531 # Move to final resting place
1a9603d09814 Uploaded
cpt
parents:
diff changeset
532 new_image_location = current.move_to_dir(self.files_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
533
1a9603d09814 Uploaded
cpt
parents:
diff changeset
534 # Build imagemagp string
1a9603d09814 Uploaded
cpt
parents:
diff changeset
535 imagemap.append(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
536 image_template.substitute(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
537 {
1a9603d09814 Uploaded
cpt
parents:
diff changeset
538 # Start at +image border so the border isn't included in
1a9603d09814 Uploaded
cpt
parents:
diff changeset
539 # start of box
1a9603d09814 Uploaded
cpt
parents:
diff changeset
540 "x1": cur_x(j),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
541 "y1": cur_y(i),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
542 "x2": cur_x(j) + j_widths[j],
1a9603d09814 Uploaded
cpt
parents:
diff changeset
543 "y2": cur_y(i) + i_height[i],
1a9603d09814 Uploaded
cpt
parents:
diff changeset
544 "alt": current.get_description(),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
545 "href": new_image_location,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
546 }
1a9603d09814 Uploaded
cpt
parents:
diff changeset
547 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
548 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
549 return "\n".join(imagemap)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
550
1a9603d09814 Uploaded
cpt
parents:
diff changeset
551 def _generate_montage(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
552 image_list = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
553 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
554 for j in range(len(self.matrix_data[i])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
555 subplot = self.matrix_data[i][j]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
556 image_list.append(subplot.thumb_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
557
1a9603d09814 Uploaded
cpt
parents:
diff changeset
558 # Montage step
1a9603d09814 Uploaded
cpt
parents:
diff changeset
559 global MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
560 global MONTAGE_BORDER_COORD
1a9603d09814 Uploaded
cpt
parents:
diff changeset
561 for rec in self.records:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
562 MONTAGE_BORDER = max(MONTAGE_BORDER, (len(rec.id) * 12) + 4)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
563 MONTAGE_BORDER_COORD = "%sx%s" % (MONTAGE_BORDER, MONTAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
564
1a9603d09814 Uploaded
cpt
parents:
diff changeset
565 m0 = os.path.join(self.tmpdir, "m0.png")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
566 # log.info(subprocess.check_output( ["cp", image_list[0], m0] ))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
567 cmd = ["montage"] + image_list
1a9603d09814 Uploaded
cpt
parents:
diff changeset
568 cmd += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
569 "-tile",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
570 "%sx%s" % (len(self.matrix_data[0]), len(self.matrix_data)),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
571 "-geometry",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
572 "+0+0",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
573 "-border",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
574 str(IMAGE_BORDER),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
575 "-bordercolor",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
576 IMAGE_BORDER_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
577 "-font",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
578 TYPEFONT,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
579 m0,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
580 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
581
1a9603d09814 Uploaded
cpt
parents:
diff changeset
582 log.debug(" ".join(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
583 try:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
584 subprocess.check_call(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
585 except:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
586 log.debug("Excepted, 2")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
587 # Add grey borders
1a9603d09814 Uploaded
cpt
parents:
diff changeset
588 montage_path = os.path.join(self.tmpdir, "montage.png")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
589 cmd = [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
590 "convert",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
591 m0,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
592 "-bordercolor",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
593 IMAGE_BORDER_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
594 "-border",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
595 IMAGE_BORDER_COORD,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
596 "-bordercolor",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
597 MONTAGE_BORDER_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
598 "-border",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
599 MONTAGE_BORDER_COORD,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
600 montage_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
601 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
602
1a9603d09814 Uploaded
cpt
parents:
diff changeset
603 log.debug(" ".join(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
604 try:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
605 subprocess.check_call(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
606 except:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
607 log.debug("Excepted, 2")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
608 os.unlink(m0)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
609 return montage_path
1a9603d09814 Uploaded
cpt
parents:
diff changeset
610
1a9603d09814 Uploaded
cpt
parents:
diff changeset
611 def _annotate_montage(self, base_path):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
612 # Calculate some expected dimension
1a9603d09814 Uploaded
cpt
parents:
diff changeset
613 cumulative_width = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
614 cumulative_height = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
615 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
616 for j in range(len(self.matrix_data[i])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
617 subplot = self.matrix_data[i][j]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
618
1a9603d09814 Uploaded
cpt
parents:
diff changeset
619 if i == 0:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
620 cumulative_width += subplot.get_thumb_dims()[0] + IMAGE_BORDER * 2
1a9603d09814 Uploaded
cpt
parents:
diff changeset
621
1a9603d09814 Uploaded
cpt
parents:
diff changeset
622 if j == 0:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
623 cumulative_height += subplot.get_thumb_dims()[1] + IMAGE_BORDER * 2
1a9603d09814 Uploaded
cpt
parents:
diff changeset
624
1a9603d09814 Uploaded
cpt
parents:
diff changeset
625 convert_arguments_top = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
626 convert_arguments_left = []
1a9603d09814 Uploaded
cpt
parents:
diff changeset
627 left_offset = cumulative_width + MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
628
1a9603d09814 Uploaded
cpt
parents:
diff changeset
629 current_sum_width = MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
630 current_sum_height = MONTAGE_BORDER
1a9603d09814 Uploaded
cpt
parents:
diff changeset
631
1a9603d09814 Uploaded
cpt
parents:
diff changeset
632 convert_arguments_top+= [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
633 "-rotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
634 "-90"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
635 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
636 # Top side
1a9603d09814 Uploaded
cpt
parents:
diff changeset
637 for j in range(len(self.matrix_data[0])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
638 subplot = self.matrix_data[0][j]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
639 convert_arguments_top += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
640 "-fill",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
641 LABEL_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
642 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
643 "-%s+%s" % (0, str(cumulative_width - current_sum_width -(subplot.get_thumb_dims()[0]/2) + (2 * MONTAGE_BORDER) + IMAGE_BORDER)),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
644 subplot.j.header,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
645 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
646 current_sum_width += subplot.get_thumb_dims()[0] + (2 * IMAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
647 log.debug("CSW %s", current_sum_width)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
648 convert_arguments_top+= [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
649 "-rotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
650 "90"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
651 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
652 # Left side
1a9603d09814 Uploaded
cpt
parents:
diff changeset
653 #convert_arguments_left += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
654 # "-rotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
655 # "90"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
656 #]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
657 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
658 subplot = self.matrix_data[i][0]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
659 convert_arguments_left += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
660 "-fill",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
661 LABEL_COLOUR,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
662 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
663 "+2+%s" % str(current_sum_height + (subplot.get_thumb_dims()[1]/2.0) + IMAGE_BORDER),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
664 "\n" + subplot.i.header,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
665 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
666 current_sum_height += subplot.get_thumb_dims()[1] + (2 * IMAGE_BORDER)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
667 log.debug("CSH %s", current_sum_height)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
668
1a9603d09814 Uploaded
cpt
parents:
diff changeset
669 cmd = [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
670 "convert",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
671 base_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
672 # "-rotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
673 # "-90",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
674 "-pointsize",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
675 "20",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
676 "-font",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
677 TYPEFONT,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
678 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
679 cmd += convert_arguments_left
1a9603d09814 Uploaded
cpt
parents:
diff changeset
680 # cmd += ["-rotate", "90"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
681 cmd += convert_arguments_top
1a9603d09814 Uploaded
cpt
parents:
diff changeset
682
1a9603d09814 Uploaded
cpt
parents:
diff changeset
683 output_path = os.path.join(self.tmpdir, "large.png")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
684 cmd += [
1a9603d09814 Uploaded
cpt
parents:
diff changeset
685 "-pointsize",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
686 "14",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
687 "-annotate",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
688 "+2+%s" % str(current_sum_height + 40),
1a9603d09814 Uploaded
cpt
parents:
diff changeset
689 CREDITS,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
690 output_path,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
691 ]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
692 log.debug(" ".join(cmd))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
693 try:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
694 subprocess.check_call(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
695 except:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
696 log.debug("Excepted, 3")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
697 #subprocess.check_output(cmd)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
698 return output_path
1a9603d09814 Uploaded
cpt
parents:
diff changeset
699
1a9603d09814 Uploaded
cpt
parents:
diff changeset
700 def run(self):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
701 # We want the final thumbnail for the overall image to have a max width/height
1a9603d09814 Uploaded
cpt
parents:
diff changeset
702 # of 700px for nice display
1a9603d09814 Uploaded
cpt
parents:
diff changeset
703 total_seqlen_j = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
704 total_seqlen_i = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
705 for j in range(len(self.matrix_data[0])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
706 total_seqlen_j += self.matrix_data[0][j]["subplot"].j.length
1a9603d09814 Uploaded
cpt
parents:
diff changeset
707
1a9603d09814 Uploaded
cpt
parents:
diff changeset
708 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
709 total_seqlen_i += self.matrix_data[i][0]["subplot"].i.length
1a9603d09814 Uploaded
cpt
parents:
diff changeset
710 total_seqlen = max(total_seqlen_i, total_seqlen_j)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
711 rescale = 200 * (700.0 / (float(total_seqlen) / self.zoom))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
712 rescale_p = "%s%%" % rescale
1a9603d09814 Uploaded
cpt
parents:
diff changeset
713
1a9603d09814 Uploaded
cpt
parents:
diff changeset
714 # Generate gepard plots for each of the sub-images
1a9603d09814 Uploaded
cpt
parents:
diff changeset
715 for i in range(len(self.matrix_data)):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
716 for j in range(len(self.matrix_data[i])):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
717 subplot = self.matrix_data[i][j]["subplot"]
1a9603d09814 Uploaded
cpt
parents:
diff changeset
718
1a9603d09814 Uploaded
cpt
parents:
diff changeset
719 # generates _orig and _thumb versions
1a9603d09814 Uploaded
cpt
parents:
diff changeset
720 subplot.run_gepard(self.matrix, self.window, global_rescale=rescale_p)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
721
1a9603d09814 Uploaded
cpt
parents:
diff changeset
722 base_montage = self._generate_montage()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
723 annotated_montage = self._annotate_montage(base_montage)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
724
1a9603d09814 Uploaded
cpt
parents:
diff changeset
725 final_montage_path = os.path.join(self.files_path, "large.png")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
726 shutil.move(annotated_montage, final_montage_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
727 return final_montage_path
1a9603d09814 Uploaded
cpt
parents:
diff changeset
728
1a9603d09814 Uploaded
cpt
parents:
diff changeset
729
1a9603d09814 Uploaded
cpt
parents:
diff changeset
730 def mist_wrapper(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
731 files,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
732 window=10,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
733 zoom=50,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
734 matrix="edna",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
735 plot_type="complete",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
736 files_path="mist_images",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
737 ):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
738 html_page = """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
739 <!DOCTYPE html>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
740 <html>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
741 <body>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
742 <h1>Mist Results</h1>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
743 <p>Each section of mist output is now clickable to view a higher resolution version of that subsection</p>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
744 <img src="large.png" usemap="#mistmap">
1a9603d09814 Uploaded
cpt
parents:
diff changeset
745 <map name="mistmap">
1a9603d09814 Uploaded
cpt
parents:
diff changeset
746 %s
1a9603d09814 Uploaded
cpt
parents:
diff changeset
747 </map>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
748 </body>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
749 </html>
1a9603d09814 Uploaded
cpt
parents:
diff changeset
750 """
1a9603d09814 Uploaded
cpt
parents:
diff changeset
751
1a9603d09814 Uploaded
cpt
parents:
diff changeset
752 m = Misty(window=window, zoom=zoom, matrix=matrix, files_path=files_path)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
753
1a9603d09814 Uploaded
cpt
parents:
diff changeset
754 # There is only one special case, so we handle that separately. Every other
1a9603d09814 Uploaded
cpt
parents:
diff changeset
755 # plot type wants ALL of the sequences available.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
756 if (
1a9603d09814 Uploaded
cpt
parents:
diff changeset
757 plot_type == "2up"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
758 and len(files) != 2
1a9603d09814 Uploaded
cpt
parents:
diff changeset
759 and matrix not in ("protidentity", "blosum62")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
760 ):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
761 idx = 0
1a9603d09814 Uploaded
cpt
parents:
diff changeset
762 # Pull top two sequences.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
763 for fasta_file in files:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
764 for record in SeqIO.parse(fasta_file, "fasta"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
765 m.register_record(record)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
766
1a9603d09814 Uploaded
cpt
parents:
diff changeset
767 # Exit after we've seen two sequences
1a9603d09814 Uploaded
cpt
parents:
diff changeset
768 idx += 1
1a9603d09814 Uploaded
cpt
parents:
diff changeset
769 if idx == 2:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
770 break
1a9603d09814 Uploaded
cpt
parents:
diff changeset
771 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
772 m.register_all_files(files)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
773
1a9603d09814 Uploaded
cpt
parents:
diff changeset
774 # Generate the matrix appropriate to this plot type. There are two matrix
1a9603d09814 Uploaded
cpt
parents:
diff changeset
775 # types: 1vN and complete. 1vN is just a line, complete is a complete
1a9603d09814 Uploaded
cpt
parents:
diff changeset
776 # square.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
777 if plot_type == "complete":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
778 # ALL sequences are used.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
779 m.set_matrix(m.generate_matrix(mtype="complete"))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
780 elif plot_type == "2up":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
781 # Just two sequences.
1a9603d09814 Uploaded
cpt
parents:
diff changeset
782 if len(files) == 2 and matrix in ("protidentity", "blosum62"):
1a9603d09814 Uploaded
cpt
parents:
diff changeset
783 m.set_matrix(m.generate_matrix(mtype="complete"))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
784 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
785 # Co-op the 1vn to do a single plot, rather than a "complete" plot
1a9603d09814 Uploaded
cpt
parents:
diff changeset
786 m.set_matrix(m.generate_matrix(mtype="1vn"))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
787 elif plot_type == "1vn":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
788 m.set_matrix(m.generate_matrix(mtype="1vn"))
1a9603d09814 Uploaded
cpt
parents:
diff changeset
789 else:
1a9603d09814 Uploaded
cpt
parents:
diff changeset
790 raise ValueError("Unknown plot type %s" % plot_type)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
791
1a9603d09814 Uploaded
cpt
parents:
diff changeset
792 m.run()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
793 # image_map will be returned from MIST
1a9603d09814 Uploaded
cpt
parents:
diff changeset
794 image_map = m.get_image_map()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
795
1a9603d09814 Uploaded
cpt
parents:
diff changeset
796 return html_page % image_map
1a9603d09814 Uploaded
cpt
parents:
diff changeset
797
1a9603d09814 Uploaded
cpt
parents:
diff changeset
798
1a9603d09814 Uploaded
cpt
parents:
diff changeset
799 if __name__ == "__main__":
1a9603d09814 Uploaded
cpt
parents:
diff changeset
800 parser = argparse.ArgumentParser(description="MIST v3", epilog="")
1a9603d09814 Uploaded
cpt
parents:
diff changeset
801 parser.add_argument(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
802 "files", nargs="+", type=argparse.FileType("r"), help="Fasta sequences"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
803 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
804
1a9603d09814 Uploaded
cpt
parents:
diff changeset
805 parser.add_argument("--zoom", type=int, help="# bases / pixel", default=50)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
806 parser.add_argument("--window", type=int, help="Window size", default=10)
1a9603d09814 Uploaded
cpt
parents:
diff changeset
807 parser.add_argument(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
808 "--matrix",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
809 type=str,
1a9603d09814 Uploaded
cpt
parents:
diff changeset
810 choices=["ednaorig", "pam250", "edna", "protidentity", "blosum62"],
1a9603d09814 Uploaded
cpt
parents:
diff changeset
811 help="# bases / pixel",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
812 default="edna",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
813 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
814
1a9603d09814 Uploaded
cpt
parents:
diff changeset
815 parser.add_argument(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
816 "--plot_type",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
817 choices=["2up", "1vn", "complete"],
1a9603d09814 Uploaded
cpt
parents:
diff changeset
818 help="Plot type",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
819 default="complete",
1a9603d09814 Uploaded
cpt
parents:
diff changeset
820 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
821
1a9603d09814 Uploaded
cpt
parents:
diff changeset
822 parser.add_argument(
1a9603d09814 Uploaded
cpt
parents:
diff changeset
823 "--files_path", type=str, help="Image directory", default="mist_images"
1a9603d09814 Uploaded
cpt
parents:
diff changeset
824 )
1a9603d09814 Uploaded
cpt
parents:
diff changeset
825
1a9603d09814 Uploaded
cpt
parents:
diff changeset
826 args = parser.parse_args()
1a9603d09814 Uploaded
cpt
parents:
diff changeset
827 print(mist_wrapper(**vars(args)))