Skip to content

Commit

Permalink
add a simple -legend option for generated diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Jul 7, 2016
1 parent 428a924 commit 6a556ac
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ the area is exponentially proportional to the count indicated. Examples:
####Diagram generation options

```
-legend draw a legend for colored regions
-syn-color="#0000ff" color to use for synonymous mutation markers
-mut-color="#ff0000" color to use for non-synonymous mutation markers
-hide-axis do not draw the amino position x-axis
-hide-disordered do not draw disordered regions on the backbone
-hide-motifs do not draw simple motif regions
-show-disordered draw disordered regions on the backbone
-show-motifs draw simple motif regions
-labels draw label text above lollipop markers
-no-patterns use solid fill instead of patterns (SVG only)
```
Expand Down
9 changes: 9 additions & 0 deletions data/fetch_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import (

const PfamGraphicURL = "http://pfam.xfam.org/protein/%s/graphic"

// PfamMotifNames has human-readable names from http://pfam.xfam.org/help#tabview=tab9
var PfamMotifNames = map[string]string{
"disorder": "Disordered region (Pfam/IUPred)",
"low_complexity": "Low complexity region (Pfam/SEG)",
"sig_p": "Signal peptide region (Pfam/Phobius)",
"coiled_coil": "Coiled-coil motif (Pfam/ncoils)",
"transmembrane": "Transmembrane region (Pfam/Phobius)",
}

// PfamGraphicFeature is a generic representation of various Pfam feature responses
type PfamGraphicFeature struct {
Color string `json:"colour"`
Expand Down
21 changes: 21 additions & 0 deletions drawing/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (s *Settings) prepare(changelist []string, g *data.PfamGraphicResponse) *di
s.GraphicHeight += s.AxisPadding + s.AxisHeight
}

if s.ShowLegend {
s.legendInfo = make(map[string]string)
}

d.startY = startY
d.ticks = append(d.ticks,
Tick{Pos: 0, Pri: 0}, // start isn't very important (0 is implied)
Expand Down Expand Up @@ -154,12 +158,16 @@ func (s *Settings) prepare(changelist []string, g *data.PfamGraphicResponse) *di
if r.Type == "pfamb" {
continue
}
if r.Type == "disorder" && s.HideDisordered {
continue
}
if r.Type != "disorder" {
tstart, _ := r.Start.Int64()
tend, _ := r.End.Int64()
d.ticks = append(d.ticks, Tick{Pos: int(tstart), Pri: 1})
d.ticks = append(d.ticks, Tick{Pos: int(tend), Pri: 1})
}
s.legendInfo[r.Type] = BlendColorStrings(r.Color, "#FFFFFF")
}
}

Expand Down Expand Up @@ -224,9 +232,22 @@ func (s *Settings) prepare(changelist []string, g *data.PfamGraphicResponse) *di
}
}

if label != r.Metadata.Description {
s.legendInfo[r.Metadata.Description] = r.Color
}
d.domainLabels = append(d.domainLabels, label)
}

if s.legendInfo != nil {
s.GraphicHeight += float64(1+len(s.legendInfo)) * 14.0
for key, color := range s.legendInfo {
if rename, found := data.PfamMotifNames[key]; found {
delete(s.legendInfo, key)
s.legendInfo[rename] = color
}
}
}

sort.Sort(d.ticks)
return d
}
17 changes: 17 additions & 0 deletions drawing/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ func (s *diagram) png(w io.Writer) {
}
blackFontDrawer.DrawString(spos)
}
startY += s.AxisHeight
}

for key, colorstring := range s.legendInfo {
startY += 14.0
// 15% darker than backbone (i.e. disordered color)
clr := color.RGBA{0x9E, 0xA0, 0x9A, 0xFF}
if key != data.PfamMotifNames["disorder"] {
clr = colorFromHex(BlendColorStrings(colorstring, "#FFFFFF"))
}
drawRectWHShadow(img, 4, startY, 12, 12, clr, 2*s.dpi/72.0)

blackFontDrawer.Dot = fixed.Point26_6{
X: fixed.I(20),
Y: fixed.I(int(startY + 12)), // 12=font height-baseline
}
blackFontDrawer.DrawString(key)
}

png.Encode(w, img)
Expand Down
5 changes: 5 additions & 0 deletions drawing/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package drawing

// Settings contains all the configurable options for lollipop diagram generation.
type Settings struct {
// ShowLegend adds a color-coding legend above the diagram.
ShowLegend bool
// ShowLabels adds mutation label text above lollipops markers.
ShowLabels bool
// HideDisordered hides disordered regions on the backbone even if motifs are shown.
Expand Down Expand Up @@ -68,11 +70,14 @@ type Settings struct {
GraphicHeight float64

dpi float64

legendInfo map[string]string
}

// DefaultSettings contains the "standard" diagram output config and is used by
// the package-level Draw invocations.
var DefaultSettings = Settings{
ShowLegend: false,
ShowLabels: false,
HideDisordered: false,
HideMotifs: false,
Expand Down
11 changes: 11 additions & 0 deletions drawing/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ func (s *diagram) svg(w io.Writer) {
}

fmt.Fprintln(w, "</g>")
startY += s.AxisHeight
}

for key, color := range s.legendInfo {
startY += 14.0
if key == data.PfamMotifNames["disorder"] {
color = disFill
}
fmt.Fprintf(w, `<rect fill="%s" x="4" y="%f" width="12" height="12" filter="url(#ds)"/>`, color, startY)
fmt.Fprintf(w, `<text style="font-size:12px;%sfill:#000000;" text-anchor="start" x="20" y="%f">%s</text>`,
fontSpec, startY+12, key) // 12=font height-baseline
}

fmt.Fprintln(w, svgFooter)
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
width = flag.Int("w", 0, "output width (default automatic fit labels)")
dpi = flag.Float64("dpi", 72, "output DPI for PNG rasterization")

showLegend = flag.Bool("legend", false, "draw a legend for colored regions")
showLabels = flag.Bool("labels", false, "draw mutation labels above lollipops")
showDisordered = flag.Bool("show-disordered", false, "draw disordered regions on the backbone")
showMotifs = flag.Bool("show-motifs", false, "draw simple motif regions")
Expand Down Expand Up @@ -91,6 +92,7 @@ Protein changes:
(N.B. color must come before count in tags)
Diagram generation options:
-legend draw a legend for colored regions
-syn-color="#0000ff" color to use for synonymous mutation markers
-mut-color="#ff0000" color to use for non-synonymous mutation markers
-hide-axis do not draw the amino position x-axis
Expand All @@ -110,6 +112,7 @@ Output options:
}

flag.Parse()
drawing.DefaultSettings.ShowLegend = *showLegend
drawing.DefaultSettings.ShowLabels = *showLabels
drawing.DefaultSettings.HideDisordered = !*showDisordered
drawing.DefaultSettings.HideMotifs = !*showMotifs
Expand Down Expand Up @@ -208,5 +211,4 @@ Press Enter/Ctrl-C to quit.`)
} else {
drawing.DrawSVG(f, flag.Args()[varStart:], data)
}

}

0 comments on commit 6a556ac

Please sign in to comment.