Debian Codegraph @

SeqLib/SeqLibUtils.h

raw symbols SCIP
1 #ifndef SEQLIB_UTILS_H
2 #define SEQLIB_UTILS_H
3
4 #include <string>
5 #include <time.h>
6 #include <ctime>
7 #include <vector>
8 #include <unistd.h>
9 #include <sstream>
10 #include <cmath>
11 #include <algorithm>
12 #include <stdio.h>
13
14 #include "SeqLib/SeqLibCommon.h"
15
16
17 #if __cplusplus > 199711L
18 #include <memory>
19 #include <unordered_set>
20 #include <unordered_map>
21 #define SeqHashMap std::unordered_map
22 #define SeqHashSet std::unordered_set
23 #define SeqPointer std::shared_ptr
24 #define HAVE_C11 1
25 #else
26
27 #ifdef __APPLE__
28 #include <memory>
29 #include <unordered_set>
30 #include <unordered_map>
31 #define SeqHashMap std::unordered_map
32 #define SeqHashSet std::unordered_set
33 #define SeqPointer std::shared_ptr
34 #else
35 #include <tr1/memory>
36 #include <tr1/unordered_set>
37 #include <tr1/unordered_map>
38 #define SeqHashMap std::tr1::unordered_map
39 #define SeqHashSet std::tr1::unordered_set
40 #define SeqPointer std::tr1::shared_ptr
41 #endif
42 #endif
43
44 namespace SeqLib {
45
46 template<typename T>
47 inline std::string tostring(T d) {
48 std::stringstream ss;
49 ss << d;
50 return ss.str();
51 }
52
53 /** Check if a file is readable and exists
54 * @param name Name of a file to test
55 * @return File is readable and exists
56 */
57 inline bool read_access_test (const std::string& name) {
58 return (access (name.c_str(), R_OK) == 0);
59 }
60
61 /** Format an integer to include commas
62 * @param data Number to format
63 * @return String with formatted number containing commas
64 */
65 template <typename T> inline
66 std::string AddCommas(T data) {
67 std::stringstream ss;
68 ss << data;
69 std::string s = ss.str();
70 if (s.length() > 3)
71 for (int i = s.length()-3; i > 0; i -= 3)
72 s.insert(i,",");
73 return s;
74 }
75
76 /** Display the runtime (CPU and Wall)
77 *
78 * @param start Running timer
79 * @return Time formatted as "CPU: XmYs Wall: XmYs"
80 * @note Does not work on OSX or Windows (returns "not configured")
81 */
82 inline std::string displayRuntime(
83 #ifndef __APPLE__
84 const timespec start
85 #endif
86 ) {
87
88 #ifndef __APPLE__
89 struct timespec finish;
90 clock_gettime(CLOCK_MONOTONIC, &finish);
91 double elapsed = (finish.tv_sec - start.tv_sec);
92 int t = clock()/CLOCKS_PER_SEC;
93 int min = (int)std::floor(elapsed / 60.0);
94 int sec = (int)(elapsed-min*60);
95 char buffer[100];
96 sprintf (buffer, "CPU: %4dm%02ds Wall: %4dm%02ds",
97 (int)floor( ((double)t) /60.0), t % 60, min, sec);
98 buffer[99] = '\0';
99 return std::string(buffer);
100 #else
101 return "--- time not configured for apple\n";
102 #endif
103 }
104
105 /** Reverse complement in-place sequence containg upper/lower case ACTGN
106 * @param a Sequence to be reverse complmented
107 */
108 inline void rcomplement(std::string &a) {
109
110 std::reverse(&a[0], &a[a.size()]);
111 std::string::iterator it = a.begin();
112 for (; it != a.end(); it++)
113 *it = RCOMPLEMENT_TABLE[(unsigned char)*it];
114 }
115
116
117 /** Calculate the percentage and return as integer
118 * @param numer Numerator
119 * @param denom Denominator
120 * @return Integer with the percentage floored
121 */
122 template <typename T> inline int percentCalc(T numer, T denom) {
123 if (denom <= 0)
124 return 0;
125 int perc = numer * 100 / denom;
126 //int perc = static_cast<int>(floor((float)numer / (float)denom * 100.0));
127 return perc;
128 }
129
130 /** Remove substrings from a string
131 * @param toscrub Input string to clean
132 * @param toremove Substring to remove from input
133 * @return Scrubbed string
134 */
135 inline std::string scrubString(const std::string& toscrub, const std::string& toremove)
136 {
137 if (toscrub.empty() || toremove.empty())
138 return toscrub;
139
140 std::string::size_type i = toscrub.find(toremove);
141 if (i == std::string::npos)
142 return toscrub;
143
144 std::string ts = toscrub;
145 while (i != std::string::npos) {
146 ts.erase(i, toremove.length());
147 i = ts.find(toremove);
148 }
149 return ts;
150 }
151
152 // Generate a weighed random integer
153 // @param cs Weighting for each integer (values must sum to one)
154 // @return Random integer bounded on [0,cs.size())
155 //
156 //int weightedRandom(const std::vector<double>& cs);
157
158 }
159
160 #endif