Skip to content

Commit

Permalink
Add+test basic superposition_context JSON-writing
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Aug 19, 2015
1 parent 969b3d9 commit 15b4f37
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
74 changes: 74 additions & 0 deletions source/superposition/superposition_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@

#include "superposition_context.h"

#include <boost/log/trivial.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <boost/range/algorithm_ext/for_each.hpp>

#include "alignment/alignment_context.h"
#include "exception/invalid_argument_exception.h"
#include "file/pdb/pdb.h"
#include "file/pdb/pdb_atom.h"
#include "file/pdb/pdb_residue.h"
#include "superposition/io/superposition_io.h"

using namespace cath;
using namespace cath::align;
Expand All @@ -33,6 +39,11 @@ using namespace cath::file;
using namespace cath::sup;
using namespace std;

using boost::log::trivial::severity_level;
using boost::property_tree::json_parser::write_json;
using boost::property_tree::ptree;
using boost::range::for_each;

/// \brief TODOCUMENT
superposition_context::superposition_context(const pdb_list &arg_pdbs, ///< TODOCUMENT
const str_vec &arg_names, ///< TODOCUMENT
Expand Down Expand Up @@ -99,3 +110,66 @@ alignment_context cath::sup::make_alignment_context(const superposition_context
arg_superposition_context.get_alignment_cref()
);
}

/// \brief TODOCUMENT
///
/// At present, this stores the names and the superposition but does nothing
/// with the alignment or the PDBs
///
/// \relates superposition_context
void cath::sup::save_to_ptree(ptree &arg_ptree, ///< TODOCUMENT
const superposition_context &arg_sup_context ///< TODOCUMENT
) {
if ( arg_sup_context.has_alignment() ) {
BOOST_LOG_TRIVIAL( warning ) << "Whilst converting a superposition_context to JSON, its alignment will be ignored because that is not currently supported";
}

/// \todo DRY
const auto transformations_key = string( "transformations" );
const auto supn_ptree = make_ptree_of( arg_sup_context.get_superposition_cref() );
const auto trans_ptrees = supn_ptree.get_child( transformations_key );

const auto entries_key = "entries";
arg_ptree.put_child( entries_key, ptree{} );
auto &entries_ptree = arg_ptree.get_child( entries_key );

for_each(
arg_sup_context.get_names_cref(),
trans_ptrees,
[&] (const string &name, const pair<string, ptree> &trans_ptree) {
ptree entry_ptree;
entry_ptree.put ( "name", name );
entry_ptree.put_child( "transformation", trans_ptree.second );
entries_ptree.push_back( make_pair( "", entry_ptree ) );
}
);
}

/// \brief TODOCUMENT
///
/// At present, this stores the names and the superposition but does nothing
/// with the alignment or the PDBs
///
/// \relates superposition_context
ptree cath::sup::make_ptree_of(const superposition_context &arg_sup_context ///< TODOCUMENT
) {
ptree new_ptree;
save_to_ptree( new_ptree, arg_sup_context );
return new_ptree;
}

/// \brief Create a JSON string to represent the specified superposition
///
/// At present, this stores the names and the superposition but does nothing
/// with the alignment or the PDBs
///
/// \relates superposition_context
string cath::sup::to_json_string(const superposition_context &arg_sup_context, ///< The superposition_context to represent in the JSON string
const bool &arg_pretty_print ///< Whether to use whitespace (including line breaks) in the JSON to make it more human-readable
) {
ostringstream json_ss;
ptree temp_ptree;
save_to_ptree( temp_ptree, arg_sup_context );
write_json( json_ss, temp_ptree, arg_pretty_print );
return json_ss.str();
}
8 changes: 8 additions & 0 deletions source/superposition/superposition_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ namespace cath {
};

align::alignment_context make_alignment_context(const superposition_context &);

void save_to_ptree(boost::property_tree::ptree &,
const superposition_context &);

boost::property_tree::ptree make_ptree_of(const superposition_context &);

std::string to_json_string(const superposition_context &,
const bool &arg_pretty_print = true);
}
}

Expand Down
40 changes: 37 additions & 3 deletions source/superposition/superposition_context_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,57 @@

#include <boost/test/auto_unit_test.hpp>

#include "file/pdb/pdb.h"
#include "file/pdb/pdb_atom.h"
#include "file/pdb/pdb_list.h"
#include "file/pdb/pdb_residue.h"
#include "structure/geometry/coord_list.h"
#include "superposition/superposition_context.h"

using namespace cath::file;
using namespace cath::geom;
using namespace cath::sup;

namespace cath {
namespace test {

/// \brief The superposition_context_test_suite_fixture to assist in testing superposition_context
struct superposition_context_test_suite_fixture {
protected:
~superposition_context_test_suite_fixture() noexcept = default;

coord_list coord_list_1{ { coord{ 1.0, 0.0, 0.0 }, coord{ 2.0, 0.0, 0.0 } } };
coord_list coord_list_2{ { coord{ 0.0, -1.0, 0.0 }, coord{ 0.0, -2.0, 0.0 } } };
superposition the_sup = create_pairwise_superposition( coord_list_1, coord_list_2 );
superposition_context the_sup_con{
pdb_list{ pdb_vec{ 2, pdb{} } },
str_vec { "name_a", "name_b" },
the_sup
};
};

}
}

BOOST_FIXTURE_TEST_SUITE(superposition_context_test_suite, cath::test::superposition_context_test_suite_fixture)

/// \brief TODOCUMENT
BOOST_AUTO_TEST_CASE(basic) {
BOOST_CHECK( true );
BOOST_AUTO_TEST_CASE(to_json_string_works_for_example_sup_con) {
BOOST_CHECK_EQUAL(
to_json_string( the_sup_con, false ),
R"({"entries":[{"name":"name_a","transformation":{"translation":)"
R"({"x":"0","y":"0","z":"0"},)"
R"("rotation":)"
R"([["1","0","0"],)"
R"(["0","1","0"],)"
R"(["0","0","1"])"
R"(]}},{"name":"name_b","transformation":{"translation":)"
R"({"x":"0","y":"0","z":"0"},)"
R"("rotation":)"
R"([["0","-1","0"],)"
R"(["0","0","-1"],)"
R"(["1","0","0"])"
R"(]}}]})" "\n"
);
}

BOOST_AUTO_TEST_SUITE_END()
Expand Down

0 comments on commit 15b4f37

Please sign in to comment.