Skip to content

Commit

Permalink
Support fragment files with header; #609
Browse files Browse the repository at this point in the history
  • Loading branch information
timoast committed May 10, 2021
1 parent e5ab0c9 commit 3d1bf00
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ int filterCells(
}

if (!eof_check) {
Rcpp::Rcerr << "Incomplete: File has only headers";
Rcpp::Rcerr << "Error: fragment file contains header only\n" << std::flush;
gzclose(ifileHandler);
return 1;
}

// looping over the fragments file
Expand Down
25 changes: 22 additions & 3 deletions src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ SEXP groupCommand(
}

// char * to string extraction
std::string cb_seq;
std::string cb_seq, line_seq;
cb_seq.reserve(32);
line_seq.reserve(buffer_length);

// metadata from fragments file
size_t start, end, reads;
Expand All @@ -64,8 +65,25 @@ SEXP groupCommand(
read_count.assign(num_whitelist_cells, 0);
}

// skip header if present
bool eof_check;
while ((eof_check = gzgets(fileHandler, buffer, buffer_length)) !=0) {
line_seq.clear();
line_seq.append(buffer);

if (line_seq.at(0) != '#') {
break;
}
}

if (!eof_check) {
Rcpp::Rcerr << "Error: fragment file contains header only" << std::flush;
gzclose(fileHandler);
return (Rcpp::DataFrame::create());
}

// looping over the fragments file
while(gzgets(fileHandler, buffer, buffer_length) !=0 ){
do {
cb_char = strtok ( buffer, "\t" );

for (auto i=1; i<=4; i++) {
Expand Down Expand Up @@ -136,7 +154,8 @@ SEXP groupCommand(
if (line_counter % 2000000 == 0) {
Rcpp::checkUserInterrupt();
}
}
line_seq.clear();
} while(gzgets(fileHandler, buffer, buffer_length) !=0 );

//Cleanup
gzclose(fileHandler);
Expand Down
23 changes: 20 additions & 3 deletions src/split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int splitFragments(
} else {
out->open( fileName.c_str() );
}

streams.push_back(out);
}

Expand Down Expand Up @@ -80,8 +80,25 @@ int splitFragments(
cb_seq.reserve(32);
line_seq.reserve(buffer_length);

// skip header if present
bool eof_check;
while ((eof_check = gzgets(ifileHandler, buffer, buffer_length)) !=0) {
line_seq.clear();
line_seq.append(buffer);

if (line_seq.at(0) != '#') {
break;
}
}

if (!eof_check) {
Rcpp::Rcerr << "Error: fragment file contains header only\n" << std::flush;
gzclose(ifileHandler);
return 1;
}

// looping over the fragments file
while(gzgets(ifileHandler, buffer, buffer_length) !=0 ){
do {
line_seq.clear();
line_seq.append(buffer);

Expand Down Expand Up @@ -124,7 +141,7 @@ int splitFragments(
if (is_ten_mil) {
Rcpp::checkUserInterrupt();
}
}
} while(gzgets(ifileHandler, buffer, buffer_length) !=0 );

// Cleanup
gzclose(ifileHandler);
Expand Down
24 changes: 21 additions & 3 deletions src/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,29 @@ bool validateCells(
}

// char * to string extraction
std::string cb_seq;
std::string cb_seq, line_seq;
cb_seq.reserve(32);
line_seq.reserve(buffer_length);

// skip header if present
bool eof_check;
while ((eof_check = gzgets(fileHandler, buffer, buffer_length)) !=0) {
line_seq.clear();
line_seq.append(buffer);

if (line_seq.at(0) != '#') {
break;
}
}

if (!eof_check) {
Rcpp::Rcerr << "Error: fragment file contains header only\n" << std::flush;
gzclose(fileHandler);
return (false);
}

// looping over the fragments file
while(gzgets(fileHandler, buffer, buffer_length) !=0 ){
do {
cb_char = strtok ( buffer, "\t" );

for (auto i=1; i<=3; i++) {
Expand Down Expand Up @@ -83,7 +101,7 @@ bool validateCells(
if (line_counter % 2000000 == 0) {
Rcpp::checkUserInterrupt();
}
}
} while(gzgets(fileHandler, buffer, buffer_length) !=0 );

//Cleanup
gzclose(fileHandler);
Expand Down

0 comments on commit 3d1bf00

Please sign in to comment.