-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfigure.cpp
More file actions
313 lines (282 loc) · 13.1 KB
/
figure.cpp
File metadata and controls
313 lines (282 loc) · 13.1 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
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
/*
* hybrid-Lambda is used to simulate gene trees given species network under
* coalescent process.
*
* Copyright (C) 2010 -- 2014 Sha (Joe) Zhu
*
* This file is part of hybrid-Lambda.
*
* hybrid-Lambda is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"figure.hpp"
#include <stdexcept> // std::invalid_argument
Figure::Figure( int argc, char* const *argv ):
argc_(argc), argv_(argv){
this->init();
while( argc_i < argc_ ){
std::string argv_i( argv_[argc_i] );
if ( argv_i == "-label" ){ this->check_option(); this->option = LABEL; }
if ( argv_i == "-branch" ){ this->check_option(); this->option = BRANCH; }
if ( argv_i == "-dot" ){
this->check_method();
this->initialize_method ( DOT, ".dot" );
this->figure_file_prefix = "dotfigure";
}
if ( argv_i == "-plot" ){
this->check_method();
this->initialize_method ( LATEX, ".tex" );
this->figure_file_prefix = "texfigure";
}
if (argv_i == "-dot_file" || argv_i == "-dotF"){
this->check_method();
this->initialize_method ( DOT, ".dot" );
//this->read_prefix();
readNextStringto( this->figure_file_prefix , this->argc_i, this->argc_, this->argv_ );
}
if (argv_i == "-plot_file" || argv_i == "-plotF"){
this->check_method();
this->initialize_method ( LATEX, ".tex" );
//this->read_prefix();
readNextStringto( this->figure_file_prefix , this->argc_i, this->argc_, this->argv_ );
}
argc_i++;
}
this->finalize();
}
void Figure::initialize_method( FIGURE_PROGRAM program, string suffix){
this->method = program;
this->figure_file_suffix = suffix;
}
void Figure::check_option(){
if ( this->option != PLOT_DEFAULT ){
cerr << "Too many figure options!"<<endl;
throw std::invalid_argument ( " Plot option can either be \"-label\" or \"-branch\" " );
}
}
void Figure::check_method(){
if ( this->method != NO_METHOD ){
cerr << "Which method (figure) do you mean?"<<endl;
throw std::invalid_argument ( " Method can either be LaTex (\"-plot\" or \"-plotF\") or DOT (\"-dot\" or \"-dotF\") " );
}
}
void Figure::init(){
this->option = PLOT_DEFAULT;
this->method = NO_METHOD;
this->argc_i = 1;
}
/*! Check and remove files*/
void Figure::finalize(){
size_t found = this->figure_file_prefix.find( this->figure_file_suffix );
if ( found != string::npos ){
this->figure_file_prefix = this->figure_file_prefix.substr(0,found);
}
this->figure_file_name = this->figure_file_prefix + this->figure_file_suffix;
ifstream tmp_file( this->figure_file_name.c_str() );
if ( tmp_file.good() ) { remove(figure_file_name.c_str()); }
}
/*! \brief Produce a tex file, which is used to draw the network
*/
void Figure::plot_in_latex( ){
figure_ofstream.open( this->figure_file_name.c_str(), ios::out | ios::app | ios::binary );
figure_ofstream << "\\documentclass[10pt]{article}\n";
figure_ofstream << "\\usepackage{tikz,graphics,graphicx,lscape,fullpage,multicol,setspace}\n \\singlespacing\n \\begin{document}\n ";
figure_ofstream << "\\ifx\\du\\undefined\\newlength{\\du}\\fi\\setlength{\\du}{30\\unitlength}\n";
figure_ofstream << "\\begin{center}\n";
figure_ofstream << "\\begin{tikzpicture}[thick]\n";
//this->obj_net->print_all_node();
this->det_x_node ( );
for (size_t node_i = 0; node_i < this->obj_net->NodeContainer.size();node_i++){
string sp_node_label = this->obj_net->NodeContainer[node_i].label;
sp_node_label=rm_and_hash_sign(sp_node_label);
if (obj_net->NodeContainer[node_i].tip_bool){
figure_ofstream<<"\\node at ("<<x_node[node_i]<<"\\du,"<<obj_net->NodeContainer[node_i].rank() << "\\du) [circle,draw] ("<<sp_node_label <<") {$"<<sp_node_label <<"$};\n";
}
else{
figure_ofstream<<"\\node at ("<<x_node[node_i]<<"\\du,"<<obj_net->NodeContainer[node_i].rank() << "\\du) [circle,fill=orange,draw] ("<<sp_node_label <<") {$"<<sp_node_label <<"$};\n";
}
}
this->plot_core();
figure_ofstream <<"\\end{tikzpicture}\n\n";
figure_ofstream << "\\end{center}\n";
figure_ofstream << "\\end{document}\n";
figure_ofstream.close();
string command = "pdflatex " + this->figure_file_name;
int sys=system(command.c_str());
std::clog << "Network figure generated in file: " + this->figure_file_name << endl;
}
void Figure::edge_entry(string from, string to, size_t label, double bl, bool tip){
if ( this->option == LABEL && tip ){
figure_ofstream << ( ( this->method == DOT ) ? "" : "\\draw (" )
<< from
<< ( ( this->method == DOT ) ? " -- " : ")--(" )
<< to
<< ( ( this->method == DOT ) ? "[label=\"" : ") node [midway,left]{" )
<< label
<< ( ( this->method == DOT ) ? "\"];\n" : "};\n" );
}
else if (this->option == BRANCH){
figure_ofstream << ( ( this->method == DOT ) ? "" : "\\draw (" )
<< from
<< ( ( this->method == DOT ) ? " -- " : ")--(" )
<< to
<< ( ( this->method == DOT ) ? "[label=\"" : ") node [midway,left]{" )
<< bl
<< ( ( this->method == DOT ) ? "\"];\n" : "};\n" );
}
else{
figure_ofstream << ( ( this->method == DOT ) ? "" : "\\draw (" )
<< from
<< ( ( this->method == DOT ) ? " -- " : ")--(" )
<< to
<< ( ( this->method == DOT ) ? ";\n" : ");\n" );
}
}
void Figure::plot_core(){
for ( size_t node_i = 0; node_i < this->obj_net->NodeContainer.size()-1; node_i++ ){
string sp_node_label = this->obj_net->NodeContainer[node_i].label;
sp_node_label=rm_and_hash_sign(sp_node_label);
string sp_node_parent1_label = this->obj_net->NodeContainer[node_i].parent1->label;
sp_node_parent1_label=rm_and_hash_sign(sp_node_parent1_label);
this->edge_entry(sp_node_label, sp_node_parent1_label, obj_net->NodeContainer[node_i].e_num(), obj_net->NodeContainer[node_i].brchlen1(), !obj_net->NodeContainer[node_i].tip_bool);
if (obj_net->NodeContainer[node_i].parent2){
string sp_node_parent2_label=obj_net->NodeContainer[node_i].parent2->label;
sp_node_parent2_label=rm_and_hash_sign(sp_node_parent2_label);
this->edge_entry( sp_node_label, sp_node_parent2_label, obj_net->NodeContainer[node_i].e_num2(), obj_net->NodeContainer[node_i].brchlen2(), !obj_net->NodeContainer[node_i].tip_bool);
}
}
}
/*! \brief Produce a dot file, which is used to draw the network, and compile the dot file to a pdf file.
*/
void Figure::plot_in_dot( ){
figure_ofstream.open ( this->figure_file_name.c_str(), ios::out | ios::app | ios::binary );
figure_ofstream <<"graph G {\n rankdir=BT; ratio=compress;\n";//page="14,14"; determines the size of the ps output
this->plot_core();
if (this->obj_net->is_ultrametric){
for (size_t rank_i = this->obj_net->NodeContainer.back().rank(); rank_i > 0; rank_i--){
figure_ofstream<<"{ rank=same; ";
for (size_t node_i=0;node_i<obj_net->NodeContainer.size();node_i++){
if (obj_net->NodeContainer[node_i].rank() == rank_i){
string sp_node_label = obj_net->NodeContainer[node_i].label;
sp_node_label = rm_and_hash_sign(sp_node_label);
figure_ofstream << sp_node_label << " ";
}
}
figure_ofstream<<"} ;\n";
}
}
figure_ofstream << "}\n";
figure_ofstream.close();
this->execute_dot ("ps2", ".eps");
this->execute_dot ("ps2", ".ps");
this->execute_dot ("pdf", ".pdf");
std::clog << "Dot figure generated in file: " + this->figure_file_prefix + ".pdf" << endl;
}
void Figure::execute_dot(string method, string suffix){
string command = "dot -T" + method + " " + this->figure_file_prefix + ".dot -o " + this->figure_file_prefix + suffix;
int sys = system( command.c_str() );
}
/*! \brief When drawing network in .tex files, detemine the x coordinates of nodes
*/
void Figure::det_x_node ( ){
this->x_node = valarray <int> (this->obj_net->NodeContainer.size());
x_node[x_node.size()-1] = 0; //root x-axis value is zero
for ( size_t rank_i = this->obj_net->NodeContainer.back().rank(); rank_i > 0; rank_i-- ){ // start from the children of the root
this->x_node_tmp.clear();
this->x_node_tmp_index.clear();
for ( size_t node_i = 0; node_i < this->obj_net->NodeContainer.size(); node_i++ ){
if ( this->obj_net->NodeContainer[node_i].rank() == rank_i){
dout << " Node " << this->obj_net->NodeContainer[node_i].label <<", "<< &(this->obj_net->NodeContainer[node_i]) << "(rank = "<< this->obj_net->NodeContainer[node_i].rank()<<")" <<endl;
assert(this->obj_net->NodeContainer[node_i].print_dout());dout<<endl;
size_t n_child = this->obj_net->NodeContainer[node_i].child.size();
int parent_x = x_node[node_i];
int start_child_x = parent_x - floor( n_child / 2 );
bool odd_num_child = (n_child % 2) == 1 ? true:false;
dout << "parent x = " << parent_x <<" " << "start_child_x = "<<start_child_x <<" "<<endl;
for ( size_t child_i = 0; child_i < n_child; child_i++ ){
dout << " child_"<<child_i << ", x = " ;
for ( size_t node_j = 0; node_j < this->obj_net->NodeContainer.size(); node_j++ ){
//if ( node_j == this->obj_net->NodeContainer[node_i].child[child_i]->node_index ){
//cout<<"this->obj_net->NodeContainer[node_i].child[child_i]="<<this->obj_net->NodeContainer[node_i].child[child_i] << " &this->obj_net->NodeContainer["<<node_j<<"]"<<&this->obj_net->NodeContainer[node_j]<<endl;
if ( &this->obj_net->NodeContainer[node_j] == this->obj_net->NodeContainer[node_i].child[child_i] ){
if ( !odd_num_child && start_child_x == parent_x ){
start_child_x++;
}
x_node[node_j] = start_child_x;
start_child_x++;
dout << x_node[node_j] ;
}
}
}
this->x_node_tmp.push_back(x_node[node_i]);
this->x_node_tmp_index.push_back(node_i);
}
else {
continue;
}
dout << endl;
}
this->x_node_shift();
}
dout << " det_X_node finished "<<endl;
}
void Figure::x_node_shift(){
if ( this->x_node_tmp.size() < 2 ){
//cout <<" no shift"<<endl;
return;
}
//cout <<" Need shift"<<endl;
bool need_to_shift=true;
while ( need_to_shift ){
for (size_t x_node_tmp_i=0;x_node_tmp_i<x_node_tmp.size();x_node_tmp_i++){
int current_x_node_tmp=x_node_tmp[x_node_tmp_i];
for (size_t x_node_tmp_j=x_node_tmp_i+1;x_node_tmp_j<x_node_tmp.size();x_node_tmp_j++){
if (current_x_node_tmp == x_node_tmp[x_node_tmp_j]){
if ( x_node_tmp[x_node_tmp_j] > 0 ){
x_node_tmp[x_node_tmp_j]++;
x_node[x_node_tmp_index[x_node_tmp_j]]++;
}
else{
x_node_tmp[x_node_tmp_j]--;
x_node[x_node_tmp_index[x_node_tmp_j]]--;
}
}
}
}
need_to_shift=false;
for (size_t x_node_tmp_i=0;x_node_tmp_i<x_node_tmp.size();x_node_tmp_i++){
int current_x_node_tmp=x_node_tmp[x_node_tmp_i];
for (size_t x_node_tmp_j=x_node_tmp_i+1;x_node_tmp_j<x_node_tmp.size();x_node_tmp_j++){
if (current_x_node_tmp==x_node_tmp[x_node_tmp_j]){
need_to_shift=true;
break;
}
}
if (need_to_shift){
break;
}
}
}
}
void Figure::plot( string net_str ){
if ( net_str.size() == 0 ) { throw std::invalid_argument ("Population structure is undefined!!!");}
this->obj_net = new Net (net_str);
if ( this->method == LATEX ){ this->plot_in_latex(); }
if ( this->method == DOT ){ this->plot_in_dot(); }
if ( this->option == BRANCH ){
std::clog << "Internal branches are labelled by post-order tree traversal." << std::endl;
}
if ( this->option == LABEL ){
std::clog << "Branch lengths are labelled." << std::endl;
}
}