class GenomeSet
@author Luis M. Rodriguez-R <lmrodriguezr at gmail dot com> @author Luis (Coto) Orellana @license artistic license 2.0 @update Dec-01-2015
Attributes
ids[R]
rocker[R]
taxa[R]
Public Class Methods
new(rocker, ids)
click to toggle source
# File lib/rocker/genome-set.rb, line 10 def initialize(rocker, ids) @rocker = rocker @ids = ids @ids = [] if ids.nil? @taxa = {} @all_taxa = {} end
Public Instance Methods
choose_genomes!(rank)
click to toggle source
# 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)
click to toggle source
# File lib/rocker/genome-set.rb, line 56 def delete!(ids) ids.map{ |i| @ids.delete(i) }.flatten.compact end
download(file)
click to toggle source
# 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?()
click to toggle source
# File lib/rocker/genome-set.rb, line 55 def empty?() self.ids.empty? end
genome2taxid(genome_id)
click to toggle source
# 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")
click to toggle source
[ Utilities ]¶ ↑
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/rocker/genome-set.rb, line 54 def size() self.ids.size end
taxa=(hash)
click to toggle source
# File lib/rocker/genome-set.rb, line 50 def taxa=(hash) @taxa = {} hash.each_pair{ |taxon, id| @taxa[taxon] = id if self.ids.include? id } end