-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.cpp
More file actions
158 lines (136 loc) · 4.09 KB
/
io.cpp
File metadata and controls
158 lines (136 loc) · 4.09 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
#include "io.hpp"
#include <algorithm>
#include <boost/tokenizer.hpp>
#include <exception>
#include <fstream>
#include <regex>
#include <unordered_map>
#include "compression.hpp"
using namespace std;
namespace PF = PoissonFactorization;
using Int = PF::Int;
using Float = PF::Float;
using Matrix = PF::Matrix;
using IMatrix = PF::IMatrix;
using Vector = PF::Vector;
Matrix read_matrix(istream &is, const string &separator) {
// TODO improve / factor / simplify implementation
vector<string> row_names, col_names;
Matrix m = read_floats(is, separator, row_names, col_names);
return m;
}
IMatrix vec_of_vec_to_multi_array(const vector<vector<Int>> &v) {
const size_t s1 = v.size();
const size_t s2 = v[0].size();
IMatrix A(s1, s2);
for (size_t i = 0; i < s1; ++i)
for (size_t j = 0; j < s2; ++j)
A(i, j) = v[i][j];
return A;
}
Matrix vec_of_vec_to_multi_array_float(const vector<vector<Float>> &v) {
const size_t s1 = v.size();
const size_t s2 = v[0].size();
Matrix A(s1, s2);
for (size_t i = 0; i < s1; ++i)
for (size_t j = 0; j < s2; ++j)
A(i, j) = v[i][j];
return A;
}
Matrix read_floats(istream &ifs, const string &separator,
vector<string> &row_names, vector<string> &col_names) {
using tokenizer = boost::tokenizer<boost::char_separator<char>>;
boost::char_separator<char> sep(separator.c_str());
vector<vector<Float>> m;
string line;
getline(ifs, line);
tokenizer tok(line, sep);
for (auto token : tok)
col_names.push_back(token.c_str());
while (getline(ifs, line)) {
tok = tokenizer(line, sep);
size_t col = 0;
vector<Float> v;
for (auto token : tok)
if (col++ == 0)
row_names.push_back(token);
else
v.push_back(atof(token.c_str()));
m.push_back(v);
}
auto matrix = vec_of_vec_to_multi_array_float(m);
size_t ncol = matrix.n_cols;
if (ncol == col_names.size())
return matrix;
else if (ncol == col_names.size() - 1) {
vector<string> new_col_names(begin(col_names) + 1, end(col_names));
col_names = new_col_names;
return matrix;
} else
throw std::runtime_error(
"Mismatch between number of columns and number of column labels.");
}
IMatrix read_counts(istream &ifs, const string &separator,
vector<string> &row_names, vector<string> &col_names) {
using tokenizer = boost::tokenizer<boost::char_separator<char>>;
boost::char_separator<char> sep(separator.c_str());
vector<vector<Int>> m;
string line;
getline(ifs, line);
tokenizer tok(line, sep);
for (auto token : tok)
col_names.push_back(token.c_str());
while (getline(ifs, line)) {
tok = tokenizer(line, sep);
size_t col = 0;
vector<Int> v;
for (auto token : tok)
if (col++ == 0)
row_names.push_back(token);
else
v.push_back(atoi(token.c_str()));
m.push_back(v);
}
auto matrix = vec_of_vec_to_multi_array(m);
size_t ncol = matrix.n_cols;
if (ncol == col_names.size())
return matrix;
else if (ncol + 1 == col_names.size()) {
vector<string> new_col_names(begin(col_names) + 1, end(col_names));
col_names = new_col_names;
return matrix;
} else
throw std::runtime_error(
"Mismatch between number of columns and number of column labels.");
}
void print_matrix_head(ostream &os, const Matrix &m, const std::string &label,
size_t n) {
if (label != "")
os << label << std::endl;
size_t X = m.n_rows;
size_t Y = m.n_cols;
for (size_t x = 0; x < std::min<size_t>(X, n); ++x) {
for (size_t y = 0; y < Y; ++y)
os << (y > 0 ? "\t" : "") << m(x, y);
os << std::endl;
}
if (label != "")
os << label << " ";
os << "column sums" << std::endl;
size_t zeros = 0;
for (size_t y = 0; y < Y; ++y) {
double sum = 0;
for (size_t x = 0; x < X; ++x) {
if (m(x, y) == 0)
zeros++;
sum += m(x, y);
}
os << (y > 0 ? "\t" : "") << sum;
}
os << std::endl;
os << "There are " << zeros << " zeros";
if (label != "")
os << " in " << label;
os << ". This corresponds to " << (100.0 * zeros / X / Y) << "%."
<< std::endl;
}