Class: GenomeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rocker/genome-set.rb

Overview

Author:

  • Luis M. Rodriguez-R <lmrodriguezr at gmail dot com>

  • Luis (Coto) Orellana

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idsObject (readonly)

Returns the value of attribute ids.



9
10
11
# File 'lib/rocker/genome-set.rb', line 9

def ids
  @ids
end

#rockerObject (readonly)

Returns the value of attribute rocker.



9
10
11
# File 'lib/rocker/genome-set.rb', line 9

def rocker
  @rocker
end

#taxaObject

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



29
30
31
32
33
34
# File 'lib/rocker/genome-set.rb', line 29

def choose_genomes!(rank)
   @taxa = {}
   self.get_taxonomy! rank
   @all_taxa.each_pair{ |taxon,ids| @taxa[taxon] = ids.sample }
   @ids = @taxa.values
end

#download(file) ⇒ Object



17
18
19
20
21
22
23
24
# 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)
   end
   ofh.close
end

#empty?Boolean

Returns:

  • (Boolean)


46
# File 'lib/rocker/genome-set.rb', line 46

def empty?() self.ids.empty? end

#genome2taxid(genome_id) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/rocker/genome-set.rb', line 59

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 ]


49
50
51
52
53
54
55
56
57
58
# File 'lib/rocker/genome-set.rb', line 49

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



35
36
37
38
39
40
# File 'lib/rocker/genome-set.rb', line 35

def get_taxonomy!(rank)
   @all_taxa = {}
   ids.each do |id|
	 self.link_taxon(id, genome2taxon(id, rank))
   end
end


25
26
27
28
# File 'lib/rocker/genome-set.rb', line 25

def link_taxon(id, taxon)
   @all_taxa[ taxon.to_sym ] ||= []
   @all_taxa[ taxon.to_sym ] << id
end

#sizeObject



45
# File 'lib/rocker/genome-set.rb', line 45

def size() self.ids.size end