Commit 125955a0 authored by Carsten Kemena's avatar Carsten Kemena

adapting domain output strategy to new design

parent 9b167203
......@@ -32,7 +32,7 @@
*/
#include "DomainArrangementSet.hpp"
#include "DomainOutStrategies.hpp"
#include "../external/Output.hpp"
......@@ -40,6 +40,83 @@ namespace BioSeqDataLib
{
template<typename DomainType>
class DASetOutputStrategy
{
public:
/**
* @brief String denoting the supported format(s)
*
* The idea is to use this so that the application can automatically create a list of supported formats.
*
* @return std::vector<std::string> The format
*/
virtual std::vector<std::string> formats() const = 0;
/**
* @brief Writes domain arrangments to a file
*
* @param daSet The domains arrangements to write
* @param outFile The file to write to
* @param fileName The file name of the inFile parmeter. Only to be used for exception throwing.
*/
virtual void writeFile(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &outFile) const = 0;
};
/**
* @brief PfamScan format reader class
*
* @tparam DomainType The domainType to be used.
*/
template<typename DomainType>
class PfamWriter final : public DASetOutputStrategy<DomainType>
{
private:
std::string (*dom2String_)(const DomainType&);
public:
PfamWriter(std::string (*f)(const DomainType&) = &dom2Pfam) : dom2String_(f)
{}
std::vector<std::string> formats() const
{
return { "PfamScan" };
}
void writeFile(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &out) const
{
out << "# pfam_scan.pl\n";
out << "# <seq id> <alignment start> <alignment end> <envelope start> <envelope end> <hmm acc> <hmm name> <type> <hmm start> <hmm end> <hmm length> <bit score> <E-value> <significance> <clan>\n\n";
size_t len = 0;
for (const auto &da : daSet)
{
if (len < da.first.length())
len = da.first.length();
}
out.precision(1);
for (auto &da : daSet)
{
out.width(static_cast<std::streamsize>(len));
for (auto &domain : da.second)
{
out << std::left << da.first << " ";
out << dom2String_(domain) << "\n";
}
}
}
};
/** @defgroup DASetOutputStrategies DASet output functions
* These functions can be used together with a DASetWriter to manage the export of domain arrangments into text files.
* @{
......@@ -72,43 +149,6 @@ writeXDomFormat(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Ou
}
}
/**
* @brief Function to write a DomainArrangementSet into pfam format.
*
* @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. The function that should be usually used with this is: dom2Pfam
* \see dom2Pfam
*
* \relates DASetWriter
*/
template<class DomainType>
void
writePfamFormat(const DomainArrangementSet<DomainType> &daSet, AlgorithmPack::Output &out, std::function<std::string(DomainType)> domOut)
{
out << "# pfam_scan.pl\n";
out << "# <seq id> <alignment start> <alignment end> <envelope start> <envelope end> <hmm acc> <hmm name> <type> <hmm start> <hmm end> <hmm length> <bit score> <E-value> <significance> <clan>\n\n";
size_t len = 0;
for (const auto &da : daSet)
{
if (len < da.first.length())
len = da.first.length();
}
out.precision(1);
for (auto &da : daSet)
{
out.width(static_cast<std::streamsize>(len));
for (auto &domain : da.second)
{
out << std::left << da.first << " ";
out << domOut(domain) << "\n";
}
}
}
/** @} */ // end of group1
......
......@@ -59,7 +59,7 @@ template<class DomainType>
class DASetWriter
{
private:
std::map<std::string, std::function<void(const DomainArrangementSet<DomainType> &, AlgorithmPack::Output &)> > strategies;
std::map<std::string, DASetOutputStrategy<DomainType> *> strategies_;
public:
......@@ -89,7 +89,7 @@ class DASetWriter
write(const DomainArrangementSet<DomainType> &daSet, const std::string &format, const fs::path &out)
{
AlgorithmPack::Output outF(out);
strategies.at(format)(daSet, outF);
strategies_.at(format)->writeFile(daSet, outF);
}
/**
......@@ -99,9 +99,9 @@ class DASetWriter
* @param strategy The function containing the strategy.
*/
void
addStrategy(const std::string &name, std::function<void(const DomainArrangementSet<DomainType> &, AlgorithmPack::Output &)> strategy)
addStrategy(const std::string &name, DASetOutputStrategy<DomainType> * strategy)
{
strategies.emplace(name, strategy);
strategies_.emplace(name, strategy);
}
};
......
......@@ -21,13 +21,14 @@ BOOST_AUTO_TEST_CASE( write_xdom )
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);
//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);
writer.write(daSet, "xdom", "test53.txt");
//writer.write(daSet, "xdom", "test53.txt");
writer.write(daSet, "pfam", "test56.txt");
}
......
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