Class: GenomeSet
- Inherits:
-
Object
- Object
- GenomeSet
- Defined in:
- lib/rocker/genome-set.rb
Overview
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#rocker ⇒ Object
readonly
Returns the value of attribute rocker.
-
#taxa ⇒ Object
Returns the value of attribute taxa.
Instance Method Summary collapse
- #choose_genomes!(rank) ⇒ Object
- #delete!(ids) ⇒ Object
- #download(file) ⇒ Object
- #empty? ⇒ Boolean
- #genome2taxid(genome_id) ⇒ Object
-
#genome2taxon(genome_id, rank = "species") ⇒ Object
[ Utilities ].
- #get_taxonomy!(rank) ⇒ Object
-
#initialize(rocker, ids) ⇒ GenomeSet
constructor
A new instance of GenomeSet.
- #link_taxon(id, taxon) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(rocker, ids) ⇒ GenomeSet
Returns a new instance of GenomeSet.
10 11 12 13 14 15 16 |
# File 'lib/rocker/genome-set.rb', line 10 def initialize(rocker, ids) @rocker = rocker @ids = ids @ids = [] if ids.nil? @taxa = {} @all_taxa = {} end |
Instance Attribute Details
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
9 10 11 |
# File 'lib/rocker/genome-set.rb', line 9 def ids @ids end |
#rocker ⇒ Object (readonly)
Returns the value of attribute rocker.
9 10 11 |
# File 'lib/rocker/genome-set.rb', line 9 def rocker @rocker end |
#taxa ⇒ Object
Returns the value of attribute taxa.
9 10 11 |
# File 'lib/rocker/genome-set.rb', line 9 def taxa @taxa end |
Instance Method Details
#choose_genomes!(rank) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/rocker/genome-set.rb', line 38 def choose_genomes!(rank) @taxa = {} self.get_taxonomy! rank @all_taxa.each_pair{ |taxon,ids| @taxa[taxon] = ids.sample } @ids = @taxa.values end |
#delete!(ids) ⇒ Object
56 57 58 |
# File 'lib/rocker/genome-set.rb', line 56 def delete!(ids) ids.map{ |i| @ids.delete(i) }.flatten.compact end |
#download(file) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rocker/genome-set.rb', line 17 def download(file) tmp_ids = Array.new(self.ids) ofh = File.open(file, "w") while tmp_ids.size>0 ofh.print rocker. ebiFetch(:embl, tmp_ids.shift(200), :fasta). each_line.to_a.select { |i| if i =~ /^Entry: (\S+) (.*)/ warn "EBI returned an error fetching #{$1}: #{$2}" false else true end }.join end ofh.close end |
#empty? ⇒ Boolean
55 |
# File 'lib/rocker/genome-set.rb', line 55 def empty?() self.ids.empty? end |
#genome2taxid(genome_id) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/rocker/genome-set.rb', line 71 def genome2taxid(genome_id) doc = rocker.ebiFetch(:embl, [genome_id], :annot).split(/[\n\r]/) ln = doc.grep(/^FT\s+\/db_xref="taxon:/).first ln = doc.grep(/^OX\s+NCBI_TaxID=/).first if ln.nil? return nil if ln.nil? ln.sub!(/.*(?:"taxon:|NCBI_TaxID=)(\d+)["; ].*/, "\\1") return nil unless ln =~ /^\d+$/ ln end |
#genome2taxon(genome_id, rank = "species") ⇒ Object
[ Utilities ]
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rocker/genome-set.rb', line 61 def genome2taxon(genome_id, rank="species") v = genome2taxid(genome_id) unless v.nil? xml = rocker.ebiFetch(:taxonomy, [v], :enataxonomyxml).gsub(/\s*\n\s*/,"") v = xml.scan(/<taxon [^>]+>/).grep(/rank="#{rank}"/).first v.sub!(/.* taxId="(\d+)".*/,"\\1") unless v.nil? end return "no-taxon-#{(0...12).map { (65 + rand(26)).chr }.join}" if v.nil? or v !~ /^\d+$/ v end |
#get_taxonomy!(rank) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/rocker/genome-set.rb', line 44 def get_taxonomy!(rank) @all_taxa = {} ids.each do |id| self.link_taxon(id, genome2taxon(id, rank)) end end |
#link_taxon(id, taxon) ⇒ Object
34 35 36 37 |
# File 'lib/rocker/genome-set.rb', line 34 def link_taxon(id, taxon) @all_taxa[ taxon.to_sym ] ||= [] @all_taxa[ taxon.to_sym ] << id end |
#size ⇒ Object
54 |
# File 'lib/rocker/genome-set.rb', line 54 def size() self.ids.size end |