diff --git a/src/utils/general/ParseTools.cpp b/src/utils/general/ParseTools.cpp index 04782a717630a0c0457df8cfa3b80427b0170002..e85065ad3a93d3671ca8da437ce824b09600b2fe 100644 --- a/src/utils/general/ParseTools.cpp +++ b/src/utils/general/ParseTools.cpp @@ -2,6 +2,9 @@ #include <climits> #include <cctype> #include <cstring> +#include <cstdio> +#include <cstdlib> +#include <sstream> //This functions recognizes only numbers with digits, plus sign, minus sign, decimal point, e, or E. Hexadecimal and pointers not currently supported. bool isNumeric(const QuickString &str) { @@ -19,12 +22,26 @@ int str2chrPos(const QuickString &str) { } int str2chrPos(const char *str, size_t ulen) { + if (ulen == 0) { ulen = strlen(str); } + + //first test for exponents / scientific notation + bool hasExponent = false; + for (size_t i=0; i < ulen; i++) { + if (str[i] == 'e' || str[i] == 'E') { + std::istringstream ss(str); + double retVal; + ss >> retVal; + return (int)retVal; + } + } + int len=(int)ulen; if (len < 1 || len > 10) { - return INT_MIN; //can't do more than 9 digits and a minus sign + fprintf(stderr, "***** ERROR: too many digits/characters for integer conversion in string %s. Exiting...\n", str); + exit(1); } register int sum=0; @@ -39,9 +56,15 @@ int str2chrPos(const char *str, size_t ulen) { for (int i=startPos; i < len; i++) { char currChar = str[i]; + if (currChar == 'e' || currChar == 'E') { + //default to atoi for scientific notation + return atoi(str); + } if (!isdigit(currChar)) { - return INT_MIN; + fprintf(stderr, "***** ERROR: illegal character '%c' found in integer conversion of string %s. Exiting...\n", currChar, str); + exit(1); } + int dig = currChar - 48; //ascii code for zero. int power = len -i -1; @@ -77,7 +100,7 @@ int str2chrPos(const char *str, size_t ulen) { sum += dig *1000000000; break; default: - return INT_MIN; + return 0; break; } } diff --git a/test/merge/expFormat.bed b/test/merge/expFormat.bed new file mode 100644 index 0000000000000000000000000000000000000000..d7bd5182a5aa9ee9cb9fb3807fdd18f0b99872c4 --- /dev/null +++ b/test/merge/expFormat.bed @@ -0,0 +1 @@ +chr1 8e02 830 diff --git a/test/merge/test-merge.sh b/test/merge/test-merge.sh index 586940bf9617b369f6c9c3150f3e167bdbe15f24..c6c804955f45db5c3bcc80900f18e9e85c2de3d5 100644 --- a/test/merge/test-merge.sh +++ b/test/merge/test-merge.sh @@ -518,3 +518,13 @@ chr1 30 100" > exp $BT merge -i a.bed -iobuf 8192 > obs check exp obs rm exp obs + +########################################################### +# Test that scientific notation is allowed for coordinates +########################################################### +echo " merge.t43...\c" +echo \ +"chr1 800 830" > exp +$BT merge -i expFormat.bed > obs +check exp obs +rm obs exp