forked from SCAU-AnimalGenetics/GPOPSIMv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPOPSIM.f90
2650 lines (2348 loc) · 64.9 KB
/
GPOPSIM.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
Program GPOPsim
implicit none
interface
subroutine popsim(mode,io_mode,nmk,nq,ngen,inh2,innq,selectlog)
implicit none
integer(1) :: mode
integer(1) :: io_mode
integer(4),optional :: nmk
integer(4),optional :: nq
integer(4),optional :: ngen
integer(4),optional :: innq
real(8),optional :: inh2
logical, optional :: selectlog
end subroutine
end interface
integer(1) :: io_mode
integer(1) :: mode
integer(4) :: nmk
integer(4) :: nq
integer(4) :: ngen
integer(4) :: io_log
io_mode=1 ! 1- interaction, 2- no interaction IN WINDOWS, 3/4 IN LINUX
mode=1 ! 1- main program, 2- subroutine
io_log=39
open(io_log, file='log.txt', status='replace')
call popsim(mode,io_mode)
close(io_log)
end program
MODULE RANDOMM
IMPLICIT NONE
INTEGER(4), SAVE :: RSEED
INTEGER(4) :: seed1
INTEGER(4) :: seed2
INTEGER(4) :: seed3
CONTAINS
FUNCTION randz( )
IMPLICIT NONE
REAL(8) :: randz
RSEED=DMOD(16807D0 *RSEED, 2147483647D0)
RANDZ=RSEED/2147483647.0
RETURN
END FUNCTION
SUBROUTINE startseed(x)
implicit none
INTEGER(4) :: rec(8)
INTEGER(4) :: x
INTEGER(4) :: clock ! lixiujin
if (x==0) then
call date_and_time (values=rec)
seed1=1.0E6*rec(8)+1.0E4*rec(3)+1.0E2*rec(6)+rec(7)
! call system_clock(count=clock)
! seed1=clock !or seed1=clock+37 lixiujin added
else
seed1=x
end if
rseed=seed1 !seed1-3 is random seed in pro. rseed is start seed.
seed2=rseed**2
seed3=rseed*(rseed-2)+5
END SUBROUTINE
Function rand1()
implicit none
real(8) :: rand1
real(8) :: u1
seed1=mod(seed1*171,30269)
seed2=mod(seed2*172,30307)
seed3=mod(seed3*170,30323)
u1=seed1*1.d0/30269.d0+seed2*1d0/30307.d0+seed3*1.d0/30323.d0
rand1=u1-int(u1)
END function
Function Randunif(a,b)
implicit none
integer(4) :: a
integer(4) :: b
integer(4) :: randunif
integer(4) :: t
real(8) :: u
u=randz()
t=b-a+1
Randunif=a+int(t*u)
return
END function
Function Randunifreal(a,b)
implicit none
real(8) :: a
real(8) :: b
real(8) :: randunifreal
real(8) :: t
real(8) :: u
u=randz()
! t=b-a+1 ! original code(error code)
t=b-a ! right code
Randunifreal=a+t*u
return
END function
Function Stdnorm()
implicit none
real(8) :: u1
real(8) :: u2
real(8) :: stdnorm
U1=821.0 !801.0
IF(U1.GE.254) GOTO 1100
Stdnorm=U1
U1=255
RETURN
1100 u1=randz()
U1=SQRT(-2*DLOG(U1))
U2=6.2831852*randz( )
stdnorm=U1*COS(U2)
U1=U1*SIN(U2)
RETURN
END function
Function gamma(a,b)
implicit none
real(4) :: a
real(4) :: b
real(8) :: u1
real(8) :: u2
real(8) :: y
real(8) :: x
real(8) :: gamma
real(8) :: lamd
real(8) :: mu
real(8) :: cit
real(8) :: d
real(8) :: u
real(8) :: z
real(8) :: w
integer(4) :: i
if (a>1.0E0 .and. amod(a,1.0E0)==0.0E0) then
x=0.0E0
do i=1,a
x=log(randz())+x
end do
gamma=-1.0E0*x/b
return
elseif (a>1.0E0 .and. amod(a,1.0E0)/=0.0E0) then
lamd=sqrt(2*a-1)
mu=a**lamd
cit=4.5
d=1+log(cit)
202 u1=randz()
do i=1,50
u2=randz()
end do
u=log(u1/(1.0E0-u1))/lamd
x=a*exp(u)
z=u1*u1*u2
w=(a-lamd)*u+a-log(4.0)-x
if (w+d-cit*z >=0.0E0 .or. log(z)<=w) then
gamma=x/b
return
else
goto 202
end if
elseif (0.0D0 <= a .and. a <= 1.0D0) then
201 u1=randz()
do i=1,50
u2=randz()
end do
y=b*u1
if (y<=1) then
x=y**(1.0E0/a)
if(u2<= exp(-x)) then
gamma=x/b
return
else
goto 201
end if
elseif (y>1.0e0) then
x=-1.0E0*dlog((b-y)/a)
if(u2<= x**(a-1.0E0)) then
gamma=x/b
return
else
goto 201
end if
else
call show(2 , 'QTL parameter error?',1)
endif
else
call show(2 ,'RANDOM GAMMA DISTRIBUTION parameter error!',1)
stop
end if
end function
Function chisq(a)
implicit none
real(4) :: a
real(4) :: chisq
chisq=gamma( a/2.0E0, 0.5E0)
END Function
function rexp(kk) ! Exponential distribution
implicit none
real(8) :: kk
real(8) :: rexp
rexp=-1.0D0*log(randz())/kk
end function
! Cholesky decomposition (test)
subroutine Chde(V,n,L)
implicit none
integer,intent(in) :: n
real(4),intent(in) :: V(n,n)
real(4),intent(out) :: L(n,n)
integer :: i,j,k
real(4) :: sum1
if(V(1,1)<=0.0E0) then
write(*,*) "Cholesky decomposition failed for the first element no more than 0 "
end if
L(1,1)=sqrt(V(1,1)) ! determine numbers in col one
do i=2,n
L(i,1)=V(i,1)/L(1,1)
end do
sum1=0.
do j=2,(n-1) !determine numbers in col j
do k=1,(j-1)
sum1=sum1+L(j,k)**2
end do
if(V(j,j)-sum1<=0.0E0) then
write(*,"(A,i,A,i,A)") "Cholesky decomposition failed for the",j,"*",j,"element no more than 0 "
end if
L(j,j)=sqrt(V(j,j)-sum1)
sum1=0.
do i=(j+1),n
do k=1,(j-1)
sum1=sum1+L(i,k)*L(j,k)
end do
L(i,j)=(V(i,j)-sum1)/L(j,j)
sum1=0.
end do
end do
sum1=0.
do k=1,(n-1) !determine numbers in col n
sum1=sum1+L(n,k)**2
end do
if(V(n,n)-sum1<=0.0E0) then
write(*,"(A,i,A,i,A)") "Cholesky decomposition failed for the",n,"*",n,"element no more than 0 "
end if
L(n,n)=sqrt(V(n,n)-sum1)
return
end subroutine Chde
subroutine multinorm(V,n,Y)
implicit none
integer,intent(in) :: n
real(4),intent(in) :: V(n,n)
! real*8,intent(in) :: u(n)
real(4),intent(out) :: Y(n)
real(4),allocatable :: L(:,:)
real(4),allocatable :: z(:)
integer :: i,j
real(4) :: u1
allocate(L(n,n),z(n))
z(1)=Stdnorm()
do i=1,(n-1)
do j=1,5000
u1=Stdnorm()
end do
z(i+1)=u1
end do
call Chde(V,n,L)
do i=1,n
y(i)=0.0D0
do j=1,i
y(i)=y(i)+L(i,j)*z(j)
end do
end do
deallocate(L,z)
return
end subroutine multinorm
END MODULE
subroutine printtime
INTEGER(4) :: DATE_TIME(8)
CHARACTER(12) :: REAL_CLOCK(3)
integer(4) :: io_log
io_log=39
CALL DATE_AND_TIME (REAL_CLOCK (1), REAL_CLOCK (2), &
REAL_CLOCK (3), DATE_TIME)
print *,real_clock(1)(7:8),'/',real_clock(1)(5:6),&
'/',real_clock(1)(1:4),' ', &
real_clock(2)(1:2),':',real_clock(2)(3:4),':',real_clock(2)(5:6)
write (io_log, *) real_clock(1)(7:8),'/',real_clock(1)(5:6),&
'/',real_clock(1)(1:4),' ', &
real_clock(2)(1:2),':',real_clock(2)(3:4),':',real_clock(2)(5:6)
end subroutine
subroutine show(iomode, a, t)
implicit none
integer(1) :: iomode
character*(*) :: a
integer(1),optional :: t ! 1 output time, 0 no time show
INTEGER(4) :: DATE_TIME(8)
CHARACTER(12) :: REAL_CLOCK(3)
integer(4) :: io_log
io_log=39
CALL DATE_AND_TIME (REAL_CLOCK (1), REAL_CLOCK (2), &
REAL_CLOCK (3), DATE_TIME)
write(io_log,'(a)') trim(a)
if (present(t) .and. t==1) then
write (io_log, *) real_clock(1)(7:8),'/',real_clock(1)(5:6),&
'/',real_clock(1)(1:4),' ', &
real_clock(2)(1:2),':',real_clock(2)(3:4),':',real_clock(2)(5:6)
endif
if (iomode /= 4) then ! linux no interaction
print *, trim(a)
if ( present(t) .and. t==1) then
print *, real_clock(1)(7:8),'/',real_clock(1)(5:6),&
'/',real_clock(1)(1:4),' ', &
real_clock(2)(1:2),':',real_clock(2)(3:4),':',real_clock(2)(5:6)
endif
endif
return
end subroutine
MODULE P_SIM
implicit none
integer(4) :: sim_nref
integer(1), allocatable :: qtype3(:,:) ! qtl genotype
integer(4), allocatable :: pednew3(:) ! working vector for pedigree
real(8), allocatable :: vq3(:) ! qtl effect
integer(4), allocatable :: qtlid3(:) ! position for selected true qtl
real(8), allocatable :: bpv3(:,:) ! breeding values, phenotype values
END MODULE
SUBROUTINE POPSIM(mode,io_mode,nmk,nq,ngen,inh2,innq,l_select)
use p_sim
use randomm
implicit none
! interface for show
interface
subroutine show(iomode, a, t)
integer(1) :: iomode
character*(*) :: a
integer(1),optional :: t
end subroutine
end interface
! subroutine parameters
integer(1) :: mode ! 1-for main program,2- for subroutine
integer(1) :: io_mode ! in/out put mode 1: interaction, 2: no interation, no screen print
integer(4), optional :: nmk ! number of markers
integer(4), optional :: nq ! number of qtl
integer(4), optional :: ngen ! number of generation
integer(4), optional :: innq ! no. qtl of input value
real(8),optional :: inh2 ! heritability of input value
logical(4),optional :: l_select !?? selection
character(20) :: parafile='para_in.txt'
!POP_para
integer(4) :: psire ! total no. sire
integer(4) :: pdam ! total no. dam
integer(4) :: psize ! population size
integer(4) :: gsize ! genome size gsize=2*psize
integer(4) :: pgen ! total no. generations
integer(4) :: mutarule ! mutation rule:1- mutation; 0- no mutation
integer(4) :: materule !?? mate rule: ??
integer(4) :: selerule ! selection rule:
integer(4) :: stage ! no. stage
integer(4) :: subnum !?? sub number
!MQ_para
real(8) :: mratem ! mutation rate of marker
real(8) :: mrateq ! mutation rate of qtl
real(4) :: lenchr ! total length of chromsome
integer(4) :: nmark ! number of markers per chromsome
integer(4) :: nchr ! number of chromsomes
integer(4) :: markrule ! rule for marker position
integer(4) :: tm ! total no. markers
integer(4) :: tq ! total no. putative qtl
integer(4) :: tqtl ! total no. qlt
integer(4),SAVE :: qtlrule ! rule for qtl
integer(4) :: nqtl ! qtl number in output
real(4),SAVE :: qtlp1 ! qtl parameter 1
real(4),SAVE :: qtlp2 ! qtl parameter 2
! real(4) :: h2 ! heritability
real(4),allocatable :: h2(:) ! heritability
real(4) :: va ! Va
!pub_matrix
integer(4), allocatable :: ped(:,:) ! pedigree
integer(4), allocatable :: pednew(:,:) ! working vector for pedigree
integer(4), allocatable :: breds(:) ! selected sires
integer(4), allocatable :: bredd(:) ! selected dams
integer(1), allocatable :: mtype(:,:) ! marker genotype
integer(1), allocatable :: mtypenew(:,:)! working vector for marker genotype
integer(1), allocatable :: mrec(:,:) !?? record for marker mutation
integer(1), allocatable :: qtype(:,:) ! qtl genotype
integer(1), allocatable :: qtypenew(:,:)! working vector for qtl genotype
integer(1), allocatable :: qrec(:,:) ! record for qtl mutation
integer(4), allocatable :: qtlid(:) ! position for selected true qtl
real(8), allocatable :: rrate(:,:) !?? recombination rate : 1 for m-q,2 for q-m
real(8), allocatable :: vq(:,:) ! qtl effect,
real(8), allocatable :: bpv(:,:,:) ! breeding values, phenotype values
! test para
integer(4) :: recom ! counter of recombination
integer(4) :: mutm ! mutation for marker
integer(4) :: mutq ! mutation for qtl
character(8) :: timer1
character(8) :: timer2
integer(4) :: i,j,tgen,k
logical :: selog, l_count
character(400) :: tmpchar
!== lixiujin added===
integer :: ntrait,ntrait_T,k1
integer :: tqtl1,tqtl2,tqtl3
integer,allocatable :: T_no(:) ! Threshold trait number
integer,allocatable :: nTr(:) ! class of threshold trait
! integer :: ind_var ! 0 to be continue trait ,1 to be threshold trait
integer :: uncommon_QTL ! for 2 traits, 0 to be all QTL affecting two traits; 1 to be partial QTL
real*4,allocatable :: ve2(:,:)
real*4,allocatable :: ra(:),re(:)
real*4 :: qtleff_r1,qtleff_r2 ! r3
real*4,allocatable :: Tr(:,:) ! incidence of threshold trait
real*4,allocatable :: T(:,:) ! threshold value
!===GbyE=== added by Xiujin Li 2020-06-28
character :: GbyE
real*4 :: alpha1 ! interactive variance
!real(4) :: alpha0,beta,alpha1,cova0beta,cova0a1,cova1beta
!real(4) :: e0,epsilon,e1,cove0ep,cove0e1,covepe1
real(4),allocatable :: gmat(:,:),emat(:,:)
character*100 :: mode_GbyE !='h1'!h1:y=a0+a1*c+e0, c=beta + epsilon ;h2: y=a0+a1*c+e0+e1*c, c=beta + epsilon
!alpha0=1.0;beta=1.0;alpha1=0.25;cova0a1=0.05
!e0=1.0;epsilon=1.0;e1=1.0;cove0e1=0.05
!=============== Program start ======================
call show(io_mode , 'GPOPsim ___________Last modified 2014/10/17')
if(.not. present(l_select)) i=1
l_count=.false.
if(l_count) open(99,file='hcount.out')
!=====STEP1=====parameter setting
call time(timer1) !timer1, record starting time
call welcomeinf
call readpara
!=====STEP2=====base population installation
call base_set
!=====STEP3=====MDE population
do i=1, pgen-10
call rand_mate(materule,psize)
call genotype()
call allele
if(l_count) call hcount(i)
call select(selerule,psire,pdam)
write(tmpchar,"(I5,3x,A20)") i,"generation finished!"
call show(io_mode, tmpchar)
end do
mutarule=1 ! no mutation
call rand_mate(materule,psize)
call genotype()
if(l_count) call hcount(i)
call RECODEM(tm, mtype,gsize,mrec)
call RECODEM(tq, qtype,gsize,qrec)
if(l_count) call hcount(i)
call select(selerule,psire,pdam)
write(tmpchar,"(I5,3x,A20)") i,"generation finished!"
call show(io_mode, tmpchar)
do i=pgen-9, pgen
call rand_mate(materule,psize)
call genotype()
if(l_count) call hcount(i)
call select(selerule,psire,pdam)
write(tmpchar,"(I5,3x,A20)") i,"generation finished!"
call show(io_mode, tmpchar)
end do
call show(io_mode,"Stage 1 finished!")
!=====STEP4=====one generation of pop - base population
if (stage>1) then
i=1
call changepara(2)
call rand_mate(materule,psize)
call genotype()
if(l_count) call hcount(i,1)
if (mode==1) then
call getqtl
call outputmq
call getBV
if(ntrait_T>=1) then ! simulate threshold
allocate(T(ntrait,0:ntrait*20))
do j=1,ntrait
if(any(j==T_no)) then
do k1=1,(nTr(j)-1)
T(j,k1)=norminv(sum(Tr(j,1:k1)),0.0,sqrt(real(j)/h2(j)))
end do
T(j,0)=-1.0*10**(10)
T(j,nTr(j))=-T(j,0)
write(tmpchar,*)"Threshold value=",T(j,1:(nTr(j)-1))
call show(io_mode, tmpchar)
end if
k1=0
end do
j=0
call threshold
end if
call show(io_mode, "Getbv passed.")
endif
if (mode==2) then
if(present(l_select))then
if( l_select) then !for gebv/phe selection, nqtl,h2 in para.in
nq=tq
call getgenotype(1) !
call show(io_mode, 'Getgenotype 1 passed')
write(tmpchar, '(I12)') RSEED
call show(io_mode, tmpchar)
call getqtl3 (qtlp1,qtlp2)
call show(io_mode, 'getqtl3 1 passed')
call getbv3(1)
endif
end if
end if
call select(selerule,psire,pdam)
call output(pgen)
i=1
write(tmpchar,"(I5,3x,A20)") i,"generation finished!"
call show(io_mode, tmpchar)
call show(io_mode, "Stage 2 finished!")
end if
tgen=1
!=====STEP5=====n generation of pedigree pop
if (stage>2) then
do k=3, stage
call changepara(k)
do i=1, pgen
call rand_mate(materule,psize)
call genotype()
if(l_count) call hcount(i+tgen)
if (mode==1) then
call getBV
if(ntrait_T>=1) then ! simulate threshold
call threshold
end if
endif
if( mode==2) then
if(present(l_select))then
if(l_select) then
call getgenotype(tgen+i)
call getbv3(tgen+i)
endif
endif
end if
call select(selerule,psire,pdam)
call output(tgen+i)
write(tmpchar,"(I5,3x,A20)") tgen+i,"generation finished!"
call show(io_mode, tmpchar)
end do
if (.false.) then
do j=2, subnum
seed1=rseed-1 !change random seed
call baseback !setbase pop value
tgen=tgen+(j-1)*pgen
do i=1, pgen
call rand_mate(materule,psize)
write(*,*) "rand_mate passed",tgen+i
call genotype()
write(*,*) "genotype passed ",tgen+i
if (mode==1) then
call getBV
write(*,*) "getbv passed ",tgen+i
endif
call select(selerule,psire,pdam)
write(*,*) "select passed ",tgen+i
call output(tgen+i)
write(*,*) "putout passed ",tgen+i
write(*,"(I5,3x,A20)") tgen+i,"generation finished!"
end do
write (*,*) "Stage 3, subpop",j,"finished!"
end do
end if
tgen=tgen+pgen
write (tmpchar,'(A5, I4, A10)') "Stage",k," finished!"
call show(io_mode, tmpchar)
end do
end if
if (mode==2) then
nmk=tm
nq=tq
ngen=tgen
end if
call time(timer2) !timer2, record endding time
call overshow(timer1, timer2, seed1)
if(l_count) close(99)
!======================================================================
CONTAINS
SUBROUTINE WELCOMEINF
call show(io_mode, " ========================================")
call show(io_mode, " | GPOPSIM |")
call show(io_mode, " |Genomic POPulation SIMulation software|")
call show(io_mode, " | by Zhang Zhe 2010/03 |")
call show(io_mode, " | [email protected] |")
call show(io_mode, " |Function module- |")
call show(io_mode, " Multiple correlated traits |")
call show(io_mode, " | (continuous and threshold) |")
call show(io_mode, " | by Xiujin Li 2019/09 |")
call show(io_mode, " | [email protected] |")
call show(io_mode, " |Function module- |")
call show(io_mode, " | G by E interaction |")
call show(io_mode, " | by Xiujin Li 2020/06 |")
call show(io_mode, " | [email protected] |")
call show(io_mode, " | by Hailiang Song 2020/06 |")
call show(io_mode, " | [email protected] |")
call show(io_mode, " ========================================")
END SUBROUTINE
! show runing information at the end of popsim
SUBROUTINE OVERSHOW(t1,t2,r)
implicit none
character(*) :: t1,t2
integer(4) :: r
character(40) :: tmpchar
write(*,*)
call show(io_mode, ' ')
call show(io_mode," ========Congratulation========")
call show(io_mode," Program end without any error!")
write(tmpchar,"(9x,A8,"" --> "",A8)") t1,t2
call show(io_mode, tmpchar)
write(tmpchar, '(A26, I14)') " With random seed ", r
call show(io_mode, tmpchar)
call show(io_mode," ==============================")
END SUBROUTINE
SUBROUTINE READPARA
implicit none
integer(4) :: x
integer(4) :: useed
! lixiujin added
integer(4) :: T_index, T_class
real*4 :: T_incidence(20) !!! max number of threhold values=20
if(io_mode==1) then
PRINT *, 'Please input the name of your parameter file.'
read *, parafile
else
call getarg(1,parafile)
parafile=trim(parafile)
end if
open(11, file=parafile)
read (11,*) !line1
read (11,*) ntrait,ntrait_T, uncommon_QTL
read (11,*) !
read (11,*) GbyE,alpha1,mode_GbyE
if(GbyE=='T') then
ntrait=2
ntrait_T=0
end if
allocate(T_no(ntrait),Tr(ntrait,ntrait_T*20))
allocate(nTr(ntrait))
read (11,*) !line2
do i=1, ntrait_T
read (11,*) T_index,T_class, T_incidence(1:(T_class-1)) !!! max number of T_class=20
T_no(T_index)=T_index
nTr(T_index)=T_class
Tr(T_index,1:(T_class-1))=T_incidence(1:(T_class-1))
end do
if(ntrait==1) then
read (11,*) ! genetic correlation
read (11,*)
read (11,*) ! residual correlation
read (11,*)
else
read (11,*) !genetic correlation
allocate(ra(ntrait*(ntrait-1)/2))
ra=0.0
read (11,*) (ra(j),j=1,ntrait*(ntrait-1)/2)
read (11,*) !residual correlation
allocate(re(ntrait*(ntrait-1)/2))
re=0.0
read (11,*) (re(j),j=1,ntrait*(ntrait-1)/2)
end if
read (11,*) !heritablity
allocate(h2(ntrait))
read (11,*) (h2(j),j=1,ntrait)
read (11,*) !line 5
read (11,*) nmark, nchr, markrule
read (11,*) !line 6
read (11,*) lenchr, mratem, mrateq
read (11,*) !line 7
read (11,*) qtlrule, nqtl, qtlp1, qtlp2
read (11,*) !line 8
read (11,*) stage, subnum, useed
read (11,*) !line 9
read (11,*) psire, pdam, psize, pgen
read (11,*) !line 10
read (11,*) mutarule, materule, selerule
close (11)
call startseed(useed) !0-random seed, nonzero- fixed seed
gsize=psize*2
tm=nmark*nchr
tq=(nmark-1)*nchr
allocate(ped(3,psize))
allocate(pednew(3,psize))
allocate(breds(psire))
allocate(bredd(pdam))
allocate(mrec(tm,2))
allocate(qrec(tq,2))
allocate(mtype(tm,gsize))
allocate(qtype(tq,gsize))
allocate(mtypenew(tm,gsize))
allocate(qtypenew(tq,gsize))
allocate(rrate(2,tq))
allocate(bpv(psize,2,ntrait)) ! lixiujin eidted
mrec=0
qrec=0
mrec(:,:)=2
qrec(:,:)=2
END SUBROUTINE
SUBROUTINE changepara(X)
implicit none
integer(4) :: x
integer(4) :: i
open (41, file=parafile)
!do i=1,4+4*x
do i=1,(15+ntrait_T)+4*x ! lixiujin added
read (41,*)
end do
read (41,*) !line 7
read (41,*) psire, pdam, psize, pgen
read (41,*) !line 8
read (41,*) mutarule, materule, selerule
gsize=psize*2
close(41)
END SUBROUTINE
SUBROUTINE READPARA3
implicit none
integer(4) :: i,j,x
integer(4) :: useed
open(11, file=parafile)
read (11,*) !line 1
read (11,*) nmark, nchr, markrule
read (11,*) !line 2
read (11,*) lenchr, mratem, mrateq
read (11,*) !line 3
read (11,*) qtlrule, nqtl, qtlp1, qtlp2,h2
read (11,*) !line 4
read (11,*) stage, subnum, useed
read (11,*) !line 5
read (11,*) psire, pdam, psize, pgen
read (11,*) !line 6
read (11,*) mutarule, materule, selerule
tgen=0
do i=1, stage-1
read (11,*)
read (11,*) j, j, j, pgen
read (11,*) !line 6
read (11,*)
tgen=pgen+tgen
end do
close (11)
tq=(nmark-1)*nchr
END SUBROUTINE
subroutine base_set
use randomm
implicit none
integer(1) :: x1=1
integer(1) :: x2=2
real(8) :: dis
real(8) :: mean
real(8) :: kk1,kk2
real(8) :: pos
integer(4) :: i,j,loc,ct
!==step1== setup genotype of marker and qtl for all indivdiuals
mtype=x1
qtype=x1
!==step2== setup bred individual, pedigree
deallocate(breds)
deallocate(bredd)
allocate(breds(psire))
allocate(bredd(pdam))
!pedigree for the founder, no relationship
do i=1,psize
ped(:,i)=(/i,i,i/)
end do
do i=1,psize/2
breds(i)=2*i-1 ! odd number to be sire
bredd(i)=2*i ! even number to be dam
end do
!==step3== setup position of all loci
rrate=0.0D0
select case (markrule)
case (0) !uniform distribution
dis=0.5E0*lenchr/real(nmark-1)
rrate=0.5E0*(1-exp((-2.0E0)*dis)) ! Haldane's map function dis=-ln(1-2r)/2
case (1) !test exponent distri. for illmima 50k chip
mean=lenchr/real(nmark-1)
!+++++++++++++++++++++++++++++++++++++++++++++++
kk1=0.5*mean !weight for mixed distribution
!+++++++++++++++++++++++++++++++++++++++++++++++
kk2=mean-kk1
ct=0
open (98,file='mposition.out')
do j=1,nchr !get interval length
do i=1,nmark-1
loc=(j-1)*(nmark-1)+i
rrate(1,loc)= Randunifreal(0.0d0, 2.0d0*kk1) + kk2*rexp(1.0D0)
end do
dis=sum(rrate(1,loc-nmark+2:loc)) !total length of chrom j
dis=lenchr/dis
rrate(1,:)=rrate(1,:)*dis
write (98,'(I6, I12, I12)') (j-1)*nmark+1, 0,0 !int(rrate(1,loc)*1.0D8) !bp
do i=1, nmark-1 !rescale to chrom length and get map distance
loc=(j-1)*(nmark-1)+i
ct=ct+int(rrate(1,loc)*1.0D8)
write (98,'(I6, I12, I12)') (j-1)*nmark+i+1, ct, int(rrate(1,loc)*1.0D8) !bp
pos=randz() !get qtl position in marker interval
rrate(2,loc)=rrate(1,loc)*pos
rrate(1,loc)=rrate(1,loc)-rrate(2,loc)
end do
ct=0
end do
rrate=0.5E0*(1-exp((-2.0E0)*rrate)) ! Haldane's map function
close(98)
end select
end subroutine
subroutine rand_mate(rule,size)
use randomm
implicit none
integer(4), intent(in) :: rule ! mating rule
integer(4), intent(in) :: size ! population size of next generation
integer(4), allocatable :: recs(:) ! times of mate for each sire
integer(4), allocatable :: recd(:) ! times of mate for each dam
integer(4) :: ts ! max no. dams to be mated to each sire
integer(4) :: td ! max no. offsprings to be produced for each dam
integer(4) :: maxs ! selected sires in last generation
integer(4) :: maxd ! selected dams in last generation
integer(4) :: id ! id number-- the id for new individuals
integer(4) :: xs ! random integer for sire
integer(4) :: xd ! random integer for dam
integer(4) :: step ! counter for error mating
logical :: sib ! logical for sib or not
logical :: dif ! logical for same family or not
integer(4) :: i,j,k
deallocate(pednew)
allocate(pednew(3,size))
maxs=ubound(breds,1)
maxd=ubound(bredd,1)
allocate(recd(maxd))
allocate(recs(maxs))
1002 step=0
recs=0
recd=0
dif=.true.
ts=ceiling(real(maxd)/real(maxs))
td=ceiling(real(size)/real(maxd))
if (maxd< maxs) then
call show(io_mode,'No. sire larger than dam. Program cannot run.',1)
stop
end if
if (mod(real(size)/real(maxd),1.0E0) /=0.0E0)then
call show(io_mode, 'litter size may not be equal for different families.')
dif=.false.
end if
step=0 !timer for coded animal number.
select case (rule)
case (0) !random mate
do i=1, maxs
do j=1,ts
1021 xd=randunif(1,maxd)
if (recd(xd) == 1) goto 1021
recd(xd)=1
if (i==maxs .and. j==ts .and. dif==.false.) td=size-(maxd-1)*td
do k=1, td
step=step+1
pednew(:,step)=(/step,breds(i),bredd(xd)/)
end do
end do
end do
case (1) !random but without sib mate xxxxxxxxxxxxxxxxxxxxxxxxx
do i=1,size
1030 xs=randunif(1,maxs)
if (recs(xs) >= ts) goto 1030
1031 xd=randunif(1,maxd)
if (recd(xd) >= td) goto 1031
sib=(ped(2,breds(xs))==ped(2,bredd(xd))) .or. (ped(3,breds(xs))==ped(3,bredd(xd)))
if (sib) then !goto 1001
! write(*,*) "Sib mate"
step=step+1
if (step==100) then
call show(io_mode,'Sib mate may not be avoided.',1)
goto 1002
end if
goto 1031
end if
recs(xs)=recs(xs)+1
recd(xd)=recd(xd)+1
pednew(:,i)=(/i,breds(xs),bredd(xd)/)
end do
case (2) ! totally random mate
do i=1, size
pednew(1,i)=i
pednew(2,i)=breds(randunif(1,maxs))
pednew(3,i)=bredd(randunif(1,maxd))
end do
end select
deallocate(ped)
allocate(ped(3,size))
ped=pednew
end subroutine
subroutine select(rule,ns,nd)
use randomm
implicit none
integer(4) :: rule
integer(4) :: ns
integer(4) :: nd
integer(4),allocatable :: rec(:)
real(4), allocatable :: tbv(:)
integer(4) :: size
integer(4) :: x
integer(4) :: tn
integer(4) :: i,j
deallocate (breds)
deallocate (bredd)
allocate(breds(ns))
allocate(bredd(nd))
breds=0