-
Notifications
You must be signed in to change notification settings - Fork 0
/
allmotifs.cpp
212 lines (180 loc) · 6.58 KB
/
allmotifs.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
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
//
// allmotifs.cpp
// APOBECXP
//
// Created by Marat Kazanov on 14/04/2019.
// Copyright © 2019 Marat Kazanov. All rights reserved.
//
#include <iostream>
#include "allmotifs.hpp"
CRTMapKey::CRTMapKey(string motif_, int RTbin_, int RTstrand_, char mutbase_)
{
motif = motif_;
RTbin = RTbin_;
RTstrand = RTstrand_;
mutbase = mutbase_;
}
CRTMapKeyNoMut::CRTMapKeyNoMut(string motif_, int RTbin_, int RTstrand_)
{
motif = motif_;
RTbin = RTbin_;
RTstrand = RTstrand_;
}
void CAllMotifs::AnalysisRTmutations(CMutations& muts, string dirpath, CReplicationTiming& rti, CHumanGenome* phuman, string cancer, string sample)
{
int i;
int res;
int RTbin;
int RTstrand;
int chrNum;
string motif;
CReplicationTime rt;
CMutation* pmut;
map<CRTMapKey, unsigned long> results, results2strands;
map<CRTMapKey, unsigned long>::iterator it, it2;
for(i=0;i<muts.mutations.size();i++)
{
pmut = &muts.mutations[i];
// RT
res = rti.GetRT(CHumanGenome::GetChrNum(string(pmut->chr)), pmut->pos, rt);
if(res)
RTbin = rti.GetRTBin(rt.RTvalue, (rti.bins));
else
RTbin = RT_NULLBIN_NOVALUE;
if(RTbin == RT_NULLBIN_NOVALUE)
RTstrand = STRAND_NULL;
else if((pmut->isForwardStrand == 1 && rt.isForward == 0) || (pmut->isForwardStrand == 0 && rt.isForward == 1))
RTstrand = STRAND_LEADING;
else if((pmut->isForwardStrand == 1 && rt.isForward == 1) || (pmut->isForwardStrand == 0 && rt.isForward == 0))
RTstrand = STRAND_LAGGING;
else
RTstrand = STRAND_NULL;
// Motif
chrNum = CHumanGenome::GetChrNum(string(pmut->chr));
motif = string(1,phuman->dna[chrNum][pmut->pos-2]) + string(1,phuman->dna[chrNum][pmut->pos-1]) + string(1,phuman->dna[chrNum][pmut->pos]);
//cout << mut.chr << '\t' << mut.pos << '\t' << motif << '\t' << RTbin << '\t' << RTstrand << '\t' << expbin << '\t' << expStrand << '\n';
// write results to map
it = results.find(CRTMapKey(motif,RTbin,RTstrand,pmut->varallele[0]));
if(it != results.end())
it->second++;
else
results.insert(pair<CRTMapKey, unsigned long>(CRTMapKey(motif,RTbin,RTstrand,pmut->varallele[0]), 1));
}
results2strands = results;
string cmotif;
char cvarallele;
int opRTstrand;
for(it=results.begin();it!=results.end();it++)
{
cmotif = CDNA::cDNA(it->first.motif);
cvarallele = CDNA::cDNA(string(1,it->first.mutbase))[0];
opRTstrand = CReplicationTiming::oppositeStrand(it->first.RTstrand);
it2 = results2strands.find(CRTMapKey(cmotif,it->first.RTbin,opRTstrand,cvarallele));
if(it2 != results2strands.end())
it2->second += it->second;
else
results2strands.insert(pair<CRTMapKey, unsigned long>(CRTMapKey(cmotif,it->first.RTbin,opRTstrand,cvarallele), it->second));
}
ofstream f;
string path;
path = dirpath + "/" + cancer + "/RT_MUT_" + sample + ".txt";
f.open(path.c_str());
if (!f.is_open())
{
printf("Directory is not exists\n");
exit(1);
}
for(it=results2strands.begin();it!=results2strands.end();it++)
f << it->first.motif << '\t' << it->first.RTbin << '\t' << it->first.RTstrand << '\t' << it->first.mutbase << '\t' << it->second << '\n';
f.close();
}
void CAllMotifs::AnalysisRTtargets(string dirpath, CReplicationTiming& rti, CHumanGenome* phuman)
{
// Targets
int i,j,k;
string motif;
int RTbin;
int RTstrand;
int res;
CReplicationTime rt;
set<string> motifs;
char nuc[4] = {'A','G','C','T'};
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
{
motif = string(1,nuc[i])+string(1,nuc[j])+string(1,nuc[k]);
motifs.insert(motif);
}
map<CRTMapKeyNoMut, unsigned long> resultsT,results2strandsT;
map<CRTMapKeyNoMut, unsigned long>::iterator itT,it2T;
set<string>::iterator mit;
for(mit=motifs.begin();mit!=motifs.end();mit++)
for(RTbin=(-RT_NULLBIN_CNT);RTbin<(int)rti.bins.size();RTbin++)
for(i=-1;i<2;i++)
resultsT.insert(pair<CRTMapKeyNoMut, unsigned long>(CRTMapKeyNoMut((*mit),RTbin,i), 0));
for(i=0;i<phuman->chrCnt;i++)
{
cout << "Chromosome: " << i << '\n';
for(j=1;j<(phuman->chrLen[i]-1);j++)
{
if(!(CDNA::inACGT(phuman->dna[i][j]) &&
CDNA::inACGT(phuman->dna[i][j-1]) &&
CDNA::inACGT(phuman->dna[i][j+1])))
continue;
// RT
res = rti.GetRT(i, j+1, rt);
if(res)
RTbin = rti.GetRTBin(rt.RTvalue, (rti.bins));
else
RTbin = RT_NULLBIN_NOVALUE;
if(RTbin == RT_NULLBIN_NOVALUE)
RTstrand = STRAND_NULL;
else if(rt.isForward == 0)
RTstrand = STRAND_LEADING;
else if(rt.isForward == 1)
RTstrand = STRAND_LAGGING;
else
RTstrand = STRAND_NULL;
// Motif
motif = string(1,phuman->dna[i][j-1]) + string(1,phuman->dna[i][j]) + string(1,phuman->dna[i][j+1]);
// write results to map
itT = resultsT.find(CRTMapKeyNoMut(motif,RTbin,RTstrand));
if(itT != resultsT.end())
itT->second++;
else
{
cerr << "Error: map key not found";
exit(1);
}
}
}
results2strandsT = resultsT;
string cmotif;
int opRTstrand;
for(itT=resultsT.begin();itT!=resultsT.end();itT++)
{
cmotif = CDNA::cDNA(itT->first.motif);
opRTstrand = CReplicationTiming::oppositeStrand(itT->first.RTstrand);
it2T = results2strandsT.find(CRTMapKeyNoMut(cmotif,itT->first.RTbin,opRTstrand));
if(it2T != results2strandsT.end())
it2T->second += itT->second;
else
{
cerr << "Error: map key not found";
exit(1);
}
}
ofstream f;
string path;
path = dirpath + "/RT_TRG.txt";
f.open(path.c_str());
if (!f.is_open())
{
printf("File not exists\n");
exit(1);
}
for(itT=results2strandsT.begin();itT!=results2strandsT.end();itT++)
f << itT->first.motif << '\t' << itT->first.RTbin << '\t' << itT->first.RTstrand << '\t' << itT->second << '\n';
f.close();
}