-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimedRPA
executable file
·1047 lines (765 loc) · 36.5 KB
/
PrimedRPA
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
#####################################################################
# PrimedRPA: RPA Primer and Probe Set Finder #
# Higgins M et al. Submitted. 2018 #
# #
# Dependencies: #
# Python 3.7
# Glob 0.6
# Pandas 0.20.3 #
# Sys 3.6.3 #
# Bio 1.70 #
#####################################################################
# Install neccessary python libraries
import os
import sys
import glob
import subprocess
import itertools
import random
import pandas as pd
from collections import Counter
from multiprocessing import Pool
import argparse
import re
def FastaToDict(InputFile):
fastadict = {}
with open(InputFile) as file_one:
for line in file_one:
line = line.strip()
if not line:
continue
if line.startswith(">"):
active_sequence_name = line[1:]
if active_sequence_name not in fastadict:
fastadict[active_sequence_name] = ''
continue
sequence = line
# If Uracil Present, Substitute for T (I.e. stick with 4 DNA bases)
fastadict[active_sequence_name] += re.sub('U','T',sequence.upper())
return fastadict
# Wrapper for clustal omega
def RunningClustalo1(ClustalOPath,
ListOfFileNames,
overwriteOutput=True):
print('Aligning Input File')
for FileName in ListOfFileNames:
OutputName = FileName.replace(".fasta",'_Aligned.fasta')
command = "{0} -i {1} -o {2} --outfmt=fasta".format(ClustalOPath,FileName, OutputName)
result = subprocess.call([command], stdout=subprocess.PIPE, shell=True,)
# Function to generate reverse complement sequence
def getComplement(seq,
reverse=False,
rule='N2N'):
seqComp = ""
for base in seq.upper():
base = base.upper()
if base == "A":
seqComp += "T"
elif base == "T":
seqComp += "A"
elif base == "C":
seqComp += "G"
elif base == "G":
seqComp += "C"
elif base == 'U':
seqComp += "A"
elif base == "N":
if rule == 'N2-':
seqComp += '-'
elif rule == 'N2N':
seqComp += "N"
elif base == "-":
seqComp += "N"
# If Character not any of above assign it as N
else:
seqComp += "N"
if reverse:
return(seqComp[::-1])
else:
return(seqComp)
## Background Binding Check = TO IMPROVE
def BlastnBackgroundCheck(seq,AllParameter):
MaxBackgroundScoreBindingScore = 0
MaxScoreBackSeq = ''
HardFailBoolean = False
RIS = str(random.randint(0,1000000))
#Create temp fasta file
tempfastainput = open('./{}/{}_{}_Blastn_Input.fa'.format(AllParameter.PrimerBlastnOutput,seq,RIS),'w')
tempfastainput.write('>Temp_Blastn_Fasta\n{}\n'.format(seq))
tempfastainput.close()
# Parameters default for basic run
Penalty = '-2'
WordSize = '4'
if AllParameter.BackgroundSearchSensitivity == 'Advanced':
Penalty = '-1'
elif AllParameter.BackgroundSearchSensitivity == 'Fast':
Penalty = '-3'
WordSize = '7'
#Triggure Blastn command
blastncommandrun = '{0} -word_size {5} -gapopen 5 -gapextend 2 -reward 1 -penalty {4} -evalue {7} -query {3}/{1}_{6}_Blastn_Input.fa -db {2}/{2} -out {3}/{1}_{6}_Blastn_Output.csv -outfmt "10 sseqid pident qstart qend sstart send evalue gaps sstrand" '.format(AllParameter.BlastnPath,
seq,
AllParameter.BlastnDBName,
AllParameter.PrimerBlastnOutput,
Penalty,
WordSize,
RIS,
AllParameter.Evalue)
subprocess.call([blastncommandrun],shell=True)
# Remove Blastn Input File
os.remove('./{}/{}_{}_Blastn_Input.fa'.format(AllParameter.PrimerBlastnOutput,seq,RIS))
# Try to read dataframe (may be empty if no alignments found)
try:
ShorterBackground = False
blastnoutdf = pd.read_csv('{}/{}_{}_Blastn_Output.csv'.format(AllParameter.PrimerBlastnOutput,seq,RIS),header=None)
blastnoutdf.columns=['Background_SeqID','Percentage Identity','qStart','qEnd','BackSeq_Start','BackSeq_End','Evalue','Number Of Gaps','Strand']
blastnoutdf['Cross Reactivity Score'] = ((((blastnoutdf['qEnd']+1) - blastnoutdf['qStart'])*(blastnoutdf['Percentage Identity']/100))/len(seq))*100
blastnoutdf = blastnoutdf.sort_values(by=['Cross Reactivity Score'],ascending=False)
for blastoutindex in blastnoutdf.index.tolist():
ReferenceID = blastnoutdf.loc[blastoutindex,'Background_SeqID']
if ReferenceID.count("|") == 2:
CleanRefID = ReferenceID[ReferenceID.find("|")+1:-1]
else:
CleanRefID = ReferenceID
# If Background Seq Is (+) Sense
if blastnoutdf.loc[blastoutindex,'Strand']=='plus':
UpperExtension = (len(seq)-blastnoutdf.loc[blastoutindex,'qEnd']) + blastnoutdf.loc[blastoutindex,'BackSeq_End']
LowerExtension = blastnoutdf.loc[blastoutindex,'BackSeq_Start'] - blastnoutdf.loc[blastoutindex,'qStart']+1
if LowerExtension<0:
LowerExtension = 0
ShorterBackground = True
SamToolsCommand = '{} faidx {} {}:{}-{} -o Adv_{}_{}_{}.fa'.format(AllParameter.SamtoolsPath,
AllParameter.BackgroundCheck,
CleanRefID,
LowerExtension,
UpperExtension,
seq,
CleanRefID,
RIS)
# If Background Seq Is (-) Sense
else:
UpperExtension = blastnoutdf.loc[blastoutindex,'BackSeq_Start'] + blastnoutdf.loc[blastoutindex,'qStart']-1
LowerExtension = blastnoutdf.loc[blastoutindex,'BackSeq_End'] - (len(seq)-blastnoutdf.loc[blastoutindex,'qEnd'])
if LowerExtension<0:
LowerExtension = 0
ShorterBackground = True
SamToolsCommand = '{} faidx -i {} {}:{}-{} -o Adv_{}_{}_{}.fa'.format(AllParameter.SamtoolsPath,
AllParameter.BackgroundCheck,
CleanRefID,
LowerExtension,
UpperExtension,
seq,
CleanRefID,
RIS)
# Run Samtools Command
try:
subprocess.run(SamToolsCommand, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as cpe:
print("stderr:", cpe.stderr.decode())
raise
# Obtain Sequence
fastadict = FastaToDict('Adv_{}_{}_{}.fa'.format(seq,CleanRefID,RIS))
# Extract Sequence & Complement
ExtraSeq = list(fastadict.values())[0]
ComplementSeq = getComplement(ExtraSeq)
# Bug Fix 2020-07-06 - I.e. Only run if global alignment is complete
if len(ComplementSeq) == len(seq):
# Run SS Check Function
BackgroundMaxBindingPercentage, BackgroundMaxBindingString, BackgroundHardFail, BackgroundString = SSIdentification(seq, ComplementSeq, False, ShorterBackground)
# Add to Blastn DataFrame
blastnoutdf.loc[blastoutindex,'Advanced Cross Reactivity Percentage'] = BackgroundMaxBindingPercentage
blastnoutdf.loc[blastoutindex,'Advanced Cross Reactivity Percentage String'] = BackgroundMaxBindingString
blastnoutdf.loc[blastoutindex,'Cross Reactivity Hard Fail'] = BackgroundHardFail
blastnoutdf.loc[blastoutindex,'Cross Reactivity Hard Fail String'] = BackgroundString
# Remove Advance Samtools Generated String
os.remove('Adv_{}_{}_{}.fa'.format(seq,CleanRefID,RIS))
# Remove Raw Blastn Output
os.remove('{}/{}_{}_Blastn_Output.csv'.format(AllParameter.PrimerBlastnOutput,seq,RIS))
# Save Clean Blast Output
blastnoutdf = blastnoutdf.sort_values(by=['Advanced Cross Reactivity Percentage'],ascending=False)
blastnoutdf.to_csv('{}/{}_Blastn_Output.csv'.format(AllParameter.PrimerBlastnOutput,seq),index=None)
IndexOfInterest = blastnoutdf.index.tolist()[0]
MaximumPercentageMatch = blastnoutdf.loc[IndexOfInterest,'Advanced Cross Reactivity Percentage']
MaxHomologyBackgroundSeq = blastnoutdf.iloc[IndexOfInterest,0]
if True in blastnoutdf['Cross Reactivity Hard Fail'].unique():
HardFailBoolean = True
# If dataframe empty e.g. no alignments at all
except pd.errors.EmptyDataError:
MaximumPercentageMatch = 0
if MaximumPercentageMatch > MaxBackgroundScoreBindingScore:
MaxBackgroundScoreBindingScore = MaximumPercentageMatch
MaxScoreBackSeq = MaxHomologyBackgroundSeq
return (MaxBackgroundScoreBindingScore, MaxScoreBackSeq, HardFailBoolean)
# Creates Exo Fluorescent Probe
def RunFluroProbeAnalysis(ProbeBindingSeq):
minIndexPosition = int(len(ProbeBindingSeq)*0.45)
maxIndexPosition = int(len(ProbeBindingSeq)*0.75)
ProbeValidPass = False
basenumber = 0
proberegionlength = len(ProbeBindingSeq)
for base in ProbeBindingSeq:
if (basenumber + 4) < proberegionlength:
basenumber += 1
if minIndexPosition < basenumber < maxIndexPosition and base == "T":
if (basenumber + 2) < proberegionlength:
if ProbeBindingSeq[(basenumber+2)] == "T" or ProbeBindingSeq[(basenumber+3)] == "T":
# State that probe seq passed in forward sense.
ProbeValidPass = True
# New Break For Loop
break
return ProbeValidPass
# Secondary Structure Filter
def SSIdentification(SeqOne, SeqTwo, ReverseOrientation, FixedBack=False ,threshold=4):
# Nucleotide Dict
NucDict = {'A':'T',
'T':'A',
'C':'G',
'G':'C',
}
# Maximum Binding sites
MaxBindingSites = 0
MaxBindingString = ''
MaxBindingPercentage = 0
# Hard Fail Sites
MaxHardFailScore = 0
HardFail = False
HardFailString = ''
# Max length of string according to shift.
MaxLength = len(SeqOne) + len(SeqTwo) - 1
# Add white space to end of sequences
SeqOneNorm = SeqOne + ' '*(MaxLength-len(SeqOne))
SeqTwoNorm = SeqTwo + ' '*(MaxLength-len(SeqTwo))
SeqTwoReversed = SeqTwo[::-1] + ' '*(MaxLength-len(SeqTwo))
if ReverseOrientation == True:
CombosToCheck = [(SeqTwoNorm,SeqOneNorm),
(SeqOneNorm,SeqTwoNorm),
(SeqOneNorm,SeqTwoReversed),
(SeqTwoReversed,SeqOneNorm)]
else:
CombosToCheck = [(SeqTwoNorm,SeqOneNorm),
(SeqOneNorm,SeqTwoNorm)]
# Loop through var pairs whereby the first element will be shifted
for VarPair in CombosToCheck:
# Loop through possible shift positions
for i in list(range(VarPair[0].count(' ')+1)):
if i == 0:
DynamicSeq = VarPair[0]
else:
DynamicSeq = ' '*i + VarPair[0][:-i]
DyanimicSeqList = list(DynamicSeq)
FixedSeqList = list(VarPair[1])
# This shall house the syntax to represent binding
PossibleBindingString = ''
for StringPos in list(range(len(DyanimicSeqList))):
if DyanimicSeqList[StringPos] in list(NucDict.keys()):
if NucDict[DyanimicSeqList[StringPos]] == FixedSeqList[StringPos]:
PossibleBindingString += '|'
else:
PossibleBindingString += '-'
else:
PossibleBindingString += '-'
NumberOfBindingMatches = PossibleBindingString.count('|')
CompleteBindingString = DynamicSeq + '\n' + PossibleBindingString + '\n' + VarPair[1]
# Fixed String
if VarPair[1] == SeqOneNorm:
FiveIndex = 0
ThreeIndex = len(SeqOne)
# Dyamic String
else:
FiveIndex = i
ThreeIndex = i + len(SeqOne)
UpperLimit = FiveIndex+22
LowerLimit = ThreeIndex-22
if LowerLimit <0:
LowerLimit = 0
# Determine 5 Prime Counts
FivePrimeCounts = PossibleBindingString[FiveIndex:UpperLimit]
# Determine 3 Prime Counts
ThreePrimeCounts = PossibleBindingString[LowerLimit:ThreeIndex]
weightings = [3,2,1.5]
ThreePrimeLoc = [-1,-2,-3]
FivePrimeLoc = [0,1,2]
OriginalScore = [FivePrimeCounts,ThreePrimeCounts]
IndexLocations = [FivePrimeLoc,ThreePrimeLoc]
AdjustedWeights = []
for ib in [0,1]:
TempScore = OriginalScore[ib].count('|') - OriginalScore[ib].count('-')
Indexes = IndexLocations[ib]
for iz in list(zip(Indexes,weightings)):
if OriginalScore[ib][iz[0]] == '|':
TempScore += iz[1] - 1
AdjustedWeights.append(TempScore)
if max(AdjustedWeights) >= 21.5:
HardFail = True
HardFailString = CompleteBindingString
if MaxBindingSites < NumberOfBindingMatches:
MaxBindingSites = NumberOfBindingMatches
MaxBindingString = CompleteBindingString
if FixedBack == False:
Denom = min([len(SeqOne),len(SeqTwo)])
else:
Denom = len(SeqOne)
MaxBindingPercentage = (MaxBindingSites/Denom)*100
return (MaxBindingPercentage,MaxBindingString, HardFail, HardFailString)
# Creating Alignment DF Multithread Function
def CreatingInputHomologyDF(fastadict,FastaIndexList):
records = []
for seqindex in FastaIndexList:
TempNucleotides = []
for faseq in fastadict.values():
TempNucleotides.append(faseq[seqindex])
# Remove NoN-specific Nucleotides = IUPAC designated the symbols for nucleotides
TempNucleotides = [TN for TN in TempNucleotides if TN not in ['W','S','M',
'K','R','Y',
'N']]
if (len(TempNucleotides) == 2 and
'-' in TempNucleotides):
MostCommonN = '-'
NRepresentation = -100 #Harsh weighting against splits
else:
BugFix = Counter(TempNucleotides)
MostCommonN = max(TempNucleotides, key=BugFix.get)
if MostCommonN == '-':
NRepresentation = -100 #Harsh weighting against splits
else:
NRepresentation = TempNucleotides.count(MostCommonN)/len(TempNucleotides)
records.append({'Index_Pos':seqindex,'Nucleotide':MostCommonN,'IdentityScore':NRepresentation})
return pd.DataFrame.from_records(records)
def IndentifyingAndFilteringOligos(AllParameter,
AlignedDF,
PossibleStartIndexes):
OligoDF = pd.DataFrame()
TargetSiteLengths = [AllParameter.PrimerLength]
if AllParameter.ProbeRequired in ['EXO','NFO']:
TargetSiteLengths.append(AllParameter.ProbeLength)
# Loop through primer or probe
oligo_records = []
for TSLP in TargetSiteLengths:
if TSLP == AllParameter.PrimerLength:
OligoType = 'Primer'
else:
OligoType = 'Probe'
# Add in ability to handle specified range or primer or probe values.
if '-' in str(TSLP):
TSLRangePrior = [int(TSLI) for TSLI in TSLP.split('-')]
TSLList = list(range(TSLRangePrior[0],TSLRangePrior[1]+1))
else:
TSLList = [int(TSLP)]
# Loop through possible oligo lengths.
for TSL in TSLList:
# Loop through possible start sites
for i in PossibleStartIndexes:
# Make Sure dont go too far out of dataframe
if i+TSL<len(AlignedDF)-TSL:
DFSubSet = AlignedDF[(AlignedDF['Index_Pos']>=i)&(AlignedDF['Index_Pos']<=(i+TSL-1))]
MeanHomologyScore = DFSubSet['IdentityScore'].mean()
IDSList = DFSubSet['IdentityScore'].tolist()
OutIDScore = []
for IDSO in [IDSList,IDSList[::-1]]:
IDScore = 0
for SCI in list(range(len(IDSO))):
if float(IDSO[SCI]) == 1.0:
IDScore +=1
else:
break
OutIDScore.append(IDScore)
ThreePrimeIdentityScore = OutIDScore[1]
FivePrimerIdentityScore = OutIDScore[0]
if MeanHomologyScore >= AllParameter.IdentityThreshold:
NucleotideSeq = ''.join(DFSubSet['Nucleotide'].tolist())
NucleotideSeq = NucleotideSeq.upper()
#Check Length of Oligo
if len(NucleotideSeq) == TSL:
# Assess GC Content
GCPercentage = ((NucleotideSeq.count('G') + NucleotideSeq.count('C'))/len(NucleotideSeq))*100
if (GCPercentage < AllParameter.MaxGC and GCPercentage > AllParameter.MinGC):
# Assess Repeat Nucleotide
NoRepeat = True
for RROI in ['N','A','G','C','T']:
RROIString = RROI * AllParameter.NucleotideRepeatLimit
if RROIString in NucleotideSeq:
NoRepeat = False
if NoRepeat == True:
# Run Secondary Structure Check / Self Dimerisation
SDSSFilterPass=True
MaxBindingSites, MaxBindingString, IgnoreThresh, IgnoreString = SSIdentification(NucleotideSeq,NucleotideSeq,True)
if MaxBindingSites > AllParameter.DimerisationThresh:
SDSSFilterPass=False
if SDSSFilterPass == True:
# Define row dictionary if passed all above stages
RowDict = {'Oligo_Sequence':NucleotideSeq,
'Oligo_Type':OligoType,
'Binding_Site_Start_Index':i,
'Oligo_Length':TSL,
'Identity_Score':MeanHomologyScore,
'GC_Content': GCPercentage,
'Dimerisation Percentage Score':MaxBindingSites,
'Dimerisation String':MaxBindingString,
'3 prime conserved identity':ThreePrimeIdentityScore,
'5 prime conserved identity':FivePrimerIdentityScore}
# Assess if Specific Probe Check is Needed.
ProbePass = True
if OligoType == 'Probe':
# Run Specific Exo Probe Checks
if AllParameter.ProbeRequired == 'EXO':
ProbePass = RunFluroProbeAnalysis(NucleotideSeq)
if ProbePass == True:
# Add Run Blastn Check
BlastnPass = True
#If Background Check Neccessary Look To Change Blastn pass
if AllParameter.BackgroundCheck.upper() != 'NO':
# Run Blastn Check And If Passess Write Out Set
MaxBackgroundScoreBindingScore, MaxScoreBackSeq, HardFailBool = BlastnBackgroundCheck(NucleotideSeq, AllParameter)
# Check to see if Max Binding Score Less Than Threshold
if MaxBackgroundScoreBindingScore > AllParameter.CrossReactivityThresh:
BlastnPass = False
else:
RowDict['Max Background Cross Reactivity Score'] = MaxBackgroundScoreBindingScore
RowDict['Max Background Cross Reactivity SeqID'] = MaxScoreBackSeq
## NEW NEW NEW ## - Check to see if oligo type is primer and then if it failed the hard-fail setting - Possible add parameter into this check as well
if (HardFailBool == True and OligoType == 'Primer' and AllParameter.HardCrossReactFilter!='NO'):
BlastnPass = False
# If it passed Background Filtering
if BlastnPass == True:
oligo_records.append(RowDict)
# Return Primer/Probe DataFrame
return pd.DataFrame.from_records(oligo_records)
def ComboIdentifyier(PrimerSS,ReversePrimerSS,ProbeSS,AllParameter,PassedOligos,FPrimerL,RPrimerL,ProbeL):
CombosList = []
ps_records = []
for FP in PrimerSS:
# If Probe Required
if AllParameter.ProbeRequired in ['EXO','NFO']:
Probes = [prss for prss in ProbeSS if (prss >= (FP+FPrimerL) and
prss<= (FP+AllParameter.AmpliconSizeLimit-RPrimerL-ProbeL))]
for Probe in Probes:
ReversePrimers = [rpss for rpss in ReversePrimerSS if (rpss >= (Probe+ProbeL) and
rpss <= (FP+AllParameter.AmpliconSizeLimit-RPrimerL))]
CombosList += list(itertools.product([FP],ReversePrimers,[Probe]))
# Probe Not Required
else:
ReversePrimers = [rpss for rpss in ReversePrimerSS if (rpss >= (FP+FPrimerL) and
rpss <= (FP+AllParameter.AmpliconSizeLimit-RPrimerL))]
CombosList += list(itertools.product([FP],ReversePrimers))
# Extract Valid Primer-Probe Combos
for Combo in CombosList:
# Identify Indexes
FPIndex = PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['Oligo_Length']==FPrimerL)&(PassedOligos['Binding_Site_Start_Index']==Combo[0])].index.tolist()[0]
RPIndex = PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['Oligo_Length']==RPrimerL)&(PassedOligos['Binding_Site_Start_Index']==Combo[1])].index.tolist()[0]
# Extract Combo Primer Sequences - (Reverse Complement RP)
ComboSeqList = [PassedOligos.loc[FPIndex,'Oligo_Sequence'],getComplement(PassedOligos.loc[RPIndex,'Oligo_Sequence'],True)]
# Add Probe Sequence If Necessary
if AllParameter.ProbeRequired in ['EXO','NFO']:
ProbeIndex = PassedOligos[(PassedOligos['Oligo_Type']=='Probe')&(PassedOligos['Oligo_Length']==ProbeL)&(PassedOligos['Binding_Site_Start_Index']==Combo[2])].index.tolist()[0]
ComboSeqList.append(PassedOligos.loc[ProbeIndex,'Oligo_Sequence'])
#Run Dimerisation Check And If Passess Continue
DimerisationPass = True
MaxComboSSScore = 0
MaxComboSSString = ''
for SSSCombo in list(itertools.combinations(ComboSeqList,r=2)):
SSMaxBindingSites, SSMaxBindingString, IgnoreThresh, IgnoreString = SSIdentification(SSSCombo[0],SSSCombo[1],True)
if SSMaxBindingSites > MaxComboSSScore:
MaxComboSSScore = SSMaxBindingSites
MaxComboSSString = SSMaxBindingString
if MaxComboSSScore > AllParameter.DimerisationThresh:
DimerisationPass = False
if DimerisationPass == True:
BlastnPass = True ### REMOVE INDEX AT LATER DATE
# Add Everything To DF If Passed All Parameters
if BlastnPass == True:
PassedComboRow = {'Forward Primer (FP)':ComboSeqList[0],
'FP GC%': PassedOligos.loc[FPIndex,'GC_Content'],
'FP Binding Start Site': PassedOligos.loc[FPIndex,'Binding_Site_Start_Index'],
'Reverse Primer (RP)': ComboSeqList[1],
'RP GC%': PassedOligos.loc[RPIndex,'GC_Content'],
'RP Binding Start Site': PassedOligos.loc[RPIndex,'Binding_Site_Start_Index'],
'Amplicon Size': PassedOligos.loc[RPIndex,'Binding_Site_Start_Index'] + RPrimerL - PassedOligos.loc[FPIndex,'Binding_Site_Start_Index'],
'Max Dimerisation Percentage Score':SSMaxBindingSites,
'Max Dimerisation String':MaxComboSSString,
'Forward Primer Length':FPrimerL,
'Reverse Primer Length':RPrimerL,
"Minimum Primer 3' Identity Anchor": min([PassedOligos.loc[RPIndex,'5 prime conserved identity'], PassedOligos.loc[FPIndex,'3 prime conserved identity']])
}
MaxBackgroundScoresIndexes = [FPIndex,RPIndex]
if AllParameter.ProbeRequired in ['EXO','NFO']:
PassedComboRow['Probe (P)'] = ComboSeqList[2]
PassedComboRow['Probe GC%'] = PassedOligos.loc[ProbeIndex,'GC_Content']
PassedComboRow['Probe Binding Start Site'] = PassedOligos.loc[ProbeIndex,'Binding_Site_Start_Index']
PassedComboRow['Probe Length'] = ProbeL
MaxBackgroundScoresIndexes.append(ProbeIndex)
if AllParameter.BackgroundCheck != 'NO':
MaxBackgroundScoreBindingScore = 0
MaxScoreBackSeq = ''
for SucIndex in MaxBackgroundScoresIndexes:
if PassedOligos.loc[SucIndex,'Max Background Cross Reactivity Score'] > MaxBackgroundScoreBindingScore:
MaxBackgroundScoreBindingScore = PassedOligos.loc[SucIndex,'Max Background Cross Reactivity Score']
MaxScoreBackSeq = PassedOligos.loc[SucIndex,'Max Background Cross Reactivity SeqID']
PassedComboRow['Max Background Cross Reactivity Score'] = MaxBackgroundScoreBindingScore
PassedComboRow['Max Background Binding Seq'] = MaxScoreBackSeq
# Add Passed Row To DataFrame
ps_records.append(PassedComboRow)
return pd.DataFrame.from_records(ps_records)
# Main Function To Generate Primer - Probe Combos
def CheckingAlignedOutputFile(AllParameter):
# Check If Binding Sites Already Exist
if AllParameter.PriorBindingSite != 'NO':
print('Reading In Oligo Binding Sites')
PassedOligos = pd.read_csv('{}'.format(AllParameter.PriorBindingSite))
# Filter on Parameters Passed In
PassedOligos = PassedOligos[(PassedOligos['GC_Content']>=AllParameter.MinGC)&
(PassedOligos['GC_Content']<=AllParameter.MaxGC)&
(PassedOligos['Dimerisation Percentage Score']<=AllParameter.DimerisationThresh)&
(PassedOligos['Identity_Score']>=AllParameter.IdentityThreshold)]
# Filter on Primer + Probe Ranges - (Tidy Up At Later Date)
PPLengthDict = {}
PrimerRange=False
ProbeRange = False
for LL in [AllParameter.PrimerLength,AllParameter.ProbeLength]:
if LL == AllParameter.PrimerLength:
LLType = 'Primer'
else:
LLType = 'Probe'
if '-' in str(LL):
LLRangePrior = [int(LLLI.strip()) for LLLI in LL.split('-')]
PPLengthDict['{}_Max'.format(LLType)] = max(LLRangePrior)
PPLengthDict['{}_Min'.format(LLType)] = min(LLRangePrior)
if LLType == 'Primer':
PrimerRange = True
else:
ProbeRange = True
else:
PPLengthDict['{}_Len'.format(LLType)] = int(LL)
if PrimerRange == True:
PrimerPassedSubet = PassedOligos[(PassedOligos['Oligo_Type']=='Primer') &
(PassedOligos['Oligo_Length']>=PPLengthDict['Primer_Min']) &
(PassedOligos['Oligo_Length']<=PPLengthDict['Primer_Max']) ]
else:
PrimerPassedSubet = PassedOligos[(PassedOligos['Oligo_Type']=='Primer') &
(PassedOligos['Oligo_Length']==PPLengthDict['Primer_Len'])]
if ProbeRange == True:
ProbePassedSubet = PassedOligos[(PassedOligos['Oligo_Type']=='Probe') &
(PassedOligos['Oligo_Length']>=PPLengthDict['Probe_Min']) &
(PassedOligos['Oligo_Length']<=PPLengthDict['Probe_Max']) ]
else:
ProbePassedSubet = PassedOligos[(PassedOligos['Oligo_Type']=='Probe') &
(PassedOligos['Oligo_Length']==PPLengthDict['Probe_Len'])]
PassedOligos = pd.concat([PrimerPassedSubet,ProbePassedSubet]).reset_index().drop(['index'],axis=1)
# If Background Check is Present filter
if AllParameter.BackgroundCheck != 'NO':
PassedOligos = PassedOligos[PassedOligos['Max Background Cross Reactivity Score']<=AllParameter.CrossReactivityThresh]
##Check If No Primers Passed Subset
if len(PrimerPassedSubet)==0:
print('No Oligos Passed Filtering')
sys.exit()
if AllParameter.ProbeRequired!='NO':
if len(ProbePassedSubet)==0:
print('No Oligos Passed Filtering')
sys.exit()
#Create Binding DataFrame If It Doesnt Exist
else:
# Load In User Specified
if AllParameter.PriorAlign != 'NO':
print('Reading Alignment Summary')
MergedAlignedDF = pd.read_csv('{}'.format(AllParameter.PriorAlign))
HDFLST = list(range(len(MergedAlignedDF)))
HomoDFInputIndexBlocks = [HDFLST[i:i + AllParameter.Threads] for i in range(0, len(HDFLST), AllParameter.Threads)]
# Create Alignment DF if it doesnt exist
else:
print('Generating Alignment Summary')
# Read in the aligned fasta sequences
fastadict = FastaToDict(AllParameter.InputFile)
# Extract Alignment Length and QC
FirstSeq = True
FastaSeqLength = 0
for fastaseq in fastadict.values():
if FirstSeq == True:
FastaSeqLength = len(fastaseq)
FirstSeq = False
if len(fastaseq) != FastaSeqLength:
sys.exit('ERROR: Alignment file error length of all sequences is not equal')
# Generate Neccessary Alignment Summary DF
HDFLST = list(range(FastaSeqLength))
HomoDFInputIndexBlocks = [HDFLST[i:i + AllParameter.Threads] for i in range(0, len(HDFLST), AllParameter.Threads)]
MTDFOVI = list(zip([fastadict]*len(HomoDFInputIndexBlocks),HomoDFInputIndexBlocks))
with Pool(processes=AllParameter.Threads) as pool:
AlignedDFMultiThreadOupt = pool.starmap(CreatingInputHomologyDF,MTDFOVI)
MergedAlignedDF = pd.concat(AlignedDFMultiThreadOupt).reset_index().drop(['index'],axis=1)
MergedAlignedDF = MergedAlignedDF.sort_values(by=['Index_Pos'])
MergedAlignedDF.to_csv('{}_Alignment_Summary.csv'.format(AllParameter.RunID),index=None)
print('Generating Primer/Probe Binding Site DataFrame')
# Generating Primer/Probe Binding Site DataFrame
PrimerProbeCheckParallelInput = list(zip([AllParameter]*len(HomoDFInputIndexBlocks),
[MergedAlignedDF]*len(HomoDFInputIndexBlocks),
HomoDFInputIndexBlocks))
with Pool(processes=AllParameter.Threads) as pool:
PotentialPrimerProbeOut = pool.starmap(IndentifyingAndFilteringOligos,PrimerProbeCheckParallelInput)
PassedOligos = pd.concat(PotentialPrimerProbeOut).reset_index().drop(['index'],axis=1)
if len(PassedOligos) == 0:
print('No Oligos Passed Filtering')
sys.exit()
PassedOligos.to_csv('{}_PrimedRPA_Oligo_Binding_Sites.csv'.format(AllParameter.RunID),index=None)
# Identify Primer-Probe Sets
print('Identifying Valid Primer-Probe Combinations')
FinalOutputDF = pd.DataFrame()
# Determine all probe lengths available
if AllParameter.ProbeRequired in ['EXO','NFO']:
PossibleProbeLengths = PassedOligos[PassedOligos['Oligo_Type']=='Probe'].loc[:,'Oligo_Length'].unique().tolist()
else:
PossibleProbeLengths = [0]
# Loop through possible FP lengths
if AllParameter.ConservedAnchor == 'NO':
AllParameter.ConservedAnchor = 0
else:
AllParameter.ConservedAnchor = int(AllParameter.ConservedAnchor)
# Loop though Starting Forward Length
for SFPL in PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['3 prime conserved identity']>=AllParameter.ConservedAnchor)].loc[:,'Oligo_Length'].unique():
# Identify all possible FP starting sites
PossibleForwardPrimerSS = PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['Oligo_Length']==SFPL)&(PassedOligos['3 prime conserved identity']>=AllParameter.ConservedAnchor)].loc[:,'Binding_Site_Start_Index'].tolist()
# Loop through all possible RP lengths
for SRPL in PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['5 prime conserved identity']>=AllParameter.ConservedAnchor)].loc[:,'Oligo_Length'].unique():
# Identify all possible RP starting sites
PossibleReversePrimerSS = PassedOligos[(PassedOligos['Oligo_Type']=='Primer')&(PassedOligos['Oligo_Length']==SRPL)&(PassedOligos['5 prime conserved identity']>=AllParameter.ConservedAnchor)].loc[:,'Binding_Site_Start_Index'].tolist()
# Split FP sites into tranches based on number of threads available.
random.shuffle(PossibleForwardPrimerSS)
PrimerStartSiteTranchesOne = [PossibleForwardPrimerSS[i:i + 2] for i in range(0, len(PossibleForwardPrimerSS), 2)]
PrimerStartSiteTranchesTwo = [PrimerStartSiteTranchesOne[i:i + AllParameter.Threads] for i in range(0, len(PrimerStartSiteTranchesOne), AllParameter.Threads)]
# Loop through possible probe lengths.
for PPL in PossibleProbeLengths:
if PPL == 0:
PossibleProbeSS = []
else:
PossibleProbeSS = PassedOligos[(PassedOligos['Oligo_Type']=='Probe')&(PassedOligos['Oligo_Length']==PPL)].loc[:,'Binding_Site_Start_Index'].tolist()
for PSST in PrimerStartSiteTranchesTwo:
if len(FinalOutputDF) < AllParameter.MaxSets: ## Check this threshold setting.
ComboSearchInput = list(zip(PSST,
[PossibleReversePrimerSS]*len(PSST),
[PossibleProbeSS]*len(PSST),
[AllParameter]*len(PSST),
[PassedOligos]*len(PSST),
[SFPL]*len(PSST),
[SRPL]*len(PSST),
[PPL]*len(PSST)))
with Pool(processes=AllParameter.Threads) as pool:
SuccessfulSets = pool.starmap(ComboIdentifyier,ComboSearchInput)
TempOutputDF = pd.concat(SuccessfulSets)
FinalOutputDF = pd.concat([FinalOutputDF,TempOutputDF])
if len(FinalOutputDF) != 0:
FinalOutputDF.to_csv('{}_Output_Sets.csv'.format(AllParameter.RunID),index=None)
print('PrimedRPA Complete')
print('If helpful, please cite:\n\nHiggins M et al. Submitted. 2018\nDOI: 10.1093/bioinformatics/bty701')
########################################################################################################################
########################################################################################################################
########################################################################################################################
print('\n\n')
print('-------------------------------------------')
print('----------------PrimedRPA------------------')
print('-----Finding RPA Primer and Probe Sets-----')
print('-------------Higgins M et al.--------------')
print('-------------------------------------------\n\n')
try:
# Utilise Either Parameters File Or Command Line Interface
if 'PrimedRPA_Parameters.txt' in sys.argv[1]:
# Import and Extract Parameters
parametersFile = sys.argv[1]
try:
paraFile = open(parametersFile,"r")
iu = []
for line in paraFile.readlines():
if ">" in line:
n = line.strip('\n')
h = n.strip('>')
iu.append(h)
u = iu[1:]
class AllParameter:
RunID = str(u[0])
PriorAlign = str(u[1])
PriorBindingSite = str(u[2])
InputFile = str(u[3])
InputFileType = str(u[4])
IdentityThreshold = int(u[5])/100
ConservedAnchor = str(u[6]).strip()
PrimerLength = u[7].strip()
ProbeRequired = str(u[8]).upper()
ProbeLength = u[9].strip()
AmpliconSizeLimit = int(u[10])
NucleotideRepeatLimit = int(u[11])
MinGC = int(u[12])
MaxGC = int(u[13])
DimerisationThresh = int(u[14])
BackgroundCheck = str(u[15])
CrossReactivityThresh = int(u[16])
HardCrossReactFilter = str(u[17])
MaxSets = int(u[18])
Threads = int(u[19])
BackgroundSearchSensitivity = str(u[20])
Evalue=int(u[21])
except:
print('Parameters File Could Not Be Opened\nCheck File Path.')
sys.exit()
else:
parser = argparse.ArgumentParser()
parser.add_argument('--RunID', help='Desired Run ID', required=True)
parser.add_argument('--PriorAlign', help='Optional: Path to Prior Binding File',default='NO')
parser.add_argument('--PriorBindingSite', help='Optional: Path to Prior Binding File',default='NO')
parser.add_argument('--InputFile', help='Path to Input File',default='NO')
parser.add_argument('--InputFileType', help='Options [SS,MS,MAS]')
parser.add_argument('--IdentityThreshold', help='Desired Identity Threshold',default=0.99)
parser.add_argument('--ConservedAnchor', help='Identity Anchor Required',type=str,default='NO')
parser.add_argument('--PrimerLength', help='Desired Primer Length',type=str,default='30')
parser.add_argument('--ProbeRequired', help='Options [NO,EXO,NFO]',type=str,default='NO')
parser.add_argument('--ProbeLength', help='Desired Probe Length',type=str,default='50')
parser.add_argument('--AmpliconSizeLimit', help='Amplicon Size Limit',type=int,default=500)
parser.add_argument('--NucleotideRepeatLimit', help='Nucleotide Repeat Limit (i.e 5 = AAAAA)',type=int,default=5)
parser.add_argument('--MinGC', help='Minimum GC Content',type=int,default=30)
parser.add_argument('--MaxGC', help='Maximum GC Content',type=int,default=70)
parser.add_argument('--DimerisationThresh', help='Percentage Dimerisation Tolerated',type=int,default=40)
parser.add_argument('--BackgroundCheck', help='Options [NO, Path To Background Fasta File]',default='NO')
parser.add_argument('--CrossReactivityThresh', help='Max Cross Reactivity Threshold',type=int,default=60)
parser.add_argument('--HardCrossReactFilter', help='Hard Cross Reactivity Filter',type=str,default='NO')
parser.add_argument('--MaxSets', help='Default Set To 100',type=int,default=100)
parser.add_argument('--Threads', help='Default Set To 1',type=int,default=1)
parser.add_argument('--BackgroundSearchSensitivity', help='Options [Basic or Advanced or Fast]',type=str,default='Basic')
parser.add_argument('--Evalue', help='Default Set To 1000',type=int,default=1000)
AllParameter = parser.parse_args()
except:
print('Please run PrimedRPA --help to see valid options')
sys.exit()
# Check Files Defined Exist
FileLocationsCheckList = [AllParameter.PriorAlign,
AllParameter.PriorBindingSite,
AllParameter.BackgroundCheck,
AllParameter.InputFile]
for FL in FileLocationsCheckList: