-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDnaDictionary.cpp
175 lines (153 loc) · 4.61 KB
/
DnaDictionary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
+
+ DnaDictionary.cpp
+
+ Copyright (c) 2002 Genoscope, CEA, CNS, Evry, France
+ Author : Jean-Marc Aury, [email protected]
+
*******************************************************************************/
#include <malloc.h>
#include "DnaDictionary.h"
using namespace std;
// to insert a new word in the dictionary
void DnaDictionary::_insert(TDnaWord& w, s32 multiplicator) {
Tdictionary::iterator it = _dictionary.find(w);
if (it == _dictionary.end()) {
it = _dictionary.insert(make_pair(w,0)).first;
_nbDiffWords++;
}
it->second+=multiplicator;
_nbWords++;
}
// to shift in position each time a "N" is found (begin a new word) to only add determined words to the dictionary
bool DnaDictionary::beginWord(const string& s, s32& pos, TDnaWord& fwd, TDnaWord& rev) {
fwd = 0;
rev = 0;
s32 i = 0, N = s.length();
if ((N - pos) < _wordlen) { return false; }
for(i = 0; i < _wordlen - 1; i++) {
if(_packedFwdWord(fwd, s[pos])) {
_packedRevWord(rev, s[pos]);
pos++;
}
else {
pos++;
return beginWord(s, pos, fwd, rev);
}
}
return true;
}
// to extract all words present in a string and insert them into the dictionary
void DnaDictionary::countWords(const string& s, s32 multiplicator) {
TDnaWord fwd = 0, rev = 0;
s32 i = 0, N = s.length();
if(!beginWord(s, i, fwd, rev)) {return;}
Tdictionary::iterator it;
while (i < N) {
if(!_packedFwdWord(fwd, s[i])) {
i++;
if(!beginWord(s, i, fwd, rev)) {
return;}
continue;
}
_packedRevWord(rev, s[i]);
string word = s.substr(i-_wordlen+1, _wordlen);
if(!isLowComplexity(word))
if (fwd < rev) { _insert(fwd, multiplicator); }
else { _insert(rev, multiplicator);}
else _discardedW++;
i++;
}
return;
}
// to test existence of a word (TDnaWord form)
bool DnaDictionary::existWord(const TDnaWord& w) {
Tdictionary::iterator it = _dictionary.find(w);
if (it == _dictionary.end()) return FALSE;
return TRUE;
}
// to test existence of a word (string form)
bool DnaDictionary::existWord(const string& s) {
if((s32)s.length() != _wordlen) return FALSE;
TDnaWord fwd=0, rev=0;
s32 oldBadchar = _badchar;
for (s32 i = 0; i < _wordlen; i++) {
_packedFwdWord(fwd, s[i]);
_packedRevWord(rev, s[i]);
}
if(oldBadchar != _badchar){
_badchar = oldBadchar; //_badchar represents bad chars present in the dictionary, should not change when testing words
return FALSE;
}
//return (fwd < rev) ? existWord(fwd) : existWord(rev);
Tdictionary::iterator it = _dictionary.find(fwd);
if (it != _dictionary.end()) return TRUE;
it = _dictionary.find(rev);
if (it != _dictionary.end()) return TRUE;
return FALSE;
}
// to find the number of occurences of a word (TDnaWord form)
s32 DnaDictionary::nbOccWord(const TDnaWord& w) {
Tdictionary::iterator it = _dictionary.find(w);
if (it == _dictionary.end()) return 0;
return it->second;
}
// to find the number of occurences of a word (string form)
s32 DnaDictionary::nbOccWord(const string& s) {
if((s32)s.length() != _wordlen) return 0;
TDnaWord fwd=0, rev=0;
for (s32 i = 0; i < _wordlen; i++) {
_packedFwdWord(fwd, s[i]);
_packedRevWord(rev, s[i]);
}
Tdictionary::iterator it = _dictionary.find(fwd);
if (it != _dictionary.end()) return it->second;
it = _dictionary.find(rev);
if (it != _dictionary.end()) return it->second;
return 0;
}
ostream& operator<<(ostream& ostr, DnaDictionary& d) {
Tdictionary::iterator it;
for( it=(d._dictionary).begin(); it != (d._dictionary).end(); it++ ) {
if(it != (d._dictionary).begin()) { ostr << endl; }
string word;
TDnaWord w = it->first;
d.bin2String(w, word);
ostr << word << " " << it->second;
}
return ostr;
}
// to test a word for low complexity
bool DnaDictionary::isLowComplexity(string& mot) { //MTF algo
if(!_testlowcomplexity) return false;
u8 i,j;
char c;
char dico[4];
dico[0] = 'a';
dico[1] = 't';
dico[2] = 'g';
dico[3] = 'c';
s32 score = 0;
for (i=0; i < mot.length(); i++){
c = tolower(mot[i]);
for (j=0; j<4; j++){
if (dico[j]==c) break;
}
score=score+j;
while (j>0){
dico[j]=dico[j-1];
j--;
}
dico[0]=c;
}
if (score <= ((s32)mot.length() / 2)) return true;
else return false;
}
// to clean unique words
void DnaDictionary::cleanUM (){
Tdictionary::iterator it, itPrec;
for( it = _dictionary.begin(); it != _dictionary.end(); ) {
if(it->second == 1) { itPrec=it; it++; _dictionary.erase(itPrec); }
else it++;
}
}