*! version 1.0.0  17apr2008
program _gntp2dptp, rclass
	version 10.0
	cap noi DoMain `0'
	ret add
	ret local cmdline `"_gntp2dptp `0'"'
	ret scalar rc = _rc
	exit _rc
end

program DoMain, rclass
	version 10.0
	syntax [varlist] [if] [in] [,	USEVARnames(namelist max=3)	///
					SAVing(string)			///
				   	BASE10				///
					PHASEINFO(string)		///
					ALLDOTs 			///
					IFopt(string)			///
					noHEADer ]
/* base10  - saved results are haplotype indices; haplotype variables are
	     saved by default
   ifopt() - when specified creates an additional binary
	     variable _ifcond indicating which subjects satisfy if() condition*/
	marksample touse, novarlist
	if `"`saving'"' != "" {
		_prefix_saving `saving'
		local saving    `"`s(filename)'"'
		local replace   `"`s(replace)'"'
		if `"`replace'"' == "" {
			confirm new file `"`s(filename)'"'
		}
	}
	if `"`alldots'"' != "" &  `"`phaseinfo'"' != "" {
		di as err "alldots and phaseinfo() are not allowed together"
		exit 198
	}
	if `"`usevarnames'"' != "" {
		if `"`base10'"' == "" { // for simplicity
			di as err "usevarnames() requires base10"
			exit 198
		}
		local nvars: word count `usevarnames'
		if `nvars'==1 {
			local idvname   `usevarnames'
			local hap1vname indhap1
			local hap2vname indhap2
		}
		else {
			tokenize `usevarnames'
			if `nvars'==2 {
				local idvname   `1'
				local hap1vname `2'
				local hap2vname indhap2
			}
			else {
				local idvname   `1'
				local hap1vname `2'
				local hap2vname `3'
			}
		}
	}
	else {
		local idvname id
		if "`base10'" != "" {
			local hap1vname indhap1
			local hap2vname indhap2
		}
	}
	local snp `varlist'
	local nsnp: word count `snp'
	if `"`phaseinfo'"' != "" {
		local allowed all phased unphased
		local 0 `phaseinfo'
		syntax [anything] [, index ]
		local phaseinfo `anything'
		if "`index'" != "" {
			local phasehaplo base10
		}
		local isallowed : list phaseinfo in allowed
		if !`isallowed' {
			di as err `"phaseinfo(): `phaseinfo' is not allowed"'
			exit 198
		}
	}
	preserve
	qui keep if `touse'
	if `c(N)' == 0 {
		error 2000
	}
	tempvar miss phased unphased
	qui egen long `miss' = rowmiss(`snp')
	qui summ `miss', meanonly
	local nmiss_snp = r(max)
	qui count if `miss'>=10
	if (r(N)!=0) {
		di as txt "Note: " as res `=string(r(N),"%9.0g")' ///
		   as txt " subject(s) have 10 or more missing SNPs"
	}
	// phase summary info
	qui replace `miss' = (`miss'>0)
	qui summ if `miss', meanonly
	local nmiss = r(N)
	egen long `phased' = anycount(`snp'), values(1)
	qui gen byte `unphased' = (`phased'>1)*(`miss'==0)
	qui replace `phased' = (`phased'<=1)*(`miss'==0)
	qui summ if `phased', meanonly
	local nphased = r(N)
	qui summ if `unphased', meanonly
	local nunphased = r(N)

	tokenize `snp'
	qui forvalues i=1/`nsnp' {
		tempvar hap`i'
		gen `hap`i'' = ``i''
		local snp`i' ``i''
		local hap `hap' `hap`i''
	}
	qui recode `hap' (2=-1) (mis=3)
	tempvar id
	qui gen long `id' = _n
	if `nmiss_snp' != 0 { // only need this if have missing genotypes
		di as txt _n "Handling missing SNPs: "
		tempvar newid pid group
		qui gen long `newid' = `id'
		forvalues i=`nsnp'(-1)1 {
			if `"`alldots'"' != "" {
				di . _c
			}
			cap drop `pid'
			qui expandcl `hap`i'', cluster(`newid') gen(`pid')
			qui by `id' `newid', sort: ///
					replace `hap`i''=_n-2 if `hap`i''==3
			qui egen `group' = group(`newid' `pid')
			qui replace `newid' = `group'
			qui drop `group'
		}
		if `"`alldots'"' != "" {
			di
		}
		drop `newid' `pid'
	}
	tempvar sid
	qui gen long `sid' = `id'
	qui replace `id' = _n

	tempvar cnt allphased ones twos
	egen long `cnt' = anycount(`hap'), values(1)
	qui gen byte `allphased' = (`cnt'<=1)
	gen byte `ones' = (`cnt'>0)
	drop `cnt'
	egen long `cnt' = anycount(`hap'), values(-1)
	gen byte `twos' = (`cnt'>0)
	drop `cnt'
	sort `id' `sid'

	tempvar ifcond
	if `"`ifopt'"' != "" {
		qui gen byte `ifcond' = (`ifopt')
		local ifvname _ifcond
	}
	else {
		qui gen byte `ifcond' = 1
	}
	mata: _genot2diplot()
	ret add
if `"`header'"' == "" {
	di _n "{txt}Genotype: " as res abbrev(`"`snp'"', 23)
	local eqalign = 24
	di _n "{txt}Number of obs{ralign `=`eqalign'-13': = }{res}" ///
							%10.0g return(N)
	di    "{txt}Number of subjects:"
	di as txt "{ralign `eqalign':total = }{res}" as res %10.0g return(N_s)
	di as txt "{ralign `eqalign':phased = }" as res %10.0g `nphased'
	di as txt "{ralign `eqalign':unphased = }" as res %10.0g `nunphased'
	di as txt "{ralign `eqalign':missing = }" as res %10.0g `nmiss'
	di "{txt}Obs per subject:{ralign `=`eqalign'-16':min = }{res}" ///
							%10.0g return(N_min)
	di "{txt}{ralign `eqalign':max = }" as res %10.0g return(N_max)
}

	if `"`saving'"' != "" {
		sort `idvname' `hap1vname' `hap2vname' `ifvname'
		qui save `"`saving'"', `replace'
	}
	restore
end