*! Version : 1.11   (TSJ-1: st0003)
*! Date    : 25 Sep 2001
*! Author  : Adrian Mander
*! Email   : adrian.mander@mrc-bsu.cam.ac.uk

program define dior
version 7.0
syntax [varlist] using/, OR(string) locus(string) caco(varname) [ BY(string) NOPRE ]
di
if "`nopre'"=="" { preserve }

/*******************************************************************
 * I of course have many more than one odds ratio in each stratum
 * hence this program can take multiple haplotypes as exposed and
 * keep one as unexposed and calculate the odds ratios
 *******************************************************************/

tokenize "`or'"
local cc "`1'"
local unexp "`2'"

use "`using'",replace

qui count if freq==.
if r(N)>0 {
di in smcl "{dup 50:{c -}}"
di as res "THESE WILL BE DELETED..."
list freq `locus' `cc' `by' if freq==.
qui drop if freq==.
di in smcl as text "{dup 50:{c -}}"
}

qui  {

if "`by'"=="" {
  drop eprob
  reshape wide freq , i(`locus') j(`cc')
  gen tec = locus=="`unexp'"
  sort tec
  gen exca = freq1
  gen exco = freq0
  gen uexca = freq1[_N]
  gen uexco = freq0[_N]

  gen or= exca*uexco/(uexca*exco)

}
else {
  sort `by'

  drop eprob
  qui reshape wide freq , i(`locus' `by') j(`cc')
  sort `by'
  qui by `by':gen tec = locus=="`unexp'"
  sort `by' tec
  qui by `by':gen exca = freq1
  qui by `by':gen exco = freq0
  qui by `by':gen uexca = freq1[_N]
  qui by `by':gen uexco = freq0[_N]
  qui gen or= exca*uexco/(uexca*exco)

}

}

if "`3'"~="" { qui keep if locus=="`3'" }

sort `by' locus
qui compress

lab var exca "Exposed Cases"
list `by' or locus  ex*, noobs

if "`nopre'"=="" { restore }
end