forked from joiningdata/lollipops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawpops.go
408 lines (370 loc) · 14.6 KB
/
drawpops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"flag"
"fmt"
"io"
"math" // to use logarithmic scale
"regexp"
"sort"
"strconv" // converts maf strings to floats
"strings"
"unicode"
)
var (
showLabels = flag.Bool("labels", false, "draw mutation labels above lollipops")
hideDisordered = flag.Bool("hide-disordered", false, "do not draw disordered regions")
hideMotifs = flag.Bool("hide-motifs", false, "do not draw motifs")
hideAxis = flag.Bool("hide-axis", false, "do not draw the aa position axis")
forPDF = flag.Bool("for-pdf", false, "use solid fill instead of patterns for PDF output")
)
const (
LollipopRadius = 4
LollipopHeight = 28
BackboneHeight = 14
MotifHeight = 18
DomainHeight = 24
Padding = 15
AxisPadding = 10
AxisHeight = 15
TextPadding = 5
GraphicHeight = DomainHeight + Padding*2
//GraphicWidth = 740
)
const svgHeader = `<?xml version='1.0'?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%d" height="%d">
<defs>
<filter id="ds" x="0" y="0">
<feOffset in="SourceAlpha" dx="2" dy="2" />
<feComponentTransfer><feFuncA type="linear" slope="0.2"/></feComponentTransfer>
<feGaussianBlur result="blurOut" stdDeviation="1" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
<pattern id="disordered-hatch" patternUnits="userSpaceOnUse" width="4" height="4">
<path d="M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2" stroke="#000000" opacity="0.3" />
</pattern>
</defs>
`
const svgFooter = `</svg>`
type Tick struct {
Pos int
Pri int
Col string //added new color field - Jim H.
Hgt float64 //added new maf height field - Jim H.
}
type TickSlice []Tick
func (t TickSlice) NextBetter(i, maxDist int) int {
for j := i; j < len(t); j++ {
if (t[j].Pos - t[i].Pos) > maxDist {
return i
}
if t[j].Pri > t[i].Pri {
return j
}
}
return i
}
// implement sort interface
func (t TickSlice) Len() int { return len(t) }
func (t TickSlice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t TickSlice) Less(i, j int) bool {
if t[i].Pos == t[j].Pos {
// sort high-priority first if same
return t[i].Pri > t[j].Pri
}
return t[i].Pos < t[j].Pos
}
var stripChangePos = regexp.MustCompile(`(\*|[[:alpha:]]*|\.)(\d*-?\d*)(\*|\w*)`) //new regex accomodates multi-nucleotide substitutions and other corner cases
// BlendColorStrings blends two CSS #RRGGBB colors together with a straight average.
func BlendColorStrings(a, b string) string {
var r1, g1, b1, r2, g2, b2 int
fmt.Sscanf(strings.ToUpper(a), "#%02X%02X%02X", &r1, &g1, &b1)
fmt.Sscanf(strings.ToUpper(b), "#%02X%02X%02X", &r2, &g2, &b2)
return fmt.Sprintf("#%02X%02X%02X", (r1+r2)/2, (g1+g2)/2, (b1+b2)/2)
}
// AutoWidth automatically determines the best width to use to fit all
// available domain names into the plot.
func AutoWidth(g *PfamGraphicResponse) int {
aaLen, _ := g.Length.Float64()
w := 400.0
for _, r := range g.Regions {
sstart, _ := r.Start.Float64()
send, _ := r.End.Float64()
aaPart := (send - sstart) / aaLen
minTextWidth := MeasureFont(r.Text, 12) + (TextPadding * 2) + 1
ww := (float64(minTextWidth) / aaPart)
if ww > w {
w = ww
}
}
return int(w + (Padding * 2))
}
// DrawSVG writes the SVG XML document to w, with the provided changes in changelist
// and Pfam domain/region information in g. If GraphicWidth=0, the AutoWidth is called
// to determine the best diagram width to fit all labels.
func DrawSVG(w io.Writer, GraphicWidth int, changelist []string, maf int, g *PfamGraphicResponse) {
var maflist = make([]float64, len(changelist)/2)
if maf != 0 {
var newlist = make([]string, len(changelist)/2)
for i, chg := range changelist {
if i%2 == 0 {
newlist[i/2] = chg
} else {
f, err := strconv.ParseFloat(chg, 64)
maflist[i/2] = f
if err != nil {
//fmt.Println(err)
}
}
}
changelist = newlist
}
if GraphicWidth == 0 {
GraphicWidth = AutoWidth(g)
}
aaLen, _ := g.Length.Int64()
scale := float64(GraphicWidth-Padding*2) / float64(aaLen)
popSpace := int(float64(LollipopRadius+2) / scale)
aaSpace := int(20 / scale)
startY := Padding
if *showLabels {
startY += Padding // add some room for labels
}
pops := TickSlice{}
col := "blue" //by default, synonymous
hgt := float64(10) // by default, log(10)=1
ht := GraphicHeight
var checkmulti = regexp.MustCompile(`(\d*)-(\d*)`)
var aachange = regexp.MustCompile(`\w+`)
max := float64(0)
stackedmax := float64(0)
if len(changelist) > 0 {
// parse changelist and check if lollipops need staggered
for i, chg := range changelist {
cpos := stripChangePos.FindStringSubmatch(chg)
spos := 0
//fmt.Printf("%[1]s\n",chg)
//fmt.Printf("%[1]s\t%[2]s\t%[3]s\n",cpos[1],cpos[2],cpos[3])
//fmt.Printf("%[1]d\n",len(cpos[1]))
if checkmulti.MatchString(cpos[2]) {
blah := checkmulti.FindStringSubmatch(cpos[2])
cpos[2] = blah[2]
col = "green" // multi-nucleotide substitution
} else if aachange.MatchString(cpos[3]) { // non-synonymous polymorphism
col = "red"
} else if !aachange.MatchString(cpos[3]) { // synonymous polymorphism
col = "blue"
}
if cpos[1] == "*" {
col = "purple" // stop_lost
} else if cpos[3] == "*" {
col = "orange" //stop_gained
}
fmt.Sscanf(cpos[2], "%d", &spos)
if maf != 0 {
hgt = maflist[i]
}
pops = append(pops, Tick{spos, -i, col, hgt}) //added new field
}
sort.Sort(pops)
maxStaggered := LollipopRadius + LollipopHeight
stackedmax = float64(0)
max = float64(0)
foo := float64(0)
for pi, pop := range pops {
h := LollipopRadius + LollipopHeight
if max < math.Abs(math.Log10(pop.Hgt)) { //max to create scale
max = math.Abs(math.Log10(pop.Hgt))
}
for pj := pi + 1; pj < len(pops); pj++ {
foo += math.Abs(math.Log10(pops[pj].Hgt))
if foo > stackedmax { // in case we do stacking later
stackedmax = foo
}
if pops[pj].Pos-pop.Pos > popSpace {
break
}
//h += LollipopRadius * 3
}
if h > maxStaggered {
maxStaggered = h
}
}
ht += int((max+1)*10) + maxStaggered + LollipopHeight
startY += int((max+1)*10) + maxStaggered - (LollipopRadius + LollipopHeight)
}
if !*hideAxis {
ht += AxisPadding + AxisHeight
}
ticks := []Tick{
Tick{0, 0, col, hgt}, // start isn't very important (0 is implied) // wrote in new field
Tick{int(aaLen), 99, col, hgt}, // always draw the length in the axis // wrote in new field
}
fmt.Fprintf(w, svgHeader, GraphicWidth, ht)
if len(pops) > 0 {
poptop := startY + LollipopRadius
popbot := poptop + LollipopHeight
startY = popbot - (DomainHeight-BackboneHeight)/2
// draw lollipops
for pi, pop := range pops {
ticks = append(ticks, Tick{pop.Pos, 10, col, hgt}) // wrote in new field
spos := Padding + (float64(pop.Pos) * scale)
//fmt.Println(int(math.Abs(math.Log10(pop.Hgt))))
mytop := poptop - int(math.Abs(math.Log10(pop.Hgt))*10)
for pj := pi + 1; pj < len(pops); pj++ {
if pops[pj].Pos-pop.Pos > popSpace {
break
}
//mytop -= LollipopRadius * 3
}
fmt.Fprintf(w, `<line x1="%f" x2="%f" y1="%d" y2="%d" stroke="#BABDB6" stroke-width="1" stroke-opacity="0.3"/>`, spos, spos, mytop, popbot) // changed stroke width and opacity
if pop.Col == "red" { // if non-synonymous - Jim H.
fmt.Fprintf(w, `<a xlink:title="%s"><circle cx="%f" cy="%d" r="%d" fill="#FF5555" fill-opacity="0.9" /></a>`, //color change
changelist[-pop.Pri], spos, mytop, LollipopRadius)
}
if pop.Col == "blue" { // if synonymous - Jim H.
fmt.Fprintf(w, `<a xlink:title="%s"><circle cx="%f" cy="%d" r="%d" fill="#0000ff" fill-opacity="0.9" /></a>`, //color change
changelist[-pop.Pri], spos, mytop, LollipopRadius)
}
if pop.Col == "purple" { // if stop-lost - Jim H.
fmt.Fprintf(w, `<a xlink:title="%s"><circle cx="%f" cy="%d" r="%d" fill="#CC00CC" fill-opacity="0.9" /></a>`, //color change
changelist[-pop.Pri], spos, mytop, LollipopRadius)
}
if pop.Col == "orange" { // if stop-gained - Jim H.
fmt.Fprintf(w, `<a xlink:title="%s"><circle cx="%f" cy="%d" r="%d" fill="#FFB733" fill-opacity="0.9" /></a>`, //color change
changelist[-pop.Pri], spos, mytop, LollipopRadius)
}
if pop.Col == "green" { // if multi-nucleotide - Jim H.
fmt.Fprintf(w, `<a xlink:title="%s"><circle cx="%f" cy="%d" r="%d" fill="#008000" fill-opacity="0.9" /></a>`, //color change
changelist[-pop.Pri], spos, mytop, LollipopRadius)
}
if *showLabels {
fmt.Fprintf(w, `<g transform="translate(%f,%d) rotate(-30)">`,
spos, mytop)
fmt.Fprintf(w, `<text style="font-size:10px;font-family:sans-serif;fill:#555;" text-anchor="middle" x="0" y="%f">%s</text></g>`,
(LollipopRadius * -1.5), changelist[-pop.Pri])
}
}
}
// draw the backbone
fmt.Fprintf(w, `<a xlink:title="%s, %s (%daa)"><rect fill="#BABDB6" x="%d" y="%d" width="%d" height="%d"/></a>`,
g.Metadata.Identifier, g.Metadata.Description, aaLen,
Padding, startY+(DomainHeight-BackboneHeight)/2, GraphicWidth-(Padding*2), BackboneHeight)
disFill := "url(#disordered-hatch)"
if *forPDF {
disFill = `#000;" opacity="0.15`
}
if !*hideMotifs {
// draw transmembrane, signal peptide, coiled-coil, etc motifs
for _, r := range g.Motifs {
if r.Type == "pfamb" {
continue
}
if r.Type == "disorder" && *hideDisordered {
continue
}
sstart, _ := r.Start.Float64()
swidth, _ := r.End.Float64()
sstart *= scale
swidth = (swidth * scale) - sstart
fmt.Fprintf(w, `<a xlink:title="%s">`, r.Type)
if r.Type == "disorder" {
// draw disordered regions with a understated diagonal hatch pattern
fmt.Fprintf(w, `<rect fill="%s" x="%f" y="%d" width="%f" height="%d"/>`, disFill,
Padding+sstart, startY+(DomainHeight-BackboneHeight)/2, swidth, BackboneHeight)
} else {
fmt.Fprintf(w, `<rect fill="%s" x="%f" y="%d" width="%f" height="%d" filter="url(#ds)"/>`, BlendColorStrings(r.Color, "#FFFFFF"),
Padding+sstart, startY+(DomainHeight-MotifHeight)/2, swidth, MotifHeight)
tstart, _ := r.Start.Int64()
tend, _ := r.End.Int64()
ticks = append(ticks, Tick{int(tstart), 1, col, hgt}) // new field
ticks = append(ticks, Tick{int(tend), 1, col, hgt}) // new field
}
fmt.Fprintln(w, `</a>`)
}
}
// draw the curated domains
for _, r := range g.Regions {
sstart, _ := r.Start.Float64()
swidth, _ := r.End.Float64()
ticks = append(ticks, Tick{int(sstart), 5, col, hgt}) // new field
ticks = append(ticks, Tick{int(swidth), 5, col, hgt}) // new field
sstart *= scale
swidth = (swidth * scale) - sstart
fmt.Fprintf(w, `<g transform="translate(%f,%d)"><a xlink:href="%s" xlink:title="%s">`, Padding+sstart, startY, "http://pfam.xfam.org"+r.Link, r.Metadata.Description)
fmt.Fprintf(w, `<rect fill="%s" x="0" y="0" width="%f" height="%d" filter="url(#ds)"/>`, r.Color, swidth, DomainHeight)
if swidth > 10 {
if len(r.Metadata.Description) > 1 && float64(MeasureFont(r.Metadata.Description, 12)) < (swidth-TextPadding) {
// we can fit the full description! nice!
fmt.Fprintf(w, `<text style="font-size:12px;font-family:sans-serif;fill:#ffffff;" text-anchor="middle" x="%f" y="%d">%s</text>`, swidth/2.0, 4+DomainHeight/2, r.Metadata.Description)
} else if float64(MeasureFont(r.Text, 12)) < (swidth - TextPadding) {
fmt.Fprintf(w, `<text style="font-size:12px;font-family:sans-serif;fill:#ffffff;" text-anchor="middle" x="%f" y="%d">%s</text>`, swidth/2.0, 4+DomainHeight/2, r.Text)
} else {
didOutput := false
if strings.IndexFunc(r.Text, unicode.IsPunct) != -1 {
// if the label is too long, we assume the most
// informative word is the last one, but if that
// still won't fit we'll move up
//
// Example: TP53 has P53_TAD and P53_tetramer
// domains but boxes aren't quite large enough.
// Showing "P53..." isn't very helpful.
parts := strings.FieldsFunc(r.Text, unicode.IsPunct)
pre := ".."
post := ""
for i := len(parts) - 1; i >= 0; i-- {
if i == 0 {
pre = ""
}
if float64(MeasureFont(pre+parts[i]+post, 12)) < (swidth - TextPadding) {
fmt.Fprintf(w, `<text style="font-size:12px;font-family:sans-serif;fill:#ffffff;" text-anchor="middle" x="%f" y="%d">%s</text>`, swidth/2.0, 4+DomainHeight/2, pre+parts[i]+post)
didOutput = true
break
}
post = ".."
}
}
if !didOutput && swidth > 40 {
sub := r.Text
for mx := len(r.Text) - 2; mx > 0; mx-- {
sub = strings.TrimFunc(r.Text[:mx], unicode.IsPunct) + ".."
if float64(MeasureFont(sub, 12)) < (swidth - TextPadding) {
break
}
}
fmt.Fprintf(w, `<text style="font-size:12px;font-family:sans-serif;fill:#ffffff;" text-anchor="middle" x="%f" y="%d">%s</text>`, swidth/2.0, 4+DomainHeight/2, sub)
}
}
}
fmt.Fprintln(w, `</a></g>`)
}
if !*hideAxis {
startY += DomainHeight + AxisPadding
fmt.Fprintln(w, `<g class="axis">`)
fmt.Fprintf(w, `<line x1="%d" x2="%d" y1="%d" y2="%d" stroke="#AAAAAA" />`, Padding, GraphicWidth-Padding, startY, startY)
fmt.Fprintf(w, `<line x1="%d" x2="%d" y1="%d" y2="%d" stroke="#AAAAAA" />`, Padding, Padding, startY, startY+(AxisHeight/3))
fmt.Fprintf(w, `<line x1="%d" x2="%d" y1="%d" y2="%d" stroke="#AAAAAA" />`, Padding, Padding, startY, -1*int((max+1)*10)-LollipopHeight-LollipopRadius-AxisHeight-DomainHeight+startY) // maf axis
fmt.Fprintf(w, `<text style="font-size:10px;font-family:sans-serif;fill:#000000;" text-anchor="middle" x="%d" y="%d">%s</text>`, Padding+10, -1*int((max+2)*10)-LollipopRadius-LollipopHeight-DomainHeight+startY, "-log10(rarity)")
for j := -1; j <= int(max); j = j + 1 {
fmt.Fprintf(w, `<line x1="%d" x2="%d" y1="%d" y2="%d" stroke="#AAAAAA" />`, Padding-10, Padding, -10*(int(max)-j)-LollipopRadius-LollipopHeight-DomainHeight+startY, -10*(int(max)-j)-LollipopRadius-LollipopHeight-DomainHeight+startY)
fmt.Fprintf(w, `<text style="font-size:10px;font-family:sans-serif;fill:#000000;" text-anchor="middle" x="%d" y="%d">%d</text>`, Padding-5, -10*(int(max)-j)-LollipopRadius-LollipopHeight-DomainHeight+startY, int(max)-j)
}
ts := TickSlice(ticks)
sort.Sort(ts)
lastDrawn := 0
for i, t := range ts {
if lastDrawn > 0 && (t.Pos-lastDrawn) < aaSpace {
continue
}
j := ts.NextBetter(i, aaSpace)
if i != j {
continue
}
lastDrawn = t.Pos
x := Padding + (float64(t.Pos) * scale)
fmt.Fprintf(w, `<line x1="%f" x2="%f" y1="%d" y2="%d" stroke="#AAAAAA" />`, x, x, startY, startY+(AxisHeight/3))
fmt.Fprintf(w, `<text style="font-size:10px;font-family:sans-serif;fill:#000000;" text-anchor="middle" x="%f" y="%d">%d</text>`, x, startY+AxisHeight, t.Pos)
}
fmt.Fprintln(w, "</g>")
}
fmt.Fprintln(w, svgFooter)
}