]> git.donarmstrong.com Git - rsem.git/blob - utils.h
278e95e9272ca67f36de745dc924f40fd7085ca6
[rsem.git] / utils.h
1 #ifndef UTILS
2 #define UTILS
3
4 #include<cmath>
5 #include<ctime>
6 #include<cstdio>
7 #include<cctype>
8 #include<cstdlib>
9 #include<cstring>
10 #include<cassert>
11 #include<string>
12 #include<vector>
13
14 const int STRLEN = 10005 ;
15 const double EPSILON = 1e-300;
16 const double MINEEL = 1.0;
17 const double ORIVALVE = 0.1;
18 const int RANGE = 201;
19 const int OLEN = 25; // overlap length, number of bases must not be in poly(A) tails
20 const int NBITS = 32; // use unsigned int, 32 bits per variable
21
22 bool verbose = true; // show detail intermediate outputs
23
24 inline bool isZero(double a) { return fabs(a) < 1e-8; }
25 inline bool isLongZero(double a) { return fabs(a) < 1e-30; }
26
27 // Assume char's range is -128..127
28 const int CHAR_RANGE = 128;
29
30 static std::vector<int> init_base2id() {
31   std::vector<int> vec(CHAR_RANGE, -1);
32   vec['a'] = vec['A'] = 0;
33   vec['c'] = vec['C'] = 1;
34   vec['g'] = vec['G'] = 2;
35   vec['t'] = vec['T'] = 3;
36   vec['n'] = vec['N'] = 4;
37
38   return vec;
39 }
40
41 static const std::vector<int> base2id = init_base2id();
42
43 inline int get_base_id(char c) {
44   if (c < 0 || base2id[c] < 0) {
45     fprintf(stderr, "Found unknown sequence letter %c at function get_base_id!\n", c);
46     exit(-1);
47   }
48   return base2id[c];
49 }
50
51 static std::vector<int> init_rbase2id() {
52   std::vector<int> vec(CHAR_RANGE, -1);
53   vec['a'] = vec['A'] = 3;
54   vec['c'] = vec['C'] = 2;
55   vec['g'] = vec['G'] = 1;
56   vec['t'] = vec['T'] = 0;
57   vec['n'] = vec['N'] = 4;
58
59   return vec;
60 }
61
62 static const std::vector<int> rbase2id = init_rbase2id();
63
64 inline int get_rbase_id(char c) {
65   if (c < 0 || rbase2id[c] < 0) {
66     fprintf(stderr, "Found unknown sequence letter %c at function get_rbase_id!\n", c);
67     exit(-1);
68   }
69   return rbase2id[c];
70 }
71
72 inline char getOpp(char c) {
73   switch(c) {
74   case 'a' : return 't';
75   case 'c' : return 'g';
76   case 'g' : return 'c';
77   case 't' : return 'a';
78   case 'n' : return 'n';
79   case 'A' : return 'T';
80   case 'C' : return 'G';
81   case 'G' : return 'C';
82   case 'T' : return 'A';
83   case 'N' : return 'N';
84   default :
85         fprintf(stderr, "Found unknown sequence letter %c!\n", c);
86         exit(-1);
87   }
88 }
89
90 inline char getCharacter(int id) {
91   switch(id) {
92   case 0 : return 'A';
93   case 1 : return 'C';
94   case 2 : return 'G';
95   case 3 : return 'T';
96   case 4 : return 'N';
97   default :
98           fprintf(stderr, "Found unknown id %d!\n", id);
99           exit(-1);
100   }
101 }
102
103 static std::vector<unsigned int> init_mask_code() {
104   std::vector<unsigned int> vec(NBITS);
105   for (int i = 0; i < NBITS; i++) vec[i] = 1 << i;
106   return vec;
107 }
108
109 static std::vector<unsigned int> mask_codes = init_mask_code();
110
111 inline std::string cleanStr(const std::string& str) {
112   int len = str.length();
113   int fr, to;
114
115   fr = 0;
116   while (fr < len && isspace(str[fr])) ++fr;
117   to = len - 1;
118   while (to >= 0 && isspace(str[to])) --to;
119
120   return (fr <= to ? str.substr(fr, to - fr + 1) : "");
121 }
122
123 void printTimeUsed(const time_t& a, const time_t& b, const char* filename = "") {
124         int hh = (b - a) / 3600;
125         int mm = (b - a) % 3600 / 60;
126         int ss = (b - a) % 60;
127
128         printf("Time Used : %d h %02d m %02d s\n", hh, mm, ss);
129
130         if (strcmp(filename, "")) {
131                 FILE *fo = fopen(filename, "w");
132                 fprintf(fo, "Time Used : %d h %02d m %02d s\n", hh, mm, ss);
133                 fclose(fo);
134         }
135 }
136
137 void genReadFileNames(const char* readFN, int tagType, int read_type, int& s, char readFs[][STRLEN]){
138         const char tags[3][STRLEN] = {"un", "alignable", "max"};
139         char suffix[STRLEN];
140
141         if (read_type == 0 || read_type == 2) {
142                 strcpy(suffix, "fa");
143         }
144         else {
145                 strcpy(suffix, "fq");
146         }
147
148         if (read_type == 0 || read_type == 1) {
149                 s = 1;
150                 sprintf(readFs[0], "%s_%s.%s", readFN, tags[tagType], suffix);
151         }
152         else {
153                 s = 2;
154                 sprintf(readFs[0], "%s_%s_1.%s", readFN, tags[tagType], suffix);
155                 sprintf(readFs[1], "%s_%s_2.%s", readFN, tags[tagType], suffix);
156         }
157 }
158
159 #endif