29 #include <ObjexxFCL/char.functions.hh>
30 #include <ObjexxFCL/string.functions.hh>
40 #include <basic/Tracer.hh>
41 #include <utility/io/izstream.hh>
42 #include <utility/io/ozstream.hh>
43 #include <utility/excn/Exceptions.hh>
45 #include <basic/options/option.hh>
46 #include <basic/options/keys/in.OptionKeys.gen.hh>
47 #include <basic/options/keys/out.OptionKeys.gen.hh>
48 #include <basic/prof.hh>
50 #include <numeric/random/random.hh>
51 #include <numeric/random/reservoir_sample.hh>
53 #include <utility/vector1.hh>
60 static basic::Tracer
tr(
"core.io.silent");
61 static numeric::random::RandomGenerator
RG(21458);
79 tag_list.push_back( it->decoy_tag() );
90 utility::io::izstream data( filename.c_str() );
92 utility_exit_with_message(
93 "ERROR: Unable to open silent_input file: '" + filename +
"'"
98 getline( data, line );
99 getline( data, line );
101 while( getline(data,line) ) {
102 if ( line.substr(0,7) ==
"SCORE: " && line.substr(8,20).find(
"score" ) == std::string::npos ) {
103 std::istringstream l( line );
106 while ( !l.fail() ) {
109 tags_in_file.push_back( current_word );
125 utility::io::izstream data(
filename().c_str() );
126 if ( !data.good() ) {
130 tr.Info <<
"file: " <<
filename() <<
" not found" << std::endl;
135 bool return_first=
false;
136 bool return_last=
false;
137 if ( mode ==
"first" ) {
139 }
else if ( mode ==
"last" ) {
141 }
else if ( mode !=
"all" ) {
142 utility_exit_with_message(
"illegal mode selection in SilentFileData::matched_tag(). available modes are first, last, and all " );
151 while( getline(data,line) ) {
153 if ( line.substr(0,7) ==
"SCORE: " && line.substr(8,20).find(
"score" ) == std::string::npos ) {
154 tr.Info <<
"reading the lines in file:\n" << line << std::endl;
156 std::istringstream l( line );
157 while ( !l.fail() ) {
160 tr.Info <<
"current_tag:" << current_tag << std::endl;
162 if ( current_tag.find(expression)!=std::string::npos ) {
164 if ( !return_last ) {
165 tags_in_file.push_back( current_tag );
166 }
else final_tag = current_tag;
167 if ( return_first )
return true;
181 if ( final_tag.find( expression ) != std::string::npos ) {
182 tags_in_file.push_back( final_tag );
186 return tags_in_file.size();
199 using ObjexxFCL::string_of;
208 new_tag = old_tag +
"_" + string_of( n_tries );
212 tr.Warning <<
"renamed tag " << old_tag <<
" to " << new_tag;
213 tr.Warning <<
" (SilentStruct with " << old_tag;
214 tr.Warning <<
" already exists!)" << std::endl;
216 new_struct->decoy_tag( new_tag );
226 std::string const & new_tag( new_struct->decoy_tag() );
230 tr.Debug <<
"added structure with tag " << new_tag << std::endl;
251 Structure_Map::iterator map_iter;
253 silent_structs.push_back( map_iter->second );
257 using ObjexxFCL::string_of;
260 for ( ss_iter = silent_structs.begin(); ss_iter != silent_structs.end(); ++ss_iter ) {
261 std::string new_tag = (*ss_iter)->decoy_tag().substr(0,2) + string_of( count );
262 (*ss_iter)->decoy_tag( new_tag );
273 bool success =
false;
275 using namespace basic::options;
276 using namespace basic::options::OptionKeys;
278 utility::io::ozstream output;
280 std::stringstream header;
298 output.open_append_if_existed( filename, header );
301 if ( !output.good() ){
302 utility_exit_with_message(
"Could not make "+filename );
316 bool bWriteScoreOnly)
const {
326 bool success =
false;
346 using namespace basic::options;
347 using namespace basic::options::OptionKeys;
349 option[ out::file::silent_print_all_score_headers ]()
359 if ( !bWriteScoreOnly ) {
374 out <<
"# " << line <<
'\n';
378 return static_cast< int > (
begin()->nres() );
382 runtime_assert(
has_tag( tag ) );
391 utility::io::ozstream output;
394 std::stringstream header;
395 begin()->print_header( header );
396 output.open_append_if_existed( filename, header );
400 it_end =
end(); iter != it_end; ++iter ) {
415 using namespace basic::options;
416 using namespace basic::options::OptionKeys;
420 !option[ OptionKeys::in::file::silent_read_through_errors ]()
422 if ( !success && !option[ OptionKeys::in::file::silent_read_through_errors ]() ) {
423 throw utility::excn::EXCN_BadInput(
"no success reading silent file "+filename);
432 bool throw_exception_on_bad_structs
437 using namespace basic::options;
438 using namespace basic::options::OptionKeys;
444 if ( option[ in::file::silent_select_random ].user() ) {
445 core::Size const n_wanted( option[ in::file::silent_select_random ]() );
446 runtime_assert( n_wanted > 0.0 );
447 runtime_assert( n_wanted <= all_tags.size() );
449 tags_wanted = numeric::random::reservoir_sample< string >(
450 all_tags, n_wanted,
RG
452 tr.Debug <<
"selected " << tags_wanted.size() <<
" tags from " <<
453 all_tags.size() << std::endl;
456 if ( option[ in::file::silent_select_range_start ]() >= 0 ) {
459 int range_start = ( option[ in::file::silent_select_range_start ]() *
460 option[ in::file::silent_select_range_mul ] )
462 int range_end = range_start + option[ in::file::silent_select_range_len ]();
465 if( option[ in::file::silent_select_range_len ]() < 0 ) range_end = all_tags.size();
468 range_end = std::min( (
int)range_end, (
int)all_tags.size() );
470 tr <<
"Reading range: " << range_start <<
" <= i < " << range_end << std::endl;
471 for(
core::Size position = range_start; position < range_end; position++){
472 tags_wanted.push_back( all_tags[position+1] );
477 if( all_tags.size() == 0 )
return true;
479 bool success =
_read_file( filename, tags_wanted, throw_exception_on_bad_structs );
489 using namespace basic::options;
490 using namespace basic::options::OptionKeys;
495 !option[ OptionKeys::in::file::silent_read_through_errors ]()
497 if ( !success && !option[ OptionKeys::in::file::silent_read_through_errors ]() ) {
498 throw utility::excn::EXCN_BadInput(
"no success reading silent file "+filename);
508 bool throw_exception_on_bad_structs
510 bool success =
false;
511 utility::io::izstream data( filename.c_str() );
512 if ( !data.good() ) {
513 if ( throw_exception_on_bad_structs ) {
514 throw utility::excn::EXCN_FileNotFound( filename );
516 utility_exit_with_message(
517 "ERROR:: Unable to open silent_input file: '" +
522 success =
read_stream(data,tags,throw_exception_on_bad_structs,filename);
530 bool throw_exception_on_bad_structs,
535 std::set<std::string> tagset(tags.begin(), tags.end());
537 bool success =
false;
539 std::getline( data, sequence_line );
540 if ( sequence_line.substr(0,9) !=
"SEQUENCE:" ) {
541 tr.Error <<
"bad format in first line of silent file " << filename <<
" (function read_stream):"
543 tr.Error << sequence_line << std::endl;
550 getline( data, score_line );
551 if ( score_line.substr(0,7) !=
"SCORE: " ) {
552 tr.Error <<
"bad format in second line of silent file " << filename <<
" (function read_stream):" << std::endl;
553 tr.Error << score_line << std::endl;
558 getline( data, line );
576 mylines.push_back( sequence_line );
577 mylines.push_back( score_line );
579 if ( tagset.size() )
tr.Info <<
"Reading " << tagset.size()
580 <<
" structures from " << filename
582 else tr.Info <<
"Reading all structures from " << filename << std::endl;
583 bool all_tags = tagset.size() == 0;
586 bool line_ok(
true );
588 if ( line.substr(0,1) ==
"#" ) {
589 tr.Info <<
"Read a comment line from " << filename << std::endl;
590 tr.Info << line << std::endl;
592 line_ok = getline(data,line);
596 if ( line.substr(0,10) ==
"SEQUENCE: " && mylines.size() == 2 && mylines[ 1 ].substr(0,10) ==
"SEQUENCE: " ) {
600 if ( ( line.substr(0,7) ==
"SCORE: " || line.substr(0,10) ==
"SEQUENCE: " ) && mylines.size() > 3 ) {
601 bool init_good = tmp_struct->init_from_lines( mylines, *
this );
602 if ( !init_good && throw_exception_on_bad_structs ) {
603 throw utility::excn::EXCN_BadInput(
604 "failure to read decoy "+tmp_struct->decoy_tag()+
612 PROF_START( basic::SILENT_READ_TAG_TEST );
619 bool good_tag =
false;
621 std::set<std::string>::iterator tag_it = tagset.find(tmp_struct->decoy_tag());
622 if ( tag_it != tagset.end() ) {
624 tagset.erase(tag_it);
627 bool add_struct( init_good && ( all_tags || good_tag ));
628 PROF_STOP( basic::SILENT_READ_TAG_TEST );
642 mylines.reserve( tmp_struct->sequence().size() + 10 );
647 mylines.push_back( line );
649 line_ok = getline(data,line);
658 bool init_good = tmp_struct->init_from_lines( mylines, *
this );
659 if ( !init_good && throw_exception_on_bad_structs )
throw utility::excn::EXCN_BadInput(
"failure to read decoy "+tmp_struct->decoy_tag()+
" from silent-file " +
filename);
661 bool good_tag =
false;
662 std::set<std::string>::iterator tag_it = tagset.find(tmp_struct->decoy_tag());
663 if ( tag_it != tagset.end() ) {
667 bool add_struct( init_good && ( all_tags || good_tag ));
673 tr.Info <<
"Finished reading " <<
structure_map_.size() <<
" structures from "
674 << filename << std::endl;
692 using namespace basic::options;
693 using namespace basic::options::OptionKeys;
695 bool changed(
false );
697 if ( line.substr(0,6) !=
"SCORE:" ) {
698 if (( line.find(
"BINARY_SILENTFILE" ) != std::string::npos ) || ( line.find (
"BINARY SILENTFILE" ) != std::string::npos )) {
699 if ( line.find(
"RNA" ) != std::string::npos || option[ in::file::residue_type_set ]() ==
"rna" ) {
705 }
else if ( header && ( line.find(
"RNA" ) != std::string::npos || option[ in::file::residue_type_set ]() ==
"rna") &&
709 }
else if (( line.find(
"PROTEIN_SILENTFILE" ) != std::string::npos ) || ( line.find(
"PROTEIN SILENTFILE" ) != std::string::npos ) ) {
710 if ( line.find(
"SINGLE") != std::string::npos ) {
718 if ( changed )
tr.Trace <<
"found new silent_struct_type_ " <<
silent_struct_type_ <<
" from line " << line << std::endl;
726 std::istringstream l( line );
731 for (
Size n = 0; n < sequence.size(); n++ ) {
732 if ( sequence[n] !=
'a' && sequence[n] !=
'g' && sequence[n] !=
'c' && sequence[n] !=
'u' ) {
733 is_rna =
false;
break;
779 Real const score_fraction
782 runtime_assert( score_fraction <= 0 );
783 runtime_assert( score_fraction >= -1 );
789 scores.push_back( iter->get_energy(
"score" ) );
792 std::sort( scores.begin(), scores.end() );
793 std::reverse( scores.begin(), scores.end() );
794 Size const idx( static_cast< Size > ( -1 * score_fraction * scores.size() ) );
795 Real const boundary( *( scores.begin() + idx ) );
796 tr.Debug <<
"reverse_score_filter: " << std::endl;
797 tr.Debug <<
"filtering for decoys with score worse than " << boundary << std::endl;
801 if ( !iter->has_energy(
"score" ) ) {
803 "Error: can't find score in SilentStuct!"
805 msg +=
"\nSilentStruct object has scores:\n";
808 std::ostringstream mystream;
809 iter->print_score_header( mystream );
810 mystream << std::endl;
811 iter->print_scores( mystream );
812 mystream << std::endl;
813 msg += mystream.str();
815 utility_exit_with_message( msg );
818 if ( iter->get_energy(
"score" ) > boundary ) {
819 new_structure_map_[ iter->decoy_tag() ] = *iter;
828 Real const score_fraction
830 if ( score_fraction < 0 ) {
834 runtime_assert( score_fraction >= 0 );
835 runtime_assert( score_fraction <= 1 );
841 scores.push_back( iter->get_energy(
"score" ) );
843 std::sort( scores.begin(), scores.end() );
844 Size const idx( static_cast< Size > (score_fraction * scores.size() ) );
845 Real const boundary( *( scores.begin() + idx ) );
846 tr.Debug <<
"score_filter: " << std::endl;
847 tr.Debug <<
"filtering for decoys with score worse than " << boundary << std::endl;
851 if ( !iter->has_energy(
"score" ) ) {
853 "Error: can't find score in SilentStuct!"
855 msg +=
"\nSilentStruct object has scores:\n";
858 std::ostringstream mystream;
859 iter->print_score_header( mystream );
860 mystream << std::endl;
861 iter->print_scores( mystream );
862 mystream << std::endl;
863 msg += mystream.str();
865 utility_exit_with_message( msg );
868 if ( iter->get_energy(
"score" ) < boundary ) {
869 new_structure_map_[ iter->decoy_tag() ] = *iter;
885 using namespace core::io::silent;
888 typedef std::list< std::pair< Real, SilentStructOP > > ScoreTagList;
889 ScoreTagList score_tag_list;
891 Real const & silent_score = (*iter)->get_energy(
"score" );
892 score_tag_list.push_back( std::make_pair( silent_score, *iter ) );
895 score_tag_list.sort();
899 for ( ScoreTagList::const_iterator iter = score_tag_list.begin();
900 iter != score_tag_list.end(); ++iter ) {
909 ObjexxFCL::lead_zero_string_of( count++,6 )+
910 silent_struct_op->decoy_tag();
912 new_structure_map_[ new_tag ] = silent_struct_op;