-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTRD.f90
463 lines (366 loc) · 12.9 KB
/
TRD.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
! Estimate rate of Transmission Ratio Distortion for each SNP
! among offspring-dam-sire trios, with all three genotyped
! Use chi-square test (?)
! note: TR may appear distorted due to genotyping errors
! ##############################################################################
module global
implicit none
integer :: nInd, nSnp, ID_len
integer, parameter :: nchar_filename = 2000, nchar_ID = 40
integer,allocatable,dimension(:,:) :: Genos, Parent
logical :: quiet
double precision :: Er, E(0:2,0:2,0:2)
character(len=nchar_ID), allocatable, dimension(:) :: Id
!=========================
contains
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! count number of columns in text file
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
integer function FileNumCol(FileName)
implicit none
character(len=*), intent(IN) :: FileName
integer :: j, strLen, numcol
character(len=500000) :: line
open(unit=102, file=trim(FileName), status="old")
read(102, '(a)' ) line
close(102)
strLen = len_trim(line)
if (strLen == 0) then
FileNumCol = 0
return
endif
numcol = 0 ! first column (no space 'after') achar(9) = \t
do j=1, strLen-1
if (j==1 .and. line(j:j) /= ' ' .and. line(j:j) /= achar(9)) then
numcol = numcol +1
endif
if (line(j:j) == ' ' .or. line(j:j) == achar(9)) then
if (line((j+1):(j+1)) /= ' ' .and. line((j+1):(j+1)) /= achar(9)) then
numcol = numcol +1 ! new column starts at j+1
endif
endif
enddo
FileNumCol = numcol
end function FileNumCol
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! count number of rows in text file
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
integer function FileNumRow(FileName)
implicit none
character(len=*), intent(IN) :: FileName
integer :: nrow, i, maxRow, IOerr
character(len=5000) :: dumC
maxRow = 5000000 ! fail safe
nrow = 0
open(unit=102, file=trim(FileName), status="old")
do i=1, maxRow
read(102,*,IOSTAT=IOerr) dumC
if (IOerr < 0) then
exit ! EOF
else
nrow = nrow +1
end if
enddo
close(102)
FileNumRow = nrow
end function FileNumRow
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
end module global
! ##############################################################################
! ## Main program ##
! ##############################################################################
program TRD
use Global
implicit none
! input
integer :: x, i, nArg
character(len=32) :: arg, argOption
character(len=nchar_filename) :: PedFileName, GenoFileName, OutFileName
logical :: FileOK
! output
! set default values
PedFileName = 'Pedigree.txt'
GenoFileName = 'Geno.txt'
quiet = .FALSE.
Er = 1e-4
! read arguments from command line
nArg = command_argument_count()
if (nArg == 0) then
if (.not. quiet) print *, 'Please specify input files!'
call print_help()
stop
else
i = 0
do x = 1, nArg
i = i+1
if (i > nArg) exit
call get_command_argument(i, arg)
select case (arg)
case ('-h', '--help')
call print_help()
stop
case ('--geno')
i = i+1
call get_command_argument(i, GenoFileName)
case ('--pedigreeIN')
i = i+1
call get_command_argument(i, PedFileName)
case ('--err')
i = i+1
call get_command_argument(i, argOption)
read(argOption, *) Er ! TODO: length 3 vector
case ('-o', '--out')
i = i+1
call get_command_argument(i, OutFileName)
case ('--quiet')
quiet = .TRUE.
case default
print '(2a, /)', 'Unrecognised command-line option: ', arg
call print_help()
stop
end select
enddo
endif
!=========================
if (Er <= 0D0 .or. Er > 0.5) then
write(*,*) "Er must be a value between 0 and 0.5"
stop
endif
! check if input files exist
inquire(file=trim(PedFileName), exist = FileOK)
if (.not. FileOK) then
write(*,*) "Input file ", trim(PedFileName), " not found"
stop
endif
inquire(file=trim(GenoFileName), exist = FileOK)
if (.not. FileOK) then
write(*,*) "Genotype file ", trim(GenoFileName), " not found"
stop
endif
!=========================
! Do stuff
if (.not. quiet) print *, "Reading genotype data in "//trim(GenoFileName)//" ... "
call ReadGeno(GenoFileName)
if (.not. quiet) print *, "Read ", nInd, " Individuals and ", nSnp, " SNPs"
if (.not. quiet) print *, "Reading pedigree in "//trim(PedFileName)//" ... "
call ReadPedFile(PedFileName)
if (.not. quiet) print *, "Read ", COUNT(Parent(1,:)>0 .and. Parent(2,:)>0), " id-dam-sire trios "
if (.not. quiet) print *, "Counting genotype combinations ... "
call prep_E()
call Calc_TRD(OutFileName)
if (.not. quiet) print *, "Output written to ", trim(OutFileName)
call deallocall()
!=========================
contains
subroutine print_help()
print *, ''
print '(a)', '<<< Count transmission ratios >>>'
print *, ''
print '(a, /)', 'command-line options:'
print '(a)', ' -h, --help print usage information and exit'
print '(a)', ' --pedigreeIN <filename> input file with id, dam, sire'
print '(a)', ' --geno <filename> input file with genotype data. Default: Geno.txt'
print '(a)', ' --err presumed genotyping error rate; default: 0.0001'
print '(a)', ' --out <filename> output file '
print '(a)', ' --quiet suppress all messages'
end subroutine print_help
end program TRD
! ##############################################################################
! ## read in data ##
! ##############################################################################
subroutine ReadGeno(GenoFileName)
use Global
implicit none
character(len=nchar_filename), intent(IN) :: GenoFileName
integer :: i, l
integer, allocatable, dimension(:) :: GenosV
character(len=3) :: maxchar_ID
character(len=nchar_ID) :: IDx
nSnp = FileNumCol(trim(GenoFileName)) -1 ! column 1 = IDs
nInd = FileNumRow(trim(GenoFileName))
allocate(GenosV(nSnp))
allocate(Genos(nSnp, nInd)) ! transpose: faster
Genos = -1
allocate(Id(nInd))
Id = "NA"
ID_len = 5
open (unit=101,file=trim(GenoFileName),status="old")
do i=1,nInd
read (101,*) IDx, GenosV
if (ANY(Id == IDx)) then
print *, "ERROR! IDs in genotype file must be unique"
stop
endif
Id(i) = IDx
if (LEN_TRIM(Id(i)) > ID_len) ID_len = LEN_TRIM(Id(i))
do l=1,nSnp
if (GenosV(l)>=0 .and. GenosV(l)<=2) then
Genos(l,i) = GenosV(l)
endif
enddo
enddo
close (101)
deallocate(GenosV)
if (ID_len > nchar_ID) then
write(maxchar_ID, '(i3)') nchar_ID
print *, "ERROR! Max length for IDs is "//maxchar_ID//" characters"
stop
endif
end subroutine ReadGeno
!===============================================================================
subroutine ReadPedFile(FileName)
use Global
implicit none
character(len=*), intent(IN) :: FileName
integer :: i, j, k, x, IOerr, nIndP
character(len=nchar_ID) :: tmpC(3)
character(len=nchar_ID), allocatable, dimension(:,:) :: NamePed
nIndP = FileNumRow(trim(FileName)) -1 ! 1st row = header
allocate(NamePed(3, nIndP))
NamePed = 'NA'
open(unit=103, file=trim(FileName), status="old")
read(103,*) ! header
do i=1,nIndP
read(103, *,IOSTAT=IOerr) tmpC
if (IOerr > 0) then
print *, "Wrong input in file "//trim(FileName)//" on line ", i
stop
else if (IOerr < 0) then
exit ! EOF
else
NamePed(:,i) = tmpC
end if
enddo
close(103)
! Pedigree names to row numbers in genotype file
allocate(Parent(2, nInd))
Parent = 0
do i = 1, nIndP
j = 0
do x= 1,nInd
if (NamePed(1,i) == Id(x)) then
j = x
exit
endif
enddo
if (j==0) cycle
do k = 1,2
do x=1, nInd
if (NamePed(k+1,i) == Id(x)) then
Parent(k,j) = x
exit
endif
enddo
enddo
enddo
if (allocated(NamePed)) deallocate(NamePed)
end subroutine ReadPedFile
! ##############################################################################
! ## Count observed genotype combo's ##
! ##############################################################################
subroutine Calc_TRD(FileName)
use Global
implicit none
character(len=*), intent(IN) :: FileName
double precision :: Chi2(nSnp), O_prop(0:2,0:2,0:2)
integer :: i,j,h,l, hdr_A(0:2,0:2,0:2)
integer :: obs_count(-1:2, -1:2, -1:2, nSnp), nObs(nSnp), O(-1:2,-1:2,-1:2)
character(len=4) :: header(3*3*3)
! observed count
obs_count = 0
nObs = 0
do l=1, nSnp
if (MOD(l, 2000)==0) print *, l
O = 0
do i = 1, nInd
j = Parent(1,i)
h = Parent(2,i)
if (j==0 .or. h==0) cycle
O(Genos(l,i), Genos(l,j), Genos(l,h)) = O(Genos(l,i), Genos(l,j), Genos(l,h)) +1
enddo
obs_count(:,:,:, l) = O
nObs(l) = SUM(O(0:2, 0:2, 0:2)) ! count of id-dam-sire all three genotyped
! Calc Chi-square
O_prop = O(0:2, 0:2, 0:2) / dble(nObs(l))
Chi2(l) = SUM( (O_prop - E)**2 / E ) ! division by zero ...
! Chi2(l) = SUM( (O_prop - E)**2 )
enddo
! Write array to a text file; do further analysis in R (incl P values)
! make header
do h=0,2
do j=0,2
do i=0,2
hdr_A(i,j,h) = 100*i + 10*j + h
enddo
enddo
enddo
write(header, '("G",i3.3)') RESHAPE(hdr_A, (/3*3*3/))
open(unit=42, file=trim(FileName), status='unknown')
write(42, '(2a6, a10, 50a6)') 'SNP', 'Nobs', 'Chi2', header
do l=1, nSnp
write (42,'(2i6, f10.3, 50i6)') l, nObs(l), Chi2(l), RESHAPE(obs_count(0:2, 0:2, 0:2, l), (/3*3*3/))
enddo
close(42)
end subroutine Calc_TRD
! ##############################################################################
subroutine prep_E
use Global
implicit none
! calc expected proportions of observed genotype combinations, with genotyping errors,
! (else division by zero issues), conditional on the trio being id-dam-sire
double precision :: AKA2P(0:2,0:2,0:2), OcA(-1:2,0:2), q, AHWE(0:2), &
AcO(0:2, -1:2), Tmp(0:2, 0:2, 0:2)
integer :: i,j,h, x,y,z
! inheritance conditional on both parents (actual genotypes)
AKA2P(0,0,:) = dble((/ 1.0, 0.5, 0.0 /))
AKA2P(0,1,:) = dble((/ 0.5, 0.25, 0.0 /))
AKA2P(0,2,:) = dble((/ 0.0, 0.0, 0.0 /))
AKA2P(1,0,:) = dble((/ 0.0, 0.5, 1.0 /))
AKA2P(1,1,:) = dble((/ 0.5, 0.5, 0.5 /))
AKA2P(1,2,:) = dble((/ 1.0, 0.5, 0.0 /))
AKA2P(2,0,:) = dble((/ 0.0, 0.0, 0.0 /))
AKA2P(2,1,:) = dble((/ 0.0, 0.25, 0.5 /))
AKA2P(2,2,:) = dble((/ 0.0, 0.5, 1.0 /))
! Prob. observed (rows) conditional on actual (columns) (ErrFlavour' = 2.0)
OcA(-1,:) = 1.0D0 ! missing
OcA(0:2, 0) = (/ (1-Er/2)**2, Er*(1-Er/2), (Er/2)**2 /) ! act=0
OcA(0:2, 1) = (/ Er/2, 1-Er, Er/2 /) ! act=1
OcA(0:2, 2) = (/ (Er/2)**2, Er*(1-Er/2), (1-Er/2)**2 /) ! act=2
! For parents, calc P(act|obs) = P(obs|act) * P(act) / P(obs)
! P(act) = under HWE, P(obs) = scaling factor
! for simplicity, assume all SNPs have allele frequency 0.5
! (TODO later: read in allele frequencies & see if it matters)
q = 0.5
AHWE = (/ (1-q)**2, 2*q*(1-q) , q**2 /)
AcO = 0D0
do y=-1,2 ! observed genotype
do x=0,2 ! actual genotype
AcO(x,y) = OcA(y,x) * AHWE(x)
enddo
AcO(:,y) = AcO(:,y) / SUM(AcO(:,y)) ! scale to sum to 1
enddo
E = 0D0
do h=0,2 ! sire
do j=0,2 ! dam
do i=0,2 ! offspring obs
do z=0,2 ! sire act
do y=0,2 ! dam act
do x=0,2 ! offspring act
Tmp(x,y,z) = OcA(i,x) * AKA2P(x,y,z) * AcO(y,j) * AcO(z,h)
enddo
enddo
enddo
E(i,j,h) = SUM(Tmp)
enddo
enddo
enddo
end subroutine prep_E
! ##############################################################################
subroutine deallocall
use Global
implicit none
if (allocated(Genos)) deallocate(Genos)
if (allocated(Parent)) deallocate(Parent)
if (allocated(Id)) deallocate(Id)
end subroutine deallocall