Class: ROCData

Inherits:
Object
  • Object
show all
Defined in:
lib/rocker/rocdata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val, aln = nil, window = nil) ⇒ ROCData

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rocker/rocdata.rb', line 16

def initialize(val, aln=nil, window=nil)
   @r = RInterface.new
   @nucl = false
   if not aln.nil?
	 @aln = aln
	 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 = []
	 while ln = f.gets
  break unless /^#:/.match(ln).nil?
  @windows << ROCWindow.new(self, ln)
	 end
	 f.close
	 @aln = Alignment.new
	 @aln.read_rocker(val)
   end
end

Instance Attribute Details

#alnObject (readonly)

Returns the value of attribute aln.



14
15
16
# File 'lib/rocker/rocdata.rb', line 14

def aln
  @aln
end

#rObject (readonly)

Returns the value of attribute r.



14
15
16
# File 'lib/rocker/rocdata.rb', line 14

def r
  @r
end

#windowsObject (readonly)

Returns the value of attribute windows.



14
15
16
# File 'lib/rocker/rocdata.rb', line 14

def windows
  @windows
end

Instance Method Details

#_refine_iter(table) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rocker/rocdata.rb', line 46

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?Boolean

Returns:

  • (Boolean)


37
# File 'lib/rocker/rocdata.rb', line 37

def in_nucl?() @nucl end

#init_windows!(size) ⇒ Object



107
108
109
110
# File 'lib/rocker/rocdata.rb', line 107

def init_windows!(size)
   @windows = []
   1.step(self.aln.cols,size).each { |a| @windows << ROCWindow.new(self, a, a+size-1) }
end

#load_table!(table, sbj = [], min_score = 0) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rocker/rocdata.rb', line 69

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) ⇒ Object



38
# File 'lib/rocker/rocdata.rb', line 38

def nucl=(nucl) @nucl=nucl end

#refine!(table) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rocker/rocdata.rb', line 39

def refine! table
   while true
	 return false unless self.load_table! table
	 break if self._refine_iter(table)==0
   end
   return true
end

#rrun(cmd, type = nil) ⇒ Object



111
# File 'lib/rocker/rocdata.rb', line 111

def rrun(cmd, type=nil) self.r.run cmd, type end

#save(file) ⇒ Object



112
113
114
115
116
# File 'lib/rocker/rocdata.rb', line 112

def save(file)
   f = File.open(file, "w")
   f.print self.to_s
   f.close
end

#to_sObject



117
118
119
120
121
122
# File 'lib/rocker/rocdata.rb', line 117

def to_s
   o = ''
   self.windows.each{|w| o += w.to_s}
   o += self.aln.to_s
   return o
end

#win_at_col(col) ⇒ Object



36
# File 'lib/rocker/rocdata.rb', line 36

def win_at_col(col) self.windows.select{|w| (w.from<=col) and (w.to>=col)}.first end