-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsge2.cpp
More file actions
430 lines (383 loc) · 15.1 KB
/
Copy pathsge2.cpp
File metadata and controls
430 lines (383 loc) · 15.1 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "sge2.h"
/////////////////////////////////////////////////////////////
// METHODS FOR sge2_stream_reader
/////////////////////////////////////////////////////////////
void sge2_stream_reader::open(const char *bcdf, const char *ftrf, const char *mtxf)
{
bcd_tr.open(bcdf);
ftr_tr.open(ftrf);
mtx_tr.delimiter = ' ';
mtx_tr.open(mtxf);
// read the header of the matrix
mtx_tr.read_line();
while (mtx_tr.str_field_at(0)[0] == '%')
{
mtx_tr.read_line();
}
nftrs = mtx_tr.uint64_field_at(0);
nbcds = mtx_tr.uint64_field_at(1);
nlines = mtx_tr.uint64_field_at(2);
cur_line = cur_iftr = 0;
cur_sbcd.nid = 0;
is_bcd_new = false;
nfields = 0;
}
void sge2_stream_reader::close()
{
bcd_tr.close();
ftr_tr.close();
mtx_tr.close();
for (int32_t i = 0; i < (int32_t)ftrs.size(); ++i)
delete ftrs[i];
ftrs.clear();
}
bool sge2_stream_reader::read_mtx()
{
// read a line from mtx file
if (mtx_tr.read_line() == 0)
{ // EOF reached
notice("EOF reached: cur_line = %llu, cur_iftr = %llu, cur_sbcd.nid = %llu", cur_line, cur_iftr, cur_sbcd.nid);
return false;
}
is_bcd_new = false;
// parse barcodes until the right barcode was found
while (cur_sbcd.nid < mtx_tr.uint64_field_at(1))
{
if (bcd_tr.read_line() == 0)
{
error("EOF reached while finding the barcode %d in the barcode file", mtx_tr.int_field_at(0));
}
is_bcd_new = true;
// parse the header if exists
// if ( cur_sbcd.nid == 0 && bcd_tr.str_field_at(0)[0] == '#' ) {
// icol_bcd_strid = icol_bcd_px = icol_bcd_py = -1;
// for(int32_t i = 0; i < bcd_tr.nfields; ++i) {
// if ( strcmp(bcd_tr.str_field_at(i), colname_bcd_strid.c_str()) == 0 ) icol_bcd_strid = i;
// else if ( strcmp(bcd_tr.str_field_at(i), colname_bcd_px.c_str()) == 0 ) icol_bcd_px = i;
// else if ( strcmp(bcd_tr.str_field_at(i), colname_bcd_py.c_str()) == 0 ) icol_bcd_py = i;
// }
// if ( icol_bcd_strid < 0 ) {
// error("Cannot find the columns %s, in the barcode file", colname_bcd_strid.c_str());
// }
// if ( bcd_pos_map.empty() && ( icol_bcd_px < 0 || icol_bcd_py < 0 ) ) {
// error("Cannot find the columns %s, and %s in the barcode file without pos file", colname_bcd_px.c_str(), colname_bcd_py.c_str());
// }
// }
// else
if ( cur_sbcd.nid == 0 ) { // no header, use default indices
if ( bcd_pos_map.empty() ) {
if ( icol_bcd_strid < 0 || icol_bcd_px < 0 || icol_bcd_py < 0 ) {
error("Indices for barcode, X, Y are not set properly - %d, %d, %d", icol_bcd_strid, icol_bcd_px, icol_bcd_py);
}
if ( icol_bcd_strid >= bcd_tr.nfields || icol_bcd_px >= bcd_tr.nfields || icol_bcd_py >= bcd_tr.nfields ) {
error("The number of fields %d in the barcode file is less than the indices %d, %d, and %d", bcd_tr.nfields, icol_bcd_strid, icol_bcd_px, icol_bcd_py);
}
}
else {
if ( icol_bcd_strid < 0 ) {
error("Negative icol_bcd_strid = %d", icol_bcd_strid);
}
if ( icol_bcd_strid >= bcd_tr.nfields ) {
error("The number of fields in the barcode file is less than the indices %d", icol_bcd_strid);
}
icol_bcd_px = icol_bcd_py = -1; // ignore the position columns
}
}
const char* strid = bcd_tr.str_field_at(icol_bcd_strid);
if ( bcd_pos_map.empty() ) {
double px = bcd_tr.double_field_at(icol_bcd_px);
double py = bcd_tr.double_field_at(icol_bcd_py);
cur_sbcd.update_values(strid, px, py);
}
else {
auto it = bcd_pos_map.find(strid);
if ( it == bcd_pos_map.end() ) {
error("Cannot find the barcode %s in the position file", strid);
}
cur_sbcd.update_values(strid, it->second.first, it->second.second);
}
}
if (cur_sbcd.nid > mtx_tr.uint64_field_at(1))
{
error("Cannot find the barcode %llu in the barcode file - cur_sbcd = %s:%d", mtx_tr.uint64_field_at(1), cur_sbcd.strid.c_str(), cur_sbcd.nid);
}
if (nfields == 0)
{
nfields = mtx_tr.nfields - 2;
}
else if (nfields != mtx_tr.nfields - 2)
{
error("Inconsistent number of fields in the mtx file - %d vs %d", nfields, mtx_tr.nfields - 2);
}
// read the contents of the mtx file
cur_iftr = mtx_tr.uint64_field_at(0);
if (cur_cnts.empty())
cur_cnts.resize(mtx_tr.nfields - 2);
for (int32_t i = 0; i < mtx_tr.nfields - 2; ++i)
{
cur_cnts[i] = mtx_tr.uint64_field_at(i + 2);
}
++cur_line;
return true;
}
// load the features and fill in the vectors
int32_t sge2_stream_reader::load_features()
{
char buf[65535];
while (ftr_tr.read_line())
{
// header line exists
// if ( ftrs.empty() && ftr_tr.str_field_at(0)[0] == '#' ) {
// icol_ftr_id = icol_ftr_name = -1;
// for(int32_t i = 0; i < ftr_tr.nfields; ++i) {
// if ( strcmp(ftr_tr.str_field_at(i), colname_ftr_id.c_str()) == 0 ) icol_ftr_id = i;
// else if ( strcmp(ftr_tr.str_field_at(i), colname_ftr_name.c_str()) == 0 ) icol_ftr_name = i;
// }
// if ( icol_ftr_id < 0 || icol_ftr_name < 0 ) {
// error("Cannot find the columns %s and %s in the feature file", colname_ftr_id.c_str(), colname_ftr_name.c_str());
// }
// }
// else {
if ( icol_ftr_id < 0 || icol_ftr_name < 0 ) {
error("icol_ftr_id = %d, icol_ftr_name = %d must be non-negative", icol_ftr_id, icol_ftr_name);
}
if ( icol_ftr_id >= ftr_tr.nfields || icol_ftr_name >= ftr_tr.nfields ) {
error("icol_ftr_id = %d, icol_ftr_name = %d must be less than nfields = %d", icol_ftr_id, icol_ftr_name, ftr_tr.nfields);
}
sge2_ftr_t *pftr = new sge2_ftr_t(ftr_tr.str_field_at(icol_ftr_id), ftr_tr.str_field_at(icol_ftr_name));
pftr->nid = (int32_t)ftrs.size() + 1;
ftrs.push_back(pftr);
// if (pftr->nid != (int32_t)ftrs.size())
// {
// error("Feature nid should be 1-based sequential number");
// }
//}
}
return (int32_t)ftrs.size();
}
uint64_t sge2_stream_reader::load_position_file(const char* filename, const char* colname_strid, const char* colname_px, const char* colname_py, char sep) {
tsv_reader pos_tr;
pos_tr.delimiter = (int32_t)sep;
pos_tr.open(filename);
// read the header to identify the columns
int32_t icol_strid = -1;
int32_t icol_px = -1;
int32_t icol_py = -1;
if (pos_tr.read_line()) {
for(int32_t i = 0; i < pos_tr.nfields; ++i) {
if ( strcmp(pos_tr.str_field_at(i), colname_strid) == 0 ) icol_strid = i;
else if ( strcmp(pos_tr.str_field_at(i), colname_px) == 0 ) icol_px = i;
else if ( strcmp(pos_tr.str_field_at(i), colname_py) == 0 ) icol_py = i;
}
}
else {
error("Cannot read the first line from %s", filename);
}
if ( icol_strid < 0 || icol_px < 0 || icol_py < 0 ) {
error("Cannot find the columns %s, %s, and %s in the position file %s", colname_strid, colname_px, colname_py, filename);
}
return load_position_file_helper(pos_tr, icol_strid, icol_px, icol_py, sep);
}
uint64_t sge2_stream_reader::load_position_file(const char* filename, int32_t icol_strid, int32_t icol_px, int32_t icol_py, bool skip_header, char sep) {
tsv_reader pos_tr;
pos_tr.delimiter = (int32_t)sep;
pos_tr.open(filename);
if (skip_header) {
if (!pos_tr.read_line()) {
error("Cannot read the first line from %s", filename);
}
if (pos_tr.nfields <= icol_strid || pos_tr.nfields <= icol_px || pos_tr.nfields <= icol_py) {
error("The number of fields is : %d, but indices are %d, %d, %d", pos_tr.nfields, icol_strid, icol_px, icol_py);
}
}
return load_position_file_helper(pos_tr, icol_strid, icol_px, icol_py, sep);
}
uint64_t sge2_stream_reader::load_position_file_helper(tsv_reader& pos_tr, int32_t icol_strid, int32_t icol_px, int32_t icol_py, char sep) {
// need to populate bcd_pos_map
bcd_pos_map.clear();
while (pos_tr.read_line()) {
bcd_pos_map[pos_tr.str_field_at(icol_strid)] = std::make_pair(pos_tr.double_field_at(icol_px), pos_tr.double_field_at(icol_py));
}
pos_tr.close();
return (uint64_t)bcd_pos_map.size();
}
/////////////////////////////////////////////////////////////
// METHODS FOR sge2_stream_writer
/////////////////////////////////////////////////////////////
void sge2_stream_writer::open(const char *bcdf, const char *ftrf, const char *mtxf)
{
char buf[65535];
fn_mtx.assign(mtxf);
snprintf(buf, 65535, "%s.tmp", mtxf);
wh_bcd = hts_open(bcdf, "wz");
wh_ftr = hts_open(ftrf, "wz");
wh_tmp = hts_open(buf, "w"); // write the contents of the matrix file first without the header
if (wh_bcd == NULL || wh_ftr == NULL || wh_tmp == NULL)
{
error("Cannot open the output files");
}
cur_sbcd.nid = nfields = nlines = 0;
}
// close the files
void sge2_stream_writer::close()
{
if (wh_tmp != NULL)
{
if (!flush_mtx())
{
error("Cannot flush the mtx file");
}
}
if (wh_ftr != NULL)
{
if (hts_close(wh_ftr) != 0)
{
error("Cannot close the feature file");
}
wh_ftr = NULL;
}
}
bool sge2_stream_writer::flush_cur_sbcd()
{
std::string strcnt;
cat_join_uint64(strcnt, cur_sbcd.cnts, ",");
if ( (double)cur_sbcd.px == floor((double)cur_sbcd.px) && (double)cur_sbcd.py == floor((double)cur_sbcd.py) )
hprintf(wh_bcd, "%s\t%.0f\t%.0f\t%s\n", cur_sbcd.strid.c_str(), cur_sbcd.px, cur_sbcd.py, strcnt.c_str());
else
hprintf(wh_bcd, "%s\t%.*f\t%.*f\t%s\n", cur_sbcd.strid.c_str(), bcd_precision, cur_sbcd.px, bcd_precision, cur_sbcd.py, strcnt.c_str());
return true;
}
// add a spatial barcode
bool sge2_stream_writer::add_sbcd(const char *strid, double px, double py)
{
if (cur_sbcd.nid > 0)
{
flush_cur_sbcd();
}
cur_sbcd.strid.assign(strid);
cur_sbcd.nid++;
cur_sbcd.px = px;
cur_sbcd.py = py;
if (cur_sbcd.cnts.empty())
{
cur_sbcd.cnts.resize(nfields, 0);
}
for (int32_t i = 0; i < nfields; ++i)
{
cur_sbcd.cnts[i] = 0;
}
return true;
}
bool sge2_stream_writer::write_ftr(const char *id, const char *name, uint64_t nid, std::vector<uint64_t> &cnts)
{
if (cnts.empty()) {
cnts.resize(nfields, 0);
}
std::string strcnt;
cat_join_uint64(strcnt, cnts, ",");
hprintf(wh_ftr, "%s\t%s\t%llu\t%s\n", id, name, nid, strcnt.c_str());
return true;
}
bool sge2_stream_writer::add_mtx(uint64_t iftr, std::vector<uint64_t> &cnts)
{
if (nfields == 0)
{
nfields = (int32_t)cnts.size();
cur_sbcd.cnts.resize(nfields, 0);
}
else if (nfields != (int32_t)cnts.size())
{
error("The number of fields in the mtx file is not consistent: %d vs %d", nfields, (int32_t)cnts.size());
}
std::string strcnt;
cat_join_uint64(strcnt, cnts, " ");
hprintf(wh_tmp, "%llu %llu %s\n", iftr, cur_sbcd.nid, strcnt.c_str());
// update ftr_cnts
if (iftr > ftr_cnts.size() + 1)
ftr_cnts.resize(iftr);
if (ftr_cnts[iftr - 1].empty())
ftr_cnts[iftr - 1].resize(nfields, 0);
if ((ftr_cnts[iftr - 1].size() != nfields) || (cnts.size() != nfields))
error("The number of fields in the mtx file is not consistent: %d vs %d vs %d", nfields, (int32_t)cnts.size(), (int32_t)ftr_cnts[iftr - 1].size());
for (int32_t i = 0; i < nfields; ++i)
ftr_cnts[iftr - 1][i] += cnts[i];
// update sbcd_cnts
// assert(cur_sbcd.cnts.size() == nfields);
for (int32_t i = 0; i < nfields; ++i)
cur_sbcd.cnts[i] += cnts[i];
++nlines;
return true;
}
bool sge2_stream_writer::add_mtx(uint64_t iftr, std::vector<uint64_t> &cnts, std::vector<int32_t>& icols)
{
if (nfields == 0)
{
nfields = (int32_t)icols.size();
cur_sbcd.cnts.resize(nfields, 0);
}
else if (nfields != (int32_t)icols.size())
{
error("The number of fields in the mtx file is not consistent: %d vs %d", nfields, (int32_t)icols.size());
}
hprintf(wh_tmp, "%llu %llu", iftr, cur_sbcd.nid);
for(int32_t i = 0; i < nfields; ++i) {
if ( icols[i] < 0 || icols[i] >= (int32_t)cnts.size() ) {
error("Invalid column index %d", icols[i]);
}
hprintf(wh_tmp, " %llu", cnts[icols[i]]);
}
hprintf(wh_tmp, "\n");
// std::string strcnt;
// cat_join_uint64(strcnt, cnts, " ");
// hprintf(wh_tmp, "%llu %llu %s\n", iftr, cur_sbcd.nid, strcnt.c_str());
// update ftr_cnts
if (iftr > ftr_cnts.size() + 1)
ftr_cnts.resize(iftr);
if (ftr_cnts[iftr - 1].empty())
ftr_cnts[iftr - 1].resize(nfields, 0);
if ((ftr_cnts[iftr - 1].size() != nfields) || (icols.size() != nfields))
error("The number of fields in the mtx file is not consistent: %d vs %d vs %d", nfields, (int32_t)icols.size(), (int32_t)ftr_cnts[iftr - 1].size());
for (int32_t i = 0; i < nfields; ++i)
ftr_cnts[iftr - 1][i] += cnts[icols[i]];
// update sbcd_cnts
// assert(cur_sbcd.cnts.size() == nfields);
for (int32_t i = 0; i < nfields; ++i)
cur_sbcd.cnts[i] += cnts[icols[i]];
++nlines;
return true;
}
// combine header and contents for mtx to a single compressed mtx.gz file
bool sge2_stream_writer::flush_mtx()
{
flush_cur_sbcd(); // write the last barcode
if (hts_close(wh_tmp) != 0) // close the temporary mtx file
error("Cannot close the temporary mtx file");
wh_tmp = NULL;
if (hts_close(wh_bcd) != 0) // close the barcode file
error("Cannot close the barcode file");
wh_bcd = NULL;
// write a header file
char buf[65535];
snprintf(buf, 65535, "%s.hdr", fn_mtx.c_str());
htsFile *wh_hdr = hts_open(buf, "w"); // write the contents of the matrix file first without the header
hprintf(wh_hdr, "%%%%MatrixMarket matrix coordinate integer general\n%%\n");
hprintf(wh_hdr, "%d %llu %llu\n", (int32_t)ftr_cnts.size(), cur_sbcd.nid, nlines);
hts_close(wh_hdr);
// merge the two files, requires gzip
notice("Generating the merged %s file", fn_mtx.c_str());
std::string cmd;
catprintf(cmd, "cat %s.hdr %s.tmp | gzip -c > %s", fn_mtx.c_str(), fn_mtx.c_str(), fn_mtx.c_str());
int32_t ret = system(cmd.c_str());
if ((ret == -1) || (WEXITSTATUS(ret) == 127))
{
error("Error in running %s", cmd.c_str());
}
// remove the header and tmp file
snprintf(buf, 65535, "%s.hdr", fn_mtx.c_str());
if (remove(buf) != 0)
error("Cannot remove %s", buf);
snprintf(buf, 65535, "%s.tmp", fn_mtx.c_str());
if (remove(buf) != 0)
error("Cannot remove %s", buf);
return true;
}