File: Model.h

package info (click to toggle)
cgal 3.6.1-2
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 62,184 kB
  • ctags: 95,782
  • sloc: cpp: 453,758; ansic: 96,821; sh: 226; makefile: 120; xml: 2
file content (327 lines) | stat: -rw-r--r-- 9,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Copyright 2004
   Stanford University

   This file is part of the DSR PDB Library.

   The DSR PDB Library is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   The DSR PDB Library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with the DSR PDB Library; see the file LICENSE.LGPL.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA. */

#ifndef DSR_MODEL_H_
#define DSR_MODEL_H_
#include <CGAL/PDB/Chain.h>
#include <CGAL/PDB/Heterogen.h>
#include <vector>
#include <string>
#include <boost/tuple/tuple.hpp>

namespace CGAL { namespace PDB {

class PDB;

class Heterogen_key {
  typedef Heterogen_key This;
  std::string name_;
  int num_;
public:
  Heterogen_key(std::string name,
                int num): name_(name), num_(num) {
  }
  Heterogen_key(): num_(-1){}
  CGAL_COMPARISONS2(name_, num_);
  const std::string &name() const {
    return name_;
  }
  int number() const {return num_;}
};

//! A class representing a single model from a PDB file.
/*!
  You can iterator through the chains and soon the heterogens. 
*/
class Model {
  typedef Model This;
  friend class PDB;
public:
  CGAL_SMALL_MAP_VALUE_TYPE(Chain_pair, CGAL::Label<Model>, Chain, chain); 
  CGAL_SMALL_MAP_VALUE_TYPE(Heterogen_pair, CGAL::PDB::Heterogen_key,
                            Heterogen, heterogen); 
private:
  typedef small_map<Chain_pair> ChainsMap; 
  typedef small_map<Heterogen_pair> HeterogensMap; 
public:
  //! Construct an empty model
  Model();

  typedef CGAL::PDB::Heterogen_key Heterogen_key;
  typedef CGAL::Label<Model> Chain_key;

  void swap_with(Model &o);

  //! write to a pdb
  void write(int model_index, std::ostream &out) const;
  
  //! write screen
  std::ostream& write(std::ostream &out) const{
    write(0,out);
    return out;
  }


  //! Iterator through the chains
  CGAL_ITERATOR(Chain, chain, ChainsMap::const_iterator, ChainsMap::iterator, 
                chains_.begin(), chains_.end());


  //! get the chain identified by k
  CGAL_FIND(Chain, chains_.find(k), chains_.end());

  CGAL_INSERT(Chain, chains_.insert(ChainsMap::value_type(k, m)););

  //! An iterator through the heterogen objects.
  CGAL_ITERATOR(Heterogen, heterogen, 
                HeterogensMap::const_iterator, HeterogensMap::iterator,
                heterogens_.begin(),
                heterogens_.end());

  //! get the chain identified by k
  CGAL_FIND(Heterogen, heterogens_.find(k), heterogens_.end());

  CGAL_INSERT(Heterogen, heterogens_.insert(HeterogensMap::value_type(k, m)););


  //! A unique identified of an atom in the Model
  struct Atom_key: public boost::tuple<Chain_key, Chain::Monomer_key, 
				       Monomer::Atom_key>{
    typedef boost::tuple<Chain_key, Chain::Monomer_key, Monomer::Atom_key> P;
    Atom_key(Chain_key ck, Chain::Monomer_key mk, Monomer::Atom_key at): P(ck, mk, at){}
    Atom_key(){}
    operator Chain_key() const {
      return chain_key();
    }
    operator Monomer::Atom_key() const {
      return atom_key();
    }
    operator Chain::Monomer_key() const {
      return monomer_key();
    }
    Monomer::Atom_key atom_key() const {
      return P::get<2>();
    }
    Chain::Monomer_key monomer_key() const {
      return P::get<1>();
    }
    Chain_key chain_key() const {
      return P::get<0>();
    }
  };



  //! The endpoint of a bond
  class Bond_endpoint {
    const Atom *atom_;
    Atom_key ak_;
    friend class Bond_it;
 public:
    Bond_endpoint(Atom_key ak, const Atom *atom): atom_(atom), ak_(ak){}    
    Bond_endpoint():atom_(NULL){}
    const Atom &atom() const {return *atom_;}
    Atom_key key() const {return ak_;}
  };


