Module: Sinatra::RbbtMiscHelpers

Defined in:
lib/rbbt/rest/helpers.rb

Instance Method Summary collapse

Instance Method Details

#exome_bed_file_for_genes(genes) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbbt/rest/helpers.rb', line 50

def exome_bed_file_for_genes(genes)
  bed = TSV.setup({}, :key_field => "Ensembl Exon ID", :fields => %w(chr start end gene), :type => :list)
  organism = genes.organism
  exon_info = Organism.exons(organism).tsv :fields => ["Chromosome Name", "Exon Chr Start", "Exon Chr End"], :persist => true
  exons_for = Organism.exons(organism).tsv :key_field => "Ensembl Gene ID", :fields => ["Ensembl Exon ID"], :persist => true, :merge => true, :type => :flat
  genes.each do |gene|
    exons = exons_for[gene.ensembl]
    exons.each do |exon|
      chr, start, eend = exon_info[exon]
      bed[exon] = [chr, start, eend, gene]
    end
  end
  bed
end

#file_mimetype(path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbbt/rest/helpers.rb', line 3

def file_mimetype(path)
  require 'mimemagic'
  mime = nil
  Open.open(path) do |io|
    begin
      mime = MimeMagic.by_path(path) 
      if mime.nil?
        io.rewind
        mime = MimeMagic.by_magic(io) 
      end
      if mime.nil?
        io.rewind
        mime = "text/tab-separated-values" if io.gets =~ /^#/ and io.gets.include? "\t"
      end
    rescue Exception
      Log.exception $!
    end
  end
  mime
end

#format_name(name) ⇒ Object



65
66
67
68
69
70
# File 'lib/rbbt/rest/helpers.rb', line 65

def format_name(name)
  parts = name.split("_")
  hash = parts.pop
  clean_name = parts * "_"
  "<span class='name' jobname='#{ name }'>#{ clean_name }</span> <span class='hash'>#{ hash }</span>"
end

#OR_matrices(m1, m2) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/rest/helpers.rb', line 24

def OR_matrices(m1, m2)
  samples = m1.fields
  new = TSV.setup({}, :key_field => "Ensembl Gene ID", :fields => samples)

  m1.each do |gene, values|
    new[gene] = values
  end

  m2.each do |gene, values|
    if new.include? gene
      new[gene] = new[gene].zip(values).collect do |old, new|
        case
        when old == new
          old
        else
          "TRUE"
        end
      end
    else
      new[gene] = values
    end
  end

  new
end

#values2d3(data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbbt/rest/helpers.rb', line 72

def values2d3(data)
  data = data.to_single if data.respond_to? :to_single and not data.type == :single

  values = []
  data.each do |key, value|
    name = key.respond_to?(:name) ? key.name || key : key
    values << {:label => name, :value => value}
  end

  values
end