1 #include <regex>
2 #include <cctype>
3
4 #include "constants.h"
5 #include "utils.h"
6
7 using namespace std;
8
stacks_handle_exceptions(const exception & e)9 int stacks_handle_exceptions(const exception& e) {
10 std::cerr << "Aborted.";
11 if (typeid(e) != typeid(std::exception)) {
12 std::cerr << " (" << e.what();
13 if (typeid(e) == typeid(std::bad_alloc))
14 cerr << " -- did you run out of memory?";
15 std::cerr << ")";
16 }
17 std::cerr << "\n";
18 return 1;
19 }
20
21 const
22 map<string, FileT> known_extensions = {
23 {".fa", FileT::fasta},
24 {".fasta", FileT::fasta},
25 {".fa.gz", FileT::gzfasta},
26 {".fasta.gz", FileT::gzfasta},
27 {".fq", FileT::fastq},
28 {".fastq", FileT::fastq},
29 {".fq.gz", FileT::gzfastq},
30 {".fastq.gz", FileT::gzfastq},
31 {".sam", FileT::sam},
32 {".bam", FileT::bam},
33 {".vcf", FileT::vcf},
34 {".vcf.gz", FileT::gzvcf},
35 {".calls", FileT::gzvcf}
36 };
37
remove_suffix(FileT type,const string & orig)38 string remove_suffix(FileT type, const string& orig) {
39 string file (orig);
40
41 int pos = file.find_last_of(".");
42
43 if ((type == FileT::gzfastq || type == FileT::gzfasta) && file.substr(pos) == ".gz")
44 file = file.substr(0, pos);
45
46 pos = file.find_last_of(".");
47
48 if (type == FileT::gzfastq || type == FileT::fastq) {
49
50 if (file.substr(pos) == ".fastq" || file.substr(pos) == ".fq")
51 file = file.substr(0, pos);
52
53 } else if (type == FileT::gzfasta || type == FileT::fasta) {
54
55 if (file.substr(pos) == ".fasta" || file.substr(pos) == ".fa")
56 file = file.substr(0, pos);
57 }
58
59 return file;
60 }
61
init_file_ext_regex()62 regex init_file_ext_regex () {
63 vector<string> exts;
64 for (auto& filet : known_extensions)
65 exts.push_back(filet.first);
66
67 stringstream ss;
68 ss << "(";
69 join(exts, '|', ss);
70 ss << ")$";
71
72 string s = ss.str();
73 escape_char('.', s);
74
75 return regex(s);
76 }
77
guess_file_type(const string & path)78 FileT guess_file_type (const string& path) {
79
80 static const regex reg = init_file_ext_regex();
81
82 // Apply std::tolower.
83 string copy = path;
84 for (char& c : copy)
85 c = tolower(c);
86
87 smatch m;
88 regex_search(copy, m, reg);
89
90 if (m.empty())
91 return FileT::unknown;
92 else
93 return known_extensions.at(m.str());
94 }
95
escape_char(char c,string & s)96 void escape_char(char c, string& s) {
97 vector<size_t> dots;
98 size_t i = -1;
99 while ((i = s.find(c, i+1)) != string::npos)
100 dots.push_back(i);
101
102 for(auto j=dots.rbegin(); j!=dots.rend(); ++j)
103 s.insert(*j, 1, '\\');
104 }
105