File: Chain.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 (378 lines) | stat: -rw-r--r-- 10,091 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* 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 CGAL_DSR_PDB_CHAIN_H
#define CGAL_DSR_PDB_CHAIN_H

#include <CGAL/PDB/basic.h>
#include <CGAL/PDB/Point.h>
#include <CGAL/PDB/Atom.h>
#include <CGAL/PDB/Monomer.h>
#include <CGAL/PDB/small_map.h>
#include <iostream>
#include <vector>
#include <iterator>
#include <cassert>
#include <CGAL/PDB/internal/dummies.h>
#include <CGAL/PDB/internal/Nested_iterator.h>
#include <boost/tuple/tuple.hpp>

namespace CGAL { namespace PDB {

/*!
  \file Chain.h 

  This file contains the class Chain.
*/

class Model;
  
//! A class representing a single chain of a protein.  
/*!
    
 */
class Chain {
  friend class Model;

public:

  //! The type for storing residue indices in the PDB
  typedef CGAL::Label<Chain> Monomer_key;

  struct IR_tag{};
  typedef CGAL::Label<IR_tag> IR_key;

  //! The value_type returned by the Monomer_iterators
  class Monomer_pair: public small_map_value_type<Monomer_key, Monomer> {
    typedef small_map_value_type<Monomer_key, Monomer> P;
  public:
    Monomer_pair(Monomer_key k, Monomer a): P(k,a){}
    Monomer_pair(Monomer_key k): P(k){}
    Monomer_pair(){}
    const Monomer &monomer() const {return P::data();}
    Monomer &monomer() {return P::data();}
  };

  typedef small_map<Monomer_pair> Container;
  
  //! Default
  Chain();

  CGAL_ITERATOR(Monomer, monomer, Container::const_iterator,
                Container::iterator,
                residues_.begin(),
		residues_.end());


  //! A unique identified of an atom in the chain
  class Atom_key {
    Monomer_key mk_;
    Monomer::Atom_key ak_;
   public:
    Atom_key(Monomer_key mk, Monomer::Atom_key ak): mk_(mk), ak_(ak){}
    Atom_key(){}
    operator Monomer::Atom_key() const {
      return ak_;
    }
    operator Monomer_key() const {
      return mk_;
    }
    Monomer::Atom_key atom_key() const {
      return ak_;
    }
    Monomer_key monomer_key() const {
      return mk_;
    }
  };


  //! 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; 

  CGAL_INSERT(Monomer, insert_internal(k,m));

  class Atom_pair: public small_map_value_type<Atom_key, Atom> {
      Atom_key index_;
      Atom *atom_;
    public:
      Atom_pair(Atom_key f, Atom* s): index_(f), atom_(s){}
      Atom_key key() const {return index_;}
      Atom &atom() const {return *atom_;}
      Atom_pair():atom_(NULL){}
    };

  class Atom_const_pair: public small_map_value_type<Atom_key, Atom> {
    Atom_key index_;
    const Atom *atom_;
  public:
    Atom_const_pair(Atom_key f, const Atom* s): index_(f), atom_(s){}
    Atom_key key() const {return index_;}
    const Atom &atom() const {return *atom_;}
    Atom_const_pair():atom_(NULL){}
  };
private:

  //! \cond
  Monomers::iterator insert_internal(Monomer_key k, const Monomer &m);

  struct Iterator_traits {
    typedef Monomers Outer;
    typedef Monomer::Atoms Inner;
    typedef Atom_pair value_type;
    struct Get_inner{
      Inner operator()(Outer::iterator it) const {
	return it->monomer().atoms();
      }
    };
    struct Make_value{
      value_type operator()(Outer::iterator oit,
                            Inner::iterator iit) const {
	return value_type(Atom_key(oit->key(), iit->key()), &iit->atom());
      }
    };
  };


