Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sequence.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 /// @file Sequence.cc
11 /// @brief method definitions for Sequence class
12 /// @author James Thompson
13 
14 // Unit headers
16 
17 // Project headers
18 #include <core/types.hh>
19 #include <core/pose/Pose.hh>
20 #include <core/pose/PDBInfo.hh>
21 
22 // Utility headers
23 #include <utility/exit.hh>
24 
25 #include <iostream>
26 #include <string>
27 
28 #include <utility/vector1.fwd.hh>
29 #include <ObjexxFCL/format.hh>
30 
31 #include <utility/vector1.hh>
32 
33 
34 namespace core {
35 namespace sequence {
36 
37 using core::Size;
38 using std::string;
39 using utility::vector1;
40 
42  start_(1),
43  gap_char_('-')
44 {
45  if(! pose.pdb_info() ) {
46  id_ = pose.pdb_info()->name();
47  } else {
48  id_ = "unknown";
49  }
50  sequence( pose.sequence() );
51 }
52 
54 
56  SequenceOP new_seq_op( new Sequence( *this ) );
57  return new_seq_op;
58 }
59 
60 /// @brief initializes this sequence object from a file.
62  utility_exit_with_message(
63  "Error: class doesn't define method read_from_file!"
64  );
65 }
66 
67 
68 void Sequence::sequence( string sequence ) {
69  seq_ = sequence;
70 }
71 
72 void Sequence::id( std::string new_id ) {
73  id_ = new_id;
74 }
75 
76 void Sequence::start( core::Size new_start ) {
77  start_ = new_start;
78 }
79 
80 void Sequence::gap_char( char new_gap_char ) {
81  gap_char_ = new_gap_char;
82 }
83 
85  return start_;
86 }
87 
89  return id_;
90 }
91 
92 char Sequence::gap_char() const {
93  return gap_char_;
94 }
95 
97  return seq_.length();
98 }
99 
101  return ungapped_sequence().length();
102 } // ungapped_length
103 
105  string ungapped("");
106  for ( core::Size i = 1; i <= length(); ++i ) {
107  if ( !is_gap(i) ) ungapped += (*this)[i];
108  }
109 
110  return ungapped;
111 }
112 
113 string Sequence::sequence() const {
114  return seq_;
115 }
116 
117 char Sequence::operator[]( core::Size pos ) const {
118  runtime_assert( pos > 0 );
119  runtime_assert( pos <= seq_.size() );
120  return seq_.at( pos - 1 ); // strings are indexed by zero
121 }
122 
123 char Sequence::at( core::Size pos ) const {
124  //runtime_assert( pos > 0 );
125  //runtime_assert( pos <= seq_.size() );
126  //return seq_.at( pos - 1 ); // strings are indexed by zero
127  return( (*this)[pos] );
128 }
129 
130 void Sequence::insert_char( Size pos, char new_char ) {
131  runtime_assert( pos <= length() + 1 );
132 
133  std::string new_seq( "" );
134  for ( Size i = 0; i <= length() + 1; ++i ) {
135  if ( i == pos ) new_seq += new_char;
136  if ( i >= 1 && i <= length() ) new_seq += (*this)[i];
137  }
138 
139  sequence( new_seq );
140 } // insert_char
141 
143 
144  std::string new_seq( "" );
145  for ( core::Size i = 1; i <= length(); ++i ) {
146  if ( i != pos ) new_seq += (*this)[i];
147  }
148 
149  sequence( new_seq );
150 }
151 
153  insert_char( pos, gap_char() );
154 }
155 
156 void Sequence::append_char( char new_char ) {
157  seq_ += new_char;
158 } // insert_char
159 
161  seq_ += gap_char();
162 }
163 
164 bool Sequence::is_gap( core::Size pos ) const {
165  // define anything outside the length of the sequence as a gap.
166  if ( pos < 1 || pos > length() ) return true;
167 
168  return ( (*this)[pos] == gap_char_ );
169 }
170 
172  runtime_assert( idx <= length() );
173  runtime_assert( idx > 0 );
174 
175  if ( is_gap( idx ) ) return 0;
176 
177  core::Size num( start() );
178  for ( core::Size i = 1; i < idx; ++i ) {
179  if ( !is_gap(i) ) ++num;
180  }
181 
182  return num;
183 }
184 
185 void Sequence::read_data( std::istream & in ) {
186  std::string seq, name;
187  core::Size begin;
188 
189  in >> name >> begin >> seq;
190 
191  id ( name );
192  start ( begin );
193  sequence( seq );
194 }
195 
196 std::ostream & operator<<(
197  std::ostream & out,
198  const Sequence & seq
199 ) {
200  out << seq.to_string();
201  // out << std::endl;
202  return out;
203 } // operator <<
204 
205 std::istream & operator>> (
206  std::istream & in, Sequence & seq
207 ) {
208  seq.read_data( in );
209  return in;
210 }
211 
213  std::string retval("");
214  Size const id_width( 20 );
215  Size const start_width( 8 );
216 
217  retval += ObjexxFCL::fmt::A( id_width, id() );
218  retval += ObjexxFCL::fmt::I( start_width, start() );
219  retval += ' ' + sequence();
220  return retval;
221 }
222 
224  return "sequence";
225 }
226 
227 } // sequence
228 } // core