Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the parsers for better compatibility and memory usage #97

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion include/FormatHandling/BaseFormatHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "Alignment/Alignment.h"
#include "reportsystem.h"
#include "utils.h"

#include <iomanip>

Expand Down Expand Up @@ -90,7 +91,37 @@ class BaseFormatHandler {
\return <b> Alignment</b> loaded with the information of the file. \n
<b> nullptr</b> if there was any error.
*/
virtual Alignment *LoadAlignment(const std::string &filename) = 0;
virtual Alignment *LoadAlignment(const std::string &filename) {
Alignment* alignment;
std::ifstream file;

file.open(filename, std::ifstream::in);
if(!utils::checkFile(file))
return nullptr;

alignment = LoadAlignment(file);
if (alignment != nullptr) {
/* Alignment title may be set by the format handler, dependending on
* the alignment format, so it should only be set here if there was
* none parsed already.
*/
if (alignment->filename.empty()) {
alignment->filename.append(filename);
alignment->filename.append(";");
}
}

file.close();
return alignment;
}

/**
\brief Function to load a file in the current format and return an alignment object.
\param filename Filename of the file to load.
\return <b> Alignment</b> loaded with the information of the file. \n
<b> nullptr</b> if there was any error.
*/
virtual Alignment *LoadAlignment(std::istream &file) = 0;

/**
\brief Function to save a \link Alignment \endlink to a file.
Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/clustal_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class clustal_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/fasta_m10_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class fasta_m10_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/fasta_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class fasta_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/htmlreport_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class htmlreport_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/mega_interleaved_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class mega_interleaved_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/mega_sequential_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class mega_sequential_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/nexus_m10_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class nexus_m10_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/nexus_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class nexus_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip32_m10_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip32_m10_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip32_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip32_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip40_m10_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip40_m10_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip40_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip40_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip_paml_m10_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip_paml_m10_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/phylip_paml_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class phylip_paml_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
2 changes: 1 addition & 1 deletion include/FormatHandling/pir_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class pir_state : public BaseFormatHandler {

int CheckAlignment(std::istream *origin) override;

Alignment *LoadAlignment(const std::string &filename) override;
Alignment *LoadAlignment(std::istream& stream) override;

bool SaveAlignment(const Alignment &alignment, std::ostream *output) override;

Expand Down
15 changes: 1 addition & 14 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,6 @@ namespace utils {
*/
bool checkFile(std::ifstream &file);

/**
\brief Read a new line from current input stream.\n
This function is better than standard one
since cares of operative system compatibility.\n
It is useful as well because removes tabs and blank spaces
at lines at beginning/ending.\n
\param file ifstream to read line from.
\return \n
Line that has been read or
nullptr if there is nothing to read.\n
*/
char *readLine(std::ifstream &file);

/**
\brief Read a new line from current input stream.\n
This function is better than standard one
Expand All @@ -278,7 +265,7 @@ namespace utils {
nullptr if there is nothing to read.\n
Line that has been read.
*/
char *readLine(std::istream &file);
char *readLine(std::istream &file, std::string &buffer);

/**
\brief Remove all content surrounded by ("") or ([]).\n
Expand Down
61 changes: 12 additions & 49 deletions source/FormatHandling/clustal_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ int clustal_state::CheckAlignment(std::istream* origin)
origin->seekg(0);
origin->clear();
char *firstWord = nullptr, *line = nullptr;

std::string buffer;

/* Read first valid line in a safer way */
do {
delete[] line;
line = utils::readLine(*origin);
line = utils::readLine(*origin, buffer);
} while ((line == nullptr) && (!origin->eof()));

/* If the file end is reached without a valid line, warn about it */
if (origin->eof())
{
delete [] line;
return false;
}

Expand All @@ -60,33 +58,22 @@ int clustal_state::CheckAlignment(std::istream* origin)
/* Clustal Format */
if((!strcmp(firstWord, "CLUSTAL")) || (!strcmp(firstWord, "clustal")))
{
delete [] line;
return 1;
}

delete[] line;

return 0;
}

Alignment* clustal_state::LoadAlignment(const std::string &filename)
Alignment* clustal_state::LoadAlignment(std::istream &file)
{
Alignment* alignment = new Alignment();
int i, seqLength, pos, firstBlock;
char *str, *line = nullptr;
std::ifstream file;
file.open(filename, std::ifstream::in);

/* Store some details about input file to be used in posterior format
* conversions */
// alignment.filename.append("!Title ");
alignment->filename.append(filename);
alignment->filename.append(";");
std::string buffer;

/* The first valid line corresponding to CLUSTAL label is ignored */
do {
delete [] line;
line = utils::readLine(file);
line = utils::readLine(file, buffer);
} while ((line == nullptr) && (!file.eof()));

/* If the file end is reached without a valid line, warn about it */
Expand All @@ -95,12 +82,8 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)

/* Ignore blank lines before first sequence block starts */
while(!file.eof()) {

/* Deallocate previously used dynamic memory */
delete [] line;

/* Read lines in safe way */
line = utils::readLine(file);
line = utils::readLine(file, buffer);

if (line != nullptr)
break;
Expand Down Expand Up @@ -130,15 +113,10 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)
break;
alignment->numberOfSequences++;

/* Deallocate previously used dynamic memory */
delete [] line;

/* Read lines in safe way */
line = utils::readLine(file);
line = utils::readLine(file, buffer);
}

delete [] line;

/* Finish to preprocess the input file. */
file.clear();
file.seekg(0);
Expand All @@ -148,19 +126,16 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)
alignment->sequences = new std::string[alignment->numberOfSequences];

/* Read the title line and store it */
line = utils::readLine(file);
line = utils::readLine(file, buffer);
if (line == nullptr)
return nullptr;
alignment->alignmentInfo.append(line, strlen(line));

/* Ignore blank lines before first sequence block starts */
while(!file.eof()) {

/* Deallocate previously used dynamic memory */
delete [] line;

/* Read lines in safe way */
line = utils::readLine(file);
line = utils::readLine(file, buffer);

if (line != nullptr)
break;
Expand All @@ -181,7 +156,7 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)
if (i == 0)
firstBlock = false;
/* Read current line and analyze it*/
line = utils::readLine(file);
line = utils::readLine(file, buffer);
continue;
}

Expand All @@ -196,11 +171,8 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)
if (pos == seqLength) {
firstBlock = false;

/* Deallocate dinamic memory if it has been used before */
delete [] line;

/* Read current line and analyze it*/
line = utils::readLine(file);
line = utils::readLine(file, buffer);

continue;
}
Expand All @@ -220,19 +192,10 @@ Alignment* clustal_state::LoadAlignment(const std::string &filename)
i = (i + 1) % alignment->numberOfSequences;
}

/* Deallocate dinamic memory if it has been used before */
delete [] line;

/* Read current line and analyze it*/
line = utils::readLine(file);
line = utils::readLine(file, buffer);
}

/* Close the input file */
file.close();

/* Deallocate dinamic memory */
delete [] line;

/* Check the matrix's content */
alignment->fillMatrices(true);
alignment->originalNumberOfSequences = alignment->numberOfSequences;
Expand Down
2 changes: 1 addition & 1 deletion source/FormatHandling/fasta_m10_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int fasta_m10_state::CheckAlignment(std::istream* origin)
return 0;
}

Alignment* fasta_m10_state::LoadAlignment(const std::string &filename)
Alignment* fasta_m10_state::LoadAlignment(std::istream &file)
{
return nullptr;
}
Expand Down
Loading
Loading