rbbt-dm 1.1.34 → 1.1.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a601a5fe0a12f90cc0729557ffd15c088c47802b
4
- data.tar.gz: bc317becbca8ac96f8042ff8a0e8c6dfc2053c3b
3
+ metadata.gz: d7f4b80f871a403ef1cdcd0253645bc22196e50f
4
+ data.tar.gz: 986977cae880eb91dc371ebfbe947d24a1b7bb84
5
5
  SHA512:
6
- metadata.gz: 50beee5add8d5a092a500686f2935085a861e79b8b64d982b8c099de121585fcdce2a5db91ad388a92760b0e8e4ab48745d567b414d4169c2bd609de395bcb26
7
- data.tar.gz: 7fca872f255c284cd54539dab39f4444af98ba539302bc317710af6b35b645a61dc73ff72142a8747a7f747e607cfb700136b8714b200ffca4b98d44d1b8c8eb
6
+ metadata.gz: c56806c3e903c453eeeacd8cbb5d95e26a7e5961d8c14779c0dcfd667ca18100486ac256a9298949a6ef624153e3c83b4fe923702d38edb62bb62f4525c56c96
7
+ data.tar.gz: 4ee8e20378f61ba5409c054b23e520b70e2175e858b5e6a8ad16bfd8eb98ec95b11a573ad653cebb022d36c2938c72fadc29652e8a8c4a37cad8c03d34e1a2dd
@@ -1,6 +1,6 @@
1
1
  require 'rbbt/util/R'
2
2
 
3
- class Matrix
3
+ class RbbtMatrix
4
4
  def barcode(outfile, factor = 2)
5
5
 
6
6
  FileUtils.mkdir_p File.dirname(outfile) unless outfile.nil? or File.exists? File.dirname(outfile)
