-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadTSV.cpp
More file actions
139 lines (118 loc) · 4.57 KB
/
ReadTSV.cpp
File metadata and controls
139 lines (118 loc) · 4.57 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
#include "Eigen/Dense"
#include "ReadTSV.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <cmath>
ReadTSV::ReadTSV(std::string filepath, bool rn, bool cn) : hasRownames(rn), hasColnames(cn), readingString(false){
//file path is full string leading to where the CSV file is
//data class is string indicating expected data type-- double, int supported right now
//missingDatSym is what is the expected symbol for missing data in this csv
//bool rn indicates whether we are expecting rownames or not
//bool cn indicates whether we are expecting colnames or not
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
}
doubleData.clear();
std::string line;
int rowCounter = 0;
while (std::getline(file, line)) {
std::vector<double> row;
std::stringstream ss(line);
std::string cell;
int colCounter = 0;
while (std::getline(ss, cell, '\t')) {
if((rn == true && colCounter == 0) && ((cn == false) || (cn == true && rowCounter > 0)))
rownames.push_back(cell);
else if(cn == true && rowCounter == 0)
colnames.push_back(cell);
else {
try {
double cellDat = stod(cell); // Convert string to double
row.push_back(cellDat); // Store the result
} catch (const std::invalid_argument&) {
// Handle non-numeric string
row.push_back(std::numeric_limits<double>::quiet_NaN());
} catch (const std::out_of_range&) {
// Handle value out of range for double
row.push_back(std::numeric_limits<double>::quiet_NaN());
}
}
colCounter++;
}
rowCounter++;
if(row.size() > 0)
doubleData.push_back(row);
}
file.close();
}
ReadTSV::ReadTSV(std::string filepath, bool rn, bool cn, bool string) : hasRownames(rn), hasColnames(cn), readingString(true){
//file path is full string leading to where the CSV file is
//data class is string indicating expected data type-- double, int supported right now
//missingDatSym is what is the expected symbol for missing data in this csv
//bool rn indicates whether we are expecting rownames or not
//bool cn indicates whether we are expecting colnames or not
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
}
stringData.clear();
std::string line;
int rowCounter = 0;
while (std::getline(file, line)) {
std::vector<std::string> row;
std::stringstream ss(line);
std::string cell;
int colCounter = 0;
while (std::getline(ss, cell, '\t')) {
if((rn == true && colCounter == 0) && ((cn == false) || (cn == true && rowCounter > 0)))
rownames.push_back(cell);
else if(cn == true && rowCounter == 0)
colnames.push_back(cell);
else {
row.push_back(cell); // Store the result
}
colCounter++;
}
rowCounter++;
if(row.size() > 0)
stringData.push_back(row);
}
file.close();
}
Eigen::MatrixXd ReadTSV::getEigenMat(void){
Eigen::MatrixXd scratch = Eigen::MatrixXd::Zero(doubleData.size(), doubleData[0].size());
for(int i = 0; i < doubleData.size(); i++){
for(int j = 0; j < doubleData[0].size(); j++){
scratch(i, j) = doubleData[i][j];
}
}
return scratch;
}
void ReadTSV::print(void){
if(hasColnames == true){
for(int i = 0; i < colnames.size(); i++)
std::cout << colnames[i] << "\t";
std::cout << std::endl;
}
if(readingString == false){
for(int i = 0; i < doubleData.size(); i++){
if(hasRownames ==true)
std::cout << rownames[i] << "\t";
for(int j = 0; j < doubleData[0].size(); j++){
std::cout << doubleData[i][j] << "\t";
}
std::cout << std::endl;
}
}else{
for(int i = 0; i < stringData.size(); i++){
if(hasRownames ==true)
std::cout << rownames[i] << "\t";
for(int j = 0; j < stringData[0].size(); j++){
std::cout << stringData[i][j] << "\t";
}
std::cout << std::endl;
}
}
}