Skip to content

Commit

Permalink
Add simple JSON output for superpositions
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Aug 13, 2015
1 parent e850e0e commit 3619f44
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 0 deletions.
24 changes: 24 additions & 0 deletions source/structure/geometry/coord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/lexical_cast.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/test/floating_point_comparison.hpp>

#include "exception/invalid_argument_exception.h"
Expand All @@ -34,6 +35,7 @@ using namespace cath::geom;
using namespace std;

using boost::lexical_cast;
using boost::property_tree::ptree;

//const double LENGTH_CHECK_PRECISION_PERCENTAGE_TOLERANCE( 1E-10 );

Expand Down Expand Up @@ -172,3 +174,25 @@ ostream & cath::geom::operator<<(ostream &arg_os, ///< TODOCUMENT
// arg_os << output_ss.str();
return arg_os;
}

/// \brief Save the specified coord to the specified Boost Property Tree ptree
///
/// \relates coord
void cath::geom::save_to_ptree(ptree &arg_ptree, ///< The ptree to which the coord should be saved
const coord &arg_coord ///< The coord to be saved
) {
arg_ptree.put( "x", arg_coord.get_x() );
arg_ptree.put( "y", arg_coord.get_y() );
arg_ptree.put( "z", arg_coord.get_z() );
}


/// \brief Make a new ptree representing the specified coord
///
/// \relates coord
ptree cath::geom::make_ptree_of(const coord &arg_coord ///< The coord that the new ptree should represent
) {
ptree new_ptree;
save_to_ptree( new_ptree, arg_coord );
return new_ptree;
}
6 changes: 6 additions & 0 deletions source/structure/geometry/coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/operators.hpp>
#include <boost/property_tree/ptree_fwd.hpp>

#include "exception/invalid_argument_exception.h"

