RNAMake
residue.h
1 //
2 // residue.h
3 // RNAMake
4 //
5 // Created by Joseph Yesselman on 1/25/15.
6 // Copyright (c) 2015 Joseph Yesselman. All rights reserved.
7 //
8 
9 #ifndef __RNAMake__residue__
10 #define __RNAMake__residue__
11 
12 #include <stdio.h>
13 
14 //RNAMake Headers
15 #include "base/types.h"
16 #include "util/uuid.h"
17 #include "structure/atom.h"
18 #include "structure/residue_type.h"
19 #include "structure/residue_type_set.h"
20 
21 Point
22 center(AtomOPs const &);
23 
32 enum BeadType { PHOS = 0, SUGAR = 1, BASE = 2 };
33 
34 
35 /*
36  * Exception for residues
37  */
38 class ResidueException : public std::runtime_error {
39 public:
44  ResidueException(String const & message):
45  std::runtime_error(message)
46  {}
47 };
48 
49 
55 class Bead {
56 public:
60  Bead():
61  center_(Point(0,0,0)),
62  btype_(BeadType(0))
63  {}
64 
70  inline
72  Point const & center,
73  BeadType const btype):
74  center_ ( center ),
75  btype_ ( btype )
76  {}
77 
82  inline
84  Bead const & b):
85  center_(b.center_),
86  btype_(b.btype_)
87  {}
88 
89 public: //accessors
90 
94  inline
95  Point
96  center() const { return center_; }
97 
101  inline
102  BeadType
103  btype() const { return btype_; }
104 
105 private:
109  Point center_;
110 
114  BeadType btype_;
115 
116 };
117 
118 typedef std::vector<Bead> Beads;
119 
158 class Residue {
159 public:
160 
170  ResidueType const & rtype,
171  String const & name,
172  int const & num,
173  String const & chain_id,
174  String const & i_code):
175  rtype_ ( rtype ),
176  name_ ( name ),
177  num_ ( num ),
178  chain_id_ ( chain_id ),
179  i_code_ ( i_code ),
180  atoms_ ( AtomOPs() ),
181  uuid_ ( Uuid() )
182  {}
183 
189  Residue const & r):
190  rtype_(r.rtype_),
191  name_(r.name_),
192  num_(r.num_),
193  chain_id_(r.chain_id_),
194  i_code_(r.i_code_),
195  atoms_ ( AtomOPs(r.atoms().size()) ),
196  uuid_(r.uuid_) {
197  int i = -1;
198  for(auto const & a : r.atoms_) {
199  i++;
200  if(a == nullptr) { continue; }
201  atoms_[i] = std::make_shared<Atom>(*a);
202  }
203  }
204 
211  String const & s,
212  ResidueTypeSet const & rts) {
213 
214  Strings spl = split_str_by_delimiter(s, ",");
215  rtype_ = rts.get_rtype_by_resname(spl[0]);
216  name_ = spl[1];
217  num_ = std::stoi(spl[2]);
218  chain_id_ = spl[3];
219  i_code_ = spl[4];
220  atoms_ = AtomOPs();
221  auto atoms = AtomOPs();
222  int i = 5;
223  while ( i < spl.size() ) {
224  if( spl[i].length() == 1) {
225  atoms.push_back( nullptr );
226  }
227  else {
228  atoms.push_back(std::make_shared<Atom>(spl[i]));
229  }
230  i++;
231  }
233  }
234 
235 
239  ~Residue() {}
240 
241 public:
247  void
248  setup_atoms(
249  AtomOPs &);
250 
251 public:
252 
264  inline
265  AtomOP
266  const &
268  String const & name) const {
269  int index = rtype_.atom_pos_by_name(name);
270  if( index == -1) {
271  throw ResidueException("cannot find atom name " + name);
272  }
273  return atoms_[index];
274  }
275 
284  inline
285  int
287  Residue const & res,
288  float cutoff = 3.0) const {
289  String o3 = "O3'", p = "P";
290 
291  // 5' to 3'
292  AtomOP o3_atom = get_atom(o3), p_atom = res.get_atom(p);
293  if(o3_atom != nullptr && p_atom != nullptr) {
294  if( o3_atom->coords().distance(p_atom->coords()) < cutoff) {
295  return 1;
296  }
297  }
298 
299  // 3' to 5'
300  o3_atom = res.get_atom(o3); p_atom = get_atom(p);
301  if(o3_atom != nullptr && p_atom != nullptr) {
302  if( o3_atom->coords().distance(p_atom->coords()) < cutoff) {
303  return -1;
304  }
305  }
306 
307  return 0;
308  }
309 
316  void
318  uuid_ = Uuid();
319  }
320 
321 
339  Beads
340  get_beads() const;
341 
357  String
358  to_str() const;
359 
365  inline
366  String
367  to_pdb_str(int & acount) {
368  return to_pdb_str(acount, -1, "");
369  }
370 
392  String
393  to_pdb_str(
394  int &,
395  int,
396  String const &) const;
397 
402  void
403  to_pdb(String const);
404 
409  bool
410  operator ==(const Residue& r) const {
411  return uuid_ == r.uuid_;
412  }
413 
414 
415 public: // setters
420  inline
421  void
422  num(int nnum) { num_ = nnum; }
423 
428  inline
429  void
430  chain_id(String const & nchain_id) { chain_id_ = nchain_id; }
431 
437  inline
438  void
439  uuid(Uuid const & uuid) { uuid_ = uuid; }
440 
441 
442 public: // getters
443 
447  inline
448  String const &
449  name() const { return name_; }
450 
454  inline
455  String const &
456  chain_id() const { return chain_id_; }
457 
461  inline
462  String const &
463  i_code() const { return i_code_; }
464 
468  inline
469  int const &
470  num() const { return num_; }
471 
475  inline
476  String const &
477  short_name() const { return rtype_.short_name(); }
478 
482  inline
483  AtomOPs const &
484  atoms() const { return atoms_; }
485 
489  inline
490  Uuid const &
491  uuid() const { return uuid_; }
492 
493 private:
497  ResidueType rtype_;
498 
502  String name_;
503 
507  String chain_id_;
508 
512  String i_code_;
513 
517  int num_;
518 
522  AtomOPs atoms_;
523 
527  Uuid uuid_;
528 
529 };
530 
534 typedef std::shared_ptr<Residue> ResidueOP;
535 
539 typedef std::vector<ResidueOP> ResidueOPs;
540 
541 
542 
543 
544 #endif /* defined(__RNAMake__residue__) */
Beads get_beads() const
Definition: residue.cc:50
Bead(Bead const &b)
Definition: residue.h:83
Residue(ResidueType const &rtype, String const &name, int const &num, String const &chain_id, String const &i_code)
Definition: residue.h:169
Definition: residue_type_set.h:15
ResidueException(String const &message)
Definition: residue.h:44
Definition: residue.h:38
Definition: residue.h:55
Uuid const & uuid() const
Definition: residue.h:491
String to_pdb_str(int &acount)
Definition: residue.h:367
String const & i_code() const
Definition: residue.h:463
String const & name() const
Definition: residue.h:449
Bead()
Definition: residue.h:60
Definition: residue.h:158
Definition: residue_type.h:19
AtomOP const & get_atom(String const &name) const
Definition: residue.h:267
bool operator==(const Residue &r) const
Definition: residue.h:410
void num(int nnum)
Definition: residue.h:422
void uuid(Uuid const &uuid)
Definition: residue.h:439
String to_str() const
Definition: residue.cc:69
int const & num() const
Definition: residue.h:470
Residue(String const &s, ResidueTypeSet const &rts)
Definition: residue.h:210
~Residue()
Definition: residue.h:239
void to_pdb(String const)
Definition: residue.cc:106
AtomOPs const & atoms() const
Definition: residue.h:484
Bead(Point const &center, BeadType const btype)
Definition: residue.h:71
int connected_to(Residue const &res, float cutoff=3.0) const
Definition: residue.h:286
BeadType btype() const
Definition: residue.h:103
void setup_atoms(AtomOPs &)
Definition: residue.cc:31
Residue(Residue const &r)
Definition: residue.h:188
String const & short_name() const
Definition: residue.h:477
void chain_id(String const &nchain_id)
Definition: residue.h:430
Point center() const
Definition: residue.h:96
String const & chain_id() const
Definition: residue.h:456
void new_uuid()
Definition: residue.h:317