1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #ifndef _FB_GENERAL_H_ |
---|
6 | #define _FB_GENERAL_H_ |
---|
7 | |
---|
8 | #include <frams/util/sstring.h> |
---|
9 | |
---|
10 | class fB_GenoHelpers |
---|
11 | { |
---|
12 | public: |
---|
13 | static int geneCount(const SString& geno) |
---|
14 | { |
---|
15 | int start = 0; |
---|
16 | int prev = 0; |
---|
17 | int count = -1; |
---|
18 | do { |
---|
19 | count++; |
---|
20 | start = geno.indexOf("aa", start) + 1; // +1 is for progress, starting codons can overlap |
---|
21 | int quotecount = 0; |
---|
22 | for (int q = prev; q < start; q++) if (geno[q] == '\"') quotecount++; |
---|
23 | prev = start; |
---|
24 | if (quotecount % 2 != 0) count--; // 'aa' sequence is within quotes |
---|
25 | } while (start - 1 != -1); |
---|
26 | return count; |
---|
27 | } |
---|
28 | |
---|
29 | static SString getNonNestedGene(int i, const SString& genotype, int &start, int &end) |
---|
30 | { |
---|
31 | int count = -1; |
---|
32 | start = 0; |
---|
33 | int tmp = 0; |
---|
34 | SString result = ""; |
---|
35 | do { |
---|
36 | count++; |
---|
37 | if (start < genotype.len()) |
---|
38 | result = getNextGene(start, genotype, tmp, start); |
---|
39 | else |
---|
40 | start = -1; |
---|
41 | } while (start != -1 && count < i); |
---|
42 | start = tmp; |
---|
43 | end = start; |
---|
44 | return result; |
---|
45 | } |
---|
46 | |
---|
47 | static int geneCountNoNested(const SString& geno) |
---|
48 | { |
---|
49 | int start = 0; |
---|
50 | int count = -1; |
---|
51 | int tmp = 0; |
---|
52 | do { |
---|
53 | count++; |
---|
54 | if (start < geno.len()) |
---|
55 | getNextGene(start, geno, tmp, start); |
---|
56 | else |
---|
57 | start = -1; |
---|
58 | } while (start != -1); |
---|
59 | return count; |
---|
60 | } |
---|
61 | |
---|
62 | static SString getGene(int i, const SString& genotype, int &start, int &end) |
---|
63 | { |
---|
64 | start = 0; |
---|
65 | int count = -1; |
---|
66 | do { |
---|
67 | count++; |
---|
68 | start = genotype.indexOf("aa", start) + 1; |
---|
69 | int quotecount = 0; |
---|
70 | for (int q = 0; q < start; q++) if (genotype[q] == '\"') quotecount++; |
---|
71 | if (quotecount % 2 != 0) count--; // 'aa' sequence is within quotes |
---|
72 | } while (start - 1 != -1 && count != i); |
---|
73 | if (start - 1 == -1) // there is no gene with a given "i" |
---|
74 | { |
---|
75 | start = -1; |
---|
76 | end = -1; |
---|
77 | return ""; |
---|
78 | } |
---|
79 | end = start; |
---|
80 | int quotecount = 0; |
---|
81 | do { |
---|
82 | quotecount = 0; |
---|
83 | end = genotype.indexOf("zz", end); |
---|
84 | if (end != -1) |
---|
85 | { |
---|
86 | for (int q = start; q < end; q++) if (genotype[q] == '\"') quotecount++; |
---|
87 | if (quotecount % 2 != 0) end++; |
---|
88 | } |
---|
89 | } while (quotecount % 2 != 0 && end != -1); |
---|
90 | |
---|
91 | if (end == -1) end = genotype.len(); |
---|
92 | else end += 2; |
---|
93 | start -= 1; |
---|
94 | return genotype.substr(start, end - start); |
---|
95 | } |
---|
96 | |
---|
97 | static SString getNextGene(int searchbegin, const SString& genotype, int &start, int &end) |
---|
98 | { |
---|
99 | start = searchbegin; |
---|
100 | int count = -1; |
---|
101 | do { |
---|
102 | count++; |
---|
103 | start = genotype.indexOf("aa", start) + 1; |
---|
104 | int quotecount = 0; |
---|
105 | for (int q = 0; q < start; q++) if (genotype[q] == '\"') quotecount++; |
---|
106 | if (quotecount % 2 != 0) count--; // 'aa' sequence is within quotes |
---|
107 | } while (start - 1 != -1 && count != 0); |
---|
108 | if (start - 1 == -1) // there is no gene with a given "i" |
---|
109 | { |
---|
110 | start = -1; |
---|
111 | end = -1; |
---|
112 | return ""; |
---|
113 | } |
---|
114 | end = start; |
---|
115 | int quotecount = 0; |
---|
116 | do { |
---|
117 | quotecount = 0; |
---|
118 | end = genotype.indexOf("zz", end); |
---|
119 | if (end != -1) |
---|
120 | { |
---|
121 | for (int q = start; q < end; q++) if (genotype[q] == '\"') quotecount++; |
---|
122 | if (quotecount % 2 != 0) end++; |
---|
123 | } |
---|
124 | } while (quotecount % 2 != 0 && end != -1); |
---|
125 | |
---|
126 | if (end == -1) end = genotype.len(); |
---|
127 | else end += 2; |
---|
128 | start -= 1; |
---|
129 | return genotype.substr(start, end - start); |
---|
130 | } |
---|
131 | private: |
---|
132 | fB_GenoHelpers() {} |
---|
133 | }; |
---|
134 | |
---|
135 | #endif // _FB_GENERAL_H_ |
---|