#########################################################################################
# Extracts IBD segments from PopulationMat and SubPopMat that are shared / not shared 
# by a certain fraction of individuals from a certain population
#########################################################################################


getoPops <- function(Mat) {
  # only this population: exclusively
  oAFR <<- which(Mat[,"AFR"]>0.9999)
  oAMR <<- which(Mat[,"AMR"]>0.9999)
  oEAS <<- which(Mat[,"EAS"]>0.9999)
  oSAS <<- which(Mat[,"SAS"]>0.9999)
  oEUR <<- which(Mat[,"EUR"]>0.9999)
  
  oAFR2 <- which(Mat[,"AFR"]+Mat[,"AMR"]>0.9999)
  oEAS2 <- which(Mat[,"EAS"]+Mat[,"AMR"]>0.9999)
  oSAS2 <- which(Mat[,"SAS"]+Mat[,"AMR"]>0.9999)
  oEUR2 <- which(Mat[,"EUR"]+Mat[,"AMR"]>0.9999)
  
  # half are observed in this population
    AFRd <- which(Mat[,"AFR"]>0.5)
    AMRd <- which(Mat[,"AMR"]>0.5)
    EASd <- which(Mat[,"EAS"]>0.5) 
    SASd <- which(Mat[,"SAS"]>0.5) 
    EURd <- which(Mat[,"EUR"]>0.5)

  
  oAFR2 <<- intersect(oAFR2, AFRd)
  oEAS2 <<- intersect(oEAS2, EASd)
  oSAS2 <<- intersect(oSAS2, SASd)
  oEUR2 <<- intersect(oEUR2, EURd)
}

getNPops <- function(Mat) {

  # not this population: not observed
  NAFR <<- which(Mat[,"AFR"]<0.0001)
  NAMR <<- which(Mat[,"AMR"]<0.0001)
  NEAS <<- which(Mat[,"EAS"]<0.0001)
  NSAS <<- which(Mat[,"SAS"]<0.0001)
  NEUR <<- which(Mat[,"EUR"]<0.0001)
}

getJPops <- function(Mat) {
  # at least once this population: observed
  JAFR <<- which(Mat[,"AFR"]>0.0001)
  JAMR <<- which(Mat[,"AMR"]>0.0001)
  JEAS <<- which(Mat[,"EAS"]>0.0001)
  JSAS <<- which(Mat[,"SAS"]>0.0001)
  JEUR <<- which(Mat[,"EUR"]>0.0001)
}



getJSubPops <- function(Mat) {
  JACB <<- which(Mat[,"ACBr"]>0.0001)
  JASW <<- which(Mat[,"ASWr"]>0.0001)
  JBEB <<- which(Mat[,"BEBr"]>0.0001)
  JCDX <<- which(Mat[,"CDXr"]>0.0001)
  JCEU <<- which(Mat[,"CEUr"]>0.0001)
  JCHB <<- which(Mat[,"CHBr"]>0.0001)
  JCHS <<- which(Mat[,"CHSr"]>0.0001)
  JCLM <<- which(Mat[,"CLMr"]>0.0001)
  JESN <<- which(Mat[,"ESNr"]>0.0001)
  JFIN <<- which(Mat[,"FINr"]>0.0001)
  JGBR <<- which(Mat[,"GBRr"]>0.0001)
  JGIH <<- which(Mat[,"GIHr"]>0.0001)
  JGWD <<- which(Mat[,"GWDr"]>0.0001)
  JIBS <<- which(Mat[,"IBSr"]>0.0001)
  JITU <<- which(Mat[,"ITUr"]>0.0001)
  JJPT <<- which(Mat[,"JPTr"]>0.0001)
  JKHV <<- which(Mat[,"KHVr"]>0.0001)
  JLWK <<- which(Mat[,"LWKr"]>0.0001)
  JMSL <<- which(Mat[,"MSLr"]>0.0001)
  JMXL <<- which(Mat[,"MXLr"]>0.0001)
  JPEL <<- which(Mat[,"PELr"]>0.0001)
  JPJL <<- which(Mat[,"PJLr"]>0.0001)
  JPUR <<- which(Mat[,"PURr"]>0.0001)
  JSTU <<- which(Mat[,"STUr"]>0.0001)
  JTSI <<- which(Mat[,"TSIr"]>0.0001)
  JYRI <<- which(Mat[,"YRIr"]>0.0001)
}



