Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PeakFileFormat.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 FragmentSampler.cc
11 /// @brief ab-initio fragment assembly protocol for proteins
12 /// @detailed
13 /// Contains currently: Classic Abinitio
14 ///
15 ///
16 /// @author Oliver Lange
17 
18 // Unit Headers
24 
25 // Package Headers
27 
28 // Project Headers
29 #include <core/types.hh>
30 #include <core/chemical/AA.hh>
31 
32 // Utility headers
33 #include <ObjexxFCL/format.hh>
34 #include <ObjexxFCL/string.functions.hh>
35 
36 #include <utility/string_util.hh>
37 // #include <utility/excn/Exceptions.hh>
38 // #include <utility/vector1.fwd.hh>
39 // #include <utility/pointer/ReferenceCount.hh>
40 // #include <numeric/numeric.functions.hh>
41 // #include <basic/prof.hh>
42 #include <basic/Tracer.hh>
43 // #include <basic/options/option.hh>
44 // #include <basic/options/keys/abinitio.OptionKeys.gen.hh>
45 // #include <basic/options/keys/run.OptionKeys.gen.hh>
46 //#include <basic/options/keys/templates.OptionKeys.gen.hh>
47 
48 //// C++ headers
49 #include <cstdlib>
50 #include <string>
51 
52 #include <basic/options/option_macros.hh>
53 
54 #include <utility/vector1.hh>
55 
56 
57 OPT_1GRP_KEY( RealVector, noesy_weights, tolerances )
58 
59 bool protocols::noesy_assign::PeakFileFormat::options_registered_( false );
60 
61 void protocols::noesy_assign::PeakFileFormat::register_options() {
62  using namespace basic::options;
63  using namespace basic::options::OptionKeys;
64  if ( options_registered_ ) return;
65  NEW_OPT3( noesy_weights::tolerances, "if no #TOLERANCE is found use this for [indirect_H, direct_H, label ]", 0.04, 0.03, 0.3 );
66  options_registered_ = true;
67 }
68 
69 
70 static basic::Tracer tr("protocols.noesy_assign.io");
71 
72 namespace protocols {
73 namespace noesy_assign {
74 
75 using namespace core;
76 PeakFileFormat::PeakFileFormat() :
77  resonances_( NULL )
78 {
79  runtime_assert( options_registered_ );
80 }
81 
83  resonances_( res )
84 {
85  runtime_assert( options_registered_ );
86 }
87 
89 
90 Size const TOL_H_INDIRECT( 1 );
91 Size const TOL_H_DIRECT( 2 );
92 Size const TOL_LABEL( 3 );
93 
94 void PeakFileFormat::write_peak( std::ostream& os, Size ct, CrossPeak const& cp ) const {
95  std::ostringstream line_end;
96  using namespace ObjexxFCL::fmt;
97  line_end << " | ";
98  line_end << F( 5, 2, cp.cumulative_peak_volume() ) << " "
99  << F( 5, 2, cp.probability() ) << " "
100  << I( 3, (int)cp.quality_class() ) << " "
101  << A( 15, cp.quality_class_str() ) << " "
102  << F( 5, 2, cp.smallest_native_violation() ) << " ";
103 
104  line_end << " #d " << cp.distance_bound();
105  if ( cp.eliminated( false /*recompute*/, true /*do_not_compute*/) ) line_end << " #eliminated: " << cp.elimination_reason();
106 
107  os << ObjexxFCL::fmt::RJ( 6, ct ) << " ";
108  // cp.write_to_stream( os );
109  write_resonances( os, cp );
110  os << " 1 U";
111  write_strength( os, cp );
112  os << " e 0";
113  write_assignments( os, cp, line_end.str() );
114 }
115 
116 void PeakFileFormat::write_header( std::ostream& os ) {
117  Size const dim( col2proton_.size() );
120  //dimension 1 - label
121  if ( info1_->has_label() ) {
122  atom_names.push_back( ObjexxFCL::lowercased( info1_->label_atom_type() ) );
123  tolerances.push_back( info1_->label_tolerance() );
124  }
125 
126  if ( info2_->has_label() ) { //dimension 2 - label
127  atom_names.push_back( ObjexxFCL::uppercased( info2_->label_atom_type() ) );
128  tolerances.push_back( info2_->label_tolerance() );
129  }
130 
131  //dimension 2
132  atom_names.push_back( ObjexxFCL::uppercased( info2_->main_atom() ) );
133  tolerances.push_back( info2_->proton_tolerance() );
134 
135  //dimension 1
136  atom_names.push_back( ObjexxFCL::lowercased( info1_->main_atom() ) );
137  tolerances.push_back( info1_->proton_tolerance() );
138 
139  os << "# Number of dimensions " << dim << std::endl;
140  os << "#FILENAME " << filename() << std::endl;
141  std::string format_str = "xeasy" + utility::to_string( dim ) + "D";
142  os << "#FORMAT " << format_str << std::endl;
143  std::string cyana_str;
144  for ( Size ct = 1; ct <= atom_names.size(); ct++ ) {
145  os << "#INAME " << ct << " " << atom_names[ ct ] << std::endl;
146  if ( atom_names[ ct ].size()>1 ) {
147  cyana_str += "["+atom_names[ct]+"]";
148  } else {
149  cyana_str += atom_names[ ct ];
150  }
151  }
152  if ( info1_->fold_proton_resonance().is_folded() ) {
153  os << "#FOLD "<< dim << " " << info1_->fold_proton_resonance().start() << " " << info1_->fold_proton_resonance().end() << std::endl;
154  }
155  if ( info1_->fold_label_resonance().is_folded() ) {
156  os << "#FOLD "<< 1 << " " << info1_->fold_label_resonance().start() << " " << info1_->fold_label_resonance().end() << std::endl;
157  }
158 
159  if ( info2_->fold_proton_resonance().is_folded() ) {
160  os << "#FOLD "<< dim-1 << " " << info2_->fold_proton_resonance().start() << " " << info2_->fold_proton_resonance().end() << std::endl;
161  }
162  if ( info2_->fold_label_resonance().is_folded() ) {
163  os << "#FOLD "<< 2 << " " << info2_->fold_label_resonance().start() << " " << info2_->fold_label_resonance().end() << std::endl;
164  }
165  os << "#MAX_NOE_DIST " << info1_->max_noe_distance() << std::endl;
166  os << "#CYANAFORMAT " << cyana_str << std::endl;
167  os << "#TOLERANCE ";
168  for ( Size ct = 1; ct <= tolerances.size(); ct++ ) {
169  os << ObjexxFCL::fmt::RJ( 8, tolerances[ ct ] );
170  }
171  os << std::endl;
172 }
173 
175  return ( *info1_ == cp.info( 1 ) && *info2_ == cp.info( 2 ) );
176 }
177 
179  info1_ = new CrossPeakInfo( cp.info( 1 ) );
180  info2_ = new CrossPeakInfo( cp.info( 2 ) );
181  col2proton_.clear();
182  col2islabel_.clear();
183 
184  //dimension 1 - label
185  if ( info1_->has_label() ) {
186  col2proton_.push_back( 1 );
187  col2islabel_.push_back( true );
188  }
189 
190  //dimension 2 - label
191  if ( info2_->has_label() ) {
192  col2proton_.push_back( 2 );
193  col2islabel_.push_back( true );
194  }
195 
196  //dimension 2
197  col2proton_.push_back( 2 );
198  col2islabel_.push_back( false );
199 
200  //dimension 1
201  col2proton_.push_back( 1 );
202  col2islabel_.push_back( false );
203  set_filename( info1_->filename() );
204 
205 }
206 
207 
208 void PeakFileFormat::read_header( std::istream& is, std::string& next_line ) {
209  col2proton_.clear();
210  col2islabel_.clear();
211  column_labels_.clear();
212  info1_ = info2_ = NULL;
213  using namespace ObjexxFCL;
215  Size dim( 0 );
218  utility::vector1< core::Real > fold_starts( 4, 0);
219  utility::vector1< core::Real > fold_ends( 4, 0);
220 
221  Real max_noe_dist( params.calibration_max_noe_dist_ );
222 
223  bool HN_column_labels( false ); //true if we find a HC or HN ( instead of h vs H )
224  std::string line;
225  std::string cyana_string("none");
226  bool simnoesy( false );
227  while ( true ) {
228  std::istringstream line_stream;
229  if ( next_line.size() ) {
230  line=next_line;
231  next_line = "";
232  } else {
233 // char c='#';
234 // is.get(c);
235 // is.unget();
236 // // std::cout << "c: " << c << std::endl;
237 // if ( c!='#' ) break;
238  getline( is, line );
239  // std::cout << " line: " << line << std::endl;
240  }
241  tr.Trace << "header line: " << line << std::endl;
242  line_stream.str( line );
243  std::string tag;
244  line_stream >> tag;
245  if ( tag[0]!='#' ) { next_line = line; }
246 
247  if ( line.find( "Number of dimensions" ) != std::string::npos ) {
248  line_stream >> tag >> tag >> tag >> dim; //read Number of dimensions N
249  atom_names.resize( dim, "" );
250  tolerances.resize( dim, 0.0 );
251  } else if ( tag == "#FILENAME" ) {
253  line_stream >> filename;
254  set_filename( filename );
255  } else if ( tag == "#FORMAT" ) {
256  std::string format;
257  line_stream >> format;
258  std::string expected_format = "xeasy" + utility::to_string( dim ) + "D";
259  if ( format != expected_format ) {
260  tr.Warning << "[WARNING] Format inconsistent: " << expected_format << " found in file: " << format << std::endl;
261  }
262  } else if ( tag == "#INAME" ) {
263  Size index;
264  std::string name;
265  line_stream >> index;
266  if ( !line_stream.good() ) {
267  throw utility::excn::EXCN_BadInput(" problem reading peak file, column index and atom-name expected after key-word #INAME ");
268  }
269  line_stream >> name;
270  if ( name == "HN" || name =="HC" || name =="H1" || name =="1H" ) HN_column_labels = true;
271  if ( name == "H1" ) name = "H";
272  if ( name == "1H" ) name = "HN";
273  if ( name == "C13" || name == "13C" ) name = "C";
274  if ( name == "N15" || name == "15N" ) name = "N";
275  if ( name == "SIM" ) name = "NC";
276  if ( name == "sim" ) name = "nc";
277  if ( name == "CN" ) name = "NC" ;
278  if ( name == "cn" ) name = "nc" ;
279  if ( atom_names.size() < index ) {
280  tr.Error << "only " << dim << "D format; but " << index << " index found in #INAME line " << line << std::endl;
281  continue;
282  }
283  if ( name == "nc" || name =="NC" ) { simnoesy = true; }
284  if ( atom_names[ index ] != "" ) tr.Warning << "found index" << index << "in two different #INAME lines "<< std::endl;
285  atom_names[ index ] = name;
286  } else if ( tag == "#CYANAFORMAT" ) {
287  line_stream >> cyana_string;
288  // } else if (tag == "#SIMNOESY") {
289 // simnoesy = true;
290  } else if ( tag == "#FOLD" ) {
291  Size fold_dim;
292  Real start;
293  Real end;
294  line_stream >> fold_dim >> start >> end;
295  fold_starts[ fold_dim ]=start;
296  fold_ends[ fold_dim ]=end;
297  } else if ( tag == "#IGNORE_NEGATIVE_INTENSITY" ) {
299  } else if ( tag == "#MAX_NOE_DIST" ) {
300  if ( max_noe_dist < 0.01 ) {
301  tr.Warning << "MAX_NOE_DIST flag in peak-file ignored because of 0.0 in -noesy::calibration::max_noe_dist" << std::endl;
302  } else {
303  line_stream >> max_noe_dist;
304  }
305  } else if ( tag == "#TOLERANCE" ) {
306  for ( Size i = 1; i <= dim; i++ ) {
307  core::Real val;
308  line_stream >> val;
309  tolerances[ i ] = val;
310  }
311  break; //last line of header ...
312  } else if ( tag[0] == '#' ) {
313  tr.Warning << "[PeakFileFormat]: ignore header line: " << line << std::endl;
314  continue; //ignore unknown comments
315  } else {
316  tr.Error << "reading line as header: " << line << std::endl
317  << "expect TOLERANCE as last header entry " << std::endl;
318  //ERROR
319  }
320  } //while
321 
322  tr.Debug << "finished with header found " << dim << " dimensions" << std::endl;
323  using namespace basic::options;
324  using namespace basic::options::OptionKeys;
325  Real const default_tolerance_h( (dim < 4) ?
326  option[ noesy_weights::tolerances ][ TOL_H_INDIRECT ] :
327  option[ noesy_weights::tolerances ][ TOL_H_DIRECT ]
328  );
329  Real const default_tolerance_H( (dim < 3) ?
330  option[ noesy_weights::tolerances ][ TOL_H_INDIRECT ] :
331  option[ noesy_weights::tolerances ][ TOL_H_DIRECT ]
332  );
333  if ( dim < 2 ) {
334  throw utility::excn::EXCN_BadInput(" problem reading peak file, no or incomplete header ");
335  }
336 
337  // info2_ = new CrossPeakInfo( "N", 0.03, 0.5 );
338  col2proton_.resize( dim );
339  col2islabel_.resize( dim, false );
340  if ( HN_column_labels && cyana_string != "none" ) {
341  tr.Info << "use CYANA string to work out column order" << std::endl;
342  }
343  for ( Size i = 1; i<=dim; i++ ) {
344  if ( HN_column_labels && cyana_string != "none" ) {
345  if ( simnoesy ) throw utility::excn::EXCN_BadInput("cannot use HN for protons when SimNOESY ( i.e., NC for label atom name");
346  atom_names[ i ]=cyana_string[ i-1 ];
347  }
348  if ( atom_names[ i ] == "h"
349  || ( HN_column_labels && atom_names[ i ]=="H" )
350  || ( atom_names[ i ]=="H" && i==2 && dim==2 && col2proton_[ 1 ] == 1 )
351  ) { //indirect in 3D
352  col2proton_[ i ] = 2;
353  if ( tolerances[ i ]==0.0 ) tolerances[ i ]=default_tolerance_h;
354  if ( !info2_ ) {
355  info2_ = new CrossPeakInfo( uppercased( atom_names[ i ] ), "", max_noe_dist, tolerances[ i ], 0.0 );
356  } else {
357  info2_->set_proton( uppercased( atom_names[ i ] ), tolerances[ i ] );
358  }
359  } else if ( atom_names[ i ] == "H" || ( HN_column_labels && atom_names[ i ]=="HN" ) || ( HN_column_labels && atom_names[ i ]=="HC" )) {
360  col2proton_[ i ] = 1; //direct h in 3D
361  if ( HN_column_labels ) atom_names[ i ] = "h";
362  if ( tolerances[ i ]==0.0 ) tolerances[ i ]=default_tolerance_H;
363  if ( !info1_ ) {
364  info1_ = new CrossPeakInfo( atom_names[ i ], "", max_noe_dist, tolerances[ i ], 0.0 );
365  } else {
366  info1_->set_proton( atom_names[ i ], tolerances[ i ] );
367  }
368  } else if ( atom_names[ i ] == "c" || atom_names[ i ] == "n" || atom_names[ i ] == "nc" ) {
369  col2proton_[ i ] = 2;
370  col2islabel_[ i ] = true;
371  if ( tolerances[ i ]==0.0 ) tolerances[ i ]= option[ noesy_weights::tolerances ][ TOL_LABEL ];
372  if ( !info2_ ) {
373  info2_ = new CrossPeakInfo( "", uppercased( atom_names[ i ] ), max_noe_dist, 0.0, tolerances[ i ] );
374  } else {
375  info2_->set_label( uppercased( atom_names[ i ] ), tolerances[ i ] );
376  }
377  } else if ( atom_names[ i ] == "C" || atom_names[ i ] == "N" || atom_names[ i ] == "NC" ) {
378  col2proton_[ i ] = 1;
379  col2islabel_[ i ] = true;
380  if ( tolerances[ i ]==0.0 ) tolerances[ i ]= option[ noesy_weights::tolerances ][ TOL_LABEL ];
381  if ( !info1_ ) {
382  info1_ = new CrossPeakInfo( "", atom_names[ i ], max_noe_dist, 0.0, tolerances[ i ] );
383  } else {
384  info1_->set_label( atom_names[ i ], tolerances[ i ] );
385  }
386  }
387  }
388  for ( Size i = 1; i<=dim; i++ ) {
389  CrossPeakInfoOP info = col2proton_[ i ]==1 ? info1_ : info2_;
390  info->set_folding_window( fold_starts[ i ], fold_ends[ i ], col2islabel_[ i ] );
391  }
392 
393  if ( !info2_ || !info1_ ) {
394  throw utility::excn::EXCN_BadInput(" problem reading peak file, no or errorenous header ");
395  }
396 
397  tr.Debug << " cross-peak infos: " << *info1_ << " and " << *info2_ << std::endl;
398 
399  if ( col2proton_.size() == 3 ) { //only one label.. make sure that it is with proton 1
400  output_diagnosis( tr.Debug );
401  tr.Debug << " check if we need to swap columns " << std::endl;
402  Size ct_col_1( 0 );
403  for ( Size i=1; i<=3; i++ ) {
404  if ( col2proton_[ i ] == 1 ) ct_col_1++;
405  }
406  if ( ct_col_1 == 1 ) {//ok we have label on 2 switch numbering around...
407  for ( Size i=1; i<=3; i++ ) {
408  col2proton_[ i ] = col2proton_[ i ]==1 ? 2 : 1;
409  }
410  CrossPeakInfoOP info = info2_;
411  info2_=info1_;
412  info1_=info;
413  }
414  } //swapping of columns
415  info1_->set_filename( filename() );
416  info2_->set_filename( filename() );
417 }
418 
419 CrossPeakOP PeakFileFormat::read_peak( std::istream& is, std::string& next_line ) const {
420 
421  CrossPeakOP cp;
422  Size const ncol( col2proton_.size() );
423  //CrossPeak factory
424  runtime_assert( ncol >=2 && ncol <= 4 );
425  if ( ncol == 2 ) {
426  cp = new CrossPeak;
427  } else if ( ncol == 3 ) {
428  cp = new CrossPeak3D;
429  } else {
430  cp = new CrossPeak4D;
431  }
432  cp->set_info( 1, info1_ );
433  cp->set_info( 2, info2_ );
434 
435  if ( !next_line.size() ) {
436  getline( is, next_line );
437  }
438  tr.Trace << " next_line: " << next_line << std::endl;
439  std::istringstream line_stream( next_line );
440 
441  core::Size id;
442  line_stream >> id;
443  if ( !line_stream.good() ) {
444  return NULL;
445  }
446  next_line = ""; //now we are consuming this line
447  cp->set_peak_id( id );
448 
449  read_resonances( line_stream, *cp );
450 
451  //eat two weird columns
452  std::string tag;
453  line_stream >> tag; line_stream >> tag;
454 
455  ///precautions for different ways to finish the line: in particular with and without assignments
456  read_strength( line_stream, *cp );
457 
458  //read e 0 -- two columns
459  line_stream >> tag; line_stream >> tag;
460  read_assignments( is, line_stream, *cp, next_line );
461 
462  return cp;
463 }
464 
465 void PeakFileFormat::read_resonances( std::istream& is, CrossPeak &cp ) const {
466  Size const ncol( col2proton_.size() );
467  for ( Size icol=1; icol<=ncol; ++icol ) {
468  Real val;
469  Size iproton( col2proton_[ icol ] );
470  bool is_label( col2islabel_[ icol ] );
471  is >> val;
472  if ( !is.good() ) {
473  throw EXCN_FileFormat( "expected resonance value" );
474  }
475  if ( !is_label ) cp.proton( iproton ).set_freq( val );
476  else {
477  runtime_assert( cp.has_label( iproton ) );
478  cp.label( iproton ).set_freq( val );
479  }
480  }
481 }
482 
483 
484 void PeakFileFormat::write_resonances( std::ostream& os, CrossPeak const& cp ) const {
485  // cp.write_to_stream( os );
486  Size const ncol( col2proton_.size() );
487  runtime_assert( col2islabel_.size() == ncol );
488 
489  for ( Size icol=1; icol<=ncol; ++icol ) {
490  Real val;
491  Size iproton( col2proton_[ icol ] );
492  bool is_label( col2islabel_[ icol ] );
493  if ( !is_label ) val = cp.proton( iproton ).freq();
494  else {
495  runtime_assert( cp.has_label( iproton ) );
496  val = cp.label( iproton ).freq();
497  }
498  os << ObjexxFCL::fmt::F( 8, 3, val ) << " ";
499  }
500 }
501 
502 void PeakFileFormat::read_strength( std::istream& is, CrossPeak& cp ) const {
503  core::Real val;
504  is >> val;
505  val = val < 0 ? -val : val;
506  cp.set_volume( val );
507  is >> val; //read 0.00E+00
508 }
509 
510 void PeakFileFormat::write_strength( std::ostream& os, CrossPeak const& cp ) const {
511  os << ObjexxFCL::fmt::E( 10, 3, cp.volume() ) << " " << ObjexxFCL::fmt::E( 10, 3, 0.0 ) << " ";
512 }
513 
514 
515 void PeakFileFormat::read_assignments( std::istream& is, std::istream& rest_is, CrossPeak& cp, std::string& new_peak_line ) const {
516 
517  Size const ncol( col2proton_.size() );
518  runtime_assert( col2islabel_.size() == ncol );
519  core::Real weight( 0.0 );
520  std::string line;
521  if ( !getline( rest_is, line ) ) return;
522  tr.Trace << "rest_of_line: --" << line << "--" << std::endl;
523  bool rest( true );
524  bool first( true );
525  while ( true ) {
526  if ( !rest ) {
527  if ( !getline( is, line ) ) {
528  if ( first ) return;
529  std::ostringstream errstr;
530  errstr << cp;
531  throw EXCN_FileFormat( "expected assignment value for " + errstr.str() );
532  }
533  }
534  rest = false;
535  std::istringstream line_stream( line );
536  new_peak_line = line;
537  { //test if this next cross-peak
538  std::istringstream line_stream_test_new_peak( line );
539  Size dummyI;
540  Real dummyR;
541  std::string tag;
542  line_stream_test_new_peak >> dummyI >> dummyR >> dummyR >> dummyR >> dummyI >> tag;
543  if ( tag == "U" ) return;
544  }
545  { //test -- new header ?
546  std::istringstream line_stream_test_new_peak( line );
547  std::string tag;
548  line_stream_test_new_peak >> tag;
549  if ( tag[0]=='#' ) return;
550  }
551  { //test -- for a decimal point in third value...
552  std::istringstream line_stream_test_new_peak( line );
553  std::string tag1, tag2, tag3;
554  line_stream_test_new_peak >> tag1 >> tag2 >> tag3;
555  if ( tag3.find(".") != std::string::npos ) return;
556  }
557  //only assign if all spins are assigned.
558  Size vals[ 5 ];
559  for ( Size icol=1; icol<=ncol; ++icol ) {
560  Size val;
561  line_stream >> val;
562  if ( !line_stream ) {
563  if ( first ) {
564  new_peak_line = ""; //if first then it is maybe just a space at end of line...
565  return;
566  }
567  std::ostringstream errstr;
568  errstr << cp;
569  throw EXCN_FileFormat( "expected assignment value for " + errstr.str() );
570  } else {
571  if ( first ) tr.Trace << " read assignments: ";
572  }
573  first = false;
574  tr.Trace << val << " ";
575  if ( val == 0 ) {
576  line_stream.setstate( std::ios_base::failbit );
577  break;
578  }
579  vals[ icol ]=val;
580  }
581  tr.Trace << std::endl;
582  Size reorder[ 5 ];//spin1 spin2 label1 label2
583  if ( line_stream && !ignore_assignments() ) {
584  for ( Size icol=1; icol<=ncol; ++icol ) {
585  Size val = vals[ icol ];
586  Size iproton( col2proton_[ icol ] );
587  bool is_label( col2islabel_[ icol ] );
588  Size index = iproton + ( is_label ? 2 : 0 );
589  runtime_assert( !is_label || cp.has_label( iproton ) );
590  reorder[ index ] = val;
591  }
592  tr.Trace << " add assignment " << std::endl;
593  cp.add_full_assignment( reorder );
594  }
595  std::string tag;
596  line_stream >> tag;
597  if ( !line_stream.good() ) {
598  weight = 1.0; //no VC -- no partial weights.
599  break; //finish while loop
600  }
601  if ( tag == "#VC" ) {
602  core::Real w;
603  line_stream >> w;
604  weight += w;
605  line_stream >> tag;
606  } else weight += 1.0;
607  if ( !line_stream.good() ) break;
608  // if ( tag != "#QU" ) {
609 // throw EXCN_FileFormat( "expected #QU " );
610 // }
611 // core::Real qu, sup;
612 // line_stream >> qu;
613 // line_stream >> tag;
614 // if ( line_stream.good() && tag != "#SUP" ) {
615 // throw EXCN_FileFormat( "expected #SUP" );
616 // }
617  // line_stream >> sup;
618  }
619  new_peak_line = "";
620 }
621 
622 void PeakFileFormat::write_assignment_indent( std::ostream& os, CrossPeak const& cp ) const {
623  os << std::endl << " ";
624  if ( cp.has_label( 1 ) && cp.has_label( 2 ) ) os << " ";
625 }
626 
627 void PeakFileFormat::write_assignment( std::ostream& os, PeakAssignment const& pa ) const {
628  for ( Size icol=1; icol<=ncol(); ++icol ) {
629  Size val;
630  Size iproton( col2proton_[ icol ] );
631  bool is_label( col2islabel_[ icol ] );
632  if ( !is_label ) val = pa.resonance_id( iproton );
633  else {
634  val = pa.label_resonance_id( iproton );
635  }
636  if ( write_atom_names_ ) {
637  if ( !is_label ) os << pa.atom( iproton ) << " ";
638  else os << pa.label_atom( iproton ) << " ";
639  } else {
640  os << ObjexxFCL::fmt::RJ( 6, val ) << " ";
641  }
642  }
643 }
644 
645 void PeakFileFormat::write_assignment_stats( std::ostream& os, PeakAssignment& pa ) const {
646  Real val( pa.normalized_peak_volume() );
647  os << "#VC " << ObjexxFCL::fmt::F( 5, 3, val );
648  os << " #W ";
649  pa.dump_weights( os );
650 }
651 
652 void PeakFileFormat::write_assignments( std::ostream& os, CrossPeak const& cp, std::string const& line_end ) const {
653  Size assignments_written( 0 );
654 
655  if ( write_only_highest_VC() ) {
656  Real bestVC( -1 );
657  CrossPeak::PeakAssignments::const_iterator best_VC_it = cp.assignments().end();
658  for ( CrossPeak::PeakAssignments::const_iterator it = cp.assignments().begin(); it != cp.assignments().end(); ++it ) {
659  Real val( (*it)->normalized_peak_volume() );
660  if ( val > bestVC ) {
661  bestVC = val;
662  best_VC_it = it;
663  }
664  }
665  if ( best_VC_it != cp.assignments().end() ) {
666  write_assignment( os, **best_VC_it );
667  write_assignment_stats( os, **best_VC_it );
668  ++assignments_written;
669  os << line_end;
670  }
671  } else {
672  for ( CrossPeak::PeakAssignments::const_iterator it = cp.assignments().begin(); it != cp.assignments().end(); ++it ) {
673  Real val( (*it)->normalized_peak_volume() );
674  if ( val >= min_VC_to_write() ) {
675  ++assignments_written;
676  if ( assignments_written > 1 ) write_assignment_indent( os, cp );
677  write_assignment( os, **it );
678  write_assignment_stats( os, **it );
679  }
680  if ( assignments_written == 1 ) os << line_end;
681  }
682  }
683  if ( assignments_written == 0 ) write_nil_assignment( os );
684 }
685 
686 void PeakFileFormat::output_diagnosis( std::ostream& os ) const {
687  os << "columns: ";
688  for ( Size i = 1; i <= col2proton_.size(); i++ ) {
689  os << col2proton_[ i ] << " ";
690  }
691  os << "\nlabels: ";
692  for ( Size i = 1; i <= col2islabel_.size(); i++ ) {
693  os << col2islabel_[ i ] << " ";
694  }
695  os << "\ninfo1: " << info1_->label_atom_type() << " " << info1_->proton_tolerance() << " " << info1_->label_tolerance();
696  os << "\ninfo2: " << info2_->label_atom_type() << " " << info2_->proton_tolerance() << " " << info2_->label_tolerance();
697  os << std::endl;
698 }
699 
700 }
701 }