14 #include <platform/types.hh>
32 #include <boost/uuid/sha1.hpp>
39 using std::stringstream;
43 ifstream in(filename.c_str());
46 ss <<
"The specified file " << filename
47 <<
"does not exist or lacks sufficient permissions";
54 (*contents) +=line +
"\n";
62 unsigned int start=0, i=0;
63 while ( start < s.size() ) {
65 std::string
add(s.begin()+
start, s.begin()+i);
66 if (
add.size() != 0 ) r.push_back(
add );
70 if ( i == s.size() ) {
71 std::string
add(s.begin()+
start, s.begin()+i);
72 if (
add.size() != 0 ) r.push_back(
add );
80 std::istringstream ss( s );
82 while ( !ss.fail() ) {
85 if ( ss.fail() )
break;
92 std::ostringstream os;
95 for ( ; begin != s.end(); ++
begin ) {
96 os<< connector<< *
begin;
101 std::string
join(std::vector<std::string>
const &
s, std::string
const & connector){
102 std::ostringstream os;
105 for ( ; begin != s.end(); ++
begin ) {
106 os<< connector<< *
begin;
111 std::string
replace_spaces(std::string
const & string_w_spaces, std::string
const & replacement){
114 return join(pieces, replacement);
119 std::list<std::string> r;
120 unsigned int start=0, i=0;
121 while ( start < s.size() ) {
123 std::string
add(s.begin()+
start, s.begin()+i);
124 if (
add.size() != 0 ) r.push_back(
add );
128 if ( i == s.size() ) {
129 std::string
add(s.begin()+
start, s.begin()+i);
130 if (
add.size() != 0 ) r.push_back(
add );
139 std::set<std::string> r;
140 unsigned int start=0, i=0;
141 while ( start < s.size() ) {
143 std::string
add(s.begin()+
start, s.begin()+i);
144 if (
add.size() != 0 ) r.insert(
add );
148 if ( i == s.size() ) {
149 std::string
add(s.begin()+
start, s.begin()+i);
150 if (
add.size() != 0 ) r.insert(
add );
162 while ( j != std::string::npos ) {
163 j = in.find( splitchar, i );
164 std::string
const part = in.substr(i,j-i);
165 parts.push_back( part );
177 while ( j != std::string::npos ) {
178 j = in.find( splitchar, i );
179 std::string
const part = in.substr(i,j-i);
180 if ( part.size() > 0 ) {
181 parts.push_back( part );
195 while ( j != std::string::npos ) {
197 j = in.find_first_of( splitchars, i );
198 parts.push_back( in.substr(i,j-i) );
208 std::stringstream ss( st );
219 std::stringstream ss( st );
230 std::stringstream ss( st );
241 std::stringstream ss( st );
254 std::stringstream ss(x);
261 std::string
const space(
" " );
263 std::size_t
const s1_start( s1.find_first_not_of( space ) );
264 std::size_t
const s1_end( s1.find_last_not_of( space ) );
265 std::size_t
const s2_start( s2.find_first_not_of( space ) );
266 std::size_t
const s2_end( s2.find_last_not_of( space ) );
268 std::size_t
const s1_len( s1_end - s1_start + 1 );
269 std::size_t
const s2_len( s2_end - s2_start + 1 );
271 return ( ( s1_len == s2_len ) && ( s1.compare( s1_start, s1_len, s2, s2_start, s2_len ) == 0 ) );
274 bool startswith(std::string
const & haystack, std::string
const & needle)
276 if ( haystack.length() < needle.length() )
return false;
277 else return ( haystack.compare(0, needle.length(), needle) == 0 );
280 bool endswith(std::string
const & haystack, std::string
const & needle)
282 if ( haystack.length() < needle.length() )
return false;
283 else return ( haystack.compare(haystack.size()-needle.size(),needle.size(),needle) == 0 );
286 void slurp(std::istream & in, std::string & out)
289 std::ostringstream os;
291 os << line << std::endl;
293 out.append( os.str());
296 void trim( std::string &
s,
const std::string & drop)
298 std::string r = s.erase( s.find_last_not_of(drop)+1 );
299 r.erase( 0, r.find_first_not_of(drop) );
304 trim( std::string
const &
s, std::string
const & drop )
306 std::string copystr( s );
307 trim( copystr, drop );
313 std::size_t
const to_add = newlen - st.length();
315 std::string st_to_add(
"");
316 st_to_add.append(to_add,
' ');
323 std::size_t
const to_add = newlen - st.length();
325 std::string st_to_add(
"");
326 st_to_add.append(to_add,
' ');
335 char current = input[i];
336 if ( std::isdigit(current,loc) || current ==
'-' || current ==
'+' || current ==
'E' ||current==
'e' ) {
351 if ( ! textstream ) {
355 while (
getline(textstream, line) ) {
356 text.push_back(line +
"\n");
357 strsize += line.size() + 1;
362 alltext.reserve( strsize );
363 for (
unsigned int ii = 1;
ii <= text.size(); ++
ii ) {
384 const std::string
start(
"${");
385 const std::string
end(
"}");
390 start_position = input.find(start);
391 if ( start_position != std::string::npos ) {
393 if ( start_position == std::string::npos ) {
399 std::string env_name = input.substr(start_position+2,env_length-2);
400 char * env_value = getenv(env_name.c_str());
405 input.replace(start_position, env_length+1,env_value);
416 unsigned int digest[5];
417 boost::uuids::detail::sha1 hasher;
418 hasher.process_bytes(input_string.c_str(),input_string.size());
422 std::stringstream output_hash;
424 hasher.get_digest(digest);
426 for (
int i = 0; i < 5; ++i ) {
427 const char* tmp =
reinterpret_cast<char*
>(digest);
428 hash[i*4] = tmp[i*4+3];
429 hash[i*4+1] = tmp[i*4+2];
430 hash[i*4+2] = tmp[i*4+1];
431 hash[i*4+3] = tmp[i*4];
434 output_hash << std::hex;
436 for (
int i = 0; i < 20 ; ++i ) {
437 output_hash << ((hash[i] & 0x000000F0) >> 4) << (hash[i] & 0x0000000F);
440 return output_hash.str();
450 char const delimiter ){
452 for (
platform::Size n = 0; n < res_vector.size(); n++ ) chains.push_back(
' ' );
463 char const delimiter ){
465 using namespace ObjexxFCL;
466 std::string
tag =
"";
468 if ( res_vector.size() == 0 )
return tag;
471 int start_segment = res_vector[1];
472 int last_res = res_vector[1];
473 int last_chain = chain_vector[1];
477 if ( res_vector[n] != last_res+1 || chain_vector[n] != last_chain ) {
478 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
479 chains_for_segments.push_back( last_chain );
480 start_segment = res_vector[n];
482 last_res = res_vector[n];
483 last_chain = chain_vector[n];
485 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
486 chains_for_segments.push_back( last_chain );
488 for (
platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
489 if ( n > 1 )
tag += delimiter;
490 std::pair< int, int >
const & segment = res_vector_segments[n];
491 if ( chains_for_segments[n] !=
'\0' &&
492 chains_for_segments[n] !=
' ' &&
493 chains_for_segments[n] !=
'_' )
tag += std::string(1,chains_for_segments[n]) +
":";
494 if ( segment.first == segment.second ) {
509 char const delimiter ){
511 using namespace ObjexxFCL;
512 std::string
tag =
"";
514 if ( res_vector.size() == 0 )
return tag;
517 int start_segment = res_vector[1];
518 int last_res = res_vector[1];
519 std::string last_segid = segid_vector[1];
525 if ( res_vector[n] != last_res+1 || segid_vector[n] != last_segid ) {
526 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
527 segids_for_segments.push_back( last_segid );
528 start_segment = res_vector[n];
530 last_res = res_vector[n];
531 last_segid = segid_vector[n];
533 res_vector_segments.push_back( std::make_pair( start_segment, last_res ) );
534 segids_for_segments.push_back( last_segid );
536 for (
platform::Size n = 1; n <= res_vector_segments.size(); n++ ) {
537 if ( n > 1 )
tag += delimiter;
538 std::pair< int, int >
const & segment = res_vector_segments[n];
539 tag += segids_for_segments[n] +
":";
540 if ( segment.first == segment.second ) {
554 using namespace ObjexxFCL;
555 std::string
tag =
"";
558 if ( n > 1 )
tag +=
" ";
570 std::pair< std::vector< int >, std::vector< char > >
574 std::vector< int >
resnum;
575 std::vector< char > chain;
577 std::string s_nocommas =
replace_in( s,
",",
" " );
581 if ( !string_is_ok )
break;
583 return std::make_pair( resnum,chain );
586 std::pair< std::vector< int >, std::vector< char > >
592 std::pair< std::vector< int >, std::vector< std::string > >
596 std::vector< int >
resnum;
597 std::vector< std::string > segid;
599 std::string s_nocommas =
replace_in( s,
",",
" " );
610 while ( s_nocommas.size() > 0 && s_nocommas[0]==
' ' ) s_nocommas = s_nocommas.substr(1);
612 std::string
tag = s_nocommas.substr( 0, s_nocommas.find_first_of(
':' )+1 );
613 s_nocommas = s_nocommas.substr( s_nocommas.find_first_of(
':' ) +1 );
616 if ( space_pos == std::string::npos ) {
618 tags.push_back( tag );
621 tag += s_nocommas.substr( 0, space_pos );
622 tags.push_back( tag );
623 s_nocommas = s_nocommas.substr( space_pos +1 );
629 if ( !string_is_ok )
break;
631 return std::make_pair( resnum, segid );
637 std::vector< int > &
resnum,
638 std::vector< char > &
chains ){
639 bool string_is_ok(
false );
640 std::vector< int > resnum_from_tag;
642 std::string
const numerical(
"-0123456789");
644 if ( tag.size() == 0 )
return true;
646 size_t found_colon = tag.find(
":" );
647 if ( found_colon == std::string::npos ) {
648 if ( numerical.find( tag[0] ) == std::string::npos ) {
655 if ( found_colon == 0 ) {
658 }
else if ( found_colon == 1 ) {
667 resnum.push_back( resnum_from_tag[ n ] );
668 chains.push_back( chain );
676 std::vector< int > &
resnum,
677 std::vector< std::string > & segids ){
678 bool string_is_ok(
false );
679 std::vector< int > resnum_from_tag;
680 std::string segid(
"" );
682 if ( tag.size() == 0 )
return true;
684 size_t found_colon = tag.find(
":" );
686 if ( found_colon == std::string::npos || found_colon > 4 )
return false;
688 for (
size_t n = found_colon; n < 4; n++ ) {
691 if ( found_colon > 0 ) segid += tag.substr(0,found_colon);
697 resnum.push_back( resnum_from_tag[ n ] );
698 segids.push_back( segid );
710 replace_in( std::string
const & name_in, std::string
const & find_string, std::string
const & replace_string ){
711 std::string
name = name_in;
715 size_t pos = name.find( find_string );
716 while ( pos != std::string::npos ) {
717 name = name.replace( pos, find_string.size(), replace_string );
722 pos = name.find( find_string, pos + replace_string.size() );
729 std::string
strip(std::string
const & source,
char c)
734 if ( *
p == c ) begin =
p+1;
739 if ( *(
p-1) == c ) end =
p-1;
743 return std::string(begin, end);
#define utility_exit_with_message(m)
Exit with file + line + message.
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.
float string2float(std::string st)
convert a string to a float, returns -1 on failure
void string2uint(const std::string &x, unsigned int *y)
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
void trim(std::string &s, const std::string &drop)
utility::keys::lookup::end< KeyType > const end
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.
File name class supporting Windows and UN*X/Linux format names.
std::string strip(std::string const &source, char c)
Return a copy of the string with leading and trailing characters removed.
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
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
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.
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.
Program exit functions and macros.
void add_spaces_left_align(std::string &st, std::size_t const newlen)
adds spaces to a left aligned string until a given length is reached
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
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
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
list resnum
if line_edit[13:14]=='P': #Nucleic acid? Skip.
void ReadFromFileOrDie(const std::string &filename, std::string *contents)
Reads the contents of into , preserving newline characters. Aborts if an error i...
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").
void add_spaces_right_align(std::string &st, std::size_t const newlen)
adds spaces to a right aligned string until a given length is reached
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)
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