barmcakes 0.0.6 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/barmcakes.rb +3 -1
  2. data/lib/bio_tools.rb +23 -0
  3. data/lib/circola.rb +141 -0
  4. metadata +3 -1
@@ -1,2 +1,4 @@
1
1
  require 'arrghs'
2
- require 'file'
2
+ require 'file'
3
+ require 'circola'
4
+ require 'bio_tools'
@@ -0,0 +1,23 @@
1
+
2
+ class Bio::DB::FastaLengthDB
3
+ require 'bio'
4
+
5
+ def initialize(fasta)
6
+ @file = fasta
7
+ @seqs = {}
8
+ file = Bio::FastaFormat.open(@file)
9
+ file.each do |entry|
10
+ @seqs[entry.entry_id] = entry.length
11
+ end
12
+
13
+ def each
14
+ @seqs.keys.sort.each do |k|
15
+ yield k, @seqs[k]
16
+ end
17
+ end
18
+
19
+ def [] (key)
20
+ @seqs[key]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,141 @@
1
+ require 'bio'
2
+ require 'pathname'
3
+
4
+ module Circola
5
+ #from any Bio::FlatFile or object iterates through the entries and writes a file in
6
+ #Circos' karyotype format.
7
+ #
8
+ #opts:
9
+ #:id => array of [string method, arg1, arg2, ... ] to apply to entry_id of Bio::Flatfile entry for Circos id column
10
+ #:label => array of [string method, arg1, arg2, ... ] to apply to entry_id of Bio::Flatfile entry for Circos id column
11
+ #:color => array of color strings for Circos color column. Used in order, looped round if too few provided
12
+ #:file => name of file to write to, Defaults to STDOUT if absent or false.
13
+ # returns a File object if written to file
14
+ def each_to_karyotype(opts)
15
+ opts = {
16
+ :id => [:gsub, //,""],
17
+ :label => [:gsub, //,""],
18
+ :colors => ['red', 'green', 'blue', 'purple', 'orange'],
19
+ :file => nil
20
+ }.merge!(opts)
21
+
22
+ #raise TypeError "not a Bio::Flatfile" unless self.is_a?(Bio::FlatFile)
23
+ self.must_be Bio::FlatFile
24
+ file = File.open(opts[:file], "w")
25
+ self.each_with_index do |entry, index|
26
+ id = entry.entry_id.send( *opts[:id] )
27
+ label = entry.entry_id.send( *opts[:label] )
28
+ color = Circola.pick_color(opts, index)#opts[:colors][offset]
29
+ file.puts "chr - #{id} #{label} 0 #{entry.length} #{color}"
30
+ end
31
+ file.close
32
+ file
33
+ end
34
+
35
+ #assumes text file has id\tlength
36
+ def text_to_karyotype(opts)
37
+ opts = {
38
+ :id => [:gsub, //,""],
39
+ :label => [:gsub, //,""],
40
+ :colors => ['red', 'green', 'blue', 'purple', 'orange'],
41
+ :file => nil
42
+ }.merge!(opts)
43
+ self.must_be File
44
+ file = File.open(opts[:file], "w")
45
+ self.each_with_index do |line, index|
46
+ color = Circola.pick_color(opts, index)
47
+ id,stop = line.chomp.split(/\t/)
48
+ label = id
49
+ id = id.send( *opts[:id] )
50
+ label = label.send( *opts[:label] )
51
+ file.puts "chr - #{id} #{label} 0 #{stop} #{color}"
52
+ end
53
+ file.close
54
+ file
55
+ end
56
+
57
+ #gets an array of colour tags from circos conf file, eg. colors.brewer.conf
58
+
59
+ def Circola.pick_color(opts, index)
60
+ offset = index - ((opts[:colors].length) * (index / opts[:colors].length) )
61
+ opts[:colors][offset]
62
+ end
63
+
64
+ def get_color_list
65
+ self.must_be File
66
+ colors = []
67
+ self.each do |line|
68
+ next if line =~ /^[<#\s]/
69
+ colors << line.split(/\s/)[0]
70
+ end
71
+ colors
72
+ end
73
+
74
+ def must_be type
75
+ begin
76
+ raise "not a #{type}" unless self.is_a?(type)
77
+ rescue Exception => e
78
+ $stderr.puts e.backtrace
79
+ end
80
+ end
81
+
82
+ #gives the full path of a File object
83
+ def full_path
84
+ must_be File
85
+ File.realpath(self.path)
86
+ end
87
+
88
+ #gives relative path from working dir to file
89
+ def relative_path
90
+ must_be File
91
+ Pathname.new(self.full_path).relative_path_from(Pathname.new(Dir.pwd)).to_s
92
+ end
93
+
94
+ def Circola.make_ideogram(opts)
95
+ opts = {
96
+ :file => nil,
97
+ :default => "2u",
98
+ :pairwise => nil
99
+ }.merge!(opts)
100
+ f = File.open(opts[:file], "w")
101
+ f.puts "<ideogram>\n<spacing>\n#default = {opts[:default]}\n</spacing></ideogram>"
102
+ f.close
103
+ f
104
+ end
105
+
106
+ def Circola.prep_conf(opts)
107
+ opts = {
108
+ :file => nil,
109
+ :chromosomes_units => 1000000,
110
+ :chromosomes_display_default => "yes",
111
+ :karyotype => nil,
112
+ :includes => []
113
+ }.merge!(opts)
114
+ f = File.open(opts[:file], "w")
115
+ [:karyotype, :chromosomes_units, :chromosomes_display_default].each do |par|
116
+ f.puts "#{par} = #{opts[par]}"
117
+ end
118
+ opts[:includes].each do |inc|
119
+ f.puts "<<include #{inc}>>"
120
+ end
121
+ f.close
122
+ f
123
+ end
124
+
125
+ def Circola.run_circos(opts)
126
+ opts = {
127
+ :circos => nil,
128
+ :conf => nil
129
+ }.merge!(opts)
130
+ command = "#{opts[:circos]} -conf #{opts[:conf]}"
131
+ $stderr.puts "executing ... #{command}"
132
+ Kernel.exec(command)
133
+ end
134
+
135
+ def Circola.clean_up(files)
136
+ files.each{|f| File.delete f }
137
+ end
138
+
139
+ end
140
+
141
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barmcakes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -21,6 +21,8 @@ files:
21
21
  - lib/barmcakes.rb
22
22
  - lib/arrghs.rb
23
23
  - lib/file.rb
24
+ - lib/circola.rb
25
+ - lib/bio_tools.rb
24
26
  homepage: http://github.com/danmaclean/barmcakes
25
27
  licenses: []
26
28
  post_install_message: