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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

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

#refinedObject (readonly)

Returns the value of attribute refined.



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

def refined
  @refined
end

#signaturesObject (readonly)

Returns the value of attribute signatures.



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

def signatures
  @signatures
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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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?Boolean

Returns:

  • (Boolean)


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

def in_nucl?() @nucl end

#init_windows!(size) ⇒ Object



128
129
130
131
132
133
# 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?Boolean

Returns:

  • (Boolean)


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

def is_refined? ; @refined ; end

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



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



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

def nucl=(nucl) @nucl=nucl end

#refine!(table) ⇒ Object



49
50
51
52
53
54
55
56
# 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) ⇒ Object



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

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

#save(file, sign = {}) ⇒ Object



135
136
137
138
# 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_sObject



139
140
141
142
143
144
# 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) ⇒ Object



44
45
46
# 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