Skip to content

Commit

Permalink
update for recent compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
riasc committed Jan 24, 2025
1 parent de65fc9 commit beceb35
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 34 deletions.
16 changes: 14 additions & 2 deletions cli/include/BEDReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@
#include "genogrove/Interval.hpp"
#include "FileEntry.hpp"

// zlib
#include <zlib.h>
#include <sstream>

#include <htslib/hts.h>
#include <htslib/bgzf.h>
#include <htslib/kseq.h>
#include <htslib/kstring.h>

class BEDReader : public FileReader {
public:
BEDReader(const std::filesystem::path&, bool gzipped);
bool readNext(FileEntry& entry) override;
bool hasNext() override;
std::string getErrorMessage() override;
size_t getCurrentLine() override;
~BEDReader();

private:
std::unique_ptr<std::ifstream> inputStream;
std::string errorMessage;
BGZF* bgzfFile;
size_t lineNum;
std::string errorMessage;
// std::unique_ptr<std::ifstream> inputStream;
// size_t lineNum;
};

#endif //GENOGROVE_BEDREADER_HPP
56 changes: 31 additions & 25 deletions cli/src/BEDReader.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
#include "BEDReader.hpp"


BEDReader::BEDReader(const std::filesystem::path& filepath, bool gzipped) {
lineNum = 1;
if(gzipped) {
// open file gzipped
} else {
inputStream = std::make_unique<std::ifstream>(filepath);
if(!inputStream->is_open()) {
throw std::runtime_error("Failed to open file: " + filepath.string());
}
bgzfFile = bgzf_open(filepath.c_str(), "r"); // open file
if(!bgzfFile) {
throw std::runtime_error("Failed to open file: " + filepath.string());
}
}

BEDReader::~BEDReader() {
if(bgzfFile) {
bgzf_close(bgzfFile);
}
}

bool BEDReader::readNext(FileEntry& entry) {
std::string line;
if(!std::getline(*inputStream, line)) {
errorMessage = "Failed to read line at line " + std::to_string(lineNum);
kstring_t str = {0, 0, nullptr};
int ret = bgzf_getline(bgzfFile, '\n', &str);
if(ret < 0) {
if(str.s) free(str.s);
return false;
}
std::string line(str.s);
free(str.s); // free the memory allocated by bgzf_getline

// skip empty or commented lines
if (line.empty() || line[0] == '#') {
lineNum++;
return readNext(entry);
}

// parse the line
std::stringstream ss(line);
std::string chrom, start, end;

try {
if(!(ss >> chrom >> start >> end)) {
if (!(ss >> chrom >> start >> end)) {
errorMessage = "Invalid line format at line " + std::to_string(lineNum);
return false;
}

// validate integers
if(!std::all_of(start.begin(), start.end(), ::isdigit) ||
if (!std::all_of(start.begin(), start.end(), ::isdigit) ||
!std::all_of(end.begin(), end.end(), ::isdigit)) {
errorMessage = "Invalid coordinate format at line " + std::to_string(lineNum);
return false;
Expand All @@ -38,27 +48,23 @@ bool BEDReader::readNext(FileEntry& entry) {
// validate and create interval object
size_t startNum = std::stoul(start);
size_t endNum = std::stoul(end);
if(startNum >= endNum) {
errorMessage = "Start coordinate must be less than end coordinate at line " + std::to_string(lineNum);
if (startNum >= endNum) {
errorMessage = "Start coordinate is greater than end coordinate at line " + std::to_string(lineNum);
return false;
}

// modify the passen entry
entry.chrom = chrom;
entry.interval = genogrove::Interval{startNum, endNum};
entry.interval = genogrove::Interval(startNum, endNum);
entry.strand = '.';

lineNum++;
return true;

} catch(std::exception& e) {
errorMessage = "Failed to parse line at line " + std::to_string(lineNum) + ": " + line;
} catch (std::exception &e) {
errorMessage = "Failed to parse line at " + std::to_string(lineNum) + ": " + line;
return false;
}
}

bool BEDReader::hasNext() {
return inputStream && !inputStream->eof();
return bgzfFile && !bgzf_check_EOF(bgzfFile);
}

std::string BEDReader::getErrorMessage() {
Expand Down
7 changes: 7 additions & 0 deletions cpm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(CPM_DOWNLOAD_VERSION 0.40.2)
set(CPM_HASH SUM "c8cdc32c03816538ce22781ed72964dc864b2a34a310d3b7104812a5ca2d835d")

# download CPM.cmake
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.2/CPM.cmake
${CMAKE_CURRENT_BINARY_DIR}/cpm/
12 changes: 7 additions & 5 deletions include/genogrove/Chroms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
#include <unordered_map>
#include <stdexcept>

// Class
#include "Constants.hpp"

namespace genogrove {
static constexpr size_t BITS = 6; // 2^6 = 64 chromosomes (should be enough for now...)

class Chroms {
public:
Chroms();

//
std::bitset<BITS> registerChrom(const std::string& chrom);
std::bitset<BITS> getNextBitset() const;
std::bitset<BITS> getBitset(std::string chrom) const;
std::bitset<constants::BITS> registerChrom(const std::string& chrom);
std::bitset<constants::BITS> getNextBitset() const;
std::bitset<constants::BITS> getBitset(std::string chrom) const;

private:
std::unordered_map<std::string, std::bitset<BITS>> chromosomes;
std::unordered_map<std::string, std::bitset<constants::BITS>> chromosomes;
size_t nextValue;
};
}
Expand Down
15 changes: 13 additions & 2 deletions src/IBPTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace genogrove {

if(node->getIsLeaf()) {
try {
std::cout << "Inserting key into leaf node with "
<< node->getKeys().size() << " keys (max: " << this->order << ")" << std::endl;
// std::cout << "Inserting key into leaf node with "
// << node->getKeys().size() << " keys (max: " << this->order << ")" << std::endl;
node->insertKey(key);
} catch (const std::exception& e) {
std::cerr << "Failed to insert key into leaf node: " << e.what() << std::endl;
Expand Down Expand Up @@ -310,7 +310,18 @@ namespace genogrove {
}

void IBPTree::exportTreeSIF(std::string filename) {
std::ofstream ofs(filename);

// for(auto& [chrom, root] : this->rootnodes) {
// // traverse through the trees
// std::cout << "chrom: " << chrom << std::endl;
// ofs <<
//
//
//
//
//
// }
}


Expand Down

0 comments on commit beceb35

Please sign in to comment.