  //! A chemical bond within the protein
  typedef std::pair<Bond_endpoint, Bond_endpoint> Bond; 
  class Atom_iterator_value_type {
      Atom_key index_;
      Atom *atom_;
  public:
      Atom_iterator_value_type(Atom_key f, Atom* s): index_(f), atom_(s){}
      Atom_key key() const {return index_;}
      Atom &atom() const {return *atom_;}
      Atom_iterator_value_type():atom_(NULL){}
    };
  class Atom_const_iterator_value_type {
      Atom_key index_;
      const Atom *atom_;
  public:
      Atom_const_iterator_value_type(Atom_key f, const Atom* s): index_(f), atom_(s){}
      Atom_key key() const {return index_;}
      const Atom &atom() const {return *atom_;}
      Atom_const_iterator_value_type():atom_(NULL){}
    };
protected:
  //! \cond
 struct Iterator_traits {
   typedef Chains Outer;
   typedef Chain::Atoms Inner;
   typedef Atom_iterator_value_type value_type;
    struct Get_inner {
      Inner operator()(Outer::iterator it) const {
	return it->chain().atoms();
      }
    };
    struct Make_value{
      value_type operator()(Outer::iterator oit, Inner::iterator iit) const {
	return value_type(Atom_key(oit->key(), iit->key().monomer_key(),
				   iit->key().atom_key()), &iit->atom());
      }
    };
  };


  struct Iterator_const_traits {
    typedef Chain_consts Outer;
    typedef Chain::Atom_consts Inner;
    typedef Atom_const_iterator_value_type value_type;
   struct Get_inner{
     Inner operator()(Outer::iterator it) const {
       return it->chain().atoms();
      }
    };
    struct Make_value{
      value_type operator()(Outer::iterator oit, Inner::iterator iit) const {
	return value_type(Atom_key(oit->key(), iit->key().monomer_key(),
				   iit->key().atom_key()), &iit->atom());
      }
    };
  };
  //! \endcond
public:
 //! An iterator to iterate through all the atoms of the protein  
  CGAL_ITERATOR(Atom, atom, 
                internal::Nested_iterator<Iterator_const_traits >,
                internal::Nested_iterator<Iterator_traits >,
                boost::make_iterator_range(chains_.begin(), chains_.end()),
                boost::make_iterator_range(chains_.end(), chains_.end()));

  //! \cond
  class Bond_it {
    friend class Model;
  public:
    //! The value_type is a CGAL::PDB::Chain::Bond
    typedef Bond value_type;
    typedef std::forward_iterator_tag iterator_category;
    typedef std::size_t difference_type;
    typedef const Bond& reference;
    typedef const Bond* pointer;
    
    reference operator*() const {
      return ret_;
    }
    pointer operator->() const {
      return &ret_;
    }
    Bond_it operator++() {
      ++ait_;
      while (ait_== rit_->chain().bonds().end()) {
	++rit_;
	if (rit_!= rend_) {
	  ait_= rit_->chain().bonds().begin();
	} else {
	  return *this;
	}
      }
      make_bond();
      return *this;
    }
    bool operator==(const Bond_it& o) const {
      if (rit_ == rend_) return rit_==o.rit_;
      else return rit_== o.rit_ && ait_ == o.ait_;
    }
  bool operator!=(const Bond_it& o) const {
      return !operator==(o);
    }
    
    Bond_it(){}

    CGAL_COPY_CONSTRUCTOR(Bond_it);

    Bond_it(Chain_consts r): rit_(r.begin()), rend_(r.end()){
      if (!r.empty()) {
	ait_= rit_->chain().bonds().begin();
	make_bond();
      }
    }

  
  protected:
    void copy_from(const Bond_it &o) {
      rit_= o.rit_;
      rend_= o.rend_;
      if (rit_!= rend_) {
	ait_= o.ait_;
	ret_= o.ret_;
      }
    }

   
    void make_bond() {
      ret_= Bond(Bond_endpoint(Atom_key(rit_->key(), 
					ait_->first.key().monomer_key(),
					ait_->first.key().atom_key()),
			       &ait_->first.atom()),
		 Bond_endpoint(Atom_key(rit_->key(), 
					ait_->second.key().monomer_key(),
					ait_->second.key().atom_key()),
			       &ait_->second.atom())); 
    }

    Chain_consts::iterator rit_, rend_;
    Chain::Bonds::iterator ait_;
    Bond ret_;
  };
  //! \endcond

  CGAL_CONST_ITERATOR(Bond, bond, Bond_it,
                      boost::make_iterator_range(chains_.begin(),
                                                 chains_.end()),
                      boost::make_iterator_range(chains_.end(), 
                                                 chains_.end()));
 

private:
  void process_line(const char *c);
  void process_atom(const char *c);
  void process_hetatom(const char *c);
  void add_hetatom(int numscan, int snum, char name[], char alt, 
                   char resname[], char chain, int resnum,
                   char insertion_residue_code, 
                   float x, float y, float z,
                   float occupancy, float tempFactor,
                   char segID[], char element[], char charge[]);

  std::vector<std::string> extra_;
  ChainsMap chains_;
  HeterogensMap heterogens_;
};

CGAL_SWAP(Model)

CGAL_OUTPUT(Model)

//! Assign unique indices to all atoms in the Model, starting at optional start value
/*!
  This returns the next unused index. 
*/
inline int index_atoms(const Model &c, int start=0) {
  CGAL_PDB_FOREACH(std::iterator_traits<Model::Chain_consts::iterator>::reference cc, c.chains()) {
    start= index_atoms(cc.chain(), start);
  }
  return start;
}

}}
#endif