module triodata
  integer chkanml(0:5,0:5,0:5), chksire(0:5,0:5,0:5), chkdam(0:5,0:5,0:5)
  integer impute_anml(0:5,0:5,0:5), impute_sire(0:5,0:5,0:5), impute_dam(0:5,0:5,0:5)
  integer chkXmale(0:5,0:5), chkXdam(0:5,0:5)
  integer chkYmale(0:5,0:5), chkYsire(0:5,0:5)
  real    anmlprob(0:5,0:5), sireprob(0:5,0:5), damprob(0:5,0:5)
  real    maleXprob(0:5), damXprob(0:5)
  real    maleYprob(0:5), sireYprob(0:5)
  real    chkmito(0:5,0:5), mitoprob(0:5)
end module triodata
! --------------------------------------------------------------------------
!                                          Initialize arrays for chktrio.f90
  subroutine trio_setup()
  use triodata
    chkanml = 0
    chksire = 0
    chkdam  = 0
    chkXmale = 0
    chkXdam  = 0
    chkYmale = 0
    chkYsire = 0
    chkmito  = 0
    anmlprob = 0.
    sireprob = 0.
    damprob  = 0.
    maleXprob = 0.
    damXprob  = 0.
    maleYprob = 0.
    sireYprob = 0.
    mitoprob  = 0.
    impute_anml = 5
    impute_sire = 5
    impute_dam  = 5
!                                                List progeny conflicts
    chkanml(1,0,0) = 1
    chkanml(2,0,0) = 1
    chkanml(2,0,1) = 1
    chkanml(2,1,0) = 1
    chkanml(0,0,2) = 1
    chkanml(2,2,0) = 1
    chkanml(0,2,0) = 1
    chkanml(2,0,2) = 1
    chkanml(0,2,2) = 1
    chkanml(1,2,2) = 1
    chkanml(0,1,2) = 1
    chkanml(0,2,1) = 1
    chkanml(0,2,5) = 1
    chkanml(0,5,2) = 1
    chkanml(2,0,5) = 1
    chkanml(2,5,0) = 1
!                                                List sire conflicts
    chksire(0,2,0) = 1
    chksire(0,2,1) = 1
    chksire(1,0,0) = 1
    chksire(1,2,2) = 1
    chksire(0,2,2) = 1
    chksire(2,0,2) = 1
    chksire(2,0,1) = 1
    chksire(2,0,0) = 1
    chksire(2,0,5) = 1
    chksire(0,2,5) = 1
!                                                List dam conflicts
    chkdam (0,0,2) = 1
    chkdam (0,1,2) = 1
    chkdam (1,0,0) = 1
    chkdam (1,2,2) = 1
    chkdam (0,2,2) = 1
    chkdam (2,2,0) = 1
    chkdam (2,1,0) = 1
    chkdam (2,0,0) = 1
    chkdam (2,5,0) = 1
    chkdam (0,5,2) = 1
!                                                List male Xchrom conflicts
    chkXmale(0,2) = 1
    chkXmale(2,0) = 1
    chkXmale(1,0) = 1
    chkXmale(1,1) = 1
    chkXmale(1,2) = 1
    chkXmale(1,5) = 1
!                                                List dam Xchrom conflicts
    chkXdam(0,2)  = 1
    chkXdam(2,0)  = 1
!                                                List male Ychrom conflicts
    chkYmale(0,2) = 1
    chkYmale(2,0) = 1
    chkYmale(1,0) = 1
    chkYmale(1,1) = 1
    chkYmale(1,2) = 1
    chkYmale(1,5) = 1
!                                                List sire Ychrom conflicts
    chkYsire(0,2) = 1
    chkYsire(2,0) = 1
!                                                List mitochondrial conflicts
    chkmito(0,2)  = 1
    chkmito(2,0)  = 1
!                                   Prob (detecting anml error | sire, dam)
    anmlprob (0,0) = .75
    anmlprob (0,1) = .25
    anmlprob (0,2) = .75
    anmlprob (0,5) = .25
    anmlprob (1,0) = .25
    anmlprob (1,1) = .00
    anmlprob (1,2) = .25
    anmlprob (1,5) = .00
    anmlprob (2,0) = .75
    anmlprob (2,1) = .25
    anmlprob (2,2) = .75
    anmlprob (2,5) = .25
    anmlprob (5,0) = .25
    anmlprob (5,1) = .00
    anmlprob (5,2) = .25
    anmlprob (5,5) = .00
!                                  Prob (detecting sire error | anml, dam)
    sireprob (0,0) = .25
    sireprob (0,1) = .25
    sireprob (0,2) = .25
    sireprob (0,5) = .25
    sireprob (1,0) = .25
    sireprob (1,1) = .00
    sireprob (1,2) = .25
    sireprob (1,5) = .00
    sireprob (2,0) = .25
    sireprob (2,1) = .25
    sireprob (2,2) = .25
    sireprob (2,5) = .25
    sireprob (5,0) = .00
    sireprob (5,1) = .00
    sireprob (5,2) = .00
    sireprob (5,5) = .00
!                                  Prob (detecting dam error | anml, sire)
    damprob (0,0) = .25
    damprob (0,1) = .25
    damprob (0,2) = .25
    damprob (0,5) = .00
    damprob (1,0) = .25
    damprob (1,1) = .00
    damprob (1,2) = .25
    damprob (1,5) = .00
    damprob (2,0) = .25
    damprob (2,1) = .25
    damprob (2,2) = .25
    damprob (2,5) = .00
    damprob (5,0) = .25
    damprob (5,1) = .00
    damprob (5,2) = .25
    damprob (5,5) = .00
