Skip to content

Commit

Permalink
Avoid separate ostringstream by saving ostrm flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed Nov 7, 2017
1 parent a9ebe87 commit 02f180b
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions source/uni/file/pdb/pdb_atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <boost/algorithm/string/trim.hpp>
#include <boost/format.hpp>
#include <boost/io/ios_state.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/math/special_functions/fpclassify.hpp>

Expand All @@ -41,6 +42,7 @@ using boost::algorithm::trim_copy;
using boost::algorithm::trim_left_copy;
using boost::algorithm::trim_right;
using boost::format;
using boost::io::ios_flags_saver;
using boost::lexical_cast;
using boost::string_ref;

Expand Down Expand Up @@ -101,50 +103,49 @@ ostream & cath::file::write_pdb_file_entry(ostream &arg_os, ///< T
BOOST_THROW_EXCEPTION(invalid_argument_exception("Empty residue_name in cath::write_pdb_file_entry()"));
}

// Save the state of the ostream's flags (and reset them in this guard's dtor)
const ios_flags_saver stream_flags_guard{ arg_os };

// Grab the necessary data
const coord atom_coord = arg_pdb_atom.get_coord();
const string residue_name_with_insert_or_space = make_residue_name_string_with_insert_or_space(
arg_res_id.get_residue_name()
);

// Output the entry to a temporary ostringstream
// (to avoid needlessly messing around with the formatting options of the ostream)
ostringstream atom_ss;
// Comments with PDB format documentation
// (http://www.wwpdb.org/documentation/format33/sect9.html#ATOM)
atom_ss << left;
atom_ss << setw( 6 ) << arg_pdb_atom.get_record_type(); // 1 - 6 Record name "ATOM " or "HETATM"
atom_ss << right;
atom_ss << setw( 5 ) << arg_pdb_atom.get_atom_serial(); // 7 - 11 Integer serial Atom serial number.
atom_ss << " ";
atom_ss << setw( 4 ) << get_element_type_untrimmed_str_ref( arg_pdb_atom ); // 13 - 16 Atom name Atom name.
atom_ss << arg_pdb_atom.get_alt_locn(); // 17 Character altLoc Alternate location indicator.
atom_ss << get_amino_acid_code_string( arg_pdb_atom ); // 18 - 20 Residue name resName Residue name.
atom_ss << " ";
atom_ss << arg_res_id.get_chain_label(); // 22 Character chainID Chain identifier.
// 23 - 26 Integer resSeq Residue sequence number.
atom_ss << setw( 5 ) << residue_name_with_insert_or_space; // 27 AChar iCode Code for insertion of residues.
atom_ss << " ";
atom_ss << fixed << setprecision( 3 );
atom_ss << setw( 8 ) << atom_coord.get_x(); // 31 - 38 Real(8.3) x Orthogonal coordinates for X in Angstroms.
atom_ss << setw( 8 ) << atom_coord.get_y(); // 39 - 46 Real(8.3) y Orthogonal coordinates for Y in Angstroms.
atom_ss << setw( 8 ) << atom_coord.get_z(); // 47 - 54 Real(8.3) z Orthogonal coordinates for Z in Angstroms.
atom_ss << fixed << setprecision( 2 );
atom_ss << setw( 6 ) << arg_pdb_atom.get_occupancy(); // 55 - 60 Real(6.2) occupancy Occupancy.
atom_ss << ( format( "%6.2f" ) % arg_pdb_atom.get_temp_factor() ).str().substr( 0, 6 ); // 61 - 66 Real(6.2) tempFactor Temperature factor.
const auto element_sym_strref = get_element_symbol_str_ref( arg_pdb_atom ); // 77 - 78 LString(2) element Element symbol, right-justified.
const auto charge_strref = get_charge_str_ref ( arg_pdb_atom ); // 79 - 80 LString(2) charge Charge on the atom.
// Comments with PDB format documentation
// (http://www.wwpdb.org/documentation/format33/sect9.html#ATOM)
arg_os << left;
arg_os << setw( 6 ) << arg_pdb_atom.get_record_type(); // 1 - 6 Record name "ATOM " or "HETATM"
arg_os << right;
arg_os << setw( 5 ) << arg_pdb_atom.get_atom_serial(); // 7 - 11 Integer serial Atom serial number.
arg_os << " ";
arg_os << setw( 4 ) << get_element_type_untrimmed_str_ref( arg_pdb_atom ); // 13 - 16 Atom name Atom name.
arg_os << arg_pdb_atom.get_alt_locn(); // 17 Character altLoc Alternate location indicator.
arg_os << get_amino_acid_code_string( arg_pdb_atom ); // 18 - 20 Residue name resName Residue name.
arg_os << " ";
arg_os << arg_res_id.get_chain_label(); // 22 Character chainID Chain identifier.
// 23 - 26 Integer resSeq Residue sequence number.
arg_os << setw( 5 ) << residue_name_with_insert_or_space; // 27 AChar iCode Code for insertion of residues.
arg_os << " ";
arg_os << fixed << setprecision( 3 );
arg_os << setw( 8 ) << atom_coord.get_x(); // 31 - 38 Real(8.3) x Orthogonal coordinates for X in Angstroms.
arg_os << setw( 8 ) << atom_coord.get_y(); // 39 - 46 Real(8.3) y Orthogonal coordinates for Y in Angstroms.
arg_os << setw( 8 ) << atom_coord.get_z(); // 47 - 54 Real(8.3) z Orthogonal coordinates for Z in Angstroms.
arg_os << fixed << setprecision( 2 );
arg_os << setw( 6 ) << arg_pdb_atom.get_occupancy(); // 55 - 60 Real(6.2) occupancy Occupancy.
arg_os << ( format( "%6.2f" ) % arg_pdb_atom.get_temp_factor() ).str().substr( 0, 6 ); // 61 - 66 Real(6.2) tempFactor Temperature factor.
const auto element_sym_strref = get_element_symbol_str_ref( arg_pdb_atom ); // 77 - 78 LString(2) element Element symbol, right-justified.
const auto charge_strref = get_charge_str_ref ( arg_pdb_atom ); // 79 - 80 LString(2) charge Charge on the atom.

if ( ! element_sym_strref.empty() || ! charge_strref.empty() ) {
atom_ss << " ";
atom_ss << right << setw( 2 ) << ( element_sym_strref.empty() ? " "s : element_sym_strref.to_string() );
arg_os << " ";
arg_os << right << setw( 2 ) << ( element_sym_strref.empty() ? " "s : element_sym_strref.to_string() );
if ( ! charge_strref.empty() ) {
atom_ss << charge_strref;
arg_os << charge_strref;
}
}

// Output the result to the ostream and return it
arg_os << atom_ss.str();
return arg_os;
}

Expand Down

0 comments on commit 02f180b

Please sign in to comment.