  struct Iterator_const_traits {
    typedef Monomer_consts Outer;
    typedef Monomer::Atom_consts Inner;
    typedef Atom_const_pair value_type;
    struct Get_inner{
      Inner operator()(Outer::iterator it) const {
	return it->monomer().atoms();
      }
    };
    struct Make_value{
      value_type operator()(Outer::iterator oit,
                            Inner::iterator iit) const {
	return value_type(Atom_key(oit->key(), iit->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(residues_.begin(), residues_.end()),
                boost::make_iterator_range(residues_.end(), residues_.end()));

  
  //! This is non-const time.
  unsigned int number_of_atoms() const;

  //! \cond
  void swap_with(Chain &o);

  class Bond_it {
    friend class Chain;
  public:
    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++() {
      if (between_) {
	between_=false;
	++rit_;
	if (rit_ != rend_) {
	  ait_= rit_->monomer().bonds().begin();
	  make_bond();
	}
      } else {
	++ait_;
	if (ait_== rit_->monomer().bonds().end()) {
          Monomer_consts::iterator nrit= rit_;
	  ++nrit;
	  between_=true;
	  if (nrit == rend_ 
	      || nrit->key().index() != rit_->key().index()+1){
	    operator++();
	  } else {
	    make_bond();
	  }
	} else {
	  make_bond();
	}
      }
      return *this;
    }
    bool operator==(const Bond_it& o) const {
      if (between_ != o.between_) return false;
      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(Monomer_consts r): rit_(r.begin()), rend_(r.end()), 
				       between_(false){
      if (!r.empty()) {
	ait_= rit_->monomer().bonds().begin();
	make_bond();
      }
    }
  private:
    void copy_from(const Bond_it &o) {
      rit_= o.rit_;
      rend_= o.rend_;
      if (rit_!= rend_) {
	ait_= o.ait_;
	ret_= o.ret_;
      }
      between_= o.between_;
    }

    void make_bond() {
      if (between_) {
	CGAL_assertion(rit_+1 != rend_);
	ret_= Bond(Bond_endpoint(Atom_key(rit_->key(), rit_->monomer().back_atom()->key()),
				 &rit_->monomer().back_atom()->atom()),
		   Bond_endpoint(Atom_key((rit_+1)->key(), (rit_+1)->monomer().front_atom()->key()),
				 &(rit_+1)->monomer().front_atom()->atom()));
      } else {
	ret_= Bond(Bond_endpoint(Atom_key(rit_->key(), ait_->first.key()),
				 &ait_->first.atom()),
		   Bond_endpoint(Atom_key(rit_->key(), ait_->second.key()),
				 &ait_->second.atom()));
      } 
    }

    Monomer_consts::iterator rit_, rend_;
    Monomer::Bonds::const_iterator ait_;
    bool between_;
    Bond ret_;
  };
  //! \endcond

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

  //! This is non-const time.
  unsigned int number_of_bonds() const;
  
  /* //! Return the spherical_coordinates of the atom relative to its parent.

     Spherical_point spherical_coordinates(Atom::Index atom_index) const;

     //! Return the parent of an atom with regards to computing spherical coordinates.
     Atom::Index parent_atom(Atom::Index atom_index) const;*/


  //! The sequence of residue types.
  std::vector<Monomer::Type> sequence() const;

  //! Write as part of pdb file.
  int  write(char chain, int start_index, std::ostream &out) const;

  //! Write a pdb file. 
  /*!
    See check_protein.cpp for an example of using this to write a
    pdb file.
  */
  void write_pdb(std::ostream &out) const;

  //! Dump as human readable.
  void dump(std::ostream &out) const;

  //! Dump as human readable.
  std::ostream& write(std::ostream &out) const{dump(out); return out;}

  CGAL_FIND(Monomer, residues_.find(k), residues_.end());

  /*const Atom &atom(unsigned int i) const {
    for (Const_residues_iterator it = residues_begin(); it != residues_.end(); ++it){
	
    }
    }*/


  //! Return whether bonds have been computed for this protein. 
  bool has_bonds() const;

  //! Set whether the protein has bonds or not.
  void set_has_bonds(bool tf);

  CGAL_GETSET(std::string, name, name_);
private:
    
  //unsigned int residue_offset_of_atom_key(Atom::Index i) const;

  //unsigned int residue_offset(Residue::Index i) const;
    
  Container residues_;
  std::vector<std::string> header_;
  //static Residue dummy_residue_;
  typedef small_map<small_map_value_type<Monomer_key,
					 small_map<small_map_value_type<IR_key, Monomer> > > > IR_Map;
  IR_Map insert_residues_;
  std::string name_;
};


CGAL_SWAP(Chain)
CGAL_OUTPUT(Chain)




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

#endif