Skip to content

Commit

Permalink
fixed issue with empty when inserting from BED file
Browse files Browse the repository at this point in the history
  • Loading branch information
riasc committed Nov 19, 2024
1 parent 91c86b2 commit dc4ca11
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
4 changes: 3 additions & 1 deletion cli/include/BEDReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <filesystem>
#include <istream>
#include <fstream>
#include <algorithm>

// Class
Expand All @@ -18,9 +19,10 @@ class BEDReader : public FileReader {
bool readNext(FileEntry& entry) override;
bool hasNext() override;
std::string getErrorMessage() override;
size_t getCurrentLine() override;

private:
std::unique_ptr<std::istream> inputStream;
std::unique_ptr<std::ifstream> inputStream;
std::string errorMessage;
size_t lineNum;
};
Expand Down
1 change: 1 addition & 0 deletions cli/include/FileReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class FileReader {
virtual bool readNext(FileEntry& entry) = 0;
virtual bool hasNext() = 0;
virtual std::string getErrorMessage() = 0;
virtual size_t getCurrentLine() = 0;
virtual ~FileReader() = default;
};

Expand Down
6 changes: 5 additions & 1 deletion cli/include/Index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
// Class
#include "genogrove/all.hpp"
#include "Subcall.hpp"
#include "FileValidatorFactory.hpp"
#include "FileReader.hpp"
#include "BEDReader.hpp"
#include "FileEntry.hpp"
#include "FileTypeDetector.hpp"
#include "FileReaderFactory.hpp"

// CXXopts
#include "cxxopts.hpp"
Expand Down
16 changes: 14 additions & 2 deletions cli/src/BEDReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ BEDReader::BEDReader(const std::filesystem::path& filepath, bool gzipped) {
if(gzipped) {
// open file gzipped
} else {
// inputStream = std::make_unique<std::ifstream>(filepath);
inputStream = std::make_unique<std::ifstream>(filepath);
if(!inputStream->is_open()) {
throw std::runtime_error("Failed to open file: " + filepath.string());
}
}
}

Expand Down Expand Up @@ -39,7 +42,12 @@ bool BEDReader::readNext(FileEntry& entry) {
errorMessage = "Start coordinate must be less than end coordinate at line " + std::to_string(lineNum);
return false;
}
FileEntry entry(chrom, genogrove::Interval{startNum, endNum}, '\0');

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

lineNum++;
return true;

Expand All @@ -57,3 +65,7 @@ std::string BEDReader::getErrorMessage() {
return errorMessage;
}

size_t BEDReader::getCurrentLine() {
return lineNum;
}

62 changes: 25 additions & 37 deletions cli/src/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,34 @@ cxxopts::Options Index::parseArgs(int argc, char** argv) {

void Index::execute(const cxxopts::ParseResult& args) {
std::cout << "Indexing file: " << args["inputfile"].as<std::string>() << std::endl;
genogrove::IBPTree tree(args["order"].as<int>());
genogrove::BEDfileValidator validator;

std::string inputfile = args["inputfile"].as<std::string>();
// detect the file type
auto [filetype, gzipped] = genogrove::FileTypeDetector().detectFileType(inputfile);
// validate the file
std::string errorMessage;
if(!genogrove::FileValidatorFactory::validate(inputfile, filetype, gzipped, errorMessage)) {
std::cerr << "Error validating file: " << errorMessage << "\n";
return;
std::filesystem::path inputfile = std::filesystem::path(args["inputfile"].as<std::string>());
auto [filetype, gzipped] = FileTypeDetector().detectFileType(inputfile); // detect the file type
// create the reader (according to the file type) - FileReaderFactory
std::unique_ptr<FileReader> reader = FileReaderFactory::create(inputfile, filetype, gzipped);

genogrove::IBPTree tree(args["order"].as<int>()); // create the tree (with the specified order)
FileEntry entry;
while(reader->hasNext()) {
// std::cout << "Reading entry: " << reader->getCurrentLine() << std::endl;
if(!reader->readNext(entry)) {
std::cerr << reader->getErrorMessage() << std::endl;
return;
}
std::cout << "chrom: " << entry.chrom << std::endl;
tree.insertData(entry.chrom, entry.interval, entry.strand);
}

std::cout << "Successfully validated file\n";






// if(!validator.validate(inputfile)) {
// std::cerr << validator.getErrorMessage() << std::endl;
// return;
// }
//
//







if(args.count("inputfile")) {
std::string inputfile = args["inputfile"].as<std::string>();
std::cout << "Indexing file: " << inputfile << std::endl;
}

if(args.count("outputfile")) {
std::string outputfile = args["outputfile"].as<std::string>();
// write the tree to a file
std::filesystem::path outputfile;
if(args.count("outputfile")) { // check if an output file was specified
outputfile = std::filesystem::path(args["outputfile"].as<std::string>());
} else { // if not, write to inputfile.gg (with a .gg extension)
outputfile = inputfile.replace_extension(".gg");
std::cout << "Writing index to file: " << outputfile << std::endl;
}

// serialize the tree
tree.store(outputfile);

}
1 change: 1 addition & 0 deletions include/genogrove/IBPTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace genogrove {
*/
void insertData(std::string chrom, Interval intvl, char strand) {
Key key(intvl, strand, nullptr);
insert(chrom, key);
}


Expand Down

0 comments on commit dc4ca11

Please sign in to comment.