forked from davidhoover/DNAWorks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores.f90
1612 lines (1372 loc) · 49.9 KB
/
scores.f90
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
SUBROUTINE AT_Score
!
! Find all the 8 nt windows of solid AT content and update AScore.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
IF (TEST1) PRINT *,"AT_Score" !TEST1
DO i=1,DNAlen
CurrDNA%AScore(i) = 0
END DO
DO i=1,DNAlen-7
IF (CurrDNA%ntID_AT(i).eq.0) THEN
DO j=i,(i+7)
CurrDNA%AScore(j)=CurrDNA%AScore(j)+1
END DO
END IF
END DO
CurrDNA%TotalAScore = 0.0 ! Initialize the repeat scores
DO i=1,DNAlen
CurrDNA%TotalAScore=CurrDNA%TotalAScore+CurrDNA%AScore(i)
END DO
CurrDNA%TotalAScore=CurrDNA%TotalAScore*20/DNAlen
END SUBROUTINE AT_Score
SUBROUTINE Average_Evaluate_Scores()
!
! This subroutine determines the average scores for the current sequence, each time
! changing the degenerate sequences. It updates
! TScore, CScore, RScore, and PScore arrays, the Total*Score values.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i
REAL :: dC, dL, dT, dR, dM, dG, dA, dF, dP, dTotal
IF (TEST1) PRINT *,"Average_Evaluate_Scores" !TEST1
dC=0
dL=0
dT=0
dR=0
dM=0
dG=0
dA=0
dF=0
dP=0
dTotal=0
DO i=1,NumDegPos*10
CALL Fix_Degenerates
CALL Create_ntID_Arrays
CALL Temp_Score ! TScore(i) based on olaps
CALL Misprime_Score ! MScore(i) based on nt
CALL Length_Score ! LScore(i) based on nt
CALL GapFix_Score ! FScore(i) based on nt
IF (ScoreCodons) CALL Codon_Score ! CScore(i) based on codons
CALL Repeat_Score ! RScore(i) based on nt
CALL GC_Score ! GScore(i) based on nt
CALL AT_Score ! AScore(i) based on nt
CALL Pattern_Score ! PScore(i) based on nt
dC=CurrDNA%TotalCScore+dC
dL=CurrDNA%TotalLScore+dL
dT=CurrDNA%TotalTScore+dT
dR=CurrDNA%TotalRScore+dR
dM=CurrDNA%TotalMScore+dM
dG=CurrDNA%TotalGScore+dG
dA=CurrDNA%TotalAScore+dA
dF=CurrDNA%TotalFScore+dF
dP=CurrDNA%TotalPScore+dP
dTotal=dC+dL+dT+dR+dM+dG+dA+dF+dP+dTotal
END DO
CurrDNA%TotalCScore=dC/(NumDegPos*10)
CurrDNA%TotalLScore=dL/(NumDegPos*10)
CurrDNA%TotalTScore=dT/(NumDegPos*10)
CurrDNA%TotalRScore=dR/(NumDegPos*10)
CurrDNA%TotalMScore=dM/(NumDegPos*10)
CurrDNA%TotalGScore=dG/(NumDegPos*10)
CurrDNA%TotalAScore=dA/(NumDegPos*10)
CurrDNA%TotalFScore=dF/(NumDegPos*10)
CurrDNA%TotalPScore=dP/(NumDegPos*10)
CurrDNA%OverallScore = (Cwt*CurrDNA%TotalCScore)+&
(Lwt*CurrDNA%TotalLScore)+&
(Twt*CurrDNA%TotalTScore)+&
(Rwt*CurrDNA%TotalRScore)+&
(Mwt*CurrDNA%TotalMScore)+&
(Gwt*CurrDNA%TotalGScore)+&
(Awt*CurrDNA%TotalAScore)+&
(Fwt*CurrDNA%TotalFScore)+&
(Pwt*CurrDNA%TotalPScore)
CALL Revert_Degenerates
END SUBROUTINE Average_Evaluate_Scores
SUBROUTINE Codon_Score
!
! This subroutine calculates a global score for codons based on frequency.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i
IF (TEST1) PRINT *,"Codon_Score" !TEST1
CurrDNA%TotalCScore=0.0
IF (MutProtPos.eq.0) THEN
DO i=1,PROTlen
CurrDNA%CScore(i)=(1-(CFT(CurrDNA%prot2cod(i))%Freq/AAT(prot2aa(i))%Freq(1)))**4
END DO
ELSE
CurrDNA%CScore(MutProtPos)=(1-(CFT(CurrDNA%prot2cod(MutProtPos))%Freq/AAT(prot2aa(MutProtPos))%Freq(1)))**4
END IF
DO i=1,PROTlen
CurrDNA%TotalCScore=CurrDNA%TotalCScore+CurrDNA%CScore(i)
END DO
CurrDNA%TotalCScore = CurrDNA%TotalCScore/DNAlen
END SUBROUTINE Codon_Score
SUBROUTINE Decrement_Misprime_Arrays()
!
! Removes potential misprime pairs within the current mutant range
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j,y,ct
INTEGER :: TempM1(9999)
INTEGER :: TempM2(9999)
INTEGER :: TempMX(9999)
IF (TEST2) PRINT *,"Decrement_Misprime_Arrays" !TEST2
ct=0
y=MutNtPos(MutNtNum)+1
IF (CurrDNA%MN.gt.0) THEN
loop: DO i=1,CurrDNA%MN
IF ((((CurrDNA%M1(i)+MPLn).ge.MutNtPos(1)).and.&
(CurrDNA%M1(i).le.y)).or.&
(((CurrDNA%M2(i)+MPLn).ge.MutNtPos(1)).and.&
(CurrDNA%M2(i).le.y))) THEN
CYCLE loop
ELSE
ct=ct+1
TempM1(ct)=CurrDNA%M1(i)
TempM2(ct)=CurrDNA%M2(i)
TempMX(ct)=CurrDNA%MX(i)
END IF
END DO loop
CurrDNA%MN=ct
DO i=1,CurrDNA%MN
CurrDNA%M1(i)=TempM1(i)
CurrDNA%M2(i)=TempM2(i)
CurrDNA%MX(i)=TempMX(i)
END DO
END IF
END SUBROUTINE Decrement_Misprime_Arrays
SUBROUTINE Decrement_Repeat_Arrays()
!
! Remove repeat pairs and erase scores
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j,k,y,ct
INTEGER :: TempRS1(9999)
INTEGER :: TempRS2(9999)
INTEGER :: TempLn(9999)
INTEGER :: TempRX(9999)
IF (TEST2) PRINT *,"Decrement_Repeat_Arrays" !TEST2
ct=0
y=MutNtPos(MutNtNum)+1
IF (CurrDNA%RN.gt.0) THEN
loop: DO i=1,CurrDNA%RN
IF ((((CurrDNA%RS1(i)+CurrDNA%RLn(i)).ge.MutNtPos(1)).and.&
(CurrDNA%RS1(i).le.y)).or.&
(((CurrDNA%RS2(i)+CurrDNA%RLn(i)).ge.MutNtPos(1)).and.&
(CurrDNA%RS2(i).le.y))) THEN
DO k=CurrDNA%RS1(i),(CurrDNA%RS1(i)+CurrDNA%RLn(i)-1)
CurrDNA%RScore(k) = CurrDNA%RScore(k)-1
END DO
DO k=CurrDNA%RS2(i),(CurrDNA%RS2(i)+CurrDNA%RLn(i)-1)
CurrDNA%RScore(k) = CurrDNA%RScore(k)-1
END DO
CYCLE loop
ELSE
ct=ct+1
TempRS1(ct)=CurrDNA%RS1(i)
TempRS2(ct)=CurrDNA%RS2(i)
TempLn(ct)=CurrDNA%RLn(i)
TempRX(ct)=CurrDNA%RX(i)
END IF
END DO loop
CurrDNA%RN=ct
DO i=1,CurrDNA%RN
CurrDNA%RS1(i)=TempRS1(i)
CurrDNA%RS2(i)=TempRS2(i)
CurrDNA%RLn(i)=TempLn(i)
CurrDNA%RX(i)=TempRX(i)
END DO
END IF
END SUBROUTINE Decrement_Repeat_Arrays
LOGICAL FUNCTION DegCmpr(instr,seq)
!
! This function compares a restriction site in degenerate form with a sequence.
! It returns .TRUE. if the site matches, and .FALSE. if it does not. The two
! strings MUST be the same length.
!
! The function uses the NEB format of nucleotide degeneracy:
!
! B = C or G or T rev. compl. = V
! D = A or G or T rev. compl. = H
! H = A or C or T rev. compl. = D
! K = G or T rev. compl. = M
! M = A or C rev. compl. = K
! N = A or C or G or T rev. compl. = N
! R = A or G rev. compl. = Y
! S = C or G rev. compl. = S
! V = A or C or G rev. compl. = B
! W = A or T rev. compl. = W
! Y = C or T rev. compl. = R
!
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
CHARACTER(LEN=100) :: instr,seq
INTEGER :: i
INTEGER :: Slen
INTEGER :: stot
IF (TEST3) PRINT *,"DegCmpr" !TEST3
Slen=LEN_TRIM(instr)
DegCmpr=.FALSE.
stot=0
! First, check site in sense orientation
DO i=1,Slen
SELECT CASE(instr(i:i))
CASE('A')
IF (seq(i:i).EQ.'A') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('C')
IF (seq(i:i).EQ.'C') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('G')
IF (seq(i:i).EQ.'G') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('T')
IF (seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('B')
IF (seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'G'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('D')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'G'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('H')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('K')
IF (seq(i:i).EQ.'G'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('M')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'C') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('N')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'G'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('R')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'G') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('S')
IF (seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'G') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('V')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'G') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('W')
IF (seq(i:i).EQ.'A'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
CASE('Y')
IF (seq(i:i).EQ.'C'.OR.seq(i:i).EQ.'T') THEN
stot=stot+1 ; ELSE ; EXIT ; END IF
END SELECT
END DO
IF (stot.EQ.Slen) DegCmpr=.TRUE.
END FUNCTION DegCmpr
SUBROUTINE Equalize_Scores()
!
! Converts individual scores to codon-based Xscores for mutation rounds.
! XScore is a combination of all scores applied to each codon. This should
! allow for a more targeted mutation.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
REAL :: CodPerOlap(999) ! number of codons per overlap, for each overlap
REAL :: TScorePerCod(999) ! average TScore per codon, for each overlap
REAL :: r1
IF (TEST1) PRINT *,"Equalize_Scores"
! Initialize XScore
DO i=1,mutPROTnum ! only use the mutatable codons
XScore(i)=0
END DO
! Initialize values for CodPerOlap
DO i=1,999
CodPerOlap(i)=0
END DO
! Find how many codons are in each overlap, avoiding non-coding regions
! If nt is within a codon, is within an overlap, and is unique protein residue
DO i=1,DNAlen
IF ((nt2prot(i).ne.0).and.(nt2overlap(i).ne.0).and.(nt2prot(i).ne.nt2prot(i-1))) THEN
CodPerOlap(nt2overlap(i))=CodPerOlap(nt2overlap(i))+1
END IF
END DO
! The TScore for each codon a fraction of the total TScore(i) for the overlap
DO j=1,CurrDNA%NumOlaps
IF (CodPerOlap(j).ne.0) TScorePerCod(j)=Twt*(CurrDNA%TScore(j)/CodPerOlap(j))
END DO
! Assign the XScore for TScore, CScore, RScore, PScore, GScore, LScore,
! and AScore for each codon
DO i=1,mutPROTnum
j=prot2nt(mutPROT2prot(i)) ! the middle nt of the codon
! if the middle nt of a codon is within an overlap, the XScore for that codon
! is the average TScore per codon for the overlap
IF (nt2overlap(j).ne.0) XScore(i)=TScorePerCod(nt2overlap(j))
! the CScore contribution is already for the codon
XScore(i)=XScore(i)+(Cwt*CurrDNA%CScore(i))+&
(Rwt*REAL(CurrDNA%RScore(j-1)+CurrDNA%RScore(j)+CurrDNA%RScore(j+1)))+&
(Mwt*REAL(CurrDNA%MScore(j-1)+CurrDNA%MScore(j)+CurrDNA%MScore(j+1)))+&
(Gwt*REAL(CurrDNA%GScore(j-1)+CurrDNA%GScore(j)+CurrDNA%GScore(j+1)))+&
(Awt*REAL(CurrDNA%AScore(j-1)+CurrDNA%AScore(j)+CurrDNA%AScore(j+1)))+&
(Lwt*REAL(CurrDNA%LScore(j-1)+CurrDNA%LScore(j)+CurrDNA%LScore(j+1)))+&
(Fwt*REAL(CurrDNA%FScore(j-1)+CurrDNA%FScore(j)+CurrDNA%FScore(j+1)))+&
(Pwt*REAL(CurrDNA%PScore(j-1)+CurrDNA%PScore(j)+CurrDNA%PScore(j+1)))
END DO
END SUBROUTINE Equalize_Scores
SUBROUTINE Evaluate_Scores()
!
! This subroutine determines the scores for the current sequence. It updates
! TScore, CScore, RScore, and PScore arrays, the Total*Score values.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
REAL :: dC, dL, dT, dR, dM, dG, dA, dF, dP, dTotal
IF (TEST1) PRINT *,"Evaluate_Scores" !TEST1
! Degenerate sequences: loop several times and get average scores
IF (NumDegPos.eq.0) THEN
! If the sequence is recently translated, create the ntID arrays
CALL Create_ntID_Arrays
CALL Temp_Score ! TScore(i) based on olaps
CALL Misprime_Score ! MScore(i) based on nt
CALL Length_Score ! LScore(i) based on nt
CALL GapFix_Score ! FScore(i) based on nt
! The following scores will not change when the overlap positions are moved,
! but only when the sequence is re-translated after a mutation (also does not
! apply for DNA-only runs)
IF (ScoreCodons) CALL Codon_Score ! CScore(i) based on codons
CALL Repeat_Score ! RScore(i) based on nt
CALL GC_Score ! GScore(i) based on nt
CALL AT_Score ! AScore(i) based on nt
CALL Pattern_Score ! PScore(i) based on nt
! Update CurrDNA%OverallScore
CurrDNA%OverallScore = (Cwt*CurrDNA%TotalCScore)+&
(Lwt*CurrDNA%TotalLScore)+&
(Twt*CurrDNA%TotalTScore)+&
(Rwt*CurrDNA%TotalRScore)+&
(Mwt*CurrDNA%TotalMScore)+&
(Gwt*CurrDNA%TotalGScore)+&
(Awt*CurrDNA%TotalAScore)+&
(Fwt*CurrDNA%TotalFScore)+&
(Pwt*CurrDNA%TotalPScore)
ELSE
CALL Average_Evaluate_Scores
END IF
END SUBROUTINE Evaluate_Scores
SUBROUTINE Find_Actual_Misprimes()
!
! If one of the positions in a misprime pair aligns to the end of an overlap,
! and if the tip of the overlap is identical (direct or inverse), then raise
! the score on the nts (CurrDNA%MScore).
! 1. direct-sense(DS): forward primer mispriming on the sense strand
!
! --------------> -------------->
! ||||||||||||||| ..........|||||
! -------------------------------------------------------
!
! 2. inverse-sense(IS): reverse primer mispriming on the sense strand
! NOTE THAT IF THE FORWARD OLIGO MATCHES M2, MSX = 5, NOT 2
!
! -------------->
! ..........|||||
! -------------------------------------------------------
! |||||||||||||||
! <--------------
!
! 3. inverse-antisense(IA): forward primer mispriming on the antisense strand
! NOTE THAT IF THE REVERSE OLIGO MATCHES M2, MSX = 6, NOT 3
!
! -------------->
! |||||||||||||||
! -------------------------------------------------------
! |||||..........
! <--------------
!
! 4. direct-antisense(DA): reverse primer mispriming on the antisense strand
!
! -------------------------------------------------------
! ||||||||||||||| |||||..........
! <-------------- <--------------
!
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j,mp
INTEGER :: o1,o2,m1,m2,mx
IF (TEST2) PRINT *,"Find_Actual_Misprimes" !TEST2
mp=(CurrDNA%NumOlaps+1)/2
! Initialize actual misprime arrays
DO i=1,DNAlen
CurrDNA%MScore(i) = 0
END DO
CurrDNA%MSN=0
mpair: DO i=1,CurrDNA%MN
m1=CurrDNA%M1(i)
m2=CurrDNA%M2(i)
mx=CurrDNA%MX(i)
olap: DO j=1,CurrDNA%NumOlaps
o1=CurrDNA%OlapsPos(j,1)
o2=(CurrDNA%OlapsPos(j,2)-MPLn+1)
IF (TBIO) THEN
IF (j.lt.mp) THEN
SELECT CASE(mx)
CASE(1) ! direct-sense
IF (o2.eq.m1) THEN
CALL Increment_Misprime_Scores(o2,m2,1,j)
ELSE IF (o2.eq.m2) THEN
CALL Increment_Misprime_Scores(o2,m1,1,j)
END IF
CASE(2) ! inverse-sense
IF (o2.eq.m2) CALL Increment_Misprime_Scores(m2,m1,5,j)
CASE(3) ! inverse-antisense
IF (o2.eq.m1) CALL Increment_Misprime_Scores(o2,m2,3,j)
END SELECT
ELSE IF (j.gt.mp) THEN
SELECT CASE(mx)
CASE(2) ! inverse-sense
IF (o1.eq.m1) CALL Increment_Misprime_Scores(o1,m2,2,j)
CASE(3) ! inverse-antisense
IF (o1.eq.m2) CALL Increment_Misprime_Scores(o1,m1,6,j)
CASE(4) ! direct-antisense
IF (o1.eq.m1) THEN
CALL Increment_Misprime_Scores(o1,m2,4,j)
ELSE IF (o1.eq.m2) THEN
CALL Increment_Misprime_Scores(o1,m1,4,j)
END IF
END SELECT
ELSE
SELECT CASE(mx)
CASE(1) ! direct-sense
IF (o2.eq.m1) THEN
CALL Increment_Misprime_Scores(o2,m2,1,j)
ELSE IF (o2.eq.m2) THEN
CALL Increment_Misprime_Scores(o2,m1,1,j)
END IF
CASE(2) ! inverse-sense
IF (o2.eq.m2) THEN
CALL Increment_Misprime_Scores(m2,m1,5,j)
ELSE IF (o1.eq.m1) THEN
CALL Increment_Misprime_Scores(o1,m2,2,j)
END IF
CASE(3) ! inverse-antisense
IF (o2.eq.m1) THEN
CALL Increment_Misprime_Scores(o2,m2,3,j)
ELSE IF (o1.eq.m2) THEN
CALL Increment_Misprime_Scores(o1,m1,6,j)
END IF
CASE(4) ! direct-antisense
IF (o1.eq.m1) THEN
CALL Increment_Misprime_Scores(o1,m2,4,j)
ELSE IF (o1.eq.m2) THEN
CALL Increment_Misprime_Scores(o1,m1,4,j)
END IF
END SELECT
END IF
ELSE
IF (MOD(j,2).eq.0) THEN
CYCLE olap
ELSE
SELECT CASE(mx)
CASE(1) ! direct-sense
IF (o2.eq.m1) THEN
CALL Increment_Misprime_Scores(o2,m2,1,j)
ELSE IF (o2.eq.m2) THEN
CALL Increment_Misprime_Scores(o2,m1,1,j)
END IF
CASE(2) ! inverse-sense
IF (o2.eq.m2) THEN
CALL Increment_Misprime_Scores(m2,m1,5,j)
ELSE IF (o1.eq.m1) THEN
CALL Increment_Misprime_Scores(o1,m2,2,j)
END IF
CASE(3) ! inverse-antisense
IF (o2.eq.m1) THEN
CALL Increment_Misprime_Scores(o2,m2,3,j)
ELSE IF (o1.eq.m2) THEN
CALL Increment_Misprime_Scores(o1,m1,6,j)
END IF
CASE(4) ! direct-antisense
IF (o1.eq.m1) THEN
CALL Increment_Misprime_Scores(o1,m2,4,j)
ELSE IF (o1.eq.m2) THEN
CALL Increment_Misprime_Scores(o1,m1,4,j)
END IF
END SELECT
END IF
END IF
END DO olap
END DO mpair
END SUBROUTINE Find_Actual_Misprimes
SUBROUTINE Find_Potential_Misprimes
!
! This subroutine finds all misprimes in the sequence, both direct and
! inverse, regardless of position, equal or longer than MPLn.
! The inverse search allows palindromic misprimes (i=j).
! The number of potential misprimes is in CurrDNA%MN
! It records the positions and sizes in the global arrays CurrDNA%M1,
! CurrDNA%M2, and CurrDNA%MX.
! The actual misprimes are determined by Find_Actual_Misprimes
! 1. direct-sense(DS): forward primer mispriming on the sense strand
!
! --------------> -------------->
! ||||||||||||||| ..........|||||
! -------------------------------------------------------
!
! 2. inverse-sense(IS): reverse primer mispriming on the sense strand
! NOTE THAT IF THE FORWARD OLIGO MATCHES M2, MSX = 5, NOT 2
!
! -------------->
! ..........|||||
! -------------------------------------------------------
! |||||||||||||||
! <--------------
!
! 3. inverse-antisense(IA): forward primer mispriming on the antisense strand
! NOTE THAT IF THE REVERSE OLIGO MATCHES M2, MSX = 6, NOT 3
!
! -------------->
! |||||||||||||||
! -------------------------------------------------------
! |||||..........
! <--------------
!
! 4. direct-antisense(DA): reverse primer mispriming on the antisense strand
!
! -------------------------------------------------------
! ||||||||||||||| |||||..........
! <-------------- <--------------
!
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
LOGICAL,EXTERNAL :: HMatchNum
IF (TEST2) PRINT *,"Find_Potential_Misprimes" !TEST2
! Initialize the potential misprime arrays
CurrDNA%MN=0
! Find the potential misprimes
DO i=1,DNAlen-MPLn+1
DO j=i,DNAlen-MPLn+1
IF (HMatchNum(i,j,1)) THEN
IF (CurrDNA%ntID_Tip(i+MPLn-MPTip).eq.CurrDNA%ntID_Tip(j+MPLn-MPTip)) &
CALL Increment_Misprime_Arrays(i,j,1)
IF (CurrDNA%ntID_Tip(i).eq.CurrDNA%ntID_Tip(j)) &
CALL Increment_Misprime_Arrays(i,j,4)
END IF
IF (HMatchNum(i,j,-1)) THEN
IF (CurrDNA%ntID_Tip(i).eq.CurrDNA%ntID_TipRC(j+MPLn-MPTip)) &
CALL Increment_Misprime_Arrays(i,j,2)
IF (CurrDNA%ntID_Tip(i+MPLn-MPTip).eq.CurrDNA%ntID_TipRC(j)) &
CALL Increment_Misprime_Arrays(i,j,3)
END IF
END DO
END DO
END SUBROUTINE Find_Potential_Misprimes
SUBROUTINE Find_Repeats()
!
! This subroutine finds all repeats in the sequence, both direct and
! inverse, regardless of position, equal or longer than RepLen.
! The inverse search allows palindromic repeats (i=j).
! It records the positions and sizes in the global arrays CurrDNA%RS1,
! CurrDNA%RS2, and CurrDNA%RLn. Then it overwrites the array CurrDNA%RScore.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
LOGICAL,EXTERNAL :: PairWithinKnownRepeat
IF (TEST2) PRINT *,"Find_Repeats" !TEST2
DO i=1,DNAlen
CurrDNA%RScore(i) = 0
END DO
CurrDNA%RN=0
DO i=1,DNAlen-RepLen+1
DO j=i,DNAlen-RepLen+1
IF (i.ne.j) THEN
IF (.not.PairWithinKnownRepeat(i,j,1)) THEN
IF (CurrDNA%ntID_Rep(i).eq.CurrDNA%ntID_Rep(j)) &
CALL Increment_Repeat_Arrays(i,j,1)
END IF
END IF
IF (.not.PairWithinKnownRepeat(i,j,-1)) THEN
IF (CurrDNA%ntID_Rep(i).eq.CurrDNA%ntID_RepRC(j)) &
CALL Increment_Repeat_Arrays(i,j,-1)
END IF
END DO
END DO
END SUBROUTINE Find_Repeats
SUBROUTINE GC_Score
!
! Find all the 8 nt windows of solid GC content and update GScore.
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
IF (TEST1) PRINT *,"GC_Score" !TEST1
DO i=1,DNAlen
CurrDNA%GScore(i) = 0
END DO
DO i=1,DNAlen-7
IF (CurrDNA%ntID_GC(i).eq.0) THEN
DO j=i,(i+7)
CurrDNA%GScore(j)=CurrDNA%GScore(j)+1
END DO
END IF
END DO
CurrDNA%TotalGScore = 0.0 ! Initialize the repeat scores
DO i=1,DNAlen
CurrDNA%TotalGScore=CurrDNA%TotalGScore+CurrDNA%GScore(i)
END DO
CurrDNA%TotalGScore=CurrDNA%TotalGScore*20/DNAlen
END SUBROUTINE GC_Score
SUBROUTINE GapFix_Score
!
! This subroutine returns the GapFix scores for each nt position in the
! GapFixPos array.
!
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j,c
IF (TEST1) PRINT *,"GapFix_Score" !TEST1
! initialize scores
CurrDNA%TotalFScore=0
DO i = 1,DNAlen
CurrDNA%FScore(i)=0
END DO
DO i=1,DNAlen
! if the position should be within gap
IF(CurrDNA%GapFixPos(i)) THEN
DO j=1,CurrDNA%NumOlaps
! and it is not within a gap (it's in an overlap instead)
IF (i.ge.CurrDNA%OlapsPos(j,1).and.i.le.CurrDNA%OlapsPos(j,2)) THEN
! increase its score
CurrDNA%Fscore(i)=10
END IF
END DO
END IF
END DO
! generate summary of scores
DO i=1,DNAlen
CurrDNA%TotalFScore=CurrDNA%TotalFScore+CurrDNA%FScore(i)
END DO
CurrDNA%TotalFScore=CurrDNA%TotalFScore*20/DNAlen
END SUBROUTINE GapFix_Score
LOGICAL FUNCTION HMatchNum(pos1,pos2,dir)
!
! If two positions of equal length are homologous (MaxNonId or fewer
! non-identical nts), returns true
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: pos1,pos2,i,a,b,dir,ct
IF (TEST3) PRINT *,"HMatchNum" !TEST3
ct=0
IF (dir.eq.1) THEN
direct: DO i=1,MPLn
HMatchNum=.FALSE.
IF (pos1.eq.pos2) EXIT direct
a=pos1+i-1
b=pos2+i-1
IF (b.gt.DNAlen) EXIT direct
IF ((CurrDNA%NUMseq(a)-CurrDNA%NUMseq(b)).ne.0) THEN
ct=ct+1
IF (ct.gt.MaxNonId) EXIT direct
END IF
! PRINT *,dir,pos1,pos2,CurrDNA%NUMseq(a),CurrDNA%NUMseq(b)
HMatchNum=.TRUE.
END DO direct
ELSE
inverse: DO i=1,MPLn
HMatchNum=.FALSE.
a=pos1+i-1
b=pos2+MPLn-i
IF (b.gt.DNAlen) EXIT inverse
IF ((CurrDNA%NUMseq(a)+CurrDNA%NUMseq(b)).ne.0) THEN
ct=ct+1
IF (ct.gt.MaxNonId) EXIT inverse
END IF
HMatchNum=.TRUE.
END DO inverse
END IF
END FUNCTION HMatchNum
SUBROUTINE Increment_Misprime_Arrays(pos1,pos2,dir)
!
! Add another misprime pair to the arrays and update scores
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: pos1,pos2,dir,i
CHARACTER(LEN=80) :: text
IF (TEST2) PRINT *,"Increment_Misprime_Arrays" !TEST2
CurrDNA%MN=CurrDNA%MN+1
IF (CurrDNA%MN.ge.MaxDNAlen) THEN
WRITE(text,FMT="('MN = ',i9,' Too many misprimes.')") CurrDNA%MN
! DO i=1,CurrDNA%MN
! PRINT *,CurrDNA%M1(i),CurrDNA%M2(i),CurrDNA%MX(i)
! END DO
CALL Stop_Program(text)
END IF
CurrDNA%M1(CurrDNA%MN)=pos1
CurrDNA%M2(CurrDNA%MN)=pos2
CurrDNA%MX(CurrDNA%MN)=dir
END SUBROUTINE Increment_Misprime_Arrays
SUBROUTINE Increment_Misprime_Scores(o,m,t,j)
!
! Increment the scores and update the actual misprime arrays
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: o,m,t,i,j
LOGICAL :: x
IF (TEST2) PRINT *,"Increment_Misprime_Scores" !TEST2
CurrDNA%MSN=CurrDNA%MSN+1
CurrDNA%MS1(CurrDNA%MSN)=o
CurrDNA%MS2(CurrDNA%MSN)=m
CurrDNA%MSX(CurrDNA%MSN)=t
CurrDNA%MOL(CurrDNA%MSN)=j
DO i=o,o+MPLn-1
CurrDNA%MScore(i)=CurrDNA%MScore(i)+1
END DO
DO i=m,m+MPLn-1
CurrDNA%MScore(i)=CurrDNA%MScore(i)+1
END DO
END SUBROUTINE Increment_Misprime_Scores
SUBROUTINE Increment_Repeat_Arrays(i,j,dir)
!
! Add another repeat pair to the arrays and update scores after expansion
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j,k
INTEGER :: pos1,pos2,dir,length
INTEGER :: last,diff,a
IF (TEST2) PRINT *,"Increment_Repeat_Arrays" !TEST2
pos1=i
pos2=j
diff=j-i
length=RepLen
! Expand direct repeats
IF (dir.eq.1) THEN
starting: DO pos1=(i-1),1,-1
pos2=pos1+diff
IF (CurrDNA%NUMseq(pos1).ne.CurrDNA%NUMseq(pos2)) THEN
pos2=pos2+1
EXIT starting
END IF
END DO starting
pos1=pos2-diff
length=RepLen+(i-pos1) ! In case pos2 is DNAlen-RepLen-1
ending: DO last=(j+RepLen),DNAlen+1
IF (last.eq.(DNAlen+1)) EXIT ending
IF (CurrDNA%NUMseq(last-diff).ne.CurrDNA%NUMseq(last)) EXIT ending
END DO ending
length=last-pos2 ! Final answer
ELSE
! Expand inverse repeats
startingRC: DO a=1,MaxDNAlen
pos1=i-a
last=j+length-1+a
IF ((pos1.lt.1).or.(last.gt.DNAlen).or.&
(CurrDNA%NUMseq(pos1).ne.(-1*(CurrDNA%NUMseq(last))))) THEN
pos1=pos1+1
last=last-1
EXIT startingRC
END IF
END DO startingRC
endingRC: DO a=1,MaxDNAlen
pos2=j-a
last=i+length-1+a
IF ((pos2.lt.1).or.(last.gt.DNAlen).or.&
(CurrDNA%NUMseq(last).ne.(-1*(CurrDNA%NUMseq(pos2))))) THEN
pos2=pos2+1
last=last-1
EXIT endingRC
END IF
END DO endingRC
length=last-pos1+1 ! Final answer
END IF
CurrDNA%RN=CurrDNA%RN+1
IF (CurrDNA%RN.ge.MaxDNAlen) CALL Stop_Program("Too many repeats.")
CurrDNA%RS1(CurrDNA%RN)=pos1
CurrDNA%RS2(CurrDNA%RN)=pos2
CurrDNA%RLn(CurrDNA%RN)=length
CurrDNA%RX(CurrDNA%RN)=dir
DO k=pos1,(pos1+length-1)
CurrDNA%RScore(k) = CurrDNA%RScore(k)+1
END DO
DO k=pos2,(pos2+length-1)
CurrDNA%RScore(k) = CurrDNA%RScore(k)+1
END DO
END SUBROUTINE Increment_Repeat_Arrays
SUBROUTINE Length_Score
!
! This subroutine evaluates the length of the oligos and gives a penalty to
! all the nts in the oligo if it exceeds OligoLen (except for the first and
! last oligos, of course).
USE dnaworks_data
USE dnaworks_test
IMPLICIT NONE
INTEGER :: i,j
INTEGER :: overrun !the length of the oligo goes past OligoLen
IF (TEST1) PRINT *,"Length_Score" !TEST1
DO i=CurrDNA%OlapsPos(1,1),CurrDNA%OlapsPos(CurrDNA%NumOlaps,2)
CurrDNA%LScore(i)=0
END DO
!PRINT *,'START'
DO i=2,CurrDNA%NumOlaps
overrun=CurrDNA%OlapsPos(i,2)-CurrDNA%OlapsPos((i-1),1)-OligoLen+1
!PRINT *,i,CurrDNA%OlapsPos(i,2),CurrDNA%OlapsPos((i-1),1),OligoLen,overrun,CurrDNA%MeltT(i)
IF (overrun.gt.0) THEN
DO j=CurrDNA%OlapsPos((i-1),1),CurrDNA%OlapsPos(i,2)
CurrDNA%LScore(j)=(overrun+2)**2
END DO
END IF
END DO
!PRINT *,'FINISH'
CurrDNA%TotalLScore = 0.0
DO i=CurrDNA%OlapsPos(1,1),CurrDNA%OlapsPos(CurrDNA%NumOlaps,2)
CurrDNA%TotalLScore = CurrDNA%TotalLScore + CurrDNA%LScore(i)
END DO
CurrDNA%TotalLScore=CurrDNA%TotalLScore*20/DNAlen