Skip to content

Commit

Permalink
Use RAII in sequence_writer_t
Browse files Browse the repository at this point in the history
  • Loading branch information
amatria committed Aug 29, 2024
1 parent ff395d7 commit fd9e40c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
11 changes: 8 additions & 3 deletions lib/PATO/sequence_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@

#include "sequence_loader.h"

bool pato::sequence_loader_t::init(const seqan::CharString &file_name) {
return seqan::open(fasta_file, seqan::toCString(file_name));
std::optional<pato::sequence_loader_t>
pato::sequence_loader_t::create(const seqan::CharString &file_name) {
seqan::SeqFileIn *fasta_file{new seqan::SeqFileIn{}};
if (seqan::open(*fasta_file, seqan::toCString(file_name))) {
return pato::sequence_loader_t{fasta_file};
}
return std::nullopt;
}

static void crop_sequence_name(seqan::CharString &name) {
Expand All @@ -36,7 +41,7 @@ static void crop_sequence_name(seqan::CharString &name) {
bool pato::sequence_loader_t::load_sequences(pato::triplex_vector_t &sequences,
pato::name_vector_t &names,
unsigned num_sequences) {
seqan::readRecords(names, sequences, fasta_file, num_sequences);
seqan::readRecords(names, sequences, *fasta_file, num_sequences);
for (seqan::CharString &name : names) {
crop_sequence_name(name);
}
Expand Down
11 changes: 8 additions & 3 deletions lib/PATO/sequence_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@
#include <PATO/options.h>
#include <seqan/seq_io.h>

#include <optional>

#include "types.h"

namespace pato {

class sequence_loader_t {
public:
// FIXME: Use RAII!
bool init(const seqan::CharString &file_name);
static std::optional<sequence_loader_t>
create(const seqan::CharString &file_name);

bool load_sequences(triplex_vector_t &sequences, name_vector_t &names,
unsigned num_sequences);

private:
seqan::SeqFileIn fasta_file;
sequence_loader_t(seqan::SeqFileIn *fasta_file_) : fasta_file{fasta_file_} {}

std::shared_ptr<seqan::SeqFileIn> fasta_file;
};

} // namespace pato
Expand Down
8 changes: 4 additions & 4 deletions lib/PATO/tfo_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ void pato::find_tfo_motifs(pato::motif_vector_t &motifs,

pato::find_tfo_motifs_result
pato::find_tfo_motifs(const pato::options_t &opts) {
pato::sequence_loader_t sequence_loader;
if (!sequence_loader.init(opts.tfo_file)) {
auto sequence_loader = pato::sequence_loader_t::create(opts.tfo_file);
if (!sequence_loader) {
return pato::find_tfo_motifs_result::cannot_open_tfo_file;
}

Expand All @@ -233,8 +233,8 @@ pato::find_tfo_motifs(const pato::options_t &opts) {

pato::name_vector_t tfo_names;
pato::triplex_vector_t tfo_sequences;
sequence_loader.load_sequences(tfo_sequences, tfo_names,
std::numeric_limits<unsigned>::max());
sequence_loader->load_sequences(tfo_sequences, tfo_names,
std::numeric_limits<unsigned>::max());
pato::motif_vector_t tfo_motifs;
pato::motif_potential_vector_t tfo_potentials;
pato::find_tfo_motifs(tfo_motifs, tfo_potentials, tfo_sequences, opts);
Expand Down
16 changes: 8 additions & 8 deletions lib/PATO/tpx_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ static void match_tfo_tts_motifs(
}

pato::find_tpx_result pato::find_tpxes(const pato::options_t &opts) {
pato::sequence_loader_t tfo_sequence_loader;
if (!tfo_sequence_loader.init(opts.tfo_file)) {
auto tfo_sequence_loader = pato::sequence_loader_t::create(opts.tfo_file);
if (!tfo_sequence_loader) {
return pato::find_tpx_result::cannot_open_tfo_file;
}
pato::sequence_loader_t tts_sequence_loader;
if (!tts_sequence_loader.init(opts.tts_file)) {
auto tts_sequence_loader = pato::sequence_loader_t::create(opts.tts_file);
if (!tts_sequence_loader) {
return pato::find_tpx_result::cannot_open_tts_file;
}

Expand All @@ -285,8 +285,8 @@ pato::find_tpx_result pato::find_tpxes(const pato::options_t &opts) {

pato::name_vector_t tfo_names;
pato::triplex_vector_t tfo_sequences;
tfo_sequence_loader.load_sequences(tfo_sequences, tfo_names,
std::numeric_limits<unsigned>::max());
tfo_sequence_loader->load_sequences(tfo_sequences, tfo_names,
std::numeric_limits<unsigned>::max());
pato::motif_vector_t tfo_motifs;
pato::motif_potential_vector_t tfo_potentials;
pato::find_tfo_motifs(tfo_motifs, tfo_potentials, tfo_sequences, opts);
Expand All @@ -304,8 +304,8 @@ pato::find_tpx_result pato::find_tpxes(const pato::options_t &opts) {
pato::potential_map_t potentials;

while (true) {
if (!tts_sequence_loader.load_sequences(tts_sequences, tts_names,
opts.chunk_size)) {
if (!tts_sequence_loader->load_sequences(tts_sequences, tts_names,
opts.chunk_size)) {
break;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/PATO/tts_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ void pato::find_tts_motifs(pato::motif_vector_t &motifs,

pato::find_tts_motifs_result
pato::find_tts_motifs(const pato::options_t &opts) {
pato::sequence_loader_t sequence_loader;
if (!sequence_loader.init(opts.tts_file)) {
auto sequence_loader = pato::sequence_loader_t::create(opts.tts_file);
if (!sequence_loader) {
return pato::find_tts_motifs_result::cannot_open_tts_file;
}

Expand All @@ -194,8 +194,8 @@ pato::find_tts_motifs(const pato::options_t &opts) {
pato::motif_potential_vector_t tts_potentials;

while (true) {
if (!sequence_loader.load_sequences(tts_sequences, tts_names,
opts.chunk_size)) {
if (!sequence_loader->load_sequences(tts_sequences, tts_names,
opts.chunk_size)) {
break;
}
pato::find_tts_motifs(tts_motifs, tts_potentials, tts_sequences, opts);
Expand Down

0 comments on commit fd9e40c

Please sign in to comment.