Expand Down Expand Up @@ -236,6 +237,11 @@ namespace cath {
std::ostream & operator<<(std::ostream &,
const coord &);

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

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

/// \brief TODOCUMENT
///
/// \relates coord
Expand Down
48 changes: 48 additions & 0 deletions source/structure/geometry/rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <boost/lexical_cast.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/test/floating_point_comparison.hpp>

#include "exception/invalid_argument_exception.h"
Expand All @@ -39,6 +40,7 @@ using namespace cath::geom;
using namespace std;

using boost::lexical_cast;
using boost::property_tree::ptree;

/// \brief The identity rotation
const rotation & rotation::IDENTITY_ROTATION() {
Expand Down Expand Up @@ -224,5 +226,51 @@ rotation cath::geom::tidy_copy(const rotation &arg_rotation, ///< The rotation m
);
}

/// \brief Make a new Boost Property Tree ptree containing a single anonymous entry for the specified rotation's
/// value at the specified row and column
///
/// \relates rotation
ptree cath::geom::detail::make_ptree_of_row_and_col(const rotation &arg_rotation, ///< The rotation containing the entry to be saved
const size_t &arg_row, ///< The row index of the entry to be saved within the specified rotation
const size_t &arg_column ///< The column index of the entry to be saved within the specified rotation
) {
ptree new_ptree;
new_ptree.put( "", arg_rotation.get_value( arg_row, arg_column ) );
return new_ptree;
}

/// \brief Make a new Boost Property Tree ptree containing an anonymous array of anonymous entries for the specified rotation's
/// values in the specified row
///
/// \relates rotation
ptree cath::geom::detail::make_ptree_of_row(const rotation &arg_rotation, ///< The rotation containing the row to be saved
const size_t &arg_row ///< The row index of the specified rotation to be saved
) {
ptree new_ptree;
new_ptree.push_back( make_pair( "", make_ptree_of_row_and_col( arg_rotation, arg_row, 0 ) ) );
new_ptree.push_back( make_pair( "", make_ptree_of_row_and_col( arg_rotation, arg_row, 1 ) ) );
new_ptree.push_back( make_pair( "", make_ptree_of_row_and_col( arg_rotation, arg_row, 2 ) ) );
return new_ptree;
}

/// \brief Save the specified rotation to the specified Boost Property Tree ptree
///
/// \relates rotation
void cath::geom::save_to_ptree(ptree &arg_ptree, ///< The ptree to which the rotation should be saved
const rotation &arg_rotation ///< The rotation to be saved
) {
arg_ptree.push_back( make_pair( "", detail::make_ptree_of_row( arg_rotation, 0 ) ) );
arg_ptree.push_back( make_pair( "", detail::make_ptree_of_row( arg_rotation, 1 ) ) );
arg_ptree.push_back( make_pair( "", detail::make_ptree_of_row( arg_rotation, 2 ) ) );
}

/// \brief Make a new Boost Property Tree ptree representing the specified rotation
///
/// \relates rotation
ptree cath::geom::make_ptree_of(const rotation &arg_rotation ///< The rotation that the new ptree should represent
) {
ptree new_ptree;
save_to_ptree( new_ptree, arg_rotation );
return new_ptree;
}

15 changes: 15 additions & 0 deletions source/structure/geometry/rotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <boost/lexical_cast.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/operators.hpp>
#include <boost/property_tree/ptree_fwd.hpp>

#include "common/difference.h"
#include "common/type_aliases.h"
Expand Down Expand Up @@ -169,6 +170,20 @@ namespace cath {
coord_list rotate_copy(const rotation &, const coord_list &);
coord rotate_copy(const rotation &, const coord &);

namespace detail {
boost::property_tree::ptree make_ptree_of_row_and_col(const rotation &,
const size_t &,
const size_t &);

boost::property_tree::ptree make_ptree_of_row(const rotation &,
const size_t &);
}

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

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

/// \brief Generate some rotation that has the specified angle of rotation
///
/// \relates rotation
Expand Down
51 changes: 51 additions & 0 deletions source/superposition/io/superposition_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.


#include "superposition_io.h"

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <boost/range/irange.hpp>

#include "common/file/open_fstream.h"
#include "common/size_t_literal.h"
#include "exception/invalid_argument_exception.h"
#include "exception/not_implemented_exception.h"
#include "exception/runtime_error_exception.h"
Expand All @@ -39,6 +45,10 @@ using namespace cath::geom;
using namespace cath::sup;
using namespace std;

using boost::irange;
using boost::property_tree::json_parser::write_json;
using boost::property_tree::ptree;

/// \brief TODOCUMENT
///
/// \relates superposition
Expand Down Expand Up @@ -351,3 +361,44 @@ void cath::sup::write_superposed_pdb_from_files(const superposition &arg_superpo

write_superposed_pdb_to_file( arg_superposition, arg_filename, pdbs, arg_write_script, arg_relabel_chain );
}

/// \brief Save the specified superposition to the specified Boost Property Tree ptree
///
/// \relates superposition
void cath::sup::save_to_ptree(ptree &arg_ptree, ///< The ptree to which the superposition should be saved
const superposition &arg_superposition ///< The superposition to save to the ptree
) {
const auto transformations_key = string( "transformations" );
arg_ptree.put_child( transformations_key, ptree{} );
auto &transformations_ptree = arg_ptree.get_child( transformations_key );

for (const size_t &index : irange( 0_z, arg_superposition.get_num_entries() ) ) {
ptree transformation_ptree;
transformation_ptree.put_child( "translation", make_ptree_of( arg_superposition.get_translation_of_index( index ) ) );
transformation_ptree.put_child( "rotation", make_ptree_of( arg_superposition.get_rotation_of_index ( index ) ) );
transformations_ptree.push_back( make_pair( "", transformation_ptree ) );
}
}

/// \brief Make a new Boost Property Tree ptree representing the specified superposition
///
/// \relates superposition
ptree cath::sup::make_ptree_of(const superposition &arg_superposition ///< The superposition that the new ptree should represent
) {
ptree new_ptree;
save_to_ptree( new_ptree, arg_superposition );
return new_ptree;
}

/// \brief TODOCUMENT
///
/// \relates superposition
string cath::sup::to_json_string(const superposition &arg_superposition, ///< TODOCUMENT
const bool &arg_pretty_print ///< TODOCUMENT
) {
ostringstream json_ss;
ptree temp_ptree;
save_to_ptree( temp_ptree, arg_superposition );
write_json( json_ss, temp_ptree, arg_pretty_print );
return json_ss.str();
}
8 changes: 8 additions & 0 deletions source/superposition/io/superposition_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ namespace cath {
const path_vec &,
const bool &,
const bool &arg_relabel_chain = false);

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

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

std::string to_json_string(const superposition &,
const bool &arg_pretty_pring = true);
}
}

Expand Down
21 changes: 21 additions & 0 deletions source/superposition/superposition_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,26 @@ BOOST_AUTO_TEST_CASE(rmsd) {
);
}

BOOST_AUTO_TEST_CASE(to_json_string_works_for_example_sup) {
BOOST_CHECK_EQUAL(
to_json_string( create_pairwise_superposition( coord_list1, coord_list2 ), false ),
R"({"transformations":[{"translation":)"
R"({"x":"0","y":"0","z":"0"},)"
R"("rotation":)"
R"([["1","0","0"],)"
R"(["0","1","0"],)"
R"(["0","0","1"])"
R"(]},{"translation":)"
R"({"x":"104.47726177086807","y":"20.66883749985081","z":"41.523834652502032"},)"
R"("rotation":)"
R"([["0.20769400083047135","-0.9111116114406772","0.35600397963647046"],)"
R"(["0.96878664188870112","0.24194236224656662","0.0540031096194028"],)"
R"(["-0.13533530403056787","0.33367577803689003","0.93292273561878114"])"
R"(]}]})"
"\n"
);
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 3619f44

Please sign in to comment.