-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
118 lines (103 loc) · 1.81 KB
/
Copy pathSequence.cpp
File metadata and controls
118 lines (103 loc) · 1.81 KB
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
#include "Sequence.h"
#include <algorithm>
#define MAXN 1200001
char c[MAXN], *a[MAXN];
int pstrcmp(const void *a, const void *b)
{
return strcmp(*(char * const*)a, *(char* const*)b);
}
Sequence::Sequence()
{
sequence = "";
}
Sequence::Sequence(const char *filename)
{
ifstream fin(filename, ios_base::in);
string line;
while (getline(fin, line))
sequence += line;
}
int Sequence::length()
{
return sequence.length();
}
int Sequence::numberOf(char base)
{
int c1=0, c2=0, c3=0, c4=0;
for (int i = 0; i < int(sequence.size()); i++)
{
if (sequence[i] == 'A')
c1++;
else if (sequence[i] == 'T')
c2++;
else if (sequence[i] == 'C')
c3++;
else if (sequence[i] == 'G')
c4++;
}
switch(base)
{
case 'A':
return c1; break;
case 'T':
return c2; break;
case 'C':
return c3; break;
case 'G':
return c4; break;
default: return -1;
}
}
string Sequence::longestConsecutive()
{
string lg_con;
int maxn = 1, tmp = 1;
char m;
int len = sequence.size();
for (int i = len - 2; i > 0; i--)
{
if (sequence[i] == sequence[i + 1])
{
tmp++;
maxn = max(maxn, tmp);
if (maxn == tmp)
m = sequence[i + 1];
}
else
{
maxn = max(maxn, tmp);
tmp = 1;
}
}
for (int i = 0; i < maxn; i++)
lg_con += m;
return lg_con;
}
string Sequence::longestRepeated()
{
string rep;
int len = sequence.size(), maxlen = -1, maxi = 0;
memcpy(c, sequence.c_str(), sequence.size());
for (int i = 0; i < len; i++)
a[i] = &c[i];
qsort(a, len, sizeof(char *), pstrcmp);
for (int i = 0; i < len - 1; i++)
{
if (comlen(a[i], a[i + 1]) > maxlen)
{
maxlen = comlen(a[i], a[i + 1]);
maxi = i;
}
}
rep = a[maxi];
return rep.substr(0,maxlen);
}
int Sequence::comlen(const char *p, const char *q)
{
int len = 0;
while (*p && (*p++ == *q++))
{
len++;
}
return len;
}