-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparse.cpp
162 lines (140 loc) · 3.61 KB
/
sparse.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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace std;
using namespace boost::numeric::ublas;
extern "C" {
void sparse (int *Kmax) {
int i, j, place = 0, num, k1, k2;
double row;
int K = Kmax[0];
//int K = 91;
//K = 91;
mapped_matrix<double> m (K, K, K * K);
ifstream inFile;
string line, tid;
int zero;
inFile.open("conditionA_1.prob", ifstream::in);
cout<< " Reading conditionA_1.prob file: " << endl;
if (!inFile) {
cerr << "Unable to open file .alignment file\n";
exit(1); // call system to stop
}
i = -1;
while (i<0){
getline( inFile, line );
//cout <<line << endl;
if (line[0] != '#'){
i = 0;
inFile.seekg(place); // this sets the ifstream to the previous line
}
place = inFile.tellg(); // this reads the current position of the ifstream
}
inFile >> tid >> num;
num = num - 1;
std::vector<int> c_row(num);
for ( j = 0; j < num; ++j){
inFile >> c_row[j] >> row;
}
for (k1 = 0; k1 < num; k1++){
for (k2 = 0; k2 < num; k2++){
m(c_row[k1],c_row[k2]) = 1;
}
}
inFile >> zero >> row;
inFile.ignore(10000000,'\n');
i = i+1;
//inFile.good()
while( !inFile.eof() ){
inFile >> tid >> num;
num = num - 1;
std::vector<int> c_row(num);
//cout << "row " << i << ", Nmaps = " << num <<". ";
for ( j = 0; j < num; ++j){
inFile >> c_row[j] >> row;
// cout << c_row[j] << " ";
}
for (k1 = 0; k1 < num; k1++){
for (k2 = 0; k2 < num; k2++){
m(c_row[k1],c_row[k2]) = 1;
}
}
inFile >> zero >> row;
inFile.ignore(10000000,'\n');
i = i+1;
if(i%1000000 == 0){cout << "read " << i << "lines"<< endl;}
}
inFile.close();
cout << "done: read " << i << "lines"<< endl;
inFile.open("conditionB_1.prob", ifstream::in);
cout<< " Reading conditionB_1.prob file: " << endl;
if (!inFile) {
cerr << "Unable to open file .alignment file\n";
exit(1); // call system to stop
}
i = -1;
while (i<0){
getline( inFile, line );
//cout <<line << endl;
if (line[0] != '#'){
i = 0;
inFile.seekg(place); // this sets the ifstream to the previous line
}
place = inFile.tellg(); // this reads the current position of the ifstream
}
inFile >> tid >> num;
num = num - 1;
std::vector<int> c_row2(num);
for ( j = 0; j < num; ++j){
inFile >> c_row2[j] >> row;
}
for (k1 = 0; k1 < num; k1++){
for (k2 = 0; k2 < num; k2++){
m(c_row2[k1],c_row2[k2]) = 1;
}
}
inFile >> zero >> row;
inFile.ignore(10000000,'\n');
i = i+1;
//inFile.good()
while( !inFile.eof() ){
inFile >> tid >> num;
num = num - 1;
std::vector<int> c_row2(num);
//cout << "row " << i << ", Nmaps = " << num <<". ";
for ( j = 0; j < num; ++j){
inFile >> c_row2[j] >> row;
// cout << c_row[j] << " ";
}
for (k1 = 0; k1 < num; k1++){
for (k2 = 0; k2 < num; k2++){
m(c_row2[k1],c_row2[k2]) = 1;
}
}
inFile >> zero >> row;
inFile.ignore(10000000,'\n');
i = i+1;
if(i%1000000 == 0){cout << "read " << i << "lines"<< endl;}
}
inFile.close();
cout << "done: read " << i << "lines"<< endl;
ofstream outFile ( "triplet_sparse_matrix.txt" );
cout << "writing sparse matrix rows and columns: " << endl;
for (i = 1; i < K; i++){
for (j = 1; j < K; j++){
if(m(i,j)>0){
outFile << i << " " << j << endl;
}
}
if(i%1000 == 0){cout << "Done " << i << "transcripts"<< endl;}
}
cout << "done." << endl;
}
}