NCBI C++ Toolkit Cross Reference

  C++/src/util/format_guess.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
/* $Id: format_guess.cpp 64120 2014-08-20 15:19:03Z ludwigf $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Anatoliy Kuznetsov * * File Description: Implemented methods to identify file formats. * */ #include <ncbi_pch.hpp> #include <util/format_guess.hpp> #include <util/util_exception.hpp> #include <corelib/ncbifile.hpp> #include <corelib/ncbistre.hpp> #include <corelib/stream_utils.hpp> BEGIN_NCBI_SCOPE enum ESymbolType { fDNA_Main_Alphabet = 1<<0, ///< Just ACGTUN-. fDNA_Ambig_Alphabet = 1<<1, ///< Anything else representable in ncbi4na. fProtein_Alphabet = 1<<2, ///< Allows BZX*-, but not JOU. fLineEnd = 1<<3, fAlpha = 1<<4, fDigit = 1<<5, fSpace = 1<<6, fInvalid = 1<<7 }; enum EConfidence { eNo = 0, eMaybe, eYes }; // ============================================================================ // Helper routine--- file scope only: // ============================================================================ static unsigned char symbol_type_table[256]; // ---------------------------------------------------------------------------- static bool s_IsTokenPosInt( const string& strToken ) { try { return ( -1 != NStr::StringToInt( strToken, NStr::fConvErr_NoThrow ) ); } catch(...) { return false; } } // ---------------------------------------------------------------------------- static bool s_IsTokenInteger( const string& strToken ) // ---------------------------------------------------------------------------- { if ( ! strToken.empty() && strToken[0] == '-' ) { return s_IsTokenPosInt( strToken.substr( 1 ) ); } return s_IsTokenPosInt( strToken ); } // ---------------------------------------------------------------------------- static bool s_IsTokenDouble( const string& strToken ) { string token( strToken ); NStr::ReplaceInPlace( token, ".", "1", 0, 1 ); if ( token.size() > 1 && token[0] == '-' ) { token[0] = '1'; } return s_IsTokenPosInt( token ); } // ---------------------------------------------------------------------------- static void init_symbol_type_table(void) { if ( symbol_type_table[0] == 0 ) { for ( const char* s = "ACGNTU"; *s; ++s ) { unsigned char c = *s; symbol_type_table[c] |= fDNA_Main_Alphabet; c = tolower(c); symbol_type_table[c] |= fDNA_Main_Alphabet; } for ( const char* s = "BDHKMRSVWY"; *s; ++s ) { unsigned char c = *s; symbol_type_table[c] |= fDNA_Ambig_Alphabet; c = tolower(c); symbol_type_table[c] |= fDNA_Ambig_Alphabet; } for ( const char* s = "ACDEFGHIKLMNPQRSTVWYBZX"; *s; ++s ) { unsigned char c = *s; symbol_type_table[c] |= fProtein_Alphabet; c = tolower(c); symbol_type_table[c] |= fProtein_Alphabet; } symbol_type_table[(unsigned char)'-'] |= fDNA_Main_Alphabet | fProtein_Alphabet; symbol_type_table[(unsigned char)'*'] |= fProtein_Alphabet; for ( const char* s = "\r\n"; *s; ++s ) { unsigned char c = *s; symbol_type_table[c] |= fLineEnd; } for ( int c = 1; c < 256; ++c ) { if ( isalpha(c) ) symbol_type_table[c] |= fAlpha; if ( isdigit(c) ) symbol_type_table[c] |= fDigit; if ( isspace(c) ) symbol_type_table[c] |= fSpace; } symbol_type_table[0] |= fInvalid; } } // ---------------------------------------------------------------------------- int CFormatGuess::s_CheckOrder[] = // ---------------------------------------------------------------------------- { // must list all EFormats except eUnknown and eFormat_max. Will cause // assertion if violated! // eBam, // must precede eGZip! eZip, eGZip, eBZip2, eLzo, eSra, eRmo, eVcf, eGtf, eGvf, eGff3, eGff2, eGlimmer3, eAgp, eXml, eWiggle, eBed, eBed15, eNewick, eHgvs, eAlignment, eDistanceMatrix, eFlatFileSequence, eFiveColFeatureTable, eSnpMarkers, eFasta, eTextASN, eTaxplot, ePhrapAce, eTable, eBinaryASN, eUCSCRegion, }; // This array must stay in sync with enum EFormat, but that's not // supposed to change in the middle anyway, so the explicit size // should suffice to avoid accidental skew. const char* const CFormatGuess::sm_FormatNames[CFormatGuess::eFormat_max] = { "unknown", "binary ASN.1", "RepeatMasker", "GFF/GTF Poisoned", "Glimmer3", "AGP", "XML", "WIGGLE", "BED", "BED15", "Newick", "alignment", "distance matrix", "flat-file sequence", "five-column feature table", "SNP Markers", "FASTA", "text ASN.1", "Taxplot", "Phrap ACE", "table", "GTF", "GFF3", "GFF2", "HGVS", "GVF", "zip", "gzip", "bzip2", "lzo", "SRA", "BAM", "VCF", "UCSC Region" }; const char* CFormatGuess::GetFormatName(EFormat format) { unsigned int i = static_cast<unsigned int>(format); if (i >= static_cast <unsigned int>(eFormat_max)) { NCBI_THROW(CUtilException, eWrongData, "CFormatGuess::GetFormatName: out-of-range format value " + NStr::IntToString(i)); } return sm_FormatNames[i]; } // ============================================================================ // Old style class interface: // ============================================================================ // ---------------------------------------------------------------------------- CFormatGuess::ESequenceType CFormatGuess::SequenceType(const char* str, unsigned length, ESTStrictness strictness) { if (length == 0) length = (unsigned)::strlen(str); init_symbol_type_table(); unsigned int main_nuc_content = 0, ambig_content = 0, bad_nuc_content = 0, amino_acid_content = 0, exotic_aa_content = 0, bad_aa_content = 0; for (unsigned i = 0; i < length; ++i) { unsigned char c = str[i]; unsigned char type = symbol_type_table[c]; if ( type & fDNA_Main_Alphabet ) { ++main_nuc_content; } else if ( type & fDNA_Ambig_Alphabet ) { ++ambig_content; } else if ( !(type & (fSpace | fDigit)) ) { ++bad_nuc_content; } if ( type & fProtein_Alphabet ) { ++amino_acid_content; } else if ( type & fAlpha ) { ++exotic_aa_content; } else if ( !(type & (fSpace | fDigit)) ) { ++bad_aa_content; } } switch (strictness) { case eST_Lax: { double dna_content = (double)main_nuc_content / (double)length; double prot_content = (double)amino_acid_content / (double)length; if (dna_content > 0.7) { return eNucleotide; } if (prot_content > 0.7) { return eProtein; } } case eST_Default: if (bad_nuc_content + ambig_content <= main_nuc_content / 9 || (bad_nuc_content + ambig_content <= main_nuc_content / 3 && bad_nuc_content <= (main_nuc_content + ambig_content) / 19)) { // >=90% main alph. (ACGTUN-) or >=75% main and >=95% 4na-encodable return eNucleotide; } else if (bad_aa_content + exotic_aa_content <= amino_acid_content / 9) { // >=90% relatively standard protein residues. (JOU don't count.) return eProtein; } case eST_Strict: // Must be 100% encodable if (bad_nuc_content == 0 && ambig_content <= main_nuc_content / 3) { return eNucleotide; } else if (bad_aa_content == 0 && exotic_aa_content <= amino_acid_content / 9) { return eProtein; } } return eUndefined; } // ---------------------------------------------------------------------------- CFormatGuess::EFormat CFormatGuess::Format(const string& path, EOnError onerror) { CNcbiIfstream input(path.c_str(), IOS_BASE::in | IOS_BASE::binary); return Format(input); } // ---------------------------------------------------------------------------- CFormatGuess::EFormat CFormatGuess::Format(CNcbiIstream& input, EOnError onerror) { CFormatGuess FG( input ); return FG.GuessFormat( onerror ); } // ============================================================================ // New style object interface: // ============================================================================ // ---------------------------------------------------------------------------- CFormatGuess::CFormatGuess() : m_Stream( * new CNcbiIfstream ) , m_bOwnsStream( true ) { Initialize(); } // ---------------------------------------------------------------------------- CFormatGuess::CFormatGuess( const string& FileName ) : m_Stream( * new CNcbiIfstream( FileName.c_str(), ios::binary ) ) , m_bOwnsStream( true ) { Initialize(); } // ---------------------------------------------------------------------------- CFormatGuess::CFormatGuess( CNcbiIstream& Stream ) : m_Stream( Stream ) , m_bOwnsStream( false ) { Initialize(); } // ---------------------------------------------------------------------------- CFormatGuess::~CFormatGuess() { delete[] m_pTestBuffer; if ( m_bOwnsStream ) { delete &m_Stream; } } // ---------------------------------------------------------------------------- CFormatGuess::EFormat CFormatGuess::GuessFormat( EMode ) { return GuessFormat(eDefault); } // ---------------------------------------------------------------------------- CFormatGuess::EFormat CFormatGuess::GuessFormat( EOnError onerror ) { if (!x_TestInput(m_Stream, onerror)) { return eUnknown; } EMode mode = eQuick; unsigned int uFormatCount = sizeof( s_CheckOrder ) / sizeof( int ); // First, try to use hints if ( !m_Hints.IsEmpty() ) { for (unsigned int f = 0; f < uFormatCount; ++f) { EFormat fmt = EFormat( s_CheckOrder[ f ] ); if (m_Hints.IsPreferred(fmt) && x_TestFormat(fmt, mode)) { return fmt; } } } // Check other formats, skip the ones that are disabled through hints for (unsigned int f = 0; f < uFormatCount; ++f) { EFormat fmt = EFormat( s_CheckOrder[ f ] ); if ( ! m_Hints.IsDisabled(fmt) && x_TestFormat(fmt, mode) ) { return fmt; } } return eUnknown; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormat( EFormat format, EMode ) { return TestFormat( format, eDefault); } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormat( EFormat format, EOnError onerror ) { if (format != eUnknown && !x_TestInput(m_Stream, onerror)) { return false; } EMode mode = eQuick; return x_TestFormat(format, mode); } // ---------------------------------------------------------------------------- bool CFormatGuess::x_TestFormat(EFormat format, EMode mode) { // First check if the format is disabled if ( m_Hints.IsDisabled(format) ) { return false; } switch( format ) { case eBinaryASN: return TestFormatBinaryAsn( mode ); case eRmo: return TestFormatRepeatMasker( mode ); case eGtf: return TestFormatGtf( mode ); case eGvf: return TestFormatGvf( mode ); case eGff3: return TestFormatGff3( mode ); case eGff2: return TestFormatGff2( mode ); case eGlimmer3: return TestFormatGlimmer3( mode ); case eAgp: return TestFormatAgp( mode ); case eXml: return TestFormatXml( mode ); case eWiggle: return TestFormatWiggle( mode ); case eBed: return TestFormatBed( mode ); case eBed15: return TestFormatBed15( mode ); case eNewick: return TestFormatNewick( mode ); case eAlignment: return TestFormatAlignment( mode ); case eDistanceMatrix: return TestFormatDistanceMatrix( mode ); case eFlatFileSequence: return TestFormatFlatFileSequence( mode ); case eFiveColFeatureTable: return TestFormatFiveColFeatureTable( mode ); case eSnpMarkers: return TestFormatSnpMarkers( mode ); case eFasta: return TestFormatFasta( mode ); case eTextASN: return TestFormatTextAsn( mode ); case eTaxplot: return TestFormatTaxplot( mode ); case ePhrapAce: return TestFormatPhrapAce( mode ); case eTable: return TestFormatTable( mode ); case eHgvs: return TestFormatHgvs( mode ); case eZip: return TestFormatZip( mode ); case eGZip: return TestFormatGZip( mode ); case eBZip2: return TestFormatBZip2( mode ); case eLzo: return TestFormatLzo( mode ); case eSra: return TestFormatSra( mode ); case eBam: return TestFormatBam( mode ); case eVcf: return TestFormatVcf( mode ); case eUCSCRegion: return false; default: NCBI_THROW( CCoreException, eInvalidArg, "CFormatGuess::x_TestFormat(): Unsupported format ID (" + NStr::NumericToString((int)format) + ")." ); } } // ---------------------------------------------------------------------------- void CFormatGuess::Initialize() { NCBI_ASSERT(eFormat_max-2 == sizeof( s_CheckOrder ) / sizeof( int ), "Indices in s_CheckOrder do not match format count ---" "update s_CheckOrder to list all formats" ); NCBI_ASSERT(eFormat_max == sizeof(sm_FormatNames) / sizeof(const char*) && sm_FormatNames[eFormat_max - 1] != NULL, "sm_FormatNames does not list all possible formats"); m_pTestBuffer = 0; m_bStatsAreValid = false; m_bSplitDone = false; m_iStatsCountData = 0; m_iStatsCountAlNumChars = 0; m_iStatsCountDnaChars = 0; m_iStatsCountAaChars = 0; } // ---------------------------------------------------------------------------- bool CFormatGuess::EnsureTestBuffer() { if ( m_pTestBuffer ) { return true; } if ( ! m_Stream.good() ) { return false; } // Fix to the all-comment problem. // Read a test buffer, // Test it for being all comment // If its all comment, read a twice as long buffer // Stop when its no longer all comment, end of the stream, // or Multiplier hits 1024 int Multiplier = 1; while(true) { m_pTestBuffer = new char[ Multiplier * s_iTestBufferSize ]; m_Stream.read( m_pTestBuffer, Multiplier * s_iTestBufferSize ); m_iTestDataSize = m_Stream.gcount(); m_Stream.clear(); // in case we reached eof CStreamUtils::Stepback( m_Stream, m_pTestBuffer, m_iTestDataSize ); if (IsAllComment()) { Multiplier *= 2; delete [] m_pTestBuffer; m_pTestBuffer = NULL; if (Multiplier >= 1024 || m_iTestDataSize < ((Multiplier/2) * s_iTestBufferSize) ) { return false; } continue; } else { break; } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::EnsureStats() { if ( m_bStatsAreValid ) { return true; } if ( ! EnsureTestBuffer() ) { return false; } if ( m_iTestDataSize == 0 ) { m_bStatsAreValid = true; return true; } CNcbiIstrstream TestBuffer( reinterpret_cast<const char*>( m_pTestBuffer ), m_iTestDataSize ); string strLine; init_symbol_type_table(); // Things we keep track of: // m_iStatsCountAlNumChars: number of characters that are letters or // digits // m_iStatsCountData: number of characters not part of a line starting // with '>', ignoring whitespace // m_iStatsCountDnaChars: number of characters counted in m_iStatsCountData // from the DNA alphabet // m_iStatsCountAaChars: number of characters counted in m_iStatsCountData // from the AA alphabet // while ( ! TestBuffer.fail() ) { NcbiGetlineEOL( TestBuffer, strLine ); // code in CFormatGuess::Format counts line ends // so, we will count them here as well if (!strLine.empty()) { strLine += '\n'; } size_t size = strLine.size(); bool is_header = size > 0 && strLine[0] == '>'; for ( size_t i=0; i < size; ++i ) { unsigned char c = strLine[i]; unsigned char type = symbol_type_table[c]; if ( type & (fAlpha | fDigit | fSpace) ) { ++m_iStatsCountAlNumChars; } if ( !is_header ) { if ( !(type & fSpace) ) { ++m_iStatsCountData; } if ( type & fDNA_Main_Alphabet ) { ++m_iStatsCountDnaChars; } if ( type & fProtein_Alphabet ) { ++m_iStatsCountAaChars; } if ( type & fLineEnd ) { ++m_iStatsCountAlNumChars; --m_iStatsCountData; } } } } m_bStatsAreValid = true; return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::x_TestInput( CNcbiIstream& input, EOnError onerror ) { if (!input) { if (onerror == eThrowOnBadSource) { NCBI_THROW(CUtilException,eNoInput,"Unreadable input stream"); } return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatRepeatMasker( EMode /* not used */ ) { if ( ! EnsureStats() || ! EnsureSplitLines() ) { return false; } return IsInputRepeatMaskerWithHeader() || IsInputRepeatMaskerWithoutHeader(); } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatPhrapAce( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { if ( IsLinePhrapId( *it ) ) { return true; } } return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatGtf( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } unsigned int uGtfLineCount = 0; list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it) { // // Make sure to ignore any UCSC track and browser lines prior to the // start of data // if ( it->empty() || (*it)[0] == '#' ) { continue; } if ( !uGtfLineCount && NStr::StartsWith( *it, "browser " ) ) { continue; } if ( !uGtfLineCount && NStr::StartsWith( *it, "track " ) ) { continue; } if ( ! IsLineGtf( *it ) ) { return false; } ++uGtfLineCount; } return (uGtfLineCount != 0); } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatGvf( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } unsigned int uGvfLineCount = 0; list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it) { // // Make sure to ignore any UCSC track and browser lines prior to the // start of data // if ( it->empty() || (*it)[0] == '#' ) { if (NStr::StartsWith(*it, "##gvf-version")) { return true; } continue; } if ( !uGvfLineCount && NStr::StartsWith( *it, "browser " ) ) { continue; } if ( !uGvfLineCount && NStr::StartsWith( *it, "track " ) ) { continue; } if ( ! IsLineGvf( *it ) ) { return false; } ++uGvfLineCount; } return (uGvfLineCount != 0); } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatGff3( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } unsigned int uGffLineCount = 0; list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it) { // // Make sure to ignore any UCSC track and browser lines prior to the // start of data // if ( it->empty() || (*it)[0] == '#' ) { continue; } if ( !uGffLineCount && NStr::StartsWith( *it, "browser " ) ) { continue; } if ( !uGffLineCount && NStr::StartsWith( *it, "track " ) ) { continue; } if ( ! IsLineGff3( *it ) ) { return false; } ++uGffLineCount; } return (uGffLineCount != 0); } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatGff2( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } unsigned int uGffLineCount = 0; list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it) { // // Make sure to ignore any UCSC track and browser lines prior to the // start of data // if ( it->empty() || (*it)[0] == '#' ) { continue; } if ( !uGffLineCount && NStr::StartsWith( *it, "browser " ) ) { continue; } if ( !uGffLineCount && NStr::StartsWith( *it, "track " ) ) { continue; } if ( ! IsLineGff2( *it ) ) { return false; } ++uGffLineCount; } return (uGffLineCount != 0); } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatGlimmer3( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } /// first line should be a FASTA defline list<string>::iterator it = m_TestLines.begin(); if (it->empty() || (*it)[0] != '>') { return false; } /// there should be additional data lines, and they should be easily parseable, /// with five columns ++it; if (it == m_TestLines.end()) { return false; } for ( /**/; it != m_TestLines.end(); ++it) { if ( !IsLineGlimmer3( *it ) ) { return false; } } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatAgp( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { try { if ( !IsLineAgp( *it ) ) { return false; } } catch(...) { return false; } } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatNewick( EMode /* not used */ ) { // ----------------------------------------------------------------------------- // newick trees can be found in nexus files. check for that first as a special case if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { const int BUFFSIZE = 8096; m_pTestBuffer = new char[BUFFSIZE]; m_Stream.read( m_pTestBuffer, BUFFSIZE ); m_iTestDataSize = m_Stream.gcount(); m_Stream.clear(); // in case we reached eof CStreamUtils::Stepback( m_Stream, m_pTestBuffer, m_iTestDataSize ); m_TestLines.push_back(m_pTestBuffer); } // Alignment files come in all different shapes and broken formats, // and some of them are hard to recognize as such, in particular // if they have been hacked up in a text editor. // This functions only concerns itself with the ones that are // easy to recognize. // Note: We can live with false negatives. Avoid false positives // at all cost. bool is_nexus = false; bool has_trees = false; ITERATE( list<string>, it, m_TestLines ) { if ( NPOS != it->find( "#NEXUS" ) ) { is_nexus = true; } // case insensitive if (NPOS != NStr::FindNoCase(*it, "begin trees;")) { has_trees = true; } } // In a nexus file with a tree, we will just read in the tree (ignoring for now // the alignment) if (is_nexus ) { if (has_trees) return true; return false; } // special newick consideration: // newick files may come with all data cramped into a single run-on line, // that single oversized line may not have a line terminator const size_t maxSampleSize = 8*1024-1; size_t sampleSize = 0; char* pSample = new char[maxSampleSize+1]; AutoArray<char> autoDelete(pSample); m_Stream.read(pSample, maxSampleSize); sampleSize = (size_t)m_Stream.gcount(); m_Stream.clear(); // in case we reached eof CStreamUtils::Stepback(m_Stream, pSample, sampleSize); if (0 == sampleSize) { return false; } pSample[sampleSize] = 0; if (!IsSampleNewick(pSample)) { // tolerant of embedded line breaks return false; } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatBinaryAsn( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } // // Criterion: Presence of any non-printing characters // EConfidence conf = eNo; for (int i = 0; i < m_iTestDataSize; ++i) { if ( !isgraph((unsigned char) m_pTestBuffer[i]) && !isspace((unsigned char) m_pTestBuffer[i]) ) { if (m_pTestBuffer[i] == '\1') { conf = eMaybe; } else { return true; } } } return (conf == eYes); } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatDistanceMatrix( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } // // criteria are odd: // list<string>::const_iterator iter = m_TestLines.begin(); list<string> toks; /// first line: one token, one number NStr::Split(*iter++, "\t ", toks); if (toks.size() != 1 || toks.front().find_first_not_of("0123456789") != string::npos) { return false; } // now, for remaining ones, we expect an alphanumeric item first, // followed by a set of floating-point values. Unless we are at the last // line, the number of values should increase monotonically for (size_t i = 1; iter != m_TestLines.end(); ++i, ++iter) { toks.clear(); NStr::Split(*iter, "\t ", toks); if (toks.size() != i) { /// we can ignore the last line ; it may be truncated list<string>::const_iterator it = iter; ++it; if (it != m_TestLines.end()) { return false; } } list<string>::const_iterator it = toks.begin(); for (++it; it != toks.end(); ++it) { if ( ! s_IsTokenDouble( *it ) ) { return false; } } } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatFlatFileSequence( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } ITERATE (list<string>, it, m_TestLines) { if ( !IsLineFlatFileSequence( *it ) ) { return false; } } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatFiveColFeatureTable( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { if (it->empty()) { continue; } if (it->find(">Feature ") != 0) { return false; } if (it->find_first_of(" \t", 9) != string::npos) { return false; } break; } return true; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatXml( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } string input( m_pTestBuffer, (size_t)m_iTestDataSize ); NStr::TruncateSpacesInPlace( input, NStr::eTrunc_Begin ); // // Test 1: If it starts with typical XML decorations such as "<?xml..." // then respect that: // if ( NStr::StartsWith( input, "<?XML", NStr::eNocase ) ) { return true; } if ( NStr::StartsWith( input, "<!DOCTYPE", NStr::eNocase ) ) { return true; } // // Test 2: In the absence of XML specific declarations, check whether the // input starts with the opening tag of a well known set of doc types: // static const char* known_types[] = { "<Blast4-request>" }; const int num_types = sizeof( known_types ) / sizeof( const char* ); for ( int i=0; i < num_types; ++i ) { if ( NStr::StartsWith( input, known_types[i], NStr::eCase ) ) { return true; } } return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatAlignment( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } // Alignment files come in all different shapes and broken formats, // and some of them are hard to recognize as such, in particular // if they have been hacked up in a text editor. // This functions only concerns itself with the ones that are // easy to recognize. // Note: We can live with false negatives. Avoid false positives // at all cost. ITERATE( list<string>, it, m_TestLines ) { if ( NPOS != it->find( "#NEXUS" ) ) { return true; } if ( NPOS != it->find( "CLUSTAL" ) ) { return true; } } return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::x_TestTableDelimiter(const string& delims) { list<string>::const_iterator iter = m_TestLines.begin(); list<string> toks; // Skip initial lines since not all headers start with comments like # or ;: // Don't skip though if file is very short - add up to 3, 1 for each line // over 5: for (size_t i=5; i<7; ++i) if (m_TestLines.size() > i) ++iter; /// determine the number of observed columns size_t ncols = 0; bool found = false; for ( ; iter != m_TestLines.end() && ! found; ++iter) { if (iter->empty() || (*iter)[0] == '#' || (*iter)[0] == ';') { continue; } toks.clear(); NStr::Split(*iter, delims, toks); ncols = toks.size(); found = true; } if ( ncols < 2 ) { return false; } size_t nlines = 1; // verify that columns all have the same size // we can add an exception for the last line for ( ; iter != m_TestLines.end(); ++iter) { if (iter->empty() || (*iter)[0] == '#' || (*iter)[0] == ';') { continue; } toks.clear(); NStr::Split(*iter, delims, toks); if (toks.size() != ncols) { list<string>::const_iterator it = iter; ++it; if (it != m_TestLines.end() || (m_iTestDataSize < s_iTestBufferSize) ) { return false; } } else { ++nlines; } } return ( nlines >= 2 ); } bool CFormatGuess::TestFormatTable( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } // // NOTE 1: // There is a bunch of file formats that are a special type of table and // that we want to identify (like Repeat Masker output). So not to shade // out those more special formats, this test should be performed only after // all the more specialized table formats have been tested. // // // NOTE 2: // The original criterion for this test was "the same number of observed // columns in every line". // In order to weed out false positives the following *additional* // conditions have been imposed: // - there are at least two observed columns // - the sample contains at least two non-comment lines. // //' ' ' \t' '\t' ',' '|' if (x_TestTableDelimiter(" ")) return true; else if (x_TestTableDelimiter(" \t")) return true; else if (x_TestTableDelimiter("\t")) return true; else if (x_TestTableDelimiter(",")) return true; else if (x_TestTableDelimiter("|")) return true; return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatFasta( EMode /* not used */ ) { if ( ! EnsureStats() ) { return false; } // reject obvious misfits: if ( m_iTestDataSize == 0 || m_pTestBuffer[0] != '>' ) { return false; } if ( m_iStatsCountData == 0 ) { if (0.75 > double(m_iStatsCountAlNumChars)/double(m_iTestDataSize) ) { return false; } return ( NStr::Find( m_pTestBuffer, "|" ) <= 10 ); } // remaining decision based on text stats: double dAlNumFraction = (double)m_iStatsCountAlNumChars / m_iTestDataSize; double dDnaFraction = (double)m_iStatsCountDnaChars / m_iStatsCountData; double dAaFraction = (double)m_iStatsCountAaChars / m_iStatsCountData; // want at least 80% text-ish overall: if ( dAlNumFraction < 0.8 ) { return false; } // want more than 91 percent of either DNA content or AA content in what we // presume is data: if ( dDnaFraction > 0.91 || dAaFraction > 0.91 ) { return true; } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatTextAsn( EMode /* not used */ ) { if ( ! EnsureStats() ) { return false; } // reject obvious misfits: if ( m_iTestDataSize == 0 || m_pTestBuffer[0] == '>' ) { return false; } // criteria: // at least 80% text-ish, // "::=" as the 2nd field of the first non-blank non comment line. // double dAlNumFraction = (double)m_iStatsCountAlNumChars / m_iTestDataSize; if ( dAlNumFraction < 0.80 ) { return false; } CNcbiIstrstream TestBuffer( reinterpret_cast<const char*>( m_pTestBuffer ), m_iTestDataSize ); string strLine; while ( ! TestBuffer.fail() ) { vector<string> Fields; NcbiGetline( TestBuffer, strLine, "\n\r" ); NStr::Tokenize( strLine, " \t", Fields, NStr::eMergeDelims ); if ( IsAsnComment( Fields ) ) { continue; } return ( Fields.size() >= 2 && Fields[1] == "::=" ); } return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatTaxplot( EMode /* not used */ ) { return false; } // ----------------------------------------------------------------------------- bool CFormatGuess::TestFormatSnpMarkers( EMode /* not used */ ) { if ( ! EnsureTestBuffer() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { string str = *it; int rsid, chr, pos, numMatched; numMatched = sscanf( it->c_str(), "rs%d\t%d\t%d", &rsid, &chr, &pos); if ( numMatched == 3) { return true; } } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatBed( EMode /* not used */ ) { if ( ! EnsureStats() || ! EnsureSplitLines() ) { return false; } bool bTrackLineFound( false ); bool bHasStartAndStop ( false ); size_t columncount = 0; ITERATE( list<string>, it, m_TestLines ) { string str = NStr::TruncateSpaces( *it ); if ( str.empty() ) { continue; } // 'chr 8' fixup, the bedreader does this too if (str.find("chr ") == 0 || str.find("Chr ") == 0 || str.find("CHR ") == 0) str.erase(3, 1); // // while occurrence of the following decorations _is_ a good sign, they could // also be indicator for a variety of other UCSC data formats // if ( NStr::StartsWith( str, "track" ) ) { bTrackLineFound = true; continue; } if ( NStr::StartsWith( str, "browser" ) ) { continue; } if ( NStr::StartsWith( str, "#" ) ) { continue; } vector<string> columns; NStr::Tokenize( str, " \t", columns, NStr::eMergeDelims ); if (columns.size() < 3 || columns.size() > 12) { return false; } if ( columns.size() != columncount ) { if ( columncount == 0 ) { columncount = columns.size(); } else { return false; } } if(columns.size() >= 3) { if (s_IsTokenPosInt(columns[1]) && s_IsTokenPosInt(columns[2])) { bHasStartAndStop = true; } } } return (bHasStartAndStop || bTrackLineFound); } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatBed15( EMode /* not used */ ) { if ( ! EnsureStats() || ! EnsureSplitLines() ) { return false; } bool LineFound = false; size_t columncount = 15; ITERATE( list<string>, it, m_TestLines ) { if ( NStr::TruncateSpaces( *it ).empty() ) { continue; } // // while occurrence of the following decorations _is_ a good sign, they could // also be indicator for a variety of other UCSC data formats // if ( NStr::StartsWith( *it, "track" ) ) { continue; } if ( NStr::StartsWith( *it, "browser" ) ) { continue; } if ( NStr::StartsWith( *it, "#" ) ) { continue; } vector<string> columns; NStr::Tokenize( *it, " \t", columns, NStr::eMergeDelims ); if ( columns.size() != columncount ) { return false; } else { if (!s_IsTokenPosInt(columns[1]) || //chr start !s_IsTokenPosInt(columns[2]) || //chr end !s_IsTokenPosInt(columns[4]) || //score !s_IsTokenPosInt(columns[6]) || //thick draw start !s_IsTokenPosInt(columns[7])) //thick draw end return false; string strand = NStr::TruncateSpaces(columns[5]); if (strand != "+" && strand != "-") return false; LineFound = true; } } return LineFound; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatWiggle( EMode /* not used */ ) { if ( ! EnsureStats() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { if ( NStr::StartsWith( *it, "track" ) ) { if ( NStr::Find( *it, "type=wiggle_0" ) != NPOS ) { return true; } if ( NStr::Find( *it, "type=bedGraph" ) != NPOS ) { return true; } } if ( NStr::StartsWith(*it, "fixedStep") ) { /* MSS-140 */ if ( NStr::Find(*it, "chrom=") && NStr::Find(*it, "start=") ) { return true; } } if ( NStr::StartsWith(*it, "variableStep") ) { /* MSS-140 */ if ( NStr::Find(*it, "chrom=") ) { return true; } return true; } } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatHgvs( EMode /* not used */ ) { if ( ! EnsureStats() || ! EnsureSplitLines() ) { const int BUFFSIZE = 1024; m_pTestBuffer = new char[BUFFSIZE]; m_Stream.read( m_pTestBuffer, BUFFSIZE ); m_iTestDataSize = m_Stream.gcount(); m_Stream.clear(); // in case we reached eof CStreamUtils::Stepback( m_Stream, m_pTestBuffer, m_iTestDataSize ); m_TestLines.push_back(m_pTestBuffer); } unsigned int uHgvsLineCount = 0; list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it) { if ( it->empty() || (*it)[0] == '#' ) { continue; } if ( ! IsLineHgvs( *it ) ) { return false; } ++uHgvsLineCount; } return (uHgvsLineCount != 0); } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatZip( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } // check if the first two bytes match with the zip magic number: 0x504B, // or BK and the next two bytes match with any of 0x0102, 0x0304, 0x0506 // and 0x0708. if ( m_iTestDataSize < 4) { return false; } if (m_pTestBuffer[0] == 'P' && m_pTestBuffer[1] == 'K' && ((m_pTestBuffer[2] == (char)1 && m_pTestBuffer[3] == (char)2) || (m_pTestBuffer[2] == (char)3 && m_pTestBuffer[3] == (char)4) || (m_pTestBuffer[2] == (char)5 && m_pTestBuffer[3] == (char)6) || (m_pTestBuffer[2] == (char)7 && m_pTestBuffer[3] == (char)8) ) ) { return true; } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatGZip( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } // check if the first two bytes match the gzip magic number: 0x1F8B if ( m_iTestDataSize < 2) { return false; } if (m_pTestBuffer[0] == (char)31 && m_pTestBuffer[1] == (char)139) { return true; } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatBZip2( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } // check if the first two bytes match with the bzip2 magic number: 0x425A, // or 'BZ' and the next two bytes match with 0x68(h) and 0x31-39(1-9) if ( m_iTestDataSize < 4) { return false; } if (m_pTestBuffer[0] == 'B' && m_pTestBuffer[1] == 'Z' && m_pTestBuffer[2] == 'h' && m_pTestBuffer[3] >= '1' && m_pTestBuffer[3] <= '9') { return true; } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatLzo( EMode /* not used */ ) { if ( ! EnsureTestBuffer() ) { return false; } if (m_iTestDataSize >= 3 && m_pTestBuffer[0] == 'L' && m_pTestBuffer[1] == 'Z' && m_pTestBuffer[2] == 'O') { if (m_iTestDataSize == 3 || (m_iTestDataSize > 3 && m_pTestBuffer[3] == '\0')) { return true; } } if (m_iTestDataSize >= 4 && m_pTestBuffer[1] == 'L' && m_pTestBuffer[2] == 'Z' && m_pTestBuffer[3] == 'O') { if (m_iTestDataSize == 4 || (m_iTestDataSize > 4 && m_pTestBuffer[4] == '\0')) { return true; } } return false; } bool CFormatGuess::TestFormatSra(EMode /* not used */ ) { if ( !EnsureTestBuffer() || m_iTestDataSize < 16 || CTempString(m_pTestBuffer, 8) != "NCBI.sra") { return false; } if (m_pTestBuffer[8] == '\x05' && m_pTestBuffer[9] == '\x03' && m_pTestBuffer[10] == '\x19' && m_pTestBuffer[11] == '\x88') { return true; } else if (m_pTestBuffer[8] == '\x88' && m_pTestBuffer[9] == '\x19' && m_pTestBuffer[10] == '\x03' && m_pTestBuffer[11] == '\x05') { return true; } else { return false; } } bool CFormatGuess::TestFormatBam(EMode mode) { // Check for a gzip header whose first (only) extra field spans // at least six bytes and has the tag BC. return (TestFormatGZip(mode) && m_iTestDataSize >= 18 && (m_pTestBuffer[3] & 4) != 0 // extra field present && (static_cast<unsigned char>(m_pTestBuffer[10]) >= 6 || m_pTestBuffer[11] != 0) // at least six bytes && m_pTestBuffer[12] == 'B' && m_pTestBuffer[13] == 'C'); } // ---------------------------------------------------------------------------- bool CFormatGuess::TestFormatVcf( EMode) // ---------------------------------------------------------------------------- { // Currently, only look for the header line identifying the VCF version. // Waive requirement this be the first line, but still expect it to by // in the initial sample. if ( ! EnsureStats() || ! EnsureSplitLines() ) { return false; } ITERATE( list<string>, it, m_TestLines ) { if (NStr::StartsWith(*it, "##fileformat=VCFv")) { return true; } } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsInputRepeatMaskerWithHeader() { // // Repeatmasker files consist of columnar data with a couple of lines // of column labels prepended to it (but sometimes someone strips those // labels). // This function tries to identify repeatmasker data by those column // label lines. They should be the first non-blanks in the file. // string labels_1st_line[] = { "SW", "perc", "query", "position", "matching", "" }; string labels_2nd_line[] = { "score", "div.", "del.", "ins.", "sequence", "" }; // // Purge junk lines: // list<string>::iterator it = m_TestLines.begin(); for ( ; it != m_TestLines.end(); ++it ) { NStr::TruncateSpacesInPlace( *it ); if ( *it != "" ) { break; } } if ( it == m_TestLines.end() ) { return false; } // // Verify first line of labels: // size_t current_offset = 0; for ( size_t i=0; labels_1st_line[i] != ""; ++i ) { current_offset = NStr::FindCase( *it, labels_1st_line[i], current_offset ); if ( current_offset == NPOS ) { return false; } } // // Verify second line of labels: // ++it; if ( it == m_TestLines.end() ) { return false; } current_offset = 0; for ( size_t j=0; labels_2nd_line[j] != ""; ++j ) { current_offset = NStr::FindCase( *it, labels_2nd_line[j], current_offset ); if ( current_offset == NPOS ) { return false; } } // // Should have at least one extra line: // ++it; if ( it == m_TestLines.end() ) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsInputRepeatMaskerWithoutHeader() { // // Repeatmasker files consist of columnar data with a couple of lines // of column labels prepended to it (but sometimes someone strips those // labels). // This function assumes the column labels have been stripped and attempts // to identify RMO by checking the data itself. // // // We declare the data as RMO if we are able to parse every record in the // sample we got: // ITERATE( list<string>, it, m_TestLines ) { string str = NStr::TruncateSpaces( *it ); if ( str == "" ) { continue; } if ( ! IsLineRmo( str ) ) { return false; } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsSampleNewick( const string& cline ) // ---------------------------------------------------------------------------- { // NOTE: // See http://evolution.genetics.washington.edu/phylip/newick_doc.html // // Note that Newick tree tend to be written out as a single long line. Thus, // we are most likely only seeing the first part of a tree. // // NOTE: // MSS-112 introduced the concept of multitree files is which after the ";" // another tree may start. The new logic accepts files as Newick if they // are Newick up to and including the first semicolon. It does not look // beyond. string line = NStr::TruncateSpaces( cline ); if ( line.empty() || line[0] != '(') { return false; } {{ // Strip out comments: string trimmed; bool in_comment = false; for ( size_t ii=0; line.c_str()[ii] != 0; ++ii ) { if ( ! in_comment ) { if ( line.c_str()[ii] != '[' ) { trimmed += line.c_str()[ii]; } else { in_comment = true; } } else /* in_comment */ { if ( line.c_str()[ii] == ']' ) { in_comment = false; } } } line = trimmed; }} {{ // Compress quoted labels: string trimmed; bool in_quote = false; for ( size_t ii=0; line.c_str()[ii] != 0; ++ii ) { if ( ! in_quote ) { if ( line.c_str()[ii] != '\'' ) { trimmed += line.c_str()[ii]; } else { in_quote = true; trimmed += 'A'; } } else { /* in_quote */ if ( line.c_str()[ii] == '\'' ) { in_quote = false; } } } line = trimmed; }} {{ // Strip distance markers: string trimmed; size_t ii=0; while ( line.c_str()[ii] != 0 ) { if ( line.c_str()[ii] != ':' ) { trimmed += line.c_str()[ii++]; } else { ii++; if ( line.c_str()[ii] == '-' || line.c_str()[ii] == '+' ) { ii++; } while ( '0' <= line.c_str()[ii] && line.c_str()[ii] <= '9' ) { ii++; } if ( line.c_str()[ii] == '.' ) { ii++; while ( '0' <= line.c_str()[ii] && line.c_str()[ii] <= '9' ) { ii++; } } } } line = trimmed; }} {{ // Rough lexical analysis of what's left. Bail immediately on fault: if (line.empty() || line[0] != '(') { return false; } size_t paren_count = 1; for ( size_t ii=1; line.c_str()[ii] != 0; ++ii ) { switch ( line.c_str()[ii] ) { default: break; case '(': ++paren_count; break; case ')': if ( paren_count == 0 ) { return false; } --paren_count; break; case ',': if ( paren_count == 0 ) { return false; } break; case ';': // if ( line[ii+1] != 0 ) { // return false; // } break; } } }} return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineFlatFileSequence( const string& line ) { // blocks of ten residues (or permitted punctuation characters) // with a count at the start or end; require at least four // (normally six) SIZE_TYPE pos = line.find_first_not_of("0123456789 \t"); if (pos == NPOS || pos + 45 >= line.size()) { return false; } for (SIZE_TYPE i = 0; i < 45; ++i) { char c = line[pos + i]; if (i % 11 == 10) { if ( !isspace(c) ) { return false; } } else { if ( !isalpha(c) && c != '-' && c != '*') { return false; } } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLabelNewick( const string& label ) { // Starts with a string of anything other than "[]:", optionally followed by // a single ':', followed by a number, optionally followed by a dot and // another number. if ( NPOS != label.find_first_of( "[]" ) ) { return false; } size_t colon = label.find( ':' ); if ( NPOS == colon ) { return true; } size_t dot = label.find_first_not_of( "0123456789", colon + 1 ); if ( NPOS == dot ) { return true; } if ( label[ dot ] != '.' ) { return false; } size_t end = label.find_first_not_of( "0123456789", dot + 1 ); return ( NPOS == end ); } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineAgp( const string& strLine ) { // // Note: The reader allows for line and endline comments starting with a '#'. // So we accept them here, too. // string line( strLine ); size_t uCommentStart = NStr::Find( line, "#" ); if ( NPOS != uCommentStart ) { line = line.substr( 0, uCommentStart ); } NStr::TruncateSpacesInPlace( line ); if ( line.empty() ) { return true; } vector<string> tokens; if ( NStr::Tokenize( line, " \t", tokens, NStr::eMergeDelims ).size() < 8 ) { return false; } if ( tokens[1].size() > 1 && tokens[1][0] == '-' ) { tokens[1][0] = '1'; } if ( -1 == NStr::StringToNonNegativeInt( tokens[1] ) ) { return false; } if ( tokens[2].size() > 1 && tokens[2][0] == '-' ) { tokens[2][0] = '1'; } if ( -1 == NStr::StringToNonNegativeInt( tokens[2] ) ) { return false; } if ( tokens[3].size() > 1 && tokens[3][0] == '-' ) { tokens[3][0] = '1'; } if ( -1 == NStr::StringToNonNegativeInt( tokens[3] ) ) { return false; } if ( tokens[4].size() != 1 || NPOS == tokens[4].find_first_of( "ADFGPNOW" ) ) { return false; } if ( tokens[4] == "N" ) { if ( -1 == NStr::StringToNonNegativeInt( tokens[5] ) ) { return false; } } else { if ( -1 == NStr::StringToNonNegativeInt( tokens[6] ) ) { return false; } if ( -1 == NStr::StringToNonNegativeInt( tokens[7] ) ) { return false; } if ( tokens.size() != 9 ) { return false; } if ( tokens[8].size() != 1 || NPOS == tokens[8].find_first_of( "+-" ) ) { return false; } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineGlimmer3( const string& line ) { list<string> toks; NStr::Split(line, "\t ", toks); if (toks.size() != 5) { return false; } list<string>::iterator i = toks.begin(); /// first column: skip (ascii identifier) ++i; /// second, third columns: both ints if ( ! s_IsTokenInteger( *i++ ) ) { return false; } if ( ! s_IsTokenInteger( *i++ ) ) { return false; } /// fourth column: int in the range of -3...3 if ( ! s_IsTokenInteger( *i ) ) { return false; } int frame = NStr::StringToInt( *i++ ); if (frame < -3 || frame > 3) { return false; } /// fifth column: score; double if ( ! s_IsTokenDouble( *i ) ) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineGtf( const string& line ) { vector<string> tokens; if ( NStr::Tokenize( line, " \t", tokens, NStr::eMergeDelims ).size() < 8 ) { return false; } if ( ! s_IsTokenPosInt( tokens[3] ) ) { return false; } if ( ! s_IsTokenPosInt( tokens[4] ) ) { return false; } if ( ! s_IsTokenDouble( tokens[5] ) ) { return false; } if ( tokens[6].size() != 1 || NPOS == tokens[6].find_first_of( ".+-" ) ) { return false; } if ( tokens[7].size() != 1 || NPOS == tokens[7].find_first_of( ".0123" ) ) { return false; } if ( tokens.size() < 9 || (NPOS == tokens[8].find( "gene_id" ) && NPOS == tokens[8].find( "transcript_id" ) ) ) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineGvf( const string& line ) // ---------------------------------------------------------------------------- { vector<string> tokens; if ( NStr::Tokenize( line, " \t", tokens, NStr::eMergeDelims ).size() < 8 ) { return false; } if ( ! s_IsTokenPosInt( tokens[3] ) ) { return false; } if ( ! s_IsTokenPosInt( tokens[4] ) ) { return false; } //make sure that "type" is a GVF admissible value: {{ bool typeOk = false; list<string> terms; terms.push_back("snv"); terms.push_back("cnv"); terms.push_back("copy_number_variation"); terms.push_back("gain"); terms.push_back("copy_number_gain"); terms.push_back("loss"); terms.push_back("copy_number_loss"); terms.push_back("loss_of_heterozygosity"); terms.push_back("complex"); terms.push_back("complex_substitution"); terms.push_back("complex_sequence_alteration"); terms.push_back("indel"); terms.push_back("insertion"); terms.push_back("inversion"); terms.push_back("substitution"); terms.push_back("deletion"); terms.push_back("duplication"); terms.push_back("translocation"); terms.push_back("upd"); terms.push_back("uniparental_disomy"); terms.push_back("maternal_uniparental_disomy"); terms.push_back("paternal_uniparental_disomy"); terms.push_back("tandom_duplication"); terms.push_back("structural_variation"); terms.push_back("sequence_alteration"); ITERATE(list<string>, termiter, terms) { if(NStr::EqualNocase(*termiter, tokens[2])) { typeOk = true; break; } } if (!typeOk) { return false; } }} if ( ! s_IsTokenDouble( tokens[5] ) ) { return false; } if ( tokens[6].size() != 1 || NPOS == tokens[6].find_first_of( ".+-" ) ) { return false; } if ( tokens[7].size() != 1 || NPOS == tokens[7].find_first_of( ".0123" ) ) { return false; } //make sure all the mandatory attributes are present: string attrs = tokens[8]; if (string::npos == attrs.find("ID=")) return false; if (string::npos == attrs.find("Variant_seq=")) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineGff3( const string& line ) { vector<string> tokens; if ( NStr::Tokenize( line, " \t", tokens, NStr::eMergeDelims ).size() < 8 ) { return false; } if ( ! s_IsTokenPosInt( tokens[3] ) ) { return false; } if ( ! s_IsTokenPosInt( tokens[4] ) ) { return false; } if ( ! s_IsTokenDouble( tokens[5] ) ) { return false; } if ( tokens[6].size() != 1 || NPOS == tokens[6].find_first_of( ".+-" ) ) { return false; } if ( tokens[7].size() != 1 || NPOS == tokens[7].find_first_of( ".0123" ) ) { return false; } if ( tokens.size() < 9 || tokens[8].empty()) { return false; } if ( tokens.size() >= 9 && tokens[8].size() > 1) { const string& col9 = tokens[8]; if ( NPOS == NStr::FindNoCase(col9, "ID") && NPOS == NStr::FindNoCase(col9, "Parent") && NPOS == NStr::FindNoCase(col9, "Target") && NPOS == NStr::FindNoCase(col9, "Name") && NPOS == NStr::FindNoCase(col9, "Alias") && NPOS == NStr::FindNoCase(col9, "Note") && NPOS == NStr::FindNoCase(col9, "Dbxref") && NPOS == NStr::FindNoCase(col9, "Xref") ) { return false; } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineGff2( const string& line ) { vector<string> tokens; if ( NStr::Tokenize( line, " \t", tokens, NStr::eMergeDelims ).size() < 8 ) { return false; } if ( ! s_IsTokenPosInt( tokens[3] ) ) { return false; } if ( ! s_IsTokenPosInt( tokens[4] ) ) { return false; } if ( ! s_IsTokenDouble( tokens[5] ) ) { return false; } if ( tokens[6].size() != 1 || NPOS == tokens[6].find_first_of( ".+-" ) ) { return false; } if ( tokens[7].size() != 1 || NPOS == tokens[7].find_first_of( ".0123" ) ) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLinePhrapId( const string& line ) { vector<string> values; if ( NStr::Tokenize( line, " \t", values, NStr::eMergeDelims ).empty() ) { return false; } // // Old style: "^DNA \\w+ " // if ( values[0] == "DNA" ) { return true; } // // New style: "^AS [0-9]+ [0-9]+" // if ( values[0] == "AS" ) { return ( 0 <= NStr::StringToNonNegativeInt( values[1] ) && 0 <= NStr::StringToNonNegativeInt( values[2] ) ); } return false; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineRmo( const string& line ) { const size_t MIN_VALUES_PER_RECORD = 14; // // Make sure there is enough stuff on that line: // list<string> values; if ( NStr::Split( line, " \t", values ).size() < MIN_VALUES_PER_RECORD ) { return false; } // // Look at specific values and make sure they are of the correct type: // // 1: positive integer: list<string>::iterator it = values.begin(); if ( ! s_IsTokenPosInt( *it ) ) { return false; } // 2: float: ++it; if ( ! s_IsTokenDouble( *it ) ) { return false; } // 3: float: ++it; if ( ! s_IsTokenDouble( *it ) ) { return false; } // 4: float: ++it; if ( ! s_IsTokenDouble( *it ) ) { return false; } // 5: string, not checked ++it; // 6: positive integer: ++it; if ( ! s_IsTokenPosInt( *it ) ) { return false; } // 7: positive integer: ++it; if ( ! s_IsTokenPosInt( *it ) ) { return false; } // 8: positive integer, likely in paretheses, not checked: ++it; // 9: '+' or 'C': ++it; if ( *it != "+" && *it != "C" ) { return false; } // and that's enough for now. But there are at least two more fields // with values that look testable. return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsAsnComment( const vector<string>& Fields ) { if ( Fields.size() == 0 ) { return true; } return ( NStr::StartsWith( Fields[0], "--" ) ); } // ---------------------------------------------------------------------------- bool CFormatGuess::EnsureSplitLines() // ---------------------------------------------------------------------------- { if ( m_bSplitDone ) { return !m_TestLines.empty(); } m_bSplitDone = true; // // Make sure the given data is ASCII before checking potential line breaks: // const size_t MIN_HIGH_RATIO = 20; size_t high_count = 0; for ( streamsize i=0; i < m_iTestDataSize; ++i ) { if ( 0x80 & m_pTestBuffer[i] ) { ++high_count; } } if ( 0 < high_count && m_iTestDataSize / high_count < MIN_HIGH_RATIO ) { return false; } // // Let's expect at least one line break in the given data: // string data( m_pTestBuffer, (size_t)m_iTestDataSize ); m_TestLines.clear(); if ( string::npos != data.find( "\r\n" ) ) { NStr::Split( data, "\r\n", m_TestLines ); } else if ( string::npos != data.find( "\n" ) ) { NStr::Split( data, "\n", m_TestLines ); } else if ( string::npos != data.find( "\r" ) ) { NStr::Split( data, "\r", m_TestLines ); } else { //single truncated line return false; } if ( m_iTestDataSize == s_iTestBufferSize && m_TestLines.size() > 1 ) { m_TestLines.pop_back(); } return !m_TestLines.empty(); } // ---------------------------------------------------------------------------- bool CFormatGuess::IsAsciiText() { const double REQUIRED_ASCII_RATIO = 0.9; // first stab - are we text? comments are only valid if we are text size_t count = 0; size_t count_print = 0; for (int i = 0; i < m_iTestDataSize; ++i, ++count) { if (isprint((unsigned char) m_pTestBuffer[i])) { ++count_print; } } if (count_print < count * REQUIRED_ASCII_RATIO) { return false; } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsAllComment() { if (!IsAsciiText()) { return false; } m_bSplitDone = false; m_TestLines.clear(); EnsureSplitLines(); ITERATE(list<string>, it, m_TestLines) { if(it->empty()) { continue; } else if(NStr::StartsWith(*it, "#")) { continue; } else if(NStr::StartsWith(*it, "--")) { continue; } else { return false; } } return true; } // ---------------------------------------------------------------------------- bool CFormatGuess::IsLineHgvs( const string& line ) { // This simple check can mistake Newwick, so Newwick is checked first // /[:alnum:]+:(g|c|r|p|m|mt|n)\.[:alnum:]+/ as in NC_000001.9:g.1234567C>T int State = 0; ITERATE(string, Iter, line) { char Char = *Iter; char Next = '\0'; string::const_iterator NextI = Iter; ++NextI; if(NextI != line.end()) Next = *NextI; if(State == 0) { if( isalnum(Char) ) State++; } else if(State == 1) { if(Char == ':') State++; } else if(State == 2) { if (Char == 'g' || Char == 'c' || Char == 'r' || Char == 'p' || Char == 'n' || Char == 'm' ) { State++; if (Char=='m' && Next == 't') { ++Iter; } } else { return false; } } else if(State == 3) { if(Char == '.') State++; else return false; } else if(State == 4) { if( isalnum(Char) ) State++; } } return (State == 5); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.