-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathepa_extract_qs_covered.cpp
134 lines (105 loc) · 3.75 KB
/
epa_extract_qs_covered.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
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara 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.
*
* papara 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 papara. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "ivymike/tree_traversal_utils.h"
#include "ivymike/tree_parser.h"
#include "ivymike/multiple_alignment.h"
#include "boost/dynamic_bitset.hpp"
using ivy_mike::tree_parser_ms::ln_pool;
using ivy_mike::tree_parser_ms::parser;
using ivy_mike::tree_parser_ms::lnode;
using ivy_mike::tip_collector_dumb;
using ivy_mike::visit_lnode;
bool is_gap( char c ) {
return c == '-' || c == '?' || toupper(c) == 'N';
}
void pad( std::ostream &os, size_t n, char c ) {
for( size_t i = 0; i < n; ++i ) {
os << c;
}
}
class tip_name_collector {
public:
typedef ivy_mike::tree_parser_ms::lnode lnode;
tip_name_collector( std::vector<std::string> *names ) : names_(names) {}
void operator()(lnode *n) {
if( n->m_data->isTip ) {
names_->push_back(n->m_data->tipName);
}
}
private:
std::vector<std::string> *names_;
};
int main( int argc, char *argv[] ) {
if( argc != 3 ) {
std::cerr << "usage: " << argv[0] << "<tree file> <phylip file>\n";
return 0;
}
ln_pool pool;
parser p(argv[1], pool );
lnode *n = p.parse();
std::vector<std::string> tip_names;
tip_name_collector tnc(&tip_names);
visit_lnode( n, tnc );
// apply_lnode(n, [&](lnode *x) {
// if( x->m_data->isTip ) {
// tip_names.push_back( x->m_data->tipName );
// }
// });
ivy_mike::multiple_alignment ma;
ma.load_phylip(argv[2]);
std::sort( tip_names.begin(), tip_names.end() );
boost::dynamic_bitset<> bs_all(ma.data.front().size() );
//bs_all.flip();
size_t max_name_len = 0;
for( size_t i = 0, e = ma.names.size(); i != e; ++i ) {
const std::string &name = ma.names[i];
max_name_len = std::max( max_name_len, name.size() );
const std::vector<uint8_t> &data = ma.data[i];
if( !std::binary_search( tip_names.begin(), tip_names.end(), name ) ) {
boost::dynamic_bitset<> bs(data.size());
for( size_t j = 0; j < data.size(); ++j ) {
bs[j] = !is_gap(data[j]);
}
bs_all |= bs;
}
}
std::cout << ma.names.size() << " " << bs_all.count() << "\n";
std::vector<size_t> unmasked;
{
size_t i = bs_all.find_first();
while( i != bs_all.npos ) {
unmasked.push_back(i);
i = bs_all.find_next(i);
}
}
for( size_t i = 0, e = ma.names.size(); i != e; ++i ) {
std::cout << ma.names[i];
const std::vector<uint8_t> &data = ma.data[i];
pad( std::cout, max_name_len - ma.names[i].size() + 1, ' ' );
// excract 'unmasked' characters from current sequence
// std::transform(unmasked.begin(), unmasked.end(), std::ostream_iterator<char>(std::cout), [&](size_t u) { return data[u]; });
//
for( std::vector<size_t>::iterator it = unmasked.begin(); it != unmasked.end(); ++it ) {
assert( *it < data.size() );
std::cout << data[*it];
}
std::cout << "\n";
}
}