class ROCData

Attributes

aln[R]
r[R]
refined[R]
signatures[R]
windows[R]

Public Class Methods

new(val, aln = nil, window = nil) click to toggle source

Use ROCData.new(table,aln,window) to re-compute from table, use ROCData.new(data) to load

# File lib/rocker/rocdata.rb, line 17
def initialize(val, aln = nil, window = nil)
   @r = RInterface.new
   @nucl = false
   @refined = false
   if not aln.nil?
      @aln = aln
      @signatures = { v: "ROCker #{ROCker.VERSION}", d: Time.now.to_s }
      self.rrun "library('pROC');"
      self.rrun "x <- read.table('#{val}', sep='\\t', h=F);"
      self.init_windows! window
   else
      f = File.open(val, "r")
      @windows = []
      @signatures = {}
      while ln = f.gets
         break unless /^#:/.match(ln).nil?
         if ln =~ /^#(\S+) (.*)/
            @signatures[$1.to_sym] = $2
         else
            @windows << ROCWindow.new(self, ln)
         end
      end
      f.close
      @aln = Alignment.new
      @aln.read_rocker(val)
   end
end

Public Instance Methods

_refine_iter(table) click to toggle source
# File lib/rocker/rocdata.rb, line 58
def _refine_iter table
   to_refine = []
   self.windows.each do |w|
      next if w.almost_empty or w.length <= 5
      self.rrun "acc <- w$accuracy[w$V1==#{w.from}];"
      to_refine << w if
         self.rrun("ifelse(is.na(acc), 100, acc)", :float) < 95.0
   end
   n = to_refine.size
   return 0 unless n > 0
   to_refine.each do |w|
      w1 = ROCWindow.new(self, w.from, (w.from+w.to)/2)
      w2 = ROCWindow.new(self, (w.from+w.to)/2, w.to)
      if w1.almost_empty or w2.almost_empty
         n -= 1
      else
         @windows << w1
         @windows << w2
         @windows.delete w
      end
   end
   @windows.sort!{ |x,y| x.from <=> y.from }
   n
end
in_nucl?() click to toggle source
# File lib/rocker/rocdata.rb, line 47
def in_nucl?() @nucl end
init_windows!(size) click to toggle source
# File lib/rocker/rocdata.rb, line 128
def init_windows!(size)
   @windows = []
   1.step(self.aln.cols,size).each do |a|
      @windows << ROCWindow.new(self, a, a+size-1)
   end
end
is_refined?() click to toggle source
# File lib/rocker/rocdata.rb, line 57
def is_refined? ; @refined ; end
load_table!(table, sbj=[], min_score=0) click to toggle source
# File lib/rocker/rocdata.rb, line 82
   def load_table! table, sbj=[], min_score=0
      self.rrun "x <- read.table('#{table}', sep='\\t', h=F);"
      self.rrun "x <- x[x$V1 %in% c('#{sbj.join("','")}'),];" if sbj.size > 0
      self.rrun "x <- x[x$V4 >= #{minscore.to_s},];" if min_score > 0
      Dir.mktmpdir do |dir|
         self.save(dir + "/rocker")
         self.rrun "w <- read.table('#{dir}/rocker', sep='\\t', h=F);"
      end
      self.rrun "w <- w[!is.na(w$V5),];"
      if self.rrun("nrow(w)", :int)==0
         warn "\nWARNING: Insufficient windows with estimated thresholds.\n\n"
         return false
      end
      self.rrun <<-EOC
         w$tp<-0; w$fp<-0; w$tn<-0; w$fn<-0;
         for(i in 1:nrow(x)){
            m <- x$V6[i];
            win <- which( (m>=w$V1) & (m<=w$V2))[1];
            if(!is.na(win)){
               if(x$V4[i] >= w$V5[win]){
                  if(x$V5[i]==1){
                     w$tp[win] <- w$tp[win]+1
                  } else {
                     w$fp[win] <- w$fp[win]+1
                  }
               }else{
                  if(x$V5[i]==1){
                     w$fn[win] <- w$fn[win]+1
                  } else {
                     w$tn[win] <- w$tn[win]+1
                  };
               }
            }
         }
      EOC
      r.run <<-EOC
         w$p <- w$tp + w$fp;
         w$n <- w$tn + w$fn;
         w$sensitivity <- 100*w$tp/(w$tp+w$fn);
         w$specificity <- 100*w$tn/(w$fp+w$tn);
         w$accuracy <- 100*(w$tp+w$tn)/(w$p+w$n);
         w$precision <- 100*w$tp/(w$tp+w$fp);
      EOC
      
      return true
   end
nucl=(nucl) click to toggle source
# File lib/rocker/rocdata.rb, line 48
def nucl=(nucl) @nucl=nucl end
refine!(table) click to toggle source
# File lib/rocker/rocdata.rb, line 49
def refine! table
   while true
      return false unless self.load_table! table
      break if self._refine_iter(table)==0
   end
   @refined = true
   return true
end
rrun(cmd, type=nil) click to toggle source
# File lib/rocker/rocdata.rb, line 134
def rrun(cmd, type=nil) self.r.run cmd, type end
save(file, sign = {}) click to toggle source
# File lib/rocker/rocdata.rb, line 135
def save(file, sign = {})
  @signatures.merge! sign
  File.open(file, 'w') { |fh| fh.print self.to_s }
end
to_s() click to toggle source
# File lib/rocker/rocdata.rb, line 139
def to_s
  o = signatures.map{ |k,v| "##{k} #{v}\n" }.join
  self.windows.each{ |w| o += w.to_s }
  o += self.aln.to_s
  return o
end
win_at_col(col) click to toggle source
# File lib/rocker/rocdata.rb, line 44
def win_at_col(col)
   self.windows.select{|w| (w.from<=col) and (w.to>=col)}.first
end