Commit 946aafb7 authored by Carsten Kemena's avatar Carsten Kemena

improved exception handling, addedexception tests to DASetReader

parent b5a14556
......@@ -62,7 +62,7 @@ namespace BioSeqDataLib
*/
template<class DomainType>
bool
readPfamFormat(AlgorithmPack::Input &inFile, DomainArrangementSet<DomainType> &daSet)
readPfamFormat(AlgorithmPack::Input &inFile, const std::string &fileName, DomainArrangementSet<DomainType> &daSet)
{
std::string line;
getline(inFile, line);
......@@ -110,7 +110,7 @@ readPfamFormat(AlgorithmPack::Input &inFile, DomainArrangementSet<DomainType> &d
}
catch(...)
{
throw FormatException("Failed to read domain annotation file in pfam format. Error occured in line: " + std::to_string(line_counter));
throw FormatException("Error: Failed to read domain annotation file '" + fileName + "'. Format of file: PfamScan. Error occured in line: " + std::to_string(line_counter) + "\n");
}
......
......@@ -60,7 +60,7 @@ template<class DomainType>
class DASetReader
{
private:
std::vector<std::function<bool(AlgorithmPack::Input &, DomainArrangementSet<DomainType> &)> > strategies;
std::vector<std::function<bool(AlgorithmPack::Input &, const std::string &, DomainArrangementSet<DomainType> &)> > strategies;
public:
/**
......@@ -87,29 +87,19 @@ class DASetReader
void
read(const fs::path &fileName, DomainArrangementSet<DomainType> &daSet)
{
try
AlgorithmPack::Input inF(fileName);
for (size_t i=0; i<strategies.size(); ++i)
{
AlgorithmPack::Input inF(fileName);
for (size_t i=0; i<strategies.size(); ++i)
inF.seekg(0);
if (strategies[i](inF, fileName.string(), daSet))
{
inF.seekg(0);
if (strategies[i](inF, daSet))
{
inF.close();
return;
}
inF.close();
return;
}
inF.close();
throw FormatException("Unknown Format in " + fileName.string());
}
catch (const FormatException&)
{
throw;
}
catch (const std::exception&)
{
throw FormatException("Failed to read file: " + fileName.string());
}
inF.close();
throw FormatException("Error: Format of file '" + fileName.string() + "' could not be identified or is not supported.\n");
}
/**
......@@ -119,7 +109,7 @@ class DASetReader
* @param strategy The function containing the strategy.
*/
void
addStrategy(std::function<bool(AlgorithmPack::Input &, DomainArrangementSet<DomainType> &)> strategy)
addStrategy(std::function<bool(AlgorithmPack::Input &, const std::string &, DomainArrangementSet<DomainType> &)> strategy)
{
strategies.emplace_back(strategy);
}
......
......@@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE( pfamScanFormatTest )
{
BSDL::DomainArrangementSet<BSDL::PfamDomain> daSet;
AlgorithmPack::Input in("../tests/domain/data_new/BB20012.pfamScan");
auto worked = BSDL::readPfamFormat(in, daSet);
auto worked = BSDL::readPfamFormat(in, "x", daSet);
in.close();
BOOST_CHECK_EQUAL(worked, true);
BOOST_CHECK_EQUAL(daSet.size(), 27);
......@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE( pfamScanFormatTest )
// check read with Domain
BioSeqDataLib::DomainArrangementSet<BioSeqDataLib::Domain> daSet2;
in.open("../tests/domain/data_new/BB20012.pfamScan");
worked = BSDL::readPfamFormat(in, daSet2);
worked = BSDL::readPfamFormat(in, "x", daSet2);
in.close();
BOOST_CHECK_EQUAL(worked, true);
BOOST_CHECK_EQUAL(daSet2.size(), 27);
......@@ -78,7 +78,7 @@ BOOST_AUTO_TEST_CASE( pfamScanFormatTest )
// check read with DomainExt
BioSeqDataLib::DomainArrangementSet<BioSeqDataLib::DomainExt> daSet3;
in.open("../tests/domain/data_new/BB20012.pfamScan");
worked = BSDL::readPfamFormat(in, daSet3);
worked = BSDL::readPfamFormat(in, "x", daSet3);
in.close();
BOOST_CHECK_EQUAL(daSet3.size(), 27);
const BioSeqDataLib::DomainArrangement<BioSeqDataLib::DomainExt> &set3 = daSet3["IF2G_ARCFU"];
......@@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE( pfamScanFormatTest )
// check format fail
BioSeqDataLib::DomainArrangementSet<BioSeqDataLib::DomainExt> daSet4;
in.open("../tests/domain/data_new/test.fasta");
worked = BSDL::readPfamFormat(in, daSet4);
worked = BSDL::readPfamFormat(in, "x", daSet4);
in.close();
BOOST_CHECK_EQUAL(worked, false);
BOOST_CHECK_EQUAL(daSet4.size(), 0);
......
......@@ -13,16 +13,32 @@ BOOST_AUTO_TEST_SUITE(DASEr_Test)
namespace BSDL = BioSeqDataLib;
bool isUnsupported(BioSeqDataLib::FormatException const& ex )
{
return ( 0 == strcmp(ex.what(), "Error: Format of file '../tests/domain/data_new/test.fasta' could not be identified or is not supported.\n"));
}
bool formatIsBroken(BioSeqDataLib::FormatException const& ex)
{
return ( 0 == strcmp(ex.what(), "Error: Failed to read domain annotation file '../tests/domain/data_new/BB20012.pfamScan_formatError'. Format of file: PfamScan. Error occured in line: 29\n"));
}
BOOST_AUTO_TEST_CASE( readPfam )
{
BSDL::DomainArrangementSet<BSDL::PfamDomain> daSet;
BSDL::DASetReader<BSDL::PfamDomain> reader;
std::function<bool(AlgorithmPack::Input &, BSDL::DomainArrangementSet<BSDL::PfamDomain> &)> f = BSDL::readPfamFormat<BSDL::PfamDomain>;
std::function<bool(AlgorithmPack::Input &, const std::string &, BSDL::DomainArrangementSet<BSDL::PfamDomain> &)> f = BSDL::readPfamFormat<BSDL::PfamDomain>;
reader.addStrategy(f);
reader.read("../tests/domain/data/BB20012.pfamScan", daSet);
reader.read("../tests/domain/data_new/BB20012.pfamScan", daSet);
BOOST_CHECK_EQUAL(daSet.size(), 27);
BOOST_CHECK_THROW(reader.read("../tests/domain/data_new/NOTEXISTENT", daSet), std::ios_base::failure);
BOOST_CHECK_EXCEPTION(reader.read("../tests/domain/data_new/BB20012.pfamScan_formatError", daSet) , BioSeqDataLib::FormatException, formatIsBroken);
BOOST_CHECK_EXCEPTION(reader.read("../tests/domain/data_new/test.fasta", daSet) , BioSeqDataLib::FormatException, isUnsupported);
}
BOOST_AUTO_TEST_SUITE_END()
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment