-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbe_design.m
325 lines (287 loc) · 11.5 KB
/
Probe_design.m
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
[~,~,all2]=xlsread(''); %Candidate path, includes Chromosome, Start and End for the candidate loci
temp=all2(2:189,1); % The column with the genomic coordinates(Chromosome, Start and End) of the candidate site
TargetStart=[];
TargetEnd=[];
TargetChrom={};
for i=1:length(temp)
st=temp{i};
tempst=strsplit(st,'.');
if length(tempst)==4
TargetStart(i)=str2num(tempst{3});
TargetEnd(i)=str2num(tempst{4});
TargetChrom{i}=tempst{2};
else
TargetStart(i)=str2num(tempst{2});
TargetEnd(i)=str2num(tempst{3});
TargetChrom{i}=tempst{1};
end
end
allStart=[];
allEnd=[];
allChrom={};
[~,~,all1]=xlsread(''); % IgG path, includes Chromosome, Start and End for all peaks in IgG control
temp=all1(1:end,1);
for i=1:76789
st=temp{i};
tempst=strsplit(st,'.');
allStart(i)=str2num(tempst{2});
allEnd(i)=str2num(tempst{3});
allChrom{i}=tempst{1};
end
[~,~,all3]=xlsread('');
iggStart=cell2mat(all3(1:end,5));
iggEnd=cell2mat(all3(1:end,6));
iggChrom=all3(1:end,4);
genomeseq=fastaread(''); %reference path for mm10.fa or hg19.fa
savePath='';
%%
Target={};
for i=1:length(TargetStart)
temp=genomeseq(find(strcmp({genomeseq.Header},TargetChrom{i}))).Sequence;
Target{i}=temp(TargetStart(i):TargetEnd(i));
end
allseq={};
for i=1:length(allStart)
temp=genomeseq(find(strcmp({genomeseq.Header},allChrom{i}))).Sequence;
allseq{i}=temp(allStart(i):allEnd(i));
end
%%
iggseq={};
for i=1:length(iggStart)
temp=genomeseq(find(strcmp({genomeseq.Header},iggChrom{i}))).Sequence;
iggseq{i}=temp(iggStart(i):iggEnd(i));
end
%% design probes for each target
warning('off');
GClow=33;
GChigh=73;
TMlow=61;
TMhigh=81;
similarthreshold=15;
targetregion=cell(length(TargetStart),1);
regionnum=zeros(1,length(TargetStart));
targetregionstart=cell(length(TargetStart),1);
targetregionend=cell(length(TargetStart),1);
parfor i=1:length(TargetStart)
temp=Target{i};
index=1;
k=1;
temps={};
temp1=[];
temp2=[];
strand=1;
while k<length(temp)-30
candidate=temp(k:k+29);
GC=(length(strfind(candidate,'G'))+length(strfind(candidate,'C')))/length(candidate)*100;
TM=64.9 +41*(length(strfind(candidate,'G'))+length(strfind(candidate,'C'))-16.4)/length(candidate);
if GC>=GClow && GC<=GChigh && TM>=TMlow && TM<=TMhigh && ~contains(candidate,{'AAAAAA','GGGGGG','CCCCCCC','TTTTTT','AGAGAG','ACACAC','TCTCTC','TGTGTG','ATATAT'})
check=1;
j=1;
while j<35000 % all peak number
m=allseq{j};
if abs(allStart(j)-TargetStart(i))>1000 | strcmp(allChrom{j},TargetChrom{i})==0
[score,align]=swalign(candidate,m);
if max(diff(find([1,diff(align(2,:)),1])))>similarthreshold % longest consecutive match
check=0;
break;
end
[score,align]=swalign(candidate,seqrcomplement(m));
if max(diff(find([1,diff(align(2,:)),1])))>similarthreshold % longest consecutive match
check=0;
break;
end
end
j=j+1;
end
j=1;
while j<7500 % all IgG peak number
m=iggseq{j};
[score,align]=swalign(candidate,m);
if max(diff(find([1,diff(align(2,:)),1])))>similarthreshold % longest consecutive match
check=0;
break;
end
[score,align]=swalign(candidate,seqrcomplement(m));
if max(diff(find([1,diff(align(2,:)),1])))>similarthreshold % longest consecutive match
check=0;
break;
end
j=j+1;
end
% disp(j);
if check
temps{index}=candidate;
temp1(index)=k;
temp2(index)=k+29;
index=index+1;
k=k+27;
% disp(candidate);
% disp(GC);
% disp(TM);
end
end
k=k+1;
end
targetregion{i}=temps;
targetregionstart{i}=temp1;
targetregionend{i}=temp2;
regionnum(i)=index-1;
disp(['Find ' num2str(index-1) ' probes for region ' num2str(i) ' from length ' num2str(length(temp)) ' nt']);
end
save([savePath '\targetregion.mat'],'targetregion','regionnum');
%% quantify probe number
figHandle = figure('Name', 'Number of probes per loci', 'Color', 'w');
[~, sind] = sort(regionnum, 'Descend');
plot(regionnum(sind), '.'); hold on;
xlabel('loci ID');
ylabel('Number of regions');
SaveFigure(figHandle, 'overwrite', true, 'formats', {'fig', 'png'},'savePath', savePath);
%% quantify probe number per 100 bp
figHandle = figure('Name', 'Number of probes per 100bp', 'Color', 'w');
temp=regionnum./(TargetEnd-TargetStart)*100;
[~, sind] = sort(temp, 'Descend');
plot(temp(sind), '.'); hold on;
xlabel('loci ID');
ylabel('Number of probes per 100 bp');
title(['Avg number of probes per 100 bp:', num2str(mean(regionnum'./(TargetEnd-TargetStart))*100)]);
SaveFigure(figHandle, 'overwrite', true, 'formats', {'fig', 'png'},'savePath', savePath);
%% Build oligos
libraryName='';
[~,~,all5]=xlsread(''); %readout path
readouts = all5(2:end,3);
readoutname=all5(2:end,2);
[~,~,all4]=xlsread(''); % Final candidate path
temp=all4(1:147,1);
newbarcodes=cell2mat(all4(1:147,2:end));
for i=1:length(temp)
st=temp{i};
tempst=strsplit(st,'.');
localGenestart(i)=str2num(tempst{2});
localGeneend(i)=str2num(tempst{3});
localGenechr{i}=tempst{1};
end
oligosPath = [savePath 'oligos.fasta'];
% if ~exist(oligosPath)
oligos = [];
for i=1:length(localGenechr)
% Display progress
localGeneName=[localGenechr{i} ':' num2str(localGenestart(i)) '-' num2str(localGeneend(i))];
PageBreak();
display(['Designing probes for ' libraryName ': ' localGeneName]);
ids2=find(TargetEnd==localGeneend(i) & TargetStart==localGenestart(i) & strcmp(TargetChrom,localGenechr{i}));
ids2=ids2(1);
% Determine the bits to include for each word
possibleReadouts = readouts(newbarcodes(i,:)==1);
readName=readoutname(newbarcodes(i,:)==1);
seqs = {};
headers = {};
missing=4;
% Build all possible oligos
for p=1:regionnum(ids2)
% Create random orientation and selection of readouts
id=boolean([1,1,1,1]);
id(missing)=0;
localReadouts = possibleReadouts(id);
seq=seqrcomplement(targetregion{ids2}{p});
if mod(p,2)==0
seq=targetregion{ids2}{p};
end
if rand(1) > 0.5
% Create header
headers{p} = [libraryName ' ' ...
'A ' ... % Add A pad
readName{1} ' ' ...
readName{2} ' '...
localGeneName ' ' ...
'A ' ... % Add A pad
readName{3}];
% Create sequence
seqs{p} = ['A ' seqrcomplement(localReadouts{1}) ' '...
'A ' seqrcomplement(localReadouts{2}) ' A '...
seq ' A ' ...
seqrcomplement(localReadouts{3})];
else
headers{p} = [libraryName ' ' ...
'A ' ... % Add A pad
readName{3} ' ' ...
localGeneName ' ' ...
'A ' ... % Add A pad
readName{1} ' ' ...
readName{2}];
% Create sequence
seqs{p} = ['A ' seqrcomplement(localReadouts{3}) ' A '...
seq ' A ' ...
seqrcomplement(localReadouts{1}) ' A ' ...
seqrcomplement(localReadouts{2})];
end
missing=missing-1;
if missing==0
missing=4;
end
end
display(['... constructed ' num2str(length(seqs)) ' possible probes']);
% Save new oligos in oligos struct
for s=1:length(seqs)
oligos(end+1).Header = headers{s};
oligos(end).Sequence = seqs{s};
end
end
PageBreak();
display(['Writing: ' oligosPath]);
writeTimer = tic;
fastawrite(oligosPath, oligos);
display(['... completed in ' num2str(toc(writeTimer))]);
%% Load fasta file and select primers for library
primersPath='';
primers = fastaread(primersPath);
usedPrimers = primers(1:2);
% Add primers to encoding probes
PageBreak();
display('Adding primers');
finalPrimersPath = [savePath libraryName '_primers.fasta'];
% if ~exist(finalPrimersPath)
% Record the used primers
fastawrite(finalPrimersPath, usedPrimers);
display(['Wrote: ' finalPrimersPath]);
% Build the final oligos
finalOligos = [];
for i=1:length(oligos)
stringParts = strsplit(oligos(i).Header, ' ');
name1 = strsplit(usedPrimers(1).Header, ' ');
name1 = name1{1};
name2 = strsplit(usedPrimers(2).Header, ' ');
name2 = name2{1};
finalOligos(i).Header = [stringParts{1} ' ' ...
name1 ' '];
for j=2:length(stringParts)
finalOligos(i).Header = [finalOligos(i).Header ...
stringParts{j} ' '];
end
finalOligos(i).Header = [finalOligos(i).Header name2];
finalOligos(i).Sequence = [usedPrimers(1).Sequence ' ' ...
oligos(i).Sequence ' ' ...
seqrcomplement(usedPrimers(2).Sequence)];
end
%% Final cross checks and writing final file
PageBreak();
rRNAOtTable = OTTable.Load(''); % Off Taget Table for rRNA
display('Running final cross checks and building final fasta file');
% Write final fasta
oligosPath = [savePath libraryName '_oligos.fasta'];
% if ~exist(oligosPath)
% Screen against the original tables
tic;
display(['Searching oligos for homology']);
hasrRNAPenalty = cellfun(@(x) sum(rRNAOtTable.CalculatePenalty(seqrcomplement(x(~isspace(x)))))>0, {finalOligos.Sequence});
display(['... completed in ' num2str(toc) ' s']);
indsToKeep = ~hasrRNAPenalty;
display(['... found ' num2str(sum(~indsToKeep)) ' oligos to remove ']);
indsToRemove = find(~indsToKeep);
for r=1:length(indsToRemove)
display(['... ' finalOligos(indsToRemove(r)).Header]);
end
% Remove bad oligos
finalOligos = finalOligos(indsToKeep);
% Write final oligos
fastawrite(oligosPath, finalOligos);
display(['Wrote: ' oligosPath]);