-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutsignature.cpp
160 lines (141 loc) · 3.89 KB
/
mutsignature.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
//
// mutsignature.cpp
// APOBECXP
//
// Created by Marat Kazanov on 01/12/2018.
// Copyright © 2018 Marat Kazanov. All rights reserved.
//
#include "mutsignature.hpp"
#include <set>
#include "options.h"
#include <iostream>
#include <cstring>
CMutationSignature::CMutationSignature(string motif_, int mutationPos_, string newbase_)
{
motif = motif_;
mutationPos = mutationPos_;
newbase = newbase_;
}
bool CMutationSignature::AnyNewBase()
{
return((newbase == "X" || newbase == "N"));
}
int CMutationSignature::CheckMotifsNotEmpty(set<string> motifs)
{
if(motifs.empty())
{
cerr << "Error: motifs array is empty." << '\n';
return(0);
}
return(1);
}
int CMutationSignature::CheckMotifsSameLength(set<string> motifs)
{
set<string>::iterator m;
unsigned long motiflen;
motiflen = (motifs.begin())->length();
for(m=motifs.begin();m!=motifs.end();m++)
if(motiflen != (*m).length())
{
cerr << "Error: motifs have different length" << '\n';
return(0);
}
return(1);
}
set<string> CMutationSignature::AddcMotifs(set<string> motifs)
{
set<string> motifsall;
set<string>::iterator m;
motifsall = motifs;
for(m=motifs.begin();m!=motifs.end();m++)
{
motif = CDNA::cDNA(*m);
motifsall.insert(motif);
}
return(motifsall);
}
CDNAPos CMutationSignature::NextMotif(CDNAPos pos, char** motifsarr, int* strandarr, int motifsnum, int motiflen, CHumanGenome* phuman, int end, int includeCurrentPos)
{
CDNAPos ret = CDNAPos(-1,0);
int endChrNum;
int i,k,n;
unsigned long j;
int break2;
unsigned long startpos;
if(end == END_CHROMOSOME)
endChrNum = pos.chrNum + 1;
else
endChrNum = phuman->chrCnt;
if(includeCurrentPos)
startpos = pos.pos;
else
startpos = pos.pos + 1;
for(i=pos.chrNum;i<endChrNum;i++)
{
for(j=startpos;j<(phuman->chrLen[i]-motiflen+1);j++)
{
for(k=0;k<motifsnum;k++)
{
break2 = 0;
for(n=0;n<motiflen;n++)
{
if(motifsarr[k][n] == 'X')
continue;
if(phuman->dna[i][j+n] != motifsarr[k][n])
{
break2 = 1;
break;
}
}
if(break2)
continue;
ret.chrNum = i;
ret.pos = j;
ret.strand = strandarr[k];
return(ret);
}
}
startpos = 0;
}
return(ret);
}
unsigned long CMutationSignature::CountMotifGenome(set<string> motifs, CHumanGenome* phuman)
{
unsigned long res = 0;
set<string> motifsall;
set<string>::iterator si;
int includeCurPos=1;
char** motifsarr;
int* strandarr;
int motiflen, motifsnum;
int i;
CheckMotifsNotEmpty(motifs);
CheckMotifsSameLength(motifs);
motifsall = AddcMotifs(motifs);
motiflen = (int)(motifsall.begin())->length();
motifsnum = (int)motifsall.size();
// Prepare char array for copying motifs
motifsarr = new char*[motifsall.size()];
strandarr = new int[motifsall.size()];
for(i=0;i<motifsall.size();i++)
{
motifsarr[i] = new char[motiflen];
if(i<motifs.size())
strandarr[i] = 1;
else
strandarr[i] = 0;
}
// Copy motifs to char array
i = 0;
for(si=motifsall.begin();si!=motifsall.end();si++)
{
strncpy(motifsarr[i],(*si).c_str(),(*si).length());
i++;
}
CDNAPos pos;
for(pos=NextMotif(CDNAPos(0,0),motifsarr,strandarr,motifsnum,motiflen,phuman,END_GENOME,includeCurPos);
!pos.isNull();
pos=NextMotif(pos,motifsarr,strandarr,motifsnum,motiflen,phuman,END_GENOME))
res++;
return(res);
}