lib/snp-search.rb in snp-search-0.31.0 vs lib/snp-search.rb in snp-search-0.32.0
- old
+ new
@@ -52,12 +52,13 @@
def populate_snps_alleles_genotypes(vcf_file, cuttoff_snp, cuttoff_genotype)
puts "Adding SNPs........"
# open vcf file and parse each line
File.open(vcf_file) do |f|
# header names
- while line = f.gets.chomp!
+ while line = f.gets
if line =~ /CHROM/
+ line.chomp
column_headings = line.split("\t")
strain_names = column_headings[9..-1]
strain_names.map!{|name| name.sub(/\..*/, '')}
strain_names.each do |str|
@@ -127,26 +128,29 @@
# create snp allele
snp_allele = Allele.new
snp_allele.base = snp_base
snp_allele.snp = s
snp_allele.save
-
+
ActiveRecord::Base.transaction do
+ genos = []
genotypes.each_with_index do |gt, index|
- genotype = Genotype.new
- genotype.strain = strains[index]
- puts index if strains[index].nil?
+ genotype = Genotype.new
+ genotype.strain = strains[index]
+ puts index if strains[index].nil?
if gt == "0/0" # wild type
- genotype.allele = ref_allele
+ genotype.allele = ref_allele
elsif gt == "1/1" # snp type
- genotype.allele = snp_allele
+ genotype.allele = snp_allele
else
puts "Strange SNP #{gt}"
end
- genotype.save
+ genos << genotype
end
- puts "Total SNPs added so far: #{good_snps}" if good_snps % 100 == 0
+ # Using activerecord-import to speed up importing
+ Genotype.import genos, :validate => false
+ puts "Total SNPs added so far: #{good_snps}" if good_snps % 100 == 0
end
end
end
end
end
@@ -157,6 +161,5 @@
x = Feature.where("features.start <= ? AND features.end >= ?", snp.ref_pos, snp.ref_pos).first
snp.feature = x
snp.save
end
end
-