-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvplot2.cpp
More file actions
141 lines (121 loc) · 3.9 KB
/
Copy pathvplot2.cpp
File metadata and controls
141 lines (121 loc) · 3.9 KB
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
#include "vplot2.h"
#include "utils.h"
#include "readcontainer.h"
#include <limits>
vPlot::vPlot() :
minLinks(std::numeric_limits<uint>::max()),
maxLinks(0),
genome(nullptr)
{
}
int vPlot::exonCount () {
int n = 0;
for (auto& chr : chromosomes) {
n += chr.second->nExons();
}
return n;
}
PlotChromosome* vPlot::addChromosome ( Genome* g, const chr_num_t id ) {
if (!genome) { // save the genome for further references... a temporary hack to avoid rewriting in later stages
genome = g;
}
auto lookup = chromosomes.find(id);
if (lookup != chromosomes.end()) { // exists already
return lookup->second;
}
PlotChromosome* chr = new PlotChromosome(chromosomes.size(),
genome->getLength(id),
genome->getChrName(id));
chromosomes.emplace(id, chr);
return chr;
}
std::shared_ptr<Rect> vPlot::writeEpsHeader ( std::ostream& out, const int dx, const int dy, Rect& contentBounds ) {
// determine longest chromosome to scale output chromosomes
int longest = 0;
for (auto& chr : chromosomes) {
if (chr.second->len > longest) {
longest = chr.second->len;
}
}
out << "%!PS-Adobe-3.0 EPSF-3.0\n";
auto br = boundingRect(); // get bounding box size from graph
// float scale = 1.0 * br->w / contentBounds.w;
float scale = WIDTH * 1.0 / br->w; // set scaling factor for graph
int h = br->h + scale * br->h;
out << "%%BoundingBox: " << 0 << " " << 0 << " " << WIDTH + 2*dx << " " << h << "\n";
br->x += dx;
br->y += dy;
br->w -= 2*dx;
br->h -= 2*dy;
// setup font
out << "/Helvetica findfont\n" << dy * 0.5 << " scalefont\n0 setgray\nsetfont\n\n";
float chr_x_scale = WIDTH * 1.0 / (longest + dx); // scale chromosomes to fit to plot
// ps-functions to save file size
// put a text label at x/y
out << "/lbl { % text, x, y\n"
" /Helvetica findfont\n"
" " << dy * 0.5 << " scalefont\n"
" 0 0 0 setrgbcolor setfont\n"
" moveto\n"
" show\n"
"}def\n\n";
// line from x y straight right by w
out << "/cL { %w x y\n";
out << " newpath\n";
out << " 0 setgray\n";
out << " moveto\n";
out << " 0 rlineto\n";
out << " 1 setlinewidth\n";
out << " stroke\n";
out << "} def\n\n";
// line from x0 y0 to x1 y1
out << "/conn { %link_num, ln_x, ln_y, x0, y0, x1, y1\n";
out << " newpath\n";
out << " moveto\n";
out << " lineto\n";
out << " 0 setgray\n";
out << " stroke\n";
out << " /Helvetica findfont\n ";
out << dy * 0.5 << " scalefont\n";
out << " 1 0 0 setrgbcolor setfont\n";
out << " moveto\n";
out << " show\n";
out << "}def\n\n";
// draw exon of color r g b with width w on position x y
out << "/dY " << dy*0.4 << " def\n\n";
out << "/exon{ % r, g, b, w, x, y\n";
out << " newpath\n";
out << " moveto % x, y\n";
out << " 0 dY rlineto\n";
out << " 0 rlineto % w\n";
out << " 0 dY neg rlineto\n closepath\n gsave\n";
out << " setrgbcolor fill\n % r g b\n";
out << " grestore \n 0 setgray\n stroke \n} def\n\n";
// write chromosomes
int chrColId(0); // pick colors from palette
for (auto& chr : chromosomes) { // write to file and adjust free space
chr.second->writeEps(out, *br, dx, dy, chr_x_scale, PALETTE[chrColId++]);
// auto h = chr.second->boundingRect()->h + dy;
int h = dy;
// br->y += h;
br->h += h;
debug("chromosome painted, bbox now: "
+ std::to_string(br->x) + "/" + std::to_string(br->y)
+ ": " + std::to_string(br->w) + "x" + std::to_string(br->h));
}
br->h += dy;
return br;
}
void vPlot::connectExons ( p_read_t lt, p_read_t rt ) {
assume(lt != nullptr, "Left exon doesn't exist");
assume(rt != nullptr, "Right exon doesn't exist");
std::cout << "---- connecting " << *lt << " to " << *rt << std::endl;
if (!lt->threePrimeRead) {
lt->threePrimeRead = new link_list_t;
}
if (!rt->fivePrimeRead) {
rt->fivePrimeRead = new link_list_t;
}
lt->threePrimeRead->push_back(rt);
rt->fivePrimeRead->push_back(lt);
}