File: ShortReadUtil.cpp

package info (click to toggle)
perm 0.4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 976 kB
  • sloc: cpp: 13,499; makefile: 98; sh: 12
file content (252 lines) | stat: -rw-r--r-- 6,428 bytes parent folder | download | duplicates (5)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "stdafx.h"
#include "ShortReadUtil.h"

void toUpperCase(char* caArray, int length)
{
    int i;
    for (i = 0; i < length; i++)	{
        caArray[i] = (char)toupper(caArray[i]);
    }
}

char* mutateRead(char* Kmer , unsigned int No_of_mutation)
{
    //This function simply generate mutations in a given string
    //To guarantee exactly No_of_mutation loci have been changed, transfer the original nt + a random number between 1-3

    const int MAX_MUTATION = 64;
    int mutatedPosition[MAX_MUTATION];
    unsigned int l = (unsigned int)strlen(Kmer);

    if (No_of_mutation > l) {
        cout << "More mutation than it could be" << endl;
        No_of_mutation = l;
    }
    //Pick the mutation position in advance and avoid picking the same position
    for (unsigned int i = 0; i < No_of_mutation; i++) {
        mutatedPosition[i] = rand() % l;
        for (unsigned int j = 0; j < i; j++) {
            if (mutatedPosition[i] == mutatedPosition[j]) {
                i--;
                break;
            }
        }
    }
    //Mutated the selected base
    for (unsigned int i = 0; i < No_of_mutation; i++) {
        int position = mutatedPosition[i];
        mutateBase(&Kmer[position]);
    }
    return(Kmer);
}

void mutateBase(char* Base)
{
    int nt = rand() % 3 + 1;
    switch (*Base) {
    case 'A':
    case 'a':
        nt = (nt + 0) % 4;
        break;
    case 'C':
    case 'c':
        nt = (nt + 1) % 4;
        break;
    case 'G':
    case 'g':
        nt = (nt + 2) % 4;
        break;
    case 'T':
    case 't':
        nt = (nt + 3) % 4;
        break;
    default:
        ;//cout<<Kmer[position];
    }
    switch (nt) {
    case 0:
        *Base = 'A';
        break;
    case 1:
        *Base = 'C';
        break;
    case 2:
        *Base = 'G';
        break;
    case 3:
        *Base = 'T';
        break;
    default:
        cout << "different nucleotide" << endl;
    }
}

char* mutatePairsOfConsecutiveBases(char* Kmer, unsigned int no_of_mutated_pairs)
{
    //This function simply generate mutations in a given string
    //To guarantee exactly No_of_mutation loci have been changed, transfer the original nt + a random number between 1-3

    const int MAX_MUTATION = 50;
    int mutatedPosition[MAX_MUTATION];
    unsigned int l = (unsigned int)strlen(Kmer);
    if (l < no_of_mutated_pairs) {
        cout << "Mutate too many pairs" << endl;
        return Kmer;
    }

    //Pick the mutation position in advance and avoid picking the same position
    for (unsigned int i = 0; i < no_of_mutated_pairs; i++) {
        mutatedPosition[i] = rand() % (l - 1);
        for (unsigned int j = 0; j < i; j++) {
            if (mutatedPosition[i] == mutatedPosition[j]) {
                i--; //selected again
                break;
            }
        }
    }
    for (unsigned int i = 0; i < no_of_mutated_pairs; i++) {
        int position = mutatedPosition[i];
        mutateBase(&(Kmer[position]));
        mutateBase(&(Kmer[position + 1]));
    }
    return(Kmer);
}

bool isBadRead(const char* Read, unsigned int ReadLength)
{
    unsigned int i;
    for (i = 0; i < ReadLength; i++) {
        if (!isACGT(Read[i])) {
            return(true);// Bad Read
        }
    }
    return(false);// Good Read
}

bool isBadSOLiDRead(const char* Read, unsigned int ReadLength)
{
    if (!isACGT(Read[0])) {
        return(true);// Bad Read
    }
    unsigned int i;
    for (i = 1; i < ReadLength; i++) {
        if (!is0123(Read[i])) {
            return(true);// Bad Read
        }
    }
    return(false);// Good Read
}

bool isBadRead(bool isSOLiD, const char* Read, unsigned int ReadLength)
{
    if(isSOLiD) {
        return(isBadSOLiDRead(Read, ReadLength));
    } else {
        return(isBadRead(Read, ReadLength));
    }
}

char* reverseKmer(char* Kmer)
{
    if (Kmer != NULL) {
        unsigned int length = (unsigned int)strlen(Kmer);
        unsigned int i;
        for (i = 0; i < length / 2; i++)	{
            swap(Kmer[i], Kmer[length-1-i]);
        }
    }
    return(Kmer);
}

// return the complement kmer from 5'->3', destroy the original kmer
char* reverseComplementKmer(char* Kmer)
{
    if (Kmer != NULL) {
        unsigned int length = 0;
        length = (unsigned int)strlen(Kmer);
        unsigned int i;
        for (i = 0; i < length / 2; i++)	{
            swap(Kmer[i], Kmer[length-1-i]);
        }
        for (i = 0; i < length; i++) {
            Kmer[i] = complimentBase(Kmer[i]);
        }
    }
    return(Kmer);
}

unsigned int strComp(char* str1, char* str2, int l)
{
    int i;
    unsigned int miscounter = 0;
    for (i = 0; i < l; i++) {
        if (str1[i] != str2[i])
            miscounter++;
    }
    return(miscounter);
}

/*
 * This function compare two string. It lower-case the character in the
 * second string, if it mismatches the corresponding position in the first
 * string
 */
unsigned int strCompMarkDiff(char* str1, char* str2)
{
    unsigned int iDiff = 0;
    for (int i = 0; str1[i] != '\0' && str2[i] != '\0'; i++) {
        if (str1[i] != str2[i]) {
            iDiff++;
            str2[i] = (char)tolower(str2[i]);
        }
    }
    return(iDiff);
}
/* return the # of wild-card mismatches bases between the two read */
unsigned int diNtStrWildCardComp(char* read1, char* read2, unsigned int readlength)
{
    unsigned int uiDiff = 0;
    for (unsigned int i = 0; i < readlength ; i++) {
        if (!diNtWildCardComp(read1[i], read2[i])) {
            uiDiff++;
        }
    }
    return(uiDiff);
}

bool isDummyRead(const char* read, int dummyT)
{
    if (dummyT > 0) {
        int dummyCount = 1;
        if (read[0] == '\0') {
            return(false);
        }
        for ( int i = 1; read[i] != '\0'; i++) {
            if (read[i] != read[i-1]) {
                dummyCount = 1;
            } else {
                dummyCount++;
            }
            if (dummyCount >= dummyT) {
                break;
            }
        }
        return(dummyCount >= dummyT);
    } else {
        return(false);
    }
}

char getBaseFromColors(char nt, const char* colors, int pos)
{
    for (int i = 0; i < pos; i++) {
        if (is0123(colors[i])) {
            // TODO The function should be named as color2base
            // TODO It should be moved to color space read
            nt = base2color(nt, colors[i]);
        } else {
            return('N');
        }
    }
    return(nt);
}