annotate bin/last-dotplot @ 2:f274c166e738 default tip

remove comments in bsfcall_wrapper.xml
author yutaka-saito
date Sun, 19 Apr 2015 23:02:04 +0900
parents 06f8460885ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
1 #! /usr/bin/env python
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
3 # Read pair-wise alignments in MAF or LAST tabular format: write an
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
4 # "Oxford grid", a.k.a. dotplot.
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
5
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
6 # TODO: Currently, pixels with zero aligned nt-pairs are white, and
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
7 # pixels with one or more aligned nt-pairs are black. This can look
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
8 # too crowded for large genome alignments. I tried shading each pixel
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
9 # according to the number of aligned nt-pairs within it, but the
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
10 # result is too faint. How can this be done better?
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
11
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
12 import fileinput, itertools, optparse, os, re, sys
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
13
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
14 # Try to make PIL/PILLOW work:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
15 try: from PIL import Image, ImageDraw, ImageFont, ImageColor
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
16 except ImportError: import Image, ImageDraw, ImageFont, ImageColor
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
17
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
18 my_name = os.path.basename(sys.argv[0])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
19 usage = """
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
20 %prog --help
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
21 %prog [options] last-tabular-output dotplot.png
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
22 %prog [options] last-tabular-output dotplot.gif
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
23 etc."""
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
24 parser = optparse.OptionParser(usage=usage)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
25 # Replace "width" & "height" with a single "length" option?
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
26 parser.add_option("-x", "--width", type="int", dest="width", default=1000,
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
27 help="maximum width in pixels (default: %default)")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
28 parser.add_option("-y", "--height", type="int", dest="height", default=1000,
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
29 help="maximum height in pixels (default: %default)")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
30 parser.add_option("-f", "--fontfile", dest="fontfile",
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
31 help="TrueType or OpenType font file")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
32 parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
33 help="TrueType or OpenType font size (default: %default)")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
34 parser.add_option("-c", "--forwardcolor", dest="forwardcolor", default="red",
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
35 help="Color for forward alignments (default: %default)")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
36 parser.add_option("-r", "--reversecolor", dest="reversecolor", default="blue",
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
37 help="Color for reverse alignments (default: %default)")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
38 (opts, args) = parser.parse_args()
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
39 if len(args) != 2: parser.error("2 arguments needed")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
40
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
41 if opts.fontfile: font = ImageFont.truetype(opts.fontfile, opts.fontsize)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
42 else: font = ImageFont.load_default()
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
43
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
44 # Make these options too?
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
45 text_color = "black"
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
46 background_color = "white"
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
47 pix_tween_seqs = 2 # number of border pixels between sequences
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
48 border_shade = 239, 239, 239 # the shade of grey to use for border pixels
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
49 label_space = 5 # minimum number of pixels between axis labels
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
50
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
51 image_mode = 'RGB'
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
52 forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
53 reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
54 overlap_color = tuple([(i+j)//2 for i, j in zip(forward_color, reverse_color)])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
55
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
56 def tabBlocks(beg1, beg2, blocks):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
57 '''Get the gapless blocks of an alignment, from LAST tabular format.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
58 for i in blocks.split(","):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
59 if ":" in i:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
60 x, y = i.split(":")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
61 beg1 += int(x)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
62 beg2 += int(y)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
63 else:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
64 size = int(i)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
65 yield beg1, beg2, size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
66 beg1 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
67 beg2 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
68
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
69 def mafBlocks(beg1, beg2, seq1, seq2):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
70 '''Get the gapless blocks of an alignment, from MAF format.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
71 size = 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
72 for x, y in itertools.izip(seq1, seq2):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
73 if x == "-":
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
74 if size:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
75 yield beg1, beg2, size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
76 beg1 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
77 beg2 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
78 size = 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
79 beg2 += 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
80 elif y == "-":
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
81 if size:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
82 yield beg1, beg2, size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
83 beg1 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
84 beg2 += size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
85 size = 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
86 beg1 += 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
87 else:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
88 size += 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
89 if size: yield beg1, beg2, size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
90
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
91 def alignmentInput(lines):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
92 '''Get alignments and sequence lengths, from MAF or tabular format.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
93 mafCount = 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
94 for line in lines:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
95 w = line.split()
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
96 if line[0].isdigit(): # tabular format
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
97 chr1, beg1, seqlen1 = w[1], int(w[2]), int(w[5])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
98 if w[4] == "-": beg1 -= seqlen1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
99 chr2, beg2, seqlen2 = w[6], int(w[7]), int(w[10])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
100 if w[9] == "-": beg2 -= seqlen2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
101 blocks = tabBlocks(beg1, beg2, w[11])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
102 yield chr1, seqlen1, chr2, seqlen2, blocks
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
103 elif line[0] == "s": # MAF format
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
104 if mafCount == 0:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
105 chr1, beg1, seqlen1, seq1 = w[1], int(w[2]), int(w[5]), w[6]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
106 if w[4] == "-": beg1 -= seqlen1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
107 mafCount = 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
108 else:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
109 chr2, beg2, seqlen2, seq2 = w[1], int(w[2]), int(w[5]), w[6]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
110 if w[4] == "-": beg2 -= seqlen2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
111 blocks = mafBlocks(beg1, beg2, seq1, seq2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
112 yield chr1, seqlen1, chr2, seqlen2, blocks
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
113 mafCount = 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
114
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
115 def readAlignments(lines):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
116 '''Get alignments and sequence lengths, from MAF or tabular format.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
117 alignments = []
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
118 seqLengths1 = {}
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
119 seqLengths2 = {}
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
120 for chr1, seqlen1, chr2, seqlen2, blocks in alignmentInput(lines):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
121 aln = chr1, chr2, blocks
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
122 alignments.append(aln)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
123 seqLengths1[chr1] = seqlen1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
124 seqLengths2[chr2] = seqlen2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
125 return alignments, seqLengths1, seqLengths2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
126
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
127 sys.stderr.write(my_name + ": reading alignments...\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
128 input = fileinput.input(args[0])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
129 alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
130 sys.stderr.write(my_name + ": done\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
131
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
132 if not alignments:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
133 sys.exit(my_name + ": there are no alignments")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
134
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
135 def natural_sort_key(my_string):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
136 '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
137 parts = re.split(r'(\d+)', my_string)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
138 parts[1::2] = map(int, parts[1::2])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
139 return parts
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
140
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
141 def get_text_sizes(my_strings):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
142 '''Get widths & heights, in pixels, of some strings.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
143 if opts.fontsize == 0: return [(0, 0) for i in my_strings]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
144 image_size = 1, 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
145 im = Image.new(image_mode, image_size)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
146 draw = ImageDraw.Draw(im)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
147 return [draw.textsize(i, font=font) for i in my_strings]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
148
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
149 def get_seq_info(seq_size_dic):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
150 '''Return miscellaneous information about the sequences.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
151 seq_names = seq_size_dic.keys()
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
152 seq_names.sort(key=natural_sort_key)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
153 seq_sizes = [seq_size_dic[i] for i in seq_names]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
154 name_sizes = get_text_sizes(seq_names)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
155 margin = max(zip(*name_sizes)[1]) # maximum text height
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
156 return seq_names, seq_sizes, name_sizes, margin
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
157
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
158 seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
159 seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
160
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
161 def div_ceil(x, y):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
162 '''Return x / y rounded up.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
163 q, r = divmod(x, y)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
164 return q + (r != 0)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
165
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
166 def tot_seq_pix(seq_sizes, bp_per_pix):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
167 '''Return the total pixels needed for sequences of the given sizes.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
168 return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
169
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
170 def get_bp_per_pix(seq_sizes, pix_limit):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
171 '''Get the minimum bp-per-pixel that fits in the size limit.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
172 seq_num = len(seq_sizes)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
173 seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
174 if seq_pix_limit < seq_num:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
175 sys.exit(my_name + ": can't fit the image: too many sequences?")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
176 lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
177 for bp_per_pix in itertools.count(lower_bound): # slow linear search
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
178 if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
179 return bp_per_pix
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
180
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
181 sys.stderr.write(my_name + ": choosing bp per pixel...\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
182 bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width - margin1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
183 bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
184 bp_per_pix = max(bp_per_pix1, bp_per_pix2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
185 sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
186
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
187 def get_seq_starts(seq_pix, pix_tween_seqs, margin):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
188 '''Get the start pixel for each sequence.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
189 seq_starts = []
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
190 pix_tot = margin - pix_tween_seqs
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
191 for i in seq_pix:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
192 pix_tot += pix_tween_seqs
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
193 seq_starts.append(pix_tot)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
194 pix_tot += i
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
195 return seq_starts
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
196
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
197 def get_pix_info(seq_sizes, margin):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
198 '''Return pixel information about the sequences.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
199 seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
200 seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
201 tot_pix = seq_starts[-1] + seq_pix[-1]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
202 return seq_pix, seq_starts, tot_pix
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
203
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
204 seq_pix1, seq_starts1, width = get_pix_info(seq_sizes1, margin1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
205 seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
206 seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
207 seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
208 hits = [0] * (width * height) # the image data
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
209
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
210 sys.stderr.write(my_name + ": processing alignments...\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
211 for seq1, seq2, blocks in alignments:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
212 seq_start1 = seq_start_dic1[seq1]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
213 seq_start2 = seq_start_dic2[seq2]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
214 my_start = seq_start2 * width + seq_start1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
215 for beg1, beg2, size in blocks:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
216 end1 = beg1 + size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
217 end2 = beg2 + size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
218 if beg1 >= 0: j = xrange(beg1, end1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
219 else: j = xrange(-1 - beg1, -1 - end1, -1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
220 if beg2 >= 0: k = xrange(beg2, end2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
221 else: k = xrange(-1 - beg2, -1 - end2, -1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
222 if beg2 >= 0: store_value = 1
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
223 else: store_value = 2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
224 for real_pos1, real_pos2 in itertools.izip(j, k):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
225 pix1 = real_pos1 // bp_per_pix
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
226 pix2 = real_pos2 // bp_per_pix
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
227 hits[my_start + pix2 * width + pix1] |= store_value
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
228 sys.stderr.write(my_name + ": done\n")
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
229
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
230 def make_label(text, text_size, range_start, range_size):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
231 '''Return an axis label with endpoint & sort-order information.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
232 text_width = text_size[0]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
233 label_start = range_start + (range_size - text_width) // 2
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
234 label_end = label_start + text_width
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
235 sort_key = text_width - range_size
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
236 return sort_key, label_start, label_end, text
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
237
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
238 def get_nonoverlapping_labels(labels):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
239 '''Get a subset of non-overlapping axis labels, greedily.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
240 nonoverlapping_labels = []
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
241 for i in labels:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
242 if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
243 for j in nonoverlapping_labels]:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
244 nonoverlapping_labels.append(i)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
245 return nonoverlapping_labels
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
246
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
247 def get_axis_image(seq_names, name_sizes, seq_starts, seq_pix):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
248 '''Make an image of axis labels.'''
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
249 min_pos = seq_starts[0]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
250 max_pos = seq_starts[-1] + seq_pix[-1]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
251 height = max(zip(*name_sizes)[1])
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
252 labels = [make_label(i, j, k, l) for i, j, k, l in
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
253 zip(seq_names, name_sizes, seq_starts, seq_pix)]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
254 labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
255 labels.sort()
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
256 labels = get_nonoverlapping_labels(labels)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
257 image_size = max_pos, height
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
258 im = Image.new(image_mode, image_size, border_shade)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
259 draw = ImageDraw.Draw(im)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
260 for i in labels:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
261 position = i[1], 0
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
262 draw.text(position, i[3], font=font, fill=text_color)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
263 return im
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
264
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
265 image_size = width, height
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
266 im = Image.new(image_mode, image_size, background_color)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
267
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
268 for i in range(height):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
269 for j in range(width):
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
270 store_value = hits[i * width + j]
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
271 xy = j, i
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
272 if store_value == 1: im.putpixel(xy, forward_color)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
273 elif store_value == 2: im.putpixel(xy, reverse_color)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
274 elif store_value == 3: im.putpixel(xy, overlap_color)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
275
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
276 if opts.fontsize != 0:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
277 axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
278 axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
279 axis2 = axis2.rotate(270)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
280 im.paste(axis1, (0, 0))
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
281 im.paste(axis2, (0, 0))
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
282
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
283 for i in seq_starts1[1:]:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
284 box = i - pix_tween_seqs, margin2, i, height
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
285 im.paste(border_shade, box)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
286
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
287 for i in seq_starts2[1:]:
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
288 box = margin1, i - pix_tween_seqs, width, i
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
289 im.paste(border_shade, box)
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
290
06f8460885ff migrate from GitHub
yutaka-saito
parents:
diff changeset
291 im.save(args[1])