!                                  Prob (detecting male Xchrom error | dam)
    maleXprob (0) = .5
    maleXprob (1) = .0
    maleXprob (2) = .5
!                                  Prob (detecting dam Xchrom error | male)
    damXprob (0) = .25
    damXprob (1) = .00
    damXprob (2) = .25
!                                  Prob (detecting male Ychrom error | sire)
    maleYprob (0) = .5
    maleYprob (1) = 1
    maleYprob (2) = .5
!                                  Prob (detecting sire Ychrom error | male)
    sireYprob (0) = .5
    sireYprob (2) = .5
!                                  Prob (detecting mitochondrial error | dam)
    mitoprob(0)  = 1
    mitoprob(2)  = 1
!                                  Impute genotype from parent genotypes
    impute_anml(5,0,0) = 0
    impute_anml(5,0,2) = 1
    impute_anml(5,2,0) = 1
    impute_anml(5,2,2) = 2
!                                  Impute sire haplotype from progeny and dam
    impute_sire(0,5,0) = 3
    impute_sire(0,5,1) = 3
    impute_sire(0,5,5) = 3
    impute_sire(1,5,2) = 3
    impute_sire(1,5,0) = 4
    impute_sire(2,5,1) = 4
    impute_sire(2,5,2) = 4
    impute_sire(2,5,5) = 4
!                                  Impute dam haplotype from progeny and sire
    impute_dam(0,0,5) = 3
    impute_dam(0,1,5) = 3
    impute_dam(0,5,5) = 3
    impute_dam(1,2,5) = 3
    impute_dam(1,0,5) = 4
    impute_dam(2,1,5) = 4
    impute_dam(2,2,5) = 4
    impute_dam(2,5,5) = 4
  end
! --------------------------------------------------------------------------------------
!                                                      Check parentage conflict at 1 SNP
!                                                              Paul VanRaden  April 2009
!
  subroutine chktrio (genanml, gensire, gendam, sex, chrome_type &
                     ,erranml, errsire, errdam   &
                     ,probanml, probsire, probdam &
                     ,fillanml, fillsire, filldam)
!
!                 INPUTS:  genotype of animal, sire, dam coded 0, 1, 2, or 5 for missing
!                          animal sex coded 'M' or 'F'
!                          chrome_type coded 'A','X','Y','M'
!                                            (A=Autosome or PAR, X, Y, M=Mitochondria)
!                          call trio_setup to initialize arrays (for parallel)
!
!                 OUTPUTS: ok = 0 or error = 1 if animal conflicts with parent,
!                                                 sire conflicts with animal,
!                                                 or dam conflicts with animal
!                          probabilities that errors are detected
!                          genotypes or haplotypes to fill missing values
!                         (haplotypes are coded 3 if 1st allele inherited, 4 if 2nd)
  use triodata
  implicit none
  character*1 sex, chrome_type
  integer genanml, gensire, gendam, first/0/
  integer erranml, errsire, errdam
  integer fillanml, fillsire, filldam
  real    probanml, probsire, probdam
! ----------------------------------------------------------------------  Check genotypes
  erranml = chkanml (genanml, gensire, gendam)
  errsire = chksire (genanml, gensire, gendam)
  errdam  = chkdam  (genanml, gensire, gendam)
  probanml = anmlprob (gensire, gendam)
  probsire = sireprob (genanml, gendam)
  probdam  = damprob  (genanml, gensire)
  fillanml = impute_anml (genanml, gensire, gendam)
  fillsire = impute_sire (genanml, gensire, gendam)
  filldam  = impute_dam  (genanml, gensire, gendam)
!                                                 Male SNPs on X chrome
  if (chrome_type == 'X') then
    if (sex == 'M') then
      erranml = chkXmale (genanml, gendam)
      errsire = 0
      errdam  = chkXdam (genanml, gendam)
      probanml = maleXprob (gendam)
      if (genanml == 1) probanml = 1
      probsire = 0.
      probdam  = damXprob (genanml)
!!!                                 Not imputing on X yet
      fillanml = 5
      fillsire = 5
      filldam  = 5
      end if
    end if
!                                                 Male SNPs on Y chrome
  if (chrome_type == 'Y') then
    if (sex == 'M') then
      erranml = chkYmale (genanml, gensire)
      errsire  = chkYsire (genanml, gensire)
      errdam = 0
      probanml = maleYprob (gensire)
      probsire  = sireYprob (genanml)
      probdam = 0.
      if (genanml == 1) probanml = 1.
      if (gensire == 1) probsire = 1.
!                                                 Females must have Y = 5
    else
      if (genanml .ne. 5) erranml = 1
      if (genanml .ne. 5) probanml = 1
      end if
!!!                                Not imputing on Y yet
    fillanml = 5
    fillsire = 5
    filldam  = 5
    end if
!                                                 SNPs on Mitochondria
  if (chrome_type == 'M') then
    erranml = chkmito (genanml, gendam)
    errsire = 0
    errdam  = chkmito (genanml, gendam)
    probanml = mitoprob (gendam)
    probsire = 0
    probdam  = mitoprob (genanml)
!!!                                Not imputing on M yet
    fillanml = 5
    fillsire = 5
    filldam  = 5
    end if
  if (genanml == 5) then
    probanml = 0.
    probsire = 0.
    probdam  = 0.
    end if
  if (gensire == 5) probsire = 0.
  if (gendam  == 5) probdam  = 0.
  return
  end
