File: sequenceUtils.cpp

package info (click to toggle)
bedtools 2.27.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 54,804 kB
  • sloc: cpp: 38,072; sh: 7,307; makefile: 2,241; python: 163
file content (222 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download | duplicates (2)
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
//
//  sequenceUtils.cpp
//  BEDTools
//
//  Created by Aaron Quinlan Spring 2009.
//  Copyright 2009 Aaron Quinlan. All rights reserved.
//
//  Summary:  Contains common functions for manipulating DNA sequences.
//
//  Acknowledgment: I am grateful to Michael Stromberg for the code below to
//                  reverse complement a sequence.

#include "sequenceUtils.h"

// Performs an in-place sequence reversal
void reverseSequence(string &seq) {
    std::reverse(seq.begin(), seq.end());
}

// Performs an in-place reverse complement conversion
void reverseComplement(string &seq) {

    // reverse the sequence
    reverseSequence(seq);

    // swap the bases
    for(unsigned int i = 0; i < seq.length(); i++) {
        switch(seq[i]) {
            // nucleotides
            case 'A':
                seq[i] = 'T';
                break;
            case 'C':
                seq[i] = 'G';
                break;
            case 'G':
                seq[i] = 'C';
                break;
            case 'T':
                seq[i] = 'A';
                break;
            case 'a':
                seq[i] = 't';
                break;
            case 'c':
                seq[i] = 'g';
                break;
            case 'g':
                seq[i] = 'c';
                break;
            case 't':
                seq[i] = 'a';
                break;

            // unknown (N)
            case 'N':
                seq[i] = 'N';
                break;
            case 'n':
                seq[i] = 'n';
                break;

            // unknown (X)
            case 'X':
                seq[i] = 'X';
                break;
            case 'x':
                seq[i] = 'x';
                break;

            // IUPAC
            case 'Y':
                seq[i] = 'R';
                break;
            case 'y':
                seq[i] = 'r';
                break;

            case 'R':
                seq[i] = 'Y';
                break;
            case 'r':
                seq[i] = 'y';
                break;

            case 'S':
                seq[i] = 'S';
                break;
            case 's':
                seq[i] = 's';
                break;

            case 'W':
                seq[i] = 'W';
                break;
            case 'w':
                seq[i] = 'w';
                break;

            case 'K':
                seq[i] = 'M';
                break;
            case 'k':
                seq[i] = 'm';
                break;

            case 'M':
                seq[i] = 'K';
                break;
            case 'm':
                seq[i] = 'k';
                break;

            case 'B':
                seq[i] = 'V';
                break;
            case 'b':
                seq[i] = 'v';
                break;

            case 'V':
                seq[i] = 'B';
                break;
            case 'v':
                seq[i] = 'b';
                break;

            case 'D':
                seq[i] = 'H';
                break;
            case 'd':
                seq[i] = 'h';
                break;

            case 'H':
                seq[i] = 'D';
                break;
            case 'h':
                seq[i] = 'd';
                break;

            default:
                break;
        }
    }
}


void toLowerCase(std::string &seq)
{
    const int length = seq.length();
    for(int i=0; i < length; ++i)
    {
        seq[i] = std::tolower(seq[i]);
    }
}


void toUpperCase(std::string &seq)
{
    const int length = seq.length();
    for(int i=0; i < length; ++i)
    {
        seq[i] = std::toupper(seq[i]);
    }
}


void getDnaContent(const string &seq, int &a, int &c, int &g, int &t, int &n, int &other)
{
    // swap the bases
    for(unsigned int i = 0; i < seq.length(); i++) {
        switch(seq[i]) {
            case 'A':
            case 'a':
                a++;
                break;
            case 'C':
            case 'c':
                c++;
                break;
            case 'G':
            case 'g':
                g++;
                break;
            case 'T':
            case 't':
                t++;
                break;
            case 'N':
            case 'n':
                n++;
                break;
            default:
                other++;
                break;
        }
    }    
}


int countPattern(const string &seq, const string &pattern, bool ignoreCase)
{
    string s = seq;
    string p = pattern;
    // standardize the seq and the pattern 
    // if case should be ignored
    if (ignoreCase) {
        toUpperCase(s);
        toUpperCase(p);
    }
    int patternLength = p.size();
    int patternCount = 0;
    for(unsigned int i = 0; i < s.length(); i++) {
        if (s.substr(i,patternLength) == p) {
            patternCount++;
        }
    }
    return patternCount;
}