Class: Bio::Ngs::Ontology
- Inherits:
-
Object
- Object
- Bio::Ngs::Ontology
- Defined in:
- lib/bio/ngs/ontology.rb
Instance Attribute Summary collapse
-
#gene_id ⇒ Object
Returns the value of attribute gene_id.
-
#go ⇒ Object
Returns the value of attribute go.
-
#library ⇒ Object
Returns the value of attribute library.
Class Method Summary collapse
-
.go_import(file, yaml_file = nil) ⇒ Object
Method to import a GO OBO file into Go table created according to ActiveRecord model Params: GO OBO file, YAML file for db connection.
-
.load_go_genes(file, yaml_file = nil) ⇒ Object
Method to lood the Gene-GO associations from a JSON file into the Ontology db Params: JSON file name, YAML file for db connection (optional).
Instance Method Summary collapse
-
#initialize(gene_id, go = [], library = nil) ⇒ Ontology
constructor
Constructor for Bio::Ngs::Ontology instances.
-
#to_db(yaml_file = nil) ⇒ Object
Method to store a single Bio::Ngs::Ontology object into the Ontology db.
Constructor Details
#initialize(gene_id, go = [], library = nil) ⇒ Ontology
Constructor for Bio::Ngs::Ontology instances
61 62 63 64 65 |
# File 'lib/bio/ngs/ontology.rb', line 61 def initialize(gene_id,go=[],library=nil) @gene_id = gene_id @go = go @library = library end |
Instance Attribute Details
#gene_id ⇒ Object
Returns the value of attribute gene_id.
59 60 61 |
# File 'lib/bio/ngs/ontology.rb', line 59 def gene_id @gene_id end |
#go ⇒ Object
Returns the value of attribute go.
59 60 61 |
# File 'lib/bio/ngs/ontology.rb', line 59 def go @go end |
#library ⇒ Object
Returns the value of attribute library.
59 60 61 |
# File 'lib/bio/ngs/ontology.rb', line 59 def library @library end |
Class Method Details
.go_import(file, yaml_file = nil) ⇒ Object
Method to import a GO OBO file into Go table created according to ActiveRecord model Params: GO OBO file, YAML file for db connection
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bio/ngs/ontology.rb', line 15 def self.go_import(file,yaml_file=nil) db = Bio::Ngs::Db.new :ontology,yaml_file inserts = [] file = File.open(file) file.each do |line| if line.start_with? "[Term]" block = file.gets("\n\n") is_a = [] data = [] block.split("\n").each do |elem| if elem.start_with? "id: " data << elem.gsub("id: ","") elsif elem.start_with? "name: " data << elem.gsub("name: ","") elsif elem.start_with? "is_a" is_a << elem.gsub("is_a: ","").split("!").first elsif elem.start_with? "namespace: " data << elem.gsub("namespace: ","") end end data << is_a.join(" ") inserts << data if inserts.size == 1000 db.insert_many(:go,"INSERT INTO go(go_id,name,namespace,is_a) VALUES(?,?,?,?)",inserts) inserts = [] end end end db.insert_many(:go,"INSERT INTO go(go_id,name,namespace,is_a) VALUES(?,?,?,?)",inserts) if inserts.size > 0 end |
.load_go_genes(file, yaml_file = nil) ⇒ Object
Method to lood the Gene-GO associations from a JSON file into the Ontology db Params: JSON file name, YAML file for db connection (optional)
48 49 50 51 52 53 54 55 56 |
# File 'lib/bio/ngs/ontology.rb', line 48 def self.load_go_genes(file,yaml_file=nil) db = Bio::Ngs::Db.new :ontology, yaml_file list = JSON.load File.read(file) ontologies = Bio::Ngs::OntologyCollection.new list.each_with_index do |gene,index| ontologies << Bio::Ngs::Ontology.new(gene["gene_id"],gene["go"],gene["library"]) end ontologies.to_db(yaml_file) end |
Instance Method Details
#to_db(yaml_file = nil) ⇒ Object
Method to store a single Bio::Ngs::Ontology object into the Ontology db
68 69 70 71 72 73 74 75 |
# File 'lib/bio/ngs/ontology.rb', line 68 def to_db(yaml_file=nil) raise RuntimeError,"You must initialize the Ontolgy db with biongs ontology:db:init" if Go.count == 0 db = Bio::Ngs::Db.new :ontology,yaml_file g = Gene.create(:gene_id => @gene_id, :library => @library) Go.where({:go_id => @go}).all.each do |go| g.gene_gos.create(:go_id => go.id) end end |