14 #include <platform/types.hh>
33 #include <boost/uuid/sha1.hpp>
41 unsigned int start=0, i=0;
42 while ( start < s.size() ) {
44 std::string
add(s.begin()+
start, s.begin()+i);
45 if (
add.size() != 0 ) r.push_back(
add );
49 if ( i == s.size() ) {
50 std::string
add(s.begin()+
start, s.begin()+i);
51 if (
add.size() != 0 ) r.push_back(
add );
59 std::istringstream
ss( s );
61 while ( !ss.fail() ) {
64 if ( ss.fail() )
break;
73 std::vector< std::string > r;
75 while ( start < s.size() ) {
76 if ( s[i] ==
'\n' || s[i] ==
'\r' ) {
77 r.push_back( std::string(s.begin()+
start, s.begin()+i) );
81 if ( i == s.size() ) {
82 r.push_back( std::string(s.begin()+
start, s.begin()+i) );
87 if ( r[i].
size() == 0 ) r.erase( r.begin()+i );
94 std::ostringstream os;
97 for ( ; begin != s.end(); ++
begin ) {
98 os<< connector<< *
begin;
103 std::string
join(std::vector<std::string>
const &
s, std::string
const & connector){
104 std::ostringstream os;
107 for ( ; begin != s.end(); ++
begin ) {
108 os<< connector<< *
begin;
113 std::string
replace_spaces(std::string
const & string_w_spaces, std::string
const & replacement){
116 return join(pieces, replacement);
121 std::list<std::string> r;
122 unsigned int start=0, i=0;
123 while ( start < s.size() ) {
125 std::string
add(s.begin()+
start, s.begin()+i);
126 if (
add.size() != 0 ) r.push_back(
add );
130 if ( i == s.size() ) {
131 std::string
add(s.begin()+
start, s.begin()+i);
132 if (
add.size() != 0 ) r.push_back(
add );
141 std::set<std::string> r;
142 unsigned int start=0, i=0;
143 while ( start < s.size() ) {
145 std::string
add(s.begin()+
start, s.begin()+i);
146 if (
add.size() != 0 ) r.insert(
add );
150 if ( i == s.size() ) {
151 std::string
add(s.begin()+
start, s.begin()+i);
152 if (
add.size() != 0 ) r.insert(
add );
164 while ( j != std::string::npos ) {
165 j = in.find( splitchar, i );
166 std::string
const part = in.substr(i,j-i);
167 parts.push_back( part );
179 while ( j != std::string::npos ) {
180 j = in.find( splitchar, i );
181 std::string
const part = in.substr(i,j-i);
182 if ( part.size() > 0 ) {
183 parts.push_back( part );
197 while ( j != std::string::npos ) {
199 j = in.find_first_of( splitchars, i );
200 parts.push_back( in.substr(i,j-i) );
210 std::stringstream
ss( st );
221 std::stringstream
ss( st );
232 std::stringstream
ss( st );
243 std::stringstream
ss( st );
254 std::ostringstream converter;
255 converter <<
std::fixed << std::setprecision(decimal_places) << num;
256 return converter.str();
273 std::stringstream
ss(x);
280 std::string
const space(
" " );
282 std::size_t
const s1_start( s1.find_first_not_of( space ) );
283 std::size_t
const s1_end( s1.find_last_not_of( space ) );
284 std::size_t
const s2_start( s2.find_first_not_of( space ) );
285 std::size_t
const s2_end( s2.find_last_not_of( space ) );
287 std::size_t
const s1_len( s1_end - s1_start + 1 );
288 std::size_t
const s2_len( s2_end - s2_start + 1 );
290 return ( ( s1_len == s2_len ) && ( s1.compare( s1_start, s1_len, s2, s2_start, s2_len ) == 0 ) );
293 bool startswith(std::string
const & haystack, std::string
const & needle)
295 if ( haystack.length() < needle.length() )
return false;
296 else return ( haystack.compare(0, needle.length(), needle) == 0 );
299 bool endswith(std::string
const & haystack, std::string
const & needle)
301 if ( haystack.length() < needle.length() )
return false;
302 else return ( haystack.compare(haystack.size()-needle.size(),needle.size(),needle) == 0 );
308 std::ostringstream os;
310 os << line << std::endl;
312 out.append( os.str());
315 void trim( std::string &
s,
const std::string & drop)
317 std::string r = s.erase( s.find_last_not_of(drop)+1 );
318 r.erase( 0, r.find_first_not_of(drop) );
323 strip( std::string
const &
s, std::string
const & drop )
325 std::string copystr( s );
326 trim( copystr, drop );
333 std::ostringstream converter;
334 converter << std::setfill( pad_with ) << std::setw( newlen ) <<
std::right <<
s;
336 return converter.str();
342 std::ostringstream converter;
343 converter << std::setfill( pad_with ) << std::setw( newlen ) <<
std::left <<
s;
345 return converter.str();
350 std::string
strip(std::string
const & source,
char c)
355 if ( *
p == c ) begin =
p+1;
360 if ( *(
p-1) == c ) end =
p-1;
364 return std::string(begin, end);
394 char current = input[i];
395 if ( std::isdigit(current,loc) || current ==
'-' || current ==
'+' || current ==
'E' ||current==
'e' ) {
410 if ( ! textstream ) {
414 while (
getline(textstream, line) ) {
415 text.push_back(line +
"\n");
416 strsize += line.size() + 1;
421 alltext.reserve( strsize );
422 for (
unsigned int ii = 1;
ii <= text.size(); ++
ii ) {
443 const std::string
start(
"${");
444 const std::string
end(
"}");
449 start_position = input.find(start);
450 if ( start_position != std::string::npos ) {
452 if ( start_position == std::string::npos ) {
458 std::string env_name = input.substr(start_position+2,env_length-2);
459 char * env_value = getenv(env_name.c_str());
464 input.replace(start_position, env_length+1,env_value);
475 unsigned int digest[5];
476 boost::uuids::detail::sha1 hasher;
477 hasher.process_bytes(input_string.c_str(),input_string.size());
481 std::stringstream output_hash;
483 hasher.get_digest(digest);
485 for (
int i = 0; i < 5; ++i ) {
486 const char* tmp =
reinterpret_cast<char*
>(digest);
487 hash[i*4] = tmp[i*4+3];
488 hash[i*4+1] = tmp[i*4+2];
489 hash[i*4+2] = tmp[i*4+1];
490 hash[i*4+3] = tmp[i*4];
493 output_hash << std::hex;
495 for (
int i = 0; i < 20 ; ++i ) {
496 output_hash << ((hash[i] & 0x000000F0) >> 4) << (hash[i] & 0x0000000F);
499 return output_hash.str();
509 char const delimiter ){
511 for (
platform::Size n = 0; n < res_vector.size(); n++ ) chains.push_back(
' ' );
522 char const delimiter ){
524 using namespace ObjexxFCL;
525 std::string
tag =
"";
527 if ( res_vector.size() == 0 )
return tag;
530 int start_segment = res_vector[1];
531 int last_res = res_vector[1];
532 int last_chain = chain_vector[1];
536 if ( res_vector[n] != last_res+1 || chain_vector[n] != last_chain ) {
537 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
538 chains_for_segments.push_back( last_chain );
539 start_segment = res_vector[n];
541 last_res = res_vector[n];
542 last_chain = chain_vector[n];
544 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
545 chains_for_segments.push_back( last_chain );
547 for (
platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
548 if ( n > 1 )
tag += delimiter;
549 std::pair< int, int >
const & segment = res_vector_segments[n];
550 if ( chains_for_segments[n] !=
'\0' &&
551 chains_for_segments[n] !=
' ' &&
552 chains_for_segments[n] !=
'_' )
tag += std::string(1,chains_for_segments[n]) +
":";
553 if ( segment.first == segment.second ) {
568 char const delimiter ){
570 using namespace ObjexxFCL;
571 std::string
tag =
"";
573 if ( res_vector.size() == 0 )
return tag;
576 int start_segment = res_vector[1];
577 int last_res = res_vector[1];
578 std::string last_segid = segid_vector[1];
584 if ( res_vector[n] != last_res+1 || segid_vector[n] != last_segid ) {
585 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
586 segids_for_segments.push_back( last_segid );
587 start_segment = res_vector[n];
589 last_res = res_vector[n];
590 last_segid = segid_vector[n];
592 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
593 segids_for_segments.push_back( last_segid );
595 for (
platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
596 if ( n > 1 )
tag += delimiter;
597 std::pair< int, int >
const & segment = res_vector_segments[n];
598 tag += segids_for_segments[n] +
":";
599 if ( segment.first == segment.second ) {
613 using namespace ObjexxFCL;
614 std::string
tag =
"";
617 if ( n > 1 )
tag +=
" ";
629 std::pair< std::vector< int >, std::vector< char > >
633 std::vector< int >
resnum;
634 std::vector< char >
chain;
636 std::string s_nocommas =
replace_in( s,
",",
" " );
640 if ( !string_is_ok )
break;
642 return std::make_pair( resnum,chain );
645 std::pair< std::vector< int >, std::vector< char > >
651 std::pair< std::vector< int >, std::vector< std::string > >
655 std::vector< int >
resnum;
656 std::vector< std::string > segid;
658 std::string s_nocommas =
replace_in( s,
",",
" " );
669 while ( s_nocommas.size() > 0 && s_nocommas[0]==
' ' ) s_nocommas = s_nocommas.substr(1);
671 std::string
tag = s_nocommas.substr( 0, s_nocommas.find_first_of(
':' )+1 );
672 s_nocommas = s_nocommas.substr( s_nocommas.find_first_of(
':' ) +1 );
675 if ( space_pos == std::string::npos ) {
677 tags.push_back( tag );
680 tag += s_nocommas.substr( 0, space_pos );
681 tags.push_back( tag );
682 s_nocommas = s_nocommas.substr( space_pos +1 );
688 if ( !string_is_ok )
break;
690 return std::make_pair( resnum, segid );
696 std::vector< int > &
resnum,
697 std::vector< char > &
chains ){
698 bool string_is_ok(
false );
699 std::vector< int > resnum_from_tag;
701 std::string
const numerical(
"-0123456789");
703 if ( tag.size() == 0 )
return true;
705 size_t found_colon = tag.find(
":" );
706 if ( found_colon == std::string::npos ) {
707 if ( numerical.find( tag[0] ) == std::string::npos ) {
714 if ( found_colon == 0 ) {
717 }
else if ( found_colon == 1 ) {
726 resnum.push_back( resnum_from_tag[ n ] );
727 chains.push_back( chain );
735 std::vector< int > &
resnum,
736 std::vector< std::string > & segids ){
737 bool string_is_ok(
false );
738 std::vector< int > resnum_from_tag;
739 std::string segid(
"" );
741 if ( tag.size() == 0 )
return true;
743 size_t found_colon = tag.find(
":" );
745 if ( found_colon == std::string::npos || found_colon > 4 )
return false;
747 for (
size_t n = found_colon; n < 4; n++ ) {
750 if ( found_colon > 0 ) segid += tag.substr(0,found_colon);
756 resnum.push_back( resnum_from_tag[ n ] );
757 segids.push_back( segid );
769 replace_in( std::string
const & name_in, std::string
const & find_string, std::string
const & replace_string ){
770 std::string
name = name_in;
774 size_t pos = name.find( find_string );
775 while ( pos != std::string::npos ) {
776 name = name.replace( pos, find_string.size(), replace_string );
781 pos = name.find( find_string, pos + replace_string.size() );
void trim(std::string &s, const std::string &drop)
Remove any charachters in "drop" from the front and back of the string. Use strip() for the value-ret...
#define utility_exit_with_message(m)
Exit with file + line + message.
BooleanOptionKey const in
StringOptionKey const chain
void slurp(std::istream &in, std::string &out)
BooleanOptionKey const text("options:table:text")
platform::Size string2Size(std::string st)
convert a string to a Size, returns numeric::get_undefined_size() on failure
FileName & extension(std::string const &ext_a)
Extension assignment.
platform::Size get_undefined_size()
Get a numeric value for Size that represents an "undefined" value.
utility::keys::KeyLookup< KeyType >::const_iterator const_iterator
Key collection iterators.
IntegerOptionKey const right
float string2float(std::string st)
convert a string to a float, returns -1 on failure
FileVectorOptionKey const s
void string2uint(const std::string &x, unsigned int *y)
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
StringVectorOptionKey const s1
utility::vector1< std::string > string_split_multi_delim(std::string const &in, std::string splitchars)
bool endswith(std::string const &haystack, std::string const &needle)
True iff haystack ends with needle.
std::pair< std::vector< int >, std::vector< std::string > > get_resnum_and_segid(std::string const &s, bool &string_is_ok)
converts string like "1-3 20-22" or "A:1-5 B:20-22" to vectors containing resnums and chains...
#define runtime_assert(_Expression)
Assert that the condition holds. Evaluated for both debug and release builds.
FileName & path(std::string const &path_a)
Path assignment.
char space()
Space Character.
std::string Real2string(platform::Real num, std::size_t const decimal_places)
convert a Real to string at a number of decimal places, optionally pad left.
File name class supporting Windows and UN*X/Linux format names.
std::set< std::string > split_to_set(const std::string &s)
split given std::string to a set using ' ' symbol.
common derived classes for thrown exceptions
IntegerOptionKey const left
utility::vector1< std::string > string_split(std::string const &in, char splitchar)
utility::vector1< std::string > split_whitespace(const std::string &s)
split given std::string using whitespace as a separator. Unlike string_split_multi_delim(), any group of mixed whitespace counts only as a single seperator.
bool get_resnum_and_segid_from_one_tag(std::string const &tag, std::vector< int > &resnum, std::vector< std::string > &segids)
helper function for get_resnum_and_chain
std::string replace_spaces(std::string const &string_w_spaces, std::string const &replacement)
replace space separations in a string with a connector such as '_'
utility::vector1< std::string > split(const std::string &s)
split given std::string using ' ' symbol.
std::string file_basename(const std::string &full_path)
platform::Real string2Real(std::string st)
convert a string to a Real, returns numeric::get_undefined_real() on failure
std::string join(utility::vector1< std::string > const &s, std::string const &connector)
combine strings with anything
FileOptionKey const fixed
std::string string_to_sha1(std::string const &input_string)
bool trimmed_compare(std::string const &s1, std::string const &s2)
compares two strings ignoring leading and trailing spaces
std::list< std::string > split_to_list(const std::string &s)
split given std::string using ' ' symbol.
BooleanOptionKey const ss
void add(xyzTriple< T > const &a, xyzTriple< T > const &b, xyzTriple< T > &r)
Add: xyzTriple + xyzTriple.
Brief utility classes for numeric usage.
void close()
Close the ifstream and reset the state.
std::string pad_left(std::string s, platform::Size const newlen, char pad_with)
Add char to the left of the string.
IntegerOptionKey const end
Program exit functions and macros.
std::vector< std::string > split_by_newlines(std::string const &s)
std::string make_segtag_with_dashes(utility::vector1< int > res_vector, utility::vector1< std::string > segid_vector, char const delimiter)
izstream: Input file stream wrapper for uncompressed and compressed files
std::string pad_right(std::string s, platform::Size const newlen, char pad_with)
Add char to the right of a string.
Input file stream wrapper for uncompressed and compressed files.
std::string make_tag_with_dashes(utility::vector1< int > res_vector, char const delimiter)
Compactifies vectors of ints: 1 2 3 9 10 11 to "1-3 9-11".
File name class supporting Windows and UN*X/Linux format names.
bool get_resnum_and_chain_from_one_tag(std::string const &tag, std::vector< int > &resnum, std::vector< char > &chains)
helper function for get_resnum_and_chain
std::string fmt_real(platform::Real num, platform::Size pad_left_n, std::size_t const decimal_places)
convert a Real to a string, padding left with spaces until total number of char on left is equal to p...
bool is_string_numeric(std::string const &input)
std::string replace_in(std::string const &name_in, std::string const &find_string, std::string const &replace_string)
Generate new string from 'source' by replacing all occurrences of 'from' to 'to' string.
platform::Real get_undefined_real()
Get a numeric value for Real that represents an "undefined" value.
std::string filename(const std::string &path)
super::const_iterator const_iterator
PathVectorOptionKey const path
list resnum
if line_edit[13:14]=='P': #Nucleic acid? Skip.
utility::vector1< std::string > string_split_simple(std::string const &in, char splitchar)
split to vector1< std::string > using arbitrary split character, but no empty strings (closer to pyth...
std::vector< int > ints_of(std::string const &s)
ints of a string (e.g., allowing "5-8" to represent "5 6 7 8").
int string2int(std::string st)
convert a string to an int, returns -1 on failure
std::string pathname(const std::string &path)
FileName & base(std::string const &base_a)
Base assignment.
std::string replace_environment_variables(std::string input)
find all environment variables with the form ${VARIABLE} and replace with the contents of that enviro...
vector1: std::vector with 1-based indexing
bool startswith(std::string const &haystack, std::string const &needle)
True iff haystack starts with needle.
platform::Size get_num_digits(platform::Size value)
utility::keys::lookup::begin< KeyType > const begin
std::string file_contents(std::string const &file_name)
Read the entire contents of a file into a string. All end-of-line characters are replaced by "\n"...
rule< Scanner, string_closure::context_t > name
std::string make_tag(utility::vector1< int > res_vector)
IntegerOptionKey const start
std::string string_of(Fstring const &s)
string of an Fstring
Some std::string helper functions.
std::pair< std::vector< int >, std::vector< char > > get_resnum_and_chain(std::string const &s, bool &string_is_ok)
converts string like "1-3 20-22" or "A:1-5 B:20-22" to vectors containing resnums and chains...
rule< Scanner, tag_closure::context_t > tag
StringVectorOptionKey const s2
BooleanOptionKey const out
StringVectorOptionKey const tags
std::string strip(std::string const &s, std::string const &drop)
Return a copy of the string with leading and trailing characters removed Any charachters in drop will...