@@ -45,12 +45,14 @@ rbbt.GE.barcode.mode(#{ R.ruby2R self.data_file }, #{ R.ruby2R outfile }, #{ R.r
45
45
  Misc.sensiblewrite(outfile, dumper.stream)
46
46
  end
47
47
 
48
- def activity_cluster(outfile, factor = 2)
48
+ def activity_cluster(outfile, clusters = 2)
49
+
50
+ clusters = Array === clusters ? clusters : (2..clusters).to_a
49
51
 
50
52
  FileUtils.mkdir_p File.dirname(outfile) unless outfile.nil? or File.exists? File.dirname(outfile)
51
53
  cmd =<<-EOF
52
54
  source('#{Rbbt.share.R['barcode.R'].find}')
53
- rbbt.GE.activity_cluster(#{ R.ruby2R self.data_file }, #{ R.ruby2R outfile }, #{R.ruby2R value_type})
55
+ rbbt.GE.activity_cluster(#{ R.ruby2R self.data_file }, #{ R.ruby2R outfile }, #{R.ruby2R key_field}, #{R.ruby2R clusters})
54
56
  EOF
55
57
 
56
58
  R.run(cmd)
@@ -1,6 +1,6 @@
1
1
  require 'rbbt/util/R'
2
2
 
3
- class Matrix
3
+ class RbbtMatrix
4
4
  def differential(main, contrast, path = nil)
5
5
  all_samples = self.samples
6
6
  if Array === main and Array === contrast
@@ -17,7 +17,7 @@ class Matrix
17
17
 
18
18
  Persist.persist(name, :tsv, :persist => true, :file => path,
19
19
  :other => {:main => main_samples, :contrast => contrast_samples},
20
- :prefix => "Diff", :dir => Matrix.matrix_dir.differential, :no_load => true) do |file|
20
+ :prefix => "Diff", :dir => RbbtMatrix.matrix_dir.differential, :no_load => true) do |file|
21
21
 
22
22
  raise if file.nil?
23
23
 
@@ -11,7 +11,7 @@ class KnowledgeBase
11
11
  def matrix(name)
12
12
  matrix, options = @matrix_registry[name]
13
13
 
14
- return matrix if Matrix === matrix
14
+ return matrix if RbbtMatrix === matrix
15
15
 
16
16
  Path.setup(matrix) if not Path === matrix and File.exists? matrix
17
17
 
@@ -35,7 +35,7 @@ class KnowledgeBase
35
35
  value_type = TSV.parse_header(data.find).key_field if data
36
36
  value_type ||= "Unknown ID"
37
37
 
38
- Matrix.new data, labels, value_type, format, organism, identifiers
38
+ RbbtMatrix.new data, labels, value_type, format, organism, identifiers
39
39
  else
40
40
  end
41
41
  end
data/lib/rbbt/matrix.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rbbt-util'
2
2
  require 'rbbt/sources/organism'
3
3
 
4
- class Matrix
4
+ class RbbtMatrix
5
5
 
6
6
  class << self
7
7
  attr_accessor :matrix_dir
@@ -29,6 +29,18 @@ class Matrix
29
29
  @identifiers = identifiers
30
30
  end
31
31
 
32
+ def all_fields
33
+ @all_fields ||= TSV.parse_header(@data_file).all_fields
34
+ end
35
+
36
+ def fields
37
+ all_fields[1..-1]
38
+ end
39
+
40
+ def key_field
41
+ all_fields.first
42
+ end
43
+
32
44
  def samples
33
45
  @samples ||= TSV.parse_header(@data_file).fields
34
46
  end
@@ -128,13 +140,27 @@ class Matrix
128
140
  [main_samples, contrast_samples]
129
141
  end
130
142
 
143
+ def to_average(identifiers = nil)
144
+ name = data_file =~ /:>/ ? File.basename(data_file) : data_file
145
+
146
+ file = Persist.persist(data_file, :tsv, :prefix => "Average", :check => [data_file], :dir => RbbtMatrix.matrix_dir.values, :no_load => true) do
147
+
148
+ data = data_file.tsv(:cast => :to_f, :type => :double)
149
+
150
+ data.to_list{|v| v.length > 1 ? Misc.mean(v) : v }
151
+ end
152
+ subsets = self.subsets
153
+ matrix = RbbtMatrix.new file, labels, value_type, key_field, organism
154
+ matrix.subsets = subsets
155
+ matrix
156
+ end
131
157
 
132
158
  def to_gene(identifiers = nil)
133
159
  require 'rbbt/tsv/change_id'
134
160
 
135
161
  name = data_file =~ /:>/ ? File.basename(data_file) : data_file
136
162
 
137
- file = Persist.persist(data_file, :tsv, :prefix => "Gene", :dir => Matrix.matrix_dir.values, :no_load => true) do
163
+ file = Persist.persist(data_file, :tsv, :prefix => "Gene", :check => [data_file], :dir => RbbtMatrix.matrix_dir.values, :no_load => true) do
138
164
 
139
165
  data = data_file.tsv(:cast => :to_f)
140
166
 
@@ -145,7 +171,7 @@ class Matrix
145
171
  end
146
172
  end
147
173
  subsets = self.subsets
148
- matrix = Matrix.new file, labels, value_type, "Ensembl Gene ID", organism
174
+ matrix = RbbtMatrix.new file, labels, value_type, "Ensembl Gene ID", organism
149
175
  matrix.subsets = subsets
150
176
  matrix
151
177
  end
@@ -153,11 +179,11 @@ class Matrix
153
179
  def to_barcode_ruby(factor = 2)
154
180
  name = data_file =~ /:>/ ? File.basename(data_file) : data_file
155
181
 
156
- file = Persist.persist(data_file, :tsv, :prefix => "Barcode #{factor}", :dir => Matrix.matrix_dir.barcode, :no_load => true) do |filename|
182
+ file = Persist.persist(data_file, :tsv, :prefix => "Barcode #{factor}", :check => [data_file], :dir => RbbtMatrix.matrix_dir.barcode, :no_load => true) do |filename|
157
183
  barcode_ruby(filename, factor)
158
184
  end
159
185
  subsets = self.subsets
160
- matrix = Matrix.new file, labels, value_type, "Ensembl Gene ID", organism
186
+ matrix = RbbtMatrix.new file, labels, value_type, key_field, organism
161
187
  matrix.subsets = subsets
162
188
  matrix
163
189
  end
@@ -165,35 +191,35 @@ class Matrix
165
191
  def to_barcode(factor = 2)
166
192
  name = data_file =~ /:>/ ? File.basename(data_file) : data_file
167
193
 
168
- file = Persist.persist(data_file, :tsv, :prefix => "Barcode R #{factor}", :dir => Matrix.matrix_dir.barcode, :no_load => true) do |filename|
194
+ file = Persist.persist(data_file, :tsv, :prefix => "Barcode R #{factor}", :check => [data_file], :dir => RbbtMatrix.matrix_dir.barcode, :no_load => true) do |filename|
169
195
  barcode(filename, factor)
170
196
  end
171
197
  subsets = self.subsets
172
- matrix = Matrix.new file, labels, value_type, "Ensembl Gene ID", organism
198
+ matrix = RbbtMatrix.new file, labels, value_type, key_field, organism
173
199
  matrix.subsets = subsets
174
200
  matrix
175
201
  end
176
202
 
177
- def to_activity(factor = 2)
203
+ def to_activity(clusters = 2)
178
204
  require 'rbbt/tsv/change_id'
179
205
 
180
206
  name = data_file =~ /:>/ ? File.basename(data_file) : data_file
181
207
 
182
- file = Persist.persist(data_file, :tsv, :prefix => "Activity #{factor}", :dir => Matrix.matrix_dir.barcode, :no_load => true) do |filename|
183
- activity_cluster(filename, factor)
208
+ file = Persist.persist(data_file, :tsv, :prefix => "Activity #{clusters.inspect}", :check => [data_file], :dir => RbbtMatrix.matrix_dir.barcode, :no_load => true) do |filename|
209
+ activity_cluster(filename, clusters)
184
210
  end
185
211
  subsets = self.subsets
186
- matrix = Matrix.new file, labels, value_type, "Ensembl Gene ID", organism
212
+ matrix = RbbtMatrix.new file, labels, value_type, key_field, organism
187
213
  matrix.subsets = subsets
188
214
  matrix
189
215
  end
190
216
 
191
217
  def tsv(to_gene=true, identifiers = nil)
192
- if to_gene and TSV.parse_header(self.data_file).key_field != "Ensembl Gene ID"
218
+ if to_gene and key_field != "Ensembl Gene ID"
193
219
  file = self.to_gene(identifiers).data_file
194
- file.tsv :persist => true, :persist_dir => Matrix.matrix_dir.persist, :type => :double, :merge => true
220
+ file.tsv :persist => true, :persist_dir => RbbtMatrix.matrix_dir.persist, :type => :double, :merge => true
195
221
  else
196
- self.data_file.tsv :persist => true, :persist_dir => Matrix.matrix_dir.persist, :merge => true
222
+ self.data_file.tsv :persist => true, :persist_dir => RbbtMatrix.matrix_dir.persist, :merge => true
197
223
  end
198
224
  end
199
225
 
data/share/R/barcode.R CHANGED
@@ -63,16 +63,26 @@ rbbt.GE.barcode.mode <- function(matrix_file, output_file, sd.factor = 2, key.fi
63
63
  close(file.barcode)
64
64
  }
65
65
 
66
- rbbt.GE.activity_cluster <- function(matrix_file, output_file, key.field = "ID"){
66
+ rbbt.GE.activity_cluster <- function(matrix_file, output_file, key.field = "ID", clusters = c(2,3)){
67
67
 
68
68
  library(mclust)
69
69
 
70
70
  data = rbbt.tsv.numeric(matrix_file)
71
71
 
72
- classes = apply(data,2,function(row){Mclust(row)$classification})
72
+ classes = apply(data,1,function(row){
73
+ row.na = is.na(row)
74
+ clust = rep(NA, length(row))
75
+ if (sum(row.na) <= length(row) - 5){
76
+ clust[!row.na] = densityMclust(row[!row.na], prior=priorControl(), G=clusters)$classification
77
+ }
78
+ clust
79
+ })
80
+
81
+ classes = data.frame(t(classes))
73
82
 
74
83
  rownames(classes) <- rownames(data)
75
- names(classes) <- c("Cluster")
84
+ names(classes) <- names(data)
76
85
 
86
+ str(classes)
77
87
  rbbt.tsv.write(output_file, classes, key.field)
78
88
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-dm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.34
4
+ version: 1.1.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbbt-util