R/binarize.r

Defines functions binarize binarizeByPercentage densityOnes

Documented in binarize binarizeByPercentage densityOnes

#Binarize by the input threshold or using median if no treshold given
binarize=function(x,threshold=NA)
  {
  matd=x
  if(is.na(threshold))
    {
    threshold=min(x)+(max(x)-min(x))/2
    print(paste("Threshold: ",threshold))
    }

  matd[matd<=threshold]=0
  matd[matd>threshold]=1
  matd
  }
  
#Binarize to get the input percentage of 1s over 0s
#The algorithm stops when it gets a density in [percentage-error, percentage+error]
#Gap is the increment taken in iterative search
binarizeByPercentage=function(x,percentage,error=0.2,gap=0.1)
  {
  if(error<=gap)
    {
    print("Error: error value must be greater than gap")
    }
  else
    {
    threshold=min(x)+(max(x)-min(x))/2
    dens=percentage
    repeat
      {
      matd=binarize(x,threshold)
      if(dens==densityOnes(matd))  {break}
      dens=densityOnes(matd)
      if(dens>percentage)
        {
        threshold=threshold+gap
        }
      else
        {
        threshold=threshold-gap
        }

      if(dens>=(percentage-error) && dens<=(percentage+error))
        break
      }
    print(paste("Threshold applied is ",threshold))
    matd
    }
  }

#---------------------- DENSITY -------------------------
#Percentage of number of 1s over number of 0s
densityOnes=function(x)
  {
  num1=length(x[x==1])
  num0=length(x[x==0])
  if((num1+num0)!=prod(dim(x))) print("Error: x must be a binary matrix")
  else
    {
    den=(num1/num0)*100  #Density of 1s over 0s
    den
    }
  }

Try the biclust package in your browser

Any scripts or data that you put into this service are public.

biclust documentation built on May 2, 2019, 5:56 p.m.