]> git.donarmstrong.com Git - rsem.git/blob - utils.h
rsem v1.1.14
[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 #include<cerrno>
14
15 const int STRLEN = 10005 ;
16 const double EPSILON = 1e-300;
17 const double MINEEL = 1.0;
18 const double ORIVALVE = 0.1;
19 const int RANGE = 201;
20 const int OLEN = 25; // overlap length, number of bases must not be in poly(A) tails
21 const int NBITS = 32; // use unsigned int, 32 bits per variable
22
23 bool verbose = true; // show detail intermediate outputs
24
25 inline bool isZero(double a) { return fabs(a) < 1e-8; }
26 inline bool isLongZero(double a) { return fabs(a) < 1e-30; }
27
28 // Assume char's range is -128..127
29 const int CHAR_RANGE = 128;
30
31 static std::vector<int> init_base2id() {
32   std::vector<int> vec(CHAR_RANGE, -1);
33   vec['a'] = vec['A'] = 0;
34   vec['c'] = vec['C'] = 1;
35   vec['g'] = vec['G'] = 2;
36   vec['t'] = vec['T'] = 3;
37   vec['n'] = vec['N'] = 4;
38
39   return vec;
40 }
41
42 static const std::vector<int> base2id = init_base2id();
43
44 inline int get_base_id(char c) {
45   if (c < 0 || base2id[c] < 0) {
46     fprintf(stderr, "Found unknown sequence letter %c at function get_base_id!\n", c);
47     exit(-1);
48   }
49   return base2id[c];
50 }
51
52 static std::vector<int> init_rbase2id() {
53   std::vector<int> vec(CHAR_RANGE, -1);
54   vec['a'] = vec['A'] = 3;
55   vec['c'] = vec['C'] = 2;
56   vec['g'] = vec['G'] = 1;
57   vec['t'] = vec['T'] = 0;
58   vec['n'] = vec['N'] = 4;
59
60   return vec;
61 }
62
63 static const std::vector<int> rbase2id = init_rbase2id();
64
65 inline int get_rbase_id(char c) {
66   if (c < 0 || rbase2id[c] < 0) {
67     fprintf(stderr, "Found unknown sequence letter %c at function get_rbase_id!\n", c);
68     exit(-1);
69   }
70   return rbase2id[c];
71 }
72
73 inline char getOpp(char c) {
74   switch(c) {
75   case 'a' : return 't';
76   case 'c' : return 'g';
77   case 'g' : return 'c';
78   case 't' : return 'a';
79   case 'n' : return 'n';
80   case 'A' : return 'T';
81   case 'C' : return 'G';
82   case 'G' : return 'C';
83   case 'T' : return 'A';
84   case 'N' : return 'N';
85   default :
86         fprintf(stderr, "Found unknown sequence letter %c!\n", c);
87         exit(-1);
88   }
89 }
90
91 inline char getCharacter(int id) {
92   switch(id) {
93   case 0 : return 'A';
94   case 1 : return 'C';
95   case 2 : return 'G';
96   case 3 : return 'T';
97   case 4 : return 'N';
98   default :
99           fprintf(stderr, "Found unknown id %d!\n", id);
100           exit(-1);
101   }
102 }
103
104 static std::vector<unsigned int> init_mask_code() {
105   std::vector<unsigned int> vec(NBITS);
106   for (int i = 0; i < NBITS; i++) vec[i] = 1 << i;
107   return vec;
108 }
109
110 static std::vector<unsigned int> mask_codes = init_mask_code();
111
112 inline std::string cleanStr(const std::string& str) {
113   int len = str.length();
114   int fr, to;
115
116   fr = 0;
117   while (fr < len && isspace(str[fr])) ++fr;
118   to = len - 1;
119   while (to >= 0 && isspace(str[to])) --to;
120
121   return (fr <= to ? str.substr(fr, to - fr + 1) : "");
122 }
123
124 void printTimeUsed(const time_t& a, const time_t& b, const char* filename = "") {
125         int hh = (b - a) / 3600;
126         int mm = (b - a) % 3600 / 60;
127         int ss = (b - a) % 60;
128
129         printf("Time Used : %d h %02d m %02d s\n", hh, mm, ss);
130
131         if (strcmp(filename, "")) {
132                 FILE *fo = fopen(filename, "w");
133                 fprintf(fo, "Time Used : %d h %02d m %02d s\n", hh, mm, ss);
134                 fclose(fo);
135         }
136 }
137
138 void genReadFileNames(const char* readFN, int tagType, int read_type, int& s, char readFs[][STRLEN]){
139         const char tags[3][STRLEN] = {"un", "alignable", "max"};
140         char suffix[STRLEN];
141
142         if (read_type == 0 || read_type == 2) {
143                 strcpy(suffix, "fa");
144         }
145         else {
146                 strcpy(suffix, "fq");
147         }
148
149         if (read_type == 0 || read_type == 1) {
150                 s = 1;
151                 sprintf(readFs[0], "%s_%s.%s", readFN, tags[tagType], suffix);
152         }
153         else {
154                 s = 2;
155                 sprintf(readFs[0], "%s_%s_1.%s", readFN, tags[tagType], suffix);
156                 sprintf(readFs[1], "%s_%s_2.%s", readFN, tags[tagType], suffix);
157         }
158 }
159
160 void exitWithError(const char* errmsg) {
161         fprintf(stderr, "%s\n", errmsg);
162         exit(-1);
163 }
164
165 void pthread_exception(int rc) {
166         switch(rc) {
167         case EAGAIN:
168                 fprintf(stderr, "Error code: EAGAIN. Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered.\n");
169                 break;
170         case EINVAL:
171                 fprintf(stderr, "Error code: EINVAL. Invalid settings in attr if pthread_create() is called. Or the implementation has detected that the value specified by thread_id does not refer to a joinable thread if pthread_join() is called.\n");
172                 break;
173         case EPERM:
174                 fprintf(stderr, "Error code: EPERM. No permission to set the scheduling policy and parameters specified in attr.\n");
175                 break;
176         case EDEADLK:
177                 fprintf(stderr, "Error code: EDEADLK. A deadlock was detected (e.g., two threads tried to join with each other); or thread_id specifies the calling thread.");
178                 break;
179         case ESRCH:
180                 fprintf(stderr, "Error code: ESRCH. No thread with thread_id could be found.\n");
181                 break;
182         default: fprintf(stderr, "Unknown error code: %d\n", rc);
183         }
184         exit(-1);
185 }
186
187 #endif