-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson 5 - GenomicRangeQuery.c
82 lines (75 loc) · 1.95 KB
/
Lesson 5 - GenomicRangeQuery.c
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
/* ● GenomicRangeQuery
Find the minimal nucleotide from a range of sequence DNA. */
#include <string.h>
int char_to_num(char c);
struct Results solution(char *S, int P[], int Q[], int M) {
struct Results result;
unsigned int i, j;
unsigned char tmp = 0;
char nucleotides[] = "ACGT";
unsigned int n = strlen(S);
unsigned char nuc;
int* a;
a = (int*)calloc(n*4, sizeof(int));
for(i = 0; i < 4; i++){
for(j = 0; j < n; j++){
switch (i){
case 0:
nuc = 'A';
break;
case 1:
nuc = 'C';
break;
case 2:
nuc = 'G';
break;
case 3:
nuc = 'T';
break;
}
if(j == 0){
if(S[0] == nuc){
a[n*i] = 1;
}else{
a[n*i] = 0;
}
continue;
}
if(S[j] == nuc){
a[(n*i)+j] = 1;
}
a[(n*i)+j] += a[(n*i)+(j-1)];
}
}
int* A;
A = (int*)calloc(M, sizeof(int));
for(i = 0; i < M; i++){
if(P[i] != Q[i]){
for(j = 0; j < 4; j++){
if((a[P[i]+n*j] < a[Q[i]+n*j]) || (S[P[i]] == nucleotides[j])){
tmp = nucleotides[j];
break;
}
}
}else{
tmp = S[P[i]];
}
A[i] = char_to_num(tmp);
}
result.A = A;
result.M = M;
return result;
}
int char_to_num(char c){
switch (c) {
case 'A':
return 1;
case 'C':
return 2;
case 'G':
return 3;
case 'T':
return 4;
}
return 0;
}