Commit 566bb5de authored by Carsten Kemena's avatar Carsten Kemena

switched to unique_ptr

- fixed memory leak
- implemented format listing
parent 125955a0
......@@ -46,6 +46,9 @@ class DASetOutputStrategy
{
public:
virtual ~DASetOutputStrategy()=0;
/**
* @brief String denoting the supported format(s)
*
......@@ -65,6 +68,10 @@ class DASetOutputStrategy
virtual void writeFile(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &outFile) const = 0;
};
template<typename DomainType>
DASetOutputStrategy<DomainType>::~DASetOutputStrategy()
{}
/**
* @brief PfamScan format reader class
......@@ -82,6 +89,9 @@ class PfamWriter final : public DASetOutputStrategy<DomainType>
PfamWriter(std::string (*f)(const DomainType&) = &dom2Pfam) : dom2String_(f)
{}
virtual ~PfamWriter()
{}
std::vector<std::string> formats() const
{
return { "PfamScan" };
......@@ -117,40 +127,41 @@ class PfamWriter final : public DASetOutputStrategy<DomainType>
/** @defgroup DASetOutputStrategies DASet output functions
* These functions can be used together with a DASetWriter to manage the export of domain arrangments into text files.
* @{
*
* \relates DASetWriter
*/
/**
* @brief Function to write DomainArrangementSet into the XDomFormat
*
* @tparam DomainType The domain type
* @param daSet The DomainArrangementSet to write to file
* @param out The name of the output file
* @param domOut A function describing how to handle the DomainOutput
* \see dom2XDom
* @brief PfamScan format reader class
*
* \relates DASetWriter
* @tparam DomainType The domainType to be used.
*/
template<class DomainType>
void
writeXDomFormat(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &out, std::function<std::string(DomainType)> domOut)
template<typename DomainType>
class XDomWriter final : public DASetOutputStrategy<DomainType>
{
for (auto &da : daSet)
public:
XDomWriter()
{}
std::vector<std::string> formats() const
{
return { "XDom" };
}
void writeFile(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &out) const
{
out << ">" << da.first << "\n";
for (auto &domain : da.second)
for (auto &da : daSet)
{
out << domOut(domain) << "\n";
out << ">" << da.first << "\n";
for (auto &dom : da.second)
{
out << std::to_string(dom.start()+1) + " " + std::to_string(dom.end()+1) + " " + dom.accession() + " " + std::to_string(dom.evalue()) << "\n";
}
}
}
}
};
/** @} */ // end of group1
} // BioSeqDataLib
......
......@@ -42,6 +42,7 @@
#include <string>
#include "DomainArrangementSet.hpp"
#include "DASetInputStrategies.hpp"
#include <boost/filesystem.hpp>
#include "../external/Input.hpp"
......@@ -60,7 +61,7 @@ template<class DomainType>
class DASetReader
{
private:
std::vector<DASetInputStrategy<DomainType> *> strategies_;
std::vector<std::unique_ptr<DASetInputStrategy<DomainType> > > strategies_;
public:
/**
......@@ -87,8 +88,6 @@ class DASetReader
DomainArrangementSet<DomainType>
read(const fs::path &fileName)
{
//DomainArrangementSet<DomainType> daSet;
//std::cout << "TEST-R: " << &daSet << "\n";
AlgorithmPack::Input inF(fileName);
for (size_t i=0; i<strategies_.size(); ++i)
{
......@@ -109,17 +108,33 @@ class DASetReader
* @param strategy The function containing the strategy.
*/
void
addStrategy(DASetInputStrategy<DomainType> *strategy)
addStrategy(std::unique_ptr<DASetInputStrategy<DomainType> > strategy)
{
strategies_.push_back(strategy);
strategies_.emplace_back(std::move(strategy));
}
void
loadAllSupportedStrategies()
loadDefaultStrategies()
{
}
std::vector<std::string>
supportedFormats()
{
std::vector<std::string> formats;
formats.reserve(strategies_.size());
for (const auto &strategy : strategies_)
{
for (const auto &format : strategy.second->formats())
{
formats.emplace_back(format);
}
}
return formats;
}
};
......
......@@ -59,7 +59,7 @@ template<class DomainType>
class DASetWriter
{
private:
std::map<std::string, DASetOutputStrategy<DomainType> *> strategies_;
std::map<std::string, std::unique_ptr<DASetOutputStrategy<DomainType> > > strategies_;
public:
......@@ -76,7 +76,8 @@ class DASetWriter
*/
virtual
~DASetWriter()
{}
{
}
/**
* @brief Function to write the data into a file.
......@@ -86,7 +87,7 @@ class DASetWriter
* @param out
*/
void
write(const DomainArrangementSet<DomainType> &daSet, const std::string &format, const fs::path &out)
write(const DomainArrangementSet<DomainType> &daSet, const std::string &format, const fs::path &out) const
{
AlgorithmPack::Output outF(out);
strategies_.at(format)->writeFile(daSet, outF);
......@@ -99,9 +100,29 @@ class DASetWriter
* @param strategy The function containing the strategy.
*/
void
addStrategy(const std::string &name, DASetOutputStrategy<DomainType> * strategy)
addStrategy(const std::string &name, std::unique_ptr<DASetOutputStrategy<DomainType> > strategy)
{
strategies_.emplace(name, std::move(strategy));
}
/**
* @brief Returns a list of all currently supported formats of this writer.
*
* @return std::vector<std::string>
*/
std::vector<std::string>
supportedFormats() const
{
strategies_.emplace(name, strategy);
std::vector<std::string> formats;
formats.reserve(strategies_.size());
for (const auto &strategy : strategies_)
{
for (const auto &format : strategy.second->formats())
{
formats.emplace_back(format);
}
}
return formats;
}
};
......
......@@ -39,20 +39,6 @@
namespace BioSeqDataLib
{
/**
* @brief Returns a string with all the information about a domain in Xdom format.
*
* @tparam DomainType The domain type
* @param dom The domain to return as string
* @return std::string The domain information
*/
template<class DomainType>
std::string
dom2XDom(const DomainType &dom)
{
return (std::to_string(dom.start()+1) + " " + std::to_string(dom.end()+1) + " " + dom.accession() + " " + std::to_string(dom.evalue()));
}
/**
* @brief Returns a string with all the information about a domain in pfamScan format.
*
......
......@@ -32,10 +32,9 @@ bool formatIsBroken(BioSeqDataLib::FormatException const& ex)
BOOST_AUTO_TEST_CASE( readPfam )
{
//BSDL::DomainArrangementSet<BSDL::PfamDomain> daSet;
BSDL::DASetReader<BSDL::PfamDomain> reader;
reader.addStrategy(new BSDL::PfamReader<BSDL::PfamDomain>());
using strategy = BSDL::DASetInputStrategy<BSDL::PfamDomain>;
reader.addStrategy(std::unique_ptr<strategy>(new BSDL::PfamReader<BSDL::PfamDomain>));
auto daSet = reader.read("../tests/domain/data_new/BB20012.pfamScan");
BOOST_CHECK_EQUAL(daSet.size(), 27);
BOOST_CHECK_THROW(reader.read("../tests/domain/data_new/NOTEXISTENT"), std::ios_base::failure);
......
......@@ -17,19 +17,20 @@ BOOST_AUTO_TEST_CASE( write_xdom )
BSDL::DomainArrangementSet<BSDL::PfamDomain> daSet;
BSDL::DomainArrangement<BSDL::PfamDomain> da;
da.emplace_back("A", 0, 2, 0.1);
da.emplace_back("bbbb", 5, 10, 0.5);
da.emplace_back("b", 5, 10, 0.5);
daSet.emplace("arg", da);
BSDL::DASetWriter<BSDL::PfamDomain> writer;
writer.addStrategy("pfam", new BSDL::PfamWriter<BSDL::PfamDomain>());
//auto f = std::bind(BSDL::writeXDomFormat<BSDL::PfamDomain>, std::placeholders::_1, std::placeholders::_2, BSDL::dom2XDom<BSDL::PfamDomain>);
//writer.addStrategy("xdom", f);
//auto x = std::bind(BSDL::writePfamFormat<BSDL::PfamDomain>, std::placeholders::_1, std::placeholders::_2, BSDL::dom2Pfam<BSDL::PfamDomain>);
//writer.addStrategy("pfam", x);
using strategy = BSDL::DASetOutputStrategy<BSDL::PfamDomain>;
writer.addStrategy("pfam", std::unique_ptr<strategy>(new BSDL::PfamWriter<BSDL::PfamDomain>));
writer.addStrategy("xdom", std::unique_ptr<strategy>(new BSDL::XDomWriter<BSDL::PfamDomain>));
//writer.write(daSet, "xdom", "test53.txt");
writer.write(daSet, "xdom", "test53.txt");
writer.write(daSet, "pfam", "test56.txt");
auto formats = writer.supportedFormats();
BOOST_CHECK_EQUAL("PfamScan", formats[0]);
BOOST_CHECK_EQUAL("XDom", formats[1]);
}
BOOST_AUTO_TEST_SUITE_END()
......
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