Skip to content

Commit

Permalink
removed uses of assert from c++ code
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverVoogd committed Jul 10, 2023
1 parent 7bb5009 commit ae50f71
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 99 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(sc_annotate_plots)
export(sc_long_multisample_pipeline)
export(sc_long_pipeline)
export(sc_mutations)
export(testfuncR)
import(zlibbioc)
importFrom(BiocGenerics,cbind)
importFrom(BiocGenerics,colnames)
Expand Down
146 changes: 73 additions & 73 deletions src/main-functions/group_bam2isoform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,96 +36,96 @@ struct DataStruct2 {
static std::unordered_map<std::string, std::string>
get_fa_dict(std::string filename)
{
// // Modify in place
// auto
// string_toupper = [] (std::string &input)
// { std::transform(input.begin(), input.end(), input.begin(), [](unsigned char c) { return std::toupper(c); }); };

// std::string ch = "";
// std::stringstream sequence;
// std::unordered_map<std::string, std::string> output;

// std::ifstream infile(filename);

// std::string line;
// while (std::getline(infile, line)) {
// if (line[0] == '>') {
// if (ch != "") {
// output[ch] = sequence.str();
// }
// ch = strip(std::string(line.begin()+1, line.end()));
// string_toupper(ch);
// sequence.str(""); // reset the stringstream to empty
// } else {
// string_toupper(line);
// sequence << strip(line);
// }
// }
// infile.close();
// Modify in place
auto
string_toupper = [] (std::string &input)
{ std::transform(input.begin(), input.end(), input.begin(), [](unsigned char c) { return std::toupper(c); }); };

std::string ch = "";
std::stringstream sequence;
std::unordered_map<std::string, std::string> output;

std::ifstream infile(filename);

std::string line;
while (std::getline(infile, line)) {
if (line[0] == '>') {
if (ch != "") {
output[ch] = sequence.str();
}
ch = strip(std::string(line.begin()+1, line.end()));
string_toupper(ch);
sequence.str(""); // reset the stringstream to empty
} else {
string_toupper(line);
sequence << strip(line);
}
}
infile.close();

// return output;
return output;
}

static std::vector<StartEndPair>
get_blocks(const BAMRecord &record) {
// std::vector<StartEndPair> blocks;
// int pos = record.reference_start;

// for (const auto & [op, len] : record.cigar) {
// if ((op == BAM_CMATCH) ||
// (op == BAM_CEQUAL) ||
// (op == BAM_CDIFF)) {
// // add it to the blocks and move on
// blocks.push_back({pos, pos + len});
// pos += len;
// } else if (
// (op == BAM_CDEL) ||
// (op == BAM_CREF_SKIP)) {
// // just skip over this position
// pos += len;
// }
// }
std::vector<StartEndPair> blocks;
int pos = record.reference_start;

for (const auto & [op, len] : record.cigar) {
if ((op == BAM_CMATCH) ||
(op == BAM_CEQUAL) ||
(op == BAM_CDIFF)) {
// add it to the blocks and move on
blocks.push_back({pos, pos + len});
pos += len;
} else if (
(op == BAM_CDEL) ||
(op == BAM_CREF_SKIP)) {
// just skip over this position
pos += len;
}
}

// return blocks;
return blocks;
}

void
merge_files_and_delete(std::ofstream &outfile, const std::vector<std::string> &infiles) {
// for (const auto &file : infiles) {
// std::ifstream infile(file);
for (const auto &file : infiles) {
std::ifstream infile(file);

// if (!infile.is_open()) continue;
if (!infile.is_open()) continue;

// std::string line;
// while (std::getline(infile, line)) {
// outfile << line;
// }
// infile.close();
std::string line;
while (std::getline(infile, line)) {
outfile << line;
}
infile.close();

// std::remove(file.c_str());
// }
std::remove(file.c_str());
}
}

static int
bam2isoform_fetch_function(const bam1_t *b, void *data)
{
// // retrieve the data from htslib and void * object
// DataStruct2 *data_struct = (DataStruct2 *)data;
// BAMRecord rec = read_record(b, data_struct->header);
// retrieve the data from htslib and void * object
DataStruct2 *data_struct = (DataStruct2 *)data;
BAMRecord rec = read_record(b, data_struct->header);

// // smooth and update the CIGAR
// std::vector<CigarPair> cigar = smooth_cigar(rec.cigar, 20);
// rec.cigar = cigar;
// rec.cigar_string = generate_cigar(cigar);
// smooth and update the CIGAR
std::vector<CigarPair> cigar = smooth_cigar(rec.cigar, 20);
rec.cigar = cigar;
rec.cigar_string = generate_cigar(cigar);

// std::vector<StartEndPair> tmp_blocks = get_blocks(rec);
// Junctions junctions = blocks_to_junctions(tmp_blocks);
std::vector<StartEndPair> tmp_blocks = get_blocks(rec);
Junctions junctions = blocks_to_junctions(tmp_blocks);

// std::vector<std::string> tmp_blocks_str = ranges::map<StartEndPair, std::string>(tmp_blocks, [](StartEndPair sep) -> std::string { return sep.getString(); });
std::vector<std::string> tmp_blocks_str = ranges::map<StartEndPair, std::string>(tmp_blocks, [](StartEndPair sep) -> std::string { return sep.getString(); });

// data_struct->isoform->add_isoform(junctions, rec.flag.read_reverse_strand);
// data_struct->i = data_struct->i + 1;
// return 0;
data_struct->isoform->add_isoform(junctions, rec.flag.read_reverse_strand);
data_struct->i = data_struct->i + 1;
return 0;
}

void group_bam2isoform(
Expand All @@ -141,11 +141,11 @@ void group_bam2isoform(
const std::string &raw_gff3)
{
// random seed stuff here
if (!file_exists(bam_in + ".bai")) {
// Rcpp::stop("Can not find corresponding .bai file %s. Cancelling group_bam2isoform.\n", bam_in);
Rcpp::Rcout << "Can not find corresponding .bai file " << bam_in << ". Cancelling group_bam2isoform.\n";
return;
}
// if (!file_exists(bam_in + ".bai")) {
// // Rcpp::stop("Can not find corresponding .bai file %s. Cancelling group_bam2isoform.\n", bam_in);
// Rcpp::Rcout << "Can not find corresponding .bai file " << bam_in << ". Cancelling group_bam2isoform.\n";
// return;
// }

// import all the values of fa_f
const std::unordered_map<std::string, std::string> fa_dict = get_fa_dict(fa_file);
Expand Down
3 changes: 1 addition & 2 deletions src/main-functions/match_cell_barcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <unordered_map>
#include <algorithm>
#include <string>
#include <cassert>
#include <fstream>

#include <Rcpp.h>
Expand Down Expand Up @@ -97,7 +96,7 @@ char complement(char n)
case 'N':
return 'N';
}
assert(false);
Rcpp::stop("No matching base complement");
return ' ';
}

Expand Down
1 change: 0 additions & 1 deletion src/main-functions/match_cell_barcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <utility>
#include <unordered_map>
#include <string>
#include <cassert>

#include <Rcpp.h>

Expand Down
13 changes: 8 additions & 5 deletions src/utility/json/json.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <Rcpp.h>

/// Json-cpp amalgamated header (http://jsoncpp.sourceforge.net/).
/// It is intended to be used with #include "json/json.h"

Expand Down Expand Up @@ -2296,7 +2298,7 @@ JSON_API OStream& operator<<(OStream&, const Value& root);
#define JSON_ASSERT(condition) \
do { \
if (!(condition)) { \
Json::throwLogicError("assert json failed"); \
Rcpp::stop("failed"); \
} \
} while (0)

Expand All @@ -2305,21 +2307,22 @@ JSON_API OStream& operator<<(OStream&, const Value& root);
OStringStream oss; \
oss << message; \
Json::throwLogicError(oss.str()); \
abort(); \
Rcpp::stop(oss.str()); \
} while (0)

#else // JSON_USE_EXCEPTION

#define JSON_ASSERT(condition) assert(condition)
// #define JSON_ASSERT(condition) assert(condition)

// The call to assert() will show the failure message in debug builds. In
// release builds we abort, for a core-dump or debugger.
#define JSON_FAIL_MESSAGE(message) \
{ \
OStringStream oss; \
oss << message; \
assert(false && oss.str().c_str()); \
abort(); \
Rcpp::stop(oss.str().c_str());
//assert(false && oss.str().c_str());
//abort();
}

#endif
Expand Down
Loading

0 comments on commit ae50f71

Please sign in to comment.