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