Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Cluster.cc
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 /// the incorrect documentation below is an embarassment.
11 /// @file protocols/jd2/Job.hh
12 /// @brief header file for ThreadingJob classes, part of August 2008 job distributor as planned at RosettaCon08. This file is responsible for three ideas: "inner" jobs, "outer" jobs (with which the job distributor works) and job container (currently just typdefed in the .fwd.hh)
13 /// @author Steven Lewis smlewi@gmail.com
14 
15 
17 #include <basic/Tracer.hh>
18 
19 #include <ObjexxFCL/format.hh>
20 
21 #include <basic/options/keys/cluster.OptionKeys.gen.hh>
22 #include <basic/options/option.hh>
23 
24 #include <utility/vector1.hh>
25 
26 
27 namespace protocols {
28 namespace toolbox {
29 
30 using namespace ObjexxFCL;
31 using namespace fmt;
32 
33 static basic::Tracer tr("protocols.cluster",basic::t_info);
34 
35 
36 using namespace core;
37 
38 ClusterOptions::ClusterOptions( bool assign_new_cluster_tag_in )
39  : assign_new_cluster_tag( assign_new_cluster_tag_in ),
40  limit_cluster_size( basic::options::option[ basic::options::OptionKeys::cluster::limit_cluster_size ] ),
41  cluster_radius( basic::options::option[ basic::options::OptionKeys::cluster::radius ] ),
42  keep_center( true )
43 { }
44 
45 void ClusterBase::print_cluster_assignment( std::ostream & out ) const {
46  for( Size i=1; i <= clusterlist_.size(); i++ ) {
47  for( Size j=1; j<= clusterlist_[i].size(); j++ ) {
48  out << RJ( 3,clusterlist_[i] [ j-1 ]) << " " << RJ( 5,i) << RJ(5, j)
49  << std::endl;
50  }
51  }
52 }
53 
54 
55 std::ostream & operator<< ( std::ostream & out, ClusterBase const & cl) {
56  cl.show( out );
57  return out;
58 }
59 
60 std::ostream & operator<< (
61  std::ostream & out,
62  ClusterBase::ClusterList const & cl
63 ) {
64  Size ncl( 1 );
65  for ( ClusterBase::ClusterList::const_iterator it = cl.begin(), eit = cl.end();
66  it != eit; ++it ) {
67  out << "CLUSTER " << RJ(3, ncl++ ) << " " << RJ(4, it->size()) << " ";
68  for ( ClusterBase::IntraClusterIterator cit = it->begin(), ecit = it->end();
69  cit != ecit; ++cit ) {
70  out << RJ( 4, *cit ) << " ";
71  }
72  }
73  return out;
74 }
75 
76 std::istream & operator>>( std::istream & in, ClusterBase & cl ) {
77  cl.read( in );
78  return in;
79 }
80 
81 
82 std::istream & operator>>( std::istream & in, ClusterBase::ClusterList & cl ) {
83  cl.clear();
84  while ( in ) {
85  std::string tag;
86  Size size,nr;
87  in >> tag >> nr >> size;
88  if ( !in.good() ) break;
89  if ( tag != "CLUSTER" ) {
90  in.setstate( std::ios_base::failbit );
91  break;
92  }
93  ClusterBase::Cluster new_clust;
94  for ( Size i = 1; i<=size && in.good(); i++) {
95  Size new_elem;
96  in >> new_elem;
97  if ( in.good() ) new_clust.push_back( new_elem );
98  }
99  if ( new_clust.size() == size ) {
100  cl.push_back( new_clust );
101  }
102  }
103  return in;
104 }
105 
106 void ClusterBase::show( std::ostream & out ) const {
107  out << clusterlist_;
108 }
109 
110 void ClusterBase::read( std::istream & in ) {
111  in >> clusterlist_;
112 }
113 
115  tr.Info << "---------- Summary ---------------------------------" << std::endl;
116 
117  int count = 0;
118 
119  for ( Size i = 1; i <= clusterlist_.size(); i++ ) {
120  tr.Info << "Cluster: " << i << " N: " << clusterlist_[i].size() << " c." << i << ".*.pdb " ;
121  tr.Info << std::endl;
122  count += clusterlist_[i].size();
123  for ( Size j=1; j <= clusterlist_[i].size(); j++ ) {
124  tr.Info << tags[ clusterlist_[i][j-1] ] << " " << all_energies[ clusterlist_[i][j-1] ]
125  << " " << dist( clusterlist_[i][j-1], clusterlist_[i][0] )
126  << std::endl;
127  }
128  tr.Info << std::endl;
129  }
130  tr.Info << "----------------------------------------------------" << std::endl;
131  tr.Info << " Clusters: " << clusterlist_.size() << std::endl;
132  tr.Info << " Structures: " << count << std::endl;
133  tr.Info << "----------------------------------------------------" << std::endl;
134 }
135 
137  utility::vector1< Size > neighbors ( dim(), 0 );
138  utility::vector1< Size > clusternr ( dim(), 0 );
139  utility::vector1< Size > clustercenter;
140  Size mostneighbors;
141  Size nclusters = 0;
142 
143  Size listsize = dim();
144 
145  utility::vector1<Size> clustercentre;
146  // now assign groupings
147  while( true ) {
148  // count each's neighbors
149  for ( Size i=1; i <= listsize; i++ ) {
150  neighbors[i] = 0;
151  if ( clusternr[i] > 0) continue; // ignore ones already taken
152  for ( Size j=1; j <= listsize; j++ ) {
153  if ( clusternr[j] > 0 ) continue; // ignore ones already taken
154  if ( dist( i, j ) < cluster_radius_) neighbors[i]++;
155  }
156  }
157 
158  mostneighbors = 1;
159  for ( Size i=1; i <= listsize; i++) {
160  if ( neighbors[i] > neighbors[mostneighbors] ) mostneighbors=i;
161  }
162  if ( neighbors[ mostneighbors ] == 0) break; // finished!
163  for ( Size i=1; i <= listsize; i++){
164  if ( clusternr[i] > 0 ) continue; // ignore ones already taken
165  if ( dist( i, mostneighbors ) < cluster_radius_ ) {
166  clusternr[i] = mostneighbors;
167  }
168  }
169 
170  clustercentre.push_back(mostneighbors);
171  nclusters++;
172 
173  if ( nclusters > n_max_cluster_ ) break; // currently fixed but ought to be a paraemter
174  }
175 
176 
177  for( Size i=1; i <= clustercentre.size(); i++ ) {
178  std::deque< Size > newlist;
179  for( Size j=1; j <=listsize; j++ ) {
180  // if that struture belongs to a given cluster
181  if ( clusternr[j] == clustercentre[i] ) {
182  //add it
183  //newlist.push_back(j);
184  if ( j!= clustercentre[i] ) newlist.push_back(j); // add structure
185  else newlist.push_front(j); // add cluster centre at the front
186  }
187  }
188 
189  // add the new list to the clusterlist_
190  clusterlist_.push_back(newlist);
191  }
192 
193  // redistribute groups - i.e. take each structure, calculate the rms to each cluster centre.
194  for ( Size i = 1; i <= clusterlist_.size(); i++) {
195  for ( Size j = 1; j <= clusterlist_[i].size(); j++) {
196  Size lowrmsi=i;
197  Real lowrms=10000.0;
198  for ( Size m=1; m <= clusterlist_.size(); m++ ) {
199  Real rms;
200  rms = dist( clusterlist_[i][j - 1], // current structure
201  clusterlist_[m][0]); // clustercentre m
202  //std::cout << "C: " << i << " S: " << j << " rms " << m << " --> " << rms << " " << std::endl;
203 
204  if ( rms < lowrms ) {
205  lowrms = rms;
206  lowrmsi = m;
207  }
208  }
209  if ( lowrmsi != i ) { // is a different cluster centre closer to us than our current centre ?
210  // switch over;
211  clusterlist_[ lowrmsi ].push_back( clusterlist_[i][j-1] );
212  std::deque< Size >::iterator it = clusterlist_[i].begin();
213  it += j-1;
214  clusterlist_[ i ].erase( it );
215  }
216  } // j
217  } // i
218 } // compute()
219 
221  const std::pair< int, float > & p1, const std::pair< int, float > & p2 )
222 {
223  return p1.second < p2.second;
224 }
225 
226 
228  Size const offset( keep_center ? 1 : 0 );
229  for ( Size i=1; i<= clusterlist_.size(); i++ ) {
231  for ( Size j=1; j<= clusterlist_[i].size(); j++ ) {
232  Real score = all_energies[ clusterlist_[i][j-1] ];
233  cluster_energies.push_back( std::pair< int, Real > ( clusterlist_[i][j-1], score ) );
234  }
235  // dont sort in the first member (the cluster center!!)
236  std::sort( cluster_energies.begin() + offset, cluster_energies.end(), compareIndexEnergyPair );
237  clusterlist_[i].clear();
238  for ( Size j=1; j <= cluster_energies.size(); j++ ) clusterlist_[i].push_back( cluster_energies[j].first );
239  }
240 }
241 
242 // take first 'limit' from each cluster
244  if ( limit == 0 ) clusterlist_.clear();
245  for ( Size i=1; i <= clusterlist_.size(); i++ ) {
246  Cluster temp = clusterlist_[i];
247  clusterlist_[i].clear();
248  for ( Size j=1; ( j<= temp.size() ) && ( j<=limit ); j++ ) {
249  clusterlist_[i].push_back( temp[j-1] );
250  }
251  }
252 }
253 
254 
255 } // toolbox
256 } // protocols