-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPAViewerWidget.cpp
369 lines (233 loc) · 10.1 KB
/
EPAViewerWidget.cpp
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
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of epa_viewer.
*
* epa_viewer 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.
*
* epa_viewer 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 epa_viewer. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <QScriptEngine>
#include <QFile>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QListView>
#include <QTimer>
#include <QTransform>
#include "ivymike/tree_parser.h"
#include "EPAViewerWidget.h"
#include "PhyloTreeView.h"
#include "ui_EPAViewerWidget.h"
namespace im_tree = ivy_mike::tree_parser_ms;
EPAViewerWidget::EPAViewerWidget(const char* jplace_name, QWidget* parent)
: QWidget(parent),
ui_(new Ui::EPAViewerWidget()),
tiTest( new QTimer(this) ),
cur_taxon_placement_(-1)
{
tiTest->setObjectName("tiTest");
ui_->setupUi(this);
// QMetaObject::connectSlotsByName(this);
QFile f( jplace_name );
f.open( QFile::ReadOnly);
QByteArray fd_ba = f.readAll();
QScriptValue sc;
QScriptEngine engine;
QString ss;
ss += "(";
ss += fd_ba;
ss += ")";
sc = engine.evaluate(ss); // In new versions it may need to look like engine.evaluate("(" + QString(result) + ")");
// sc.
QScriptValue fields = sc.property("fields");
assert( fields.isArray() );
qint32 num_fields = fields.property("length").toInt32();
qint32 idx_edge_num = -1;
qint32 idx_like_weight_ratio = -1;
for( qint32 i = 0; i < num_fields; ++i ) {
QString name = fields.property(i).toString();
// std::cout << name.toStdString() << std::endl;
if( name == "edge_num" ) {
idx_edge_num = i;
} else if( name == "like_weight_ratio" ) {
idx_like_weight_ratio = i;
}
}
assert( idx_edge_num >= 0 );
assert( idx_like_weight_ratio >= 0 );
QScriptValue placements = sc.property("placements");
assert( placements.isArray() );
qint32 num_placements = placements.property( "length" ).toInt32();
std::cout << "num_placements: " << num_placements << "\n";
for( qint32 i = 0; i < num_placements; ++i ) {
QScriptValue pm = placements.property( i );
assert( pm.isValid() );
QScriptValue p = pm.property("p");
QScriptValue n = pm.property("n");
assert( n.isArray() );
QString name = n.property(0).toString();
taxon_placements_.push_back( taxon_placement(name.toStdString(), p, idx_edge_num, idx_like_weight_ratio ) );
}
taxon_list_model_.reset( new TaxonListModel(taxon_placements_) );
ui_->lvTaxon->setModel( taxon_list_model_.data() );
connect( ui_->lvTaxon->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT( lvTaxon_selectionChanged(QItemSelection,QItemSelection)));
std::cout << "isArray: " << sc.property("tree").toString().toStdString() << "\n";
QScriptValue tree_v = sc.property("tree");
QByteArray tree_ascii = tree_v.toString().toAscii();
im_tree::ln_pool pool;
im_tree::parser p(tree_ascii.begin(), tree_ascii.end(), pool );
std::copy( tree_ascii.begin(), tree_ascii.end(), std::ostream_iterator<char>(std::cout) );
std::cout << std::endl;
im_tree::lnode *t = p.parse();
PhyloTreeView *ptv = new PhyloTreeView(t->get_smart_ptr().lock());
QGraphicsScene *gs = ptv->initGraphicsScene(&pi_map_);
gvTree = new QGraphicsView( gs, this );
gvTree->setRenderHint(QPainter::Antialiasing, true );
gvTree->setRenderHint( QPainter::HighQualityAntialiasing, true );
gvTree->setBackgroundBrush(QBrush(Qt::black));
ui_->saTree->setWidget(gvTree);
gs->setParent(gvTree);
tiTest->setInterval(10);
tiTest->setSingleShot(false);
// tiTest->start();
// qs->setVisible(true);
/*
if (sc.property("result").isArray())
{
QStringList items;
qScriptValueToSequence(sc.property("result"), items);
foreach (QString str, items) {
qDebug("value %s",str.toStdString().c_str());
}
}*/
}
void EPAViewerWidget::on_tiTest_timeout() {
size_t n = pi_map_.size();
size_t r = rand() % n;
std::stringstream ss;
ss << r + 1;
std::map< std::string, QGraphicsPathItem* >::iterator it = pi_map_.find( ss.str() );
if( it == pi_map_.end() ) {
return;
}
QColor rc( rand() % 256, rand() % 256, rand() % 256 );
it->second->setPen( rc );
gvTree->repaint();
}
QVariant TaxonListModel::data(const QModelIndex& index, int role) const {
if( role == Qt::DisplayRole ) {
return QString(placement_list_.at( index.row() ).taxon().c_str());
}
return QVariant();
}
int TaxonListModel::rowCount(const QModelIndex& parent) const {
return placement_list_.size();
}
QVariant PlacementListModel::data(const QModelIndex& index, int role) const {
if( role == Qt::DisplayRole ) {
return placement_.pos_at(index.row() ).lh_weight();
}
return QVariant();
}
int PlacementListModel::rowCount(const QModelIndex& parent) const {
return placement_.size();
}
taxon_placement::taxon_placement(const std::string& name, const QScriptValue& pos_list, qint32 idx_name, qint32 idx_lhw) : taxon_(name)
{
assert( pos_list.isArray() );
qint32 len = pos_list.property( "length" ).toInt32();
std::cout << "taxon: " << name << "\n";
for( qint32 i = 0; i < len; ++i ) {
QScriptValue qspos = pos_list.property(i);
assert( qspos.isArray() );
QScriptValue name = qspos.property(idx_name).toString();
qsreal lhw = qspos.property(idx_lhw).toNumber();
std::cout << "pos: " << name.toString().toStdString() << " " << float(lhw) << std::endl;
pos_.push_back(position( name.toString().toStdString(), float(lhw) ));
}
}
void EPAViewerWidget::lvTaxon_selectionChanged( const QItemSelection & selected, const QItemSelection & deselected) {
// size_t row = idx.row();
// assert(taxon_placements_.size() > row );
size_t row = selected.front().top();
std::cout << "row: " << row << "\n";
if( ui_->lvPosition->model() != 0 ) {
ui_->lvPosition->selectionModel()->clear();
}
cur_taxon_placement_ = row;
placement_list_model_.reset( new PlacementListModel( taxon_placements_.at(cur_taxon_placement_) ) );
ui_->lvPosition->setModel( placement_list_model_.data());
connect( ui_->lvPosition->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT( lvPosition_selectionChanged(QItemSelection,QItemSelection)));
ui_->lvPosition->setSelectionMode( QListView::ExtendedSelection );
ui_->lvPosition->selectAll();
}
void EPAViewerWidget::lvPosition_selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) {
const taxon_placement &p = taxon_placements_.at(cur_taxon_placement_);
QPen pen1 = PhyloTreeView::default_pen();
// pen1.setColor( QColor(Qt::red) );
// pen1.setWidth(2);
// pen1.setCapStyle(Qt::RoundCap);
// pen1.setJoinStyle(Qt::RoundJoin);
//
QColor c0( Qt::red );
QColor c1( Qt::blue );
qreal rd = c1.redF() - c0.redF();
qreal gd = c1.greenF() - c0.greenF();
qreal bd = c1.blueF() - c0.blueF();
// qreal hue0 = c0.hsvHueF();
// qreal hue1 = c1.hsvHueF();
for( QList< QItemSelectionRange >::const_iterator its = deselected.begin(); its != deselected.end(); ++its ) {
for( int i = its->top(); i <= its->bottom(); ++ i ) {
const taxon_placement::position &ppos = p.pos_at(i);
const std::string &branch_name = ppos.branch_name();
std::map< std::string, QGraphicsPathItem* >::iterator it = pi_map_.find( branch_name );
assert( it != pi_map_.end() );
it->second->setPen(PhyloTreeView::default_pen());
}
}
QRectF br_all;
bool have_item = false;
for( QList< QItemSelectionRange >::const_iterator its = selected.begin(); its != selected.end(); ++its ) {
for( int i = its->top(); i <= its->bottom(); ++ i ) {
const taxon_placement::position &ppos = p.pos_at(i);
const std::string &branch_name = ppos.branch_name();
std::map< std::string, QGraphicsPathItem* >::iterator it = pi_map_.find( branch_name );
// it->second->
assert( it != pi_map_.end() );
br_all = br_all.united(it->second->boundingRect());
have_item = true;
float lhw = ppos.lh_weight();
QColor col;
col.setRedF(c0.redF() + rd * lhw );
col.setGreenF(c0.greenF() + gd * lhw );
col.setBlueF(c0.blueF() + bd * lhw );
pen1.setColor(col);
it->second->setPen(pen1);
}
}
if( have_item ) {
// QRectF br = main_item->boundingRect();
// gvTree->ensureVisible(br_all);
gvTree->centerOn(br_all.center());
// gvTree->ens`
// gvTree->ensureVisible( main_item );
}
gvTree->repaint();
}
void EPAViewerWidget::on_slZoom_valueChanged(int value) {
float scale = value / 100.0;
QTransform trans;
trans.scale(scale,scale);
gvTree->setTransform(trans);
}