Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
atom_tree_diff.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/io/atom_tree_diffs/atom_tree_diff.hh
11 ///
12 /// @brief Silent-file format based on "diffs" of AtomTree DOFs
13 /// @author Ian W. Davis
14 
15 #ifndef INCLUDED_core_import_pose_atom_tree_diffs_atom_tree_diff_hh
16 #define INCLUDED_core_import_pose_atom_tree_diffs_atom_tree_diff_hh
17 
18 #include <core/types.hh>
19 #include <core/pose/Pose.fwd.hh>
20 #ifdef WIN32
21 #include <core/pose/Pose.hh> // WIN32 INCLUDE
22 #endif
24 // AUTO-REMOVED #include <utility/vector1.hh>
25 #include <utility/pointer/ReferenceCount.hh>
26 
27 #include <fstream>
28 // AUTO-REMOVED #include <iostream>
29 #include <map>
30 // AUTO-REMOVED #include <string>
31 // AUTO-REMOVED #include <set>
32 
33 #include <utility/vector1.hh>
34 
35 
36 namespace core {
37 namespace import_pose {
38 namespace atom_tree_diffs {
39 
40 
41 class AtomTreeDiff; // fwd declaration
44 
45 typedef std::pair<std::string, core::Real> ScorePair;
46 typedef std::map< std::string, core::Real > Scores;
47 typedef std::pair< std::string, Scores > ScoresPair;
48 
49 //typedef std::map< std::string, Scores > ScoresMap;
50 /// Just like ScoresMap, but can be sorted into some particular order *by scores*.
51 /// Maps can only be sorted by key, which here is just a pose tag.
53 
54 typedef std::pair<std::string, int> RefTag;
55 typedef std::map<std::string, int> RefTags;
56 
57 typedef std::pair<std::string, Size> TagScorePair;
58 typedef std::map<std::string, Size> TagScoreMap;
59 
60 
61 ///@brief An object wrapper for reading atom_tree_diff files,
62 /// complete with embedded reference structures.
63 ///
64 ///@details Only works with uncompressed files, because we have to be able to
65 /// to random access (seekg) to pull out single structures in random order.
66 ///
68 {
69 public:
70 
71  AtomTreeDiff();
73  virtual ~AtomTreeDiff();
74 
75  ///@brief returns true if a reference struct with the given tag is present
76  bool has_ref_pose(std::string const & tag) const;
77 
78  /// @brief True if a (non-reference) structure with the given tag is present in the file
79  bool has_tag(std::string const & tag) const;
80 
81  bool has_ref_tag(std::string const & tag) const;
82 
83  /// @brief Return list of (pose tag, score sets) pairs for all poses, in file order.
84  ScoresPairList const & scores() const { return scores_; }
85 
86  /// @brief Utility function for selecting subsets of structures by score.
87  static void sort_by(std::string const & score_name, ScoresPairList & scores, bool descending=false);
88 
89  /// @brief Reads the pose data from file and reconstructs the complete pose.
90  void read_pose(std::string const & tag, core::pose::Pose & pose_out);
91 
92  /// @brief Reads the pose data from file and reconstructs the complete pose, using the supplied reference pose.
93  void read_pose(std::string const & tag, core::pose::Pose & pose_out, core::pose::Pose const & ref_pose);
94 
95  void read_file(std::string filename);
96 
97  /// @brief Returns the default reference pose for the given tag. Fails if none is available.
99 
100  /// @brief Allows access to and mutation of (!) the references poses stored in this file. Use with caution.
101  /// @details This exists to allow setup on stored reference poses for properties that don't get saved/restored
102  /// in PDB format, like covalent constraints for enzyme design.
104  { return unique_ref_poses_; }
105 
106  RefTags const & get_ref_tags() const {return ref_tags_;}
107 
108  TagScoreMap const & get_tag_score_map() const{return tag_idx_;}
109 
110  int count(std::string const & tag) const{
111  std::map<std::string, int>::const_iterator const iter= ref_tags_.find(tag);
112  return iter->second;
113  }
114 
115 private:
116  AtomTreeDiff(AtomTreeDiff const &);
117 
118  /// The list of (tag,scores) pairs for the diffed structures, for efficient selection of subsets.
119  /// (E.g. the top 5% by total score.) Memory cost is probably ~1 kb / structure.
121 
122  /// Maps reference tags to how many associated atom_tree_diff structs exist...
123  /// int is needed here because there are 0 atom tree diffs to start with
125 
126  /// Maps tags to indices in scores_
128 
129  /// The map of (tag, file position) for getting random access to the structural data.
130  std::map< std::string, long > offsets_;
131 
132  /// The map of (tag, reference pose) for decoding the diffs. Some could be null.
133  std::map< std::string, core::pose::PoseOP > ref_poses_;
134 
135  /// All references poses from the file, one copy each for convenience.
137 
138  // The file being read.
139  std::ifstream in_;
140 
141  mutable bool file_read_;
142 
143 }; // class AtomTreeDiff
144 
145 ///@brief Helper function for writing entries -- not usually called by clients.
146 void dump_score_line(
147  std::ostream & out,
148  std::string const & pose_tag,
149  std::map< std::string, core::Real > const & scores
150 );
151 
152 ///@brief Helper function for writing entries -- not usually called by clients.
153 /* void dump_score_line(
154  std::ostream & out,
155  std::string const & pose_tag
156 ); */
157 
158 ///@brief Embeds a reference pose as PDB coords + foldtree; will be used for reconstructing subsequent diffs.
160  std::ostream & out,
161  std::string const & pose_tag,
162  std::map< std::string, core::Real > const & scores,
163  core::pose::Pose const & pose
164 );
165 
166 ///@brief Encodes pose relative to ref_pose by noting which atom_tree DOFs are different.
168  std::ostream & out,
169  std::string const & pose_tag,
170  std::map< std::string, core::Real > const & scores,
171  core::pose::Pose const & ref_pose_in,
172  core::pose::Pose const & pose,
173  int bb_precision = 6,
174  int sc_precision = 4,
175  int bondlen_precision = 2
176 );
177 
178 ///@brief Gets next tag and scores from the stream, or returns false if none.
179 /// Call this to find desired structure, then call pose_from_atom_tree_diff().
181  std::istream & in,
182  std::string & pose_tag_out,
183  std::map< std::string, core::Real > & scores_out
184 );
185 
186 
187 ///@brief Sets pose = ref_pose and then starts modifying DOFs in pose to recreate a saved structure.
188 /// Call after header_from_atom_tree_diff(). Returns false on error.
190  std::istream & in,
191  core::pose::Pose const & ref_pose,
192  core::pose::Pose & pose
193 );
194 
195 
196 ///@brief Helper for dump_atom_tree_diff(), fills map with weighted score terms.
198  core::pose::Pose & pose, //< pose is not modified but scoring is a non-const op
199  core::scoring::ScoreFunction const & sfxn,
200  std::map< std::string, core::Real > & scores_out
201 );
202 
203 
204 ///@brief For use in deciding how many digits of precision you need when diffing an atom tree.
206  core::pose::Pose const & ref_pose,
207  int bb_precision = 6,
208  int sc_precision = 4
209 );
210 
211 ///@brief Test if given file is an atom_tree_diff
213 
214 ///@brief Test if given stream is an atom_tree_diff
215 ///@details If everything goes right, after the call, the read position should be at the same place it was to start with
216 bool file_is_atom_tree_diff( std::istream & in );
217 
218 } // silent
219 } // io
220 } // core
221 
222 #endif // INCLUDED_core_import_pose_silent_atom_tree_diff_HH