getoSubPops <- function(Mat) {
  
  # only this population: exclusively
  oACB <<- which(Mat[,"ACBr"]>0.9999)
  oASW <<- which(Mat[,"ASWr"]>0.9999)
  oBEB <<- which(Mat[,"BEBr"]>0.9999)
  oCDX <<- which(Mat[,"CDXr"]>0.9999)
  oCEU <<- which(Mat[,"CEUr"]>0.9999)
  oCHB <<- which(Mat[,"CHBr"]>0.9999)
  oCHS <<- which(Mat[,"CHSr"]>0.9999)
  oCLM <<- which(Mat[,"CLMr"]>0.9999)
  oESN <<- which(Mat[,"ESNr"]>0.9999)
  oFIN <<- which(Mat[,"FINr"]>0.9999)
  oGBR <<- which(Mat[,"GBRr"]>0.9999)
  oGIH <<- which(Mat[,"GIHr"]>0.9999)
  oGWD <<- which(Mat[,"GWDr"]>0.9999)
  oIBS <<- which(Mat[,"IBSr"]>0.9999)
  oITU <<- which(Mat[,"ITUr"]>0.9999)
  oJPT <<- which(Mat[,"JPTr"]>0.9999)
  oKHV <<- which(Mat[,"KHVr"]>0.9999)
  oLWK <<- which(Mat[,"LWKr"]>0.9999)
  oMSL <<- which(Mat[,"MSLr"]>0.9999)
  oMXL <<- which(Mat[,"MXLr"]>0.9999)
  oPEL <<- which(Mat[,"PELr"]>0.9999)
  oPJL <<- which(Mat[,"PJLr"]>0.9999)
  oPUR <<- which(Mat[,"PURr"]>0.9999)
  oSTU <<- which(Mat[,"STUr"]>0.9999)
  oTSI <<- which(Mat[,"TSIr"]>0.9999)
  oYRI <<- which(Mat[,"YRIr"]>0.9999)
}

getNSubPops <- function(Mat) {
  
  # not this population: not observed
  NACB <<- which(Mat[,"ACBr"]<0.0001)
  NASW <<- which(Mat[,"ASWr"]<0.0001)
  NBEB <<- which(Mat[,"BEBr"]<0.0001)
  NCDX <<- which(Mat[,"CDXr"]<0.0001)
  NCEU <<- which(Mat[,"CEUr"]<0.0001)
  NCHB <<- which(Mat[,"CHBr"]<0.0001)
  NCHS <<- which(Mat[,"CHSr"]<0.0001)
  NCLM <<- which(Mat[,"CLMr"]<0.0001)
  NESN <<- which(Mat[,"ESNr"]<0.0001)
  NFIN <<- which(Mat[,"FINr"]<0.0001)
  NGBR <<- which(Mat[,"GBRr"]<0.0001)
  NGIH <<- which(Mat[,"GIHr"]<0.0001)
  NGWD <<- which(Mat[,"GWDr"]<0.0001)
  NIBS <<- which(Mat[,"IBSr"]<0.0001)
  NITU <<- which(Mat[,"ITUr"]<0.0001)
  NJPT <<- which(Mat[,"JPTr"]<0.0001)
  NKHV <<- which(Mat[,"KHVr"]<0.0001)
  NLWK <<- which(Mat[,"LWKr"]<0.0001)
  NMSL <<- which(Mat[,"MSLr"]<0.0001)
  NMXL <<- which(Mat[,"MXLr"]<0.0001)
  NPEL <<- which(Mat[,"PELr"]<0.0001)
  NPJL <<- which(Mat[,"PJLr"]<0.0001)
  NPUR <<- which(Mat[,"PURr"]<0.0001)
  NSTU <<- which(Mat[,"STUr"]<0.0001)
  NTSI <<- which(Mat[,"TSIr"]<0.0001)
  NYRI <<- which(Mat[,"YRIr"]<0.0001)
}

