Skip to content

Commit

Permalink
all tests clean
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujigiri committed Oct 20, 2011
1 parent da4335a commit 64fddf5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/controllers/genotypes_controller.rb
Expand Up @@ -35,7 +35,7 @@ def create
end

@genotype.move_file
Resque.enqueue(Preparsing, @genotype)
Resque.enqueue(Preparsing, @genotype.id)
format.html { redirect_to(current_user, :notice => 'Genotype was successfully uploaded! Parsing and stuff might take a couple of hours.') }
format.xml { render :xml => current_user, :status => :created, :location => @user }
else
Expand Down
7 changes: 4 additions & 3 deletions app/jobs/parsing.rb
Expand Up @@ -3,13 +3,14 @@
class Parsing
@queue = :parse

def self.perform(genotyp,temp_file)
def self.perform(genotype_id, temp_file)
Rails.logger.level = 0
Rails.logger = Logger.new("#{Rails.root}/log/parsing_#{Rails.env}.log")
@genotype = Genotype.find_by_id(genotyp["genotype"]["id"].to_i)
genotype_id = genotype_id["genotype"]["id"].to_i if genotype_id.is_a?(Hash)
@genotype = Genotype.find(genotype_id)

if @genotype.filetype != "other"

genotype_file = File.open(temp_file, "r")
log "Loading known Snps."
known_snps = {}
Expand Down
9 changes: 5 additions & 4 deletions app/jobs/preparsing.rb
Expand Up @@ -3,16 +3,17 @@
class Preparsing
@queue = :preparse

def self.perform(genotyp)
@genotype = Genotype.find_by_id(genotyp["genotype"]["id"].to_i)
def self.perform(genotype_id)
genotype_id = genotype_id["genotype"]["id"].to_i if genotype_id.is_a?(Hash)
@genotype = Genotype.find(genotype_id)
filename = "#{Rails.root}/public/data/#{@genotype.fs_filename}"

system("csplit -k -f #{@genotype.id}_tmpfile -n 4 #{filename} 20000 {2000}")
system("mv #{@genotype.id}_tmpfile* tmp/")

temp_files = Dir.glob("tmp/#{@genotype.id}_tmpfile*")
temp_files.each do |single_temp_file|
Resque.enqueue(Parsing,@genotype,single_temp_file)
Resque.enqueue(Parsing, @genotype.id, single_temp_file)
end
end
end
end
5 changes: 5 additions & 0 deletions test/data/23andMe_test.csv
@@ -0,0 +1,5 @@
rs4477212 1 72017 AA
rs3094315 1 742429 AA
rs3131972 1 742584 GG
rs12124819 1 766409 AG
rs11240777 1 788822 AG
20 changes: 0 additions & 20 deletions test/data/23andMe_test.txt

This file was deleted.

1 change: 0 additions & 1 deletion test/data/deCODEme_test.csv 100755 → 100644
@@ -1,4 +1,3 @@
Name,Variation,Chromosome,Position,Strand,YourCode
rs4477212,A/G,1,72017,+,AA
rs2185539,C/T,1,556738,+,CC
rs6681105,C/T,1,581938,+,TT
Expand Down
5 changes: 3 additions & 2 deletions test/functional/genotypes_controller_test.rb
Expand Up @@ -5,6 +5,7 @@ class GenotypesControllerTest < ActionController::TestCase
setup do
Sunspot.stubs(:index)
@genotype = Factory :genotype
UserAchievement.delete_all
end

context "unauthenticated users" do
Expand Down Expand Up @@ -53,11 +54,11 @@ class GenotypesControllerTest < ActionController::TestCase
Resque.expects(:enqueue).with do |*args|
assert_equal 2, args.size
assert_equal Preparsing, args[0]
assert args[1].is_a?(Genotype)
assert args[1].is_a?(Fixnum)
end
genotype_file_upload = ActionDispatch::Http::UploadedFile.new(
filename: '23andme.txt', content_type: 'text/plain',
tempfile: File.new("#{Rails.root}/test/data/23andMe_test.txt"))
tempfile: File.new("#{Rails.root}/test/data/23andMe_test.csv"))
assert_difference 'UserAchievement.count' do
put :create, commit: "Upload", genotype:
{ filename: genotype_file_upload, filetype: "23andme"}
Expand Down
63 changes: 19 additions & 44 deletions test/unit/parsing_test.rb
Expand Up @@ -3,38 +3,38 @@
class ParsingTest < ActiveSupport::TestCase
context "parser" do
setup do

Sunspot.stubs(:index)
Snp.delete_all
UserSnp.delete_all

@file_23andMe = "#{Rails.root}/test/data/23andMe_test.txt"
@file_23andMe = "#{Rails.root}/test/data/23andMe_test.csv"
@genotype_23andme = Factory :genotype,
originalfilename: @file_23andMe.split('/').last, filetype: '23andme'
FileUtils.cp @file_23andMe,
"#{Rails.root}/public/data/#{@genotype_23andme.user.id}.#{@genotype_23andme.filetype}.#{@genotype_23andme.id}"

@file_deCODEme = "#{Rails.root}/test/data/deCODEme_test.csv"

@genotype_decodeme = Factory :genotype,
originalfilename: @file_deCODEme.split('/').last, filetype: 'decodeme'
FileUtils.cp @file_deCODEme,
"#{Rails.root}/public/data/#{@genotype_decodeme.user.id}.#{@genotype_decodeme.filetype}.#{@genotype_decodeme.id}"

@temp_file = "#{Rails.root}/tmp/snp_file.txt"
FileUtils.rm(@temp_file) if File.exist?(@temp_file)
end

should "parse 23andMe data" do
Parsing.perform('genotype' => @genotype_23andme.attributes)
FileUtils.cp @file_23andMe, @temp_file
Parsing.perform(@genotype_23andme.id, @temp_file)

# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking ]
end.sort_by { |s| s[0] }

expected =
[ [ "rs11240777", "788822", "1", {"AG"=>1}, {"A"=>1, "T"=>0, "G"=>1, "C"=>0}, 0 ],
[ "rs12124819", "766409", "1", {"AG"=>1}, {"A"=>1, "T"=>0, "G"=>1, "C"=>0}, 0 ],
[ "rs3094315", "742429", "1", {"AA"=>1}, {"A"=>2, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3131972", "742584", "1", {"GG"=>1}, {"A"=>0, "T"=>0, "G"=>2, "C"=>0}, 0 ],
[ "rs4477212", "72017", "1", {"AA"=>1}, {"A"=>2, "T"=>0, "G"=>0, "C"=>0}, 0 ]]
[ [ "rs11240777", "788822", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs12124819", "766409", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3131972", "742584", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ]]

assert_equal expected, snp_data

Expand All @@ -52,19 +52,20 @@ class ParsingTest < ActiveSupport::TestCase
end

should "parse deCODEme data" do
Parsing.perform('genotype' => @genotype_decodeme.attributes)
FileUtils.cp @file_deCODEme, @temp_file
Parsing.perform(@genotype_decodeme.id, @temp_file)

# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking ]
end.sort_by { |s| s[0] }

expected =
[ [ "rs11240767", "718814", "1", {"CC"=>1}, {"A"=>0, "T"=>0, "G"=>0, "C"=>2}, 0 ],
[ "rs2185539", "556738", "1", {"CC"=>1}, {"A"=>0, "T"=>0, "G"=>0, "C"=>2}, 0 ],
[ "rs3094315", "742429", "1", {"TT"=>1}, {"A"=>0, "T"=>2, "G"=>0, "C"=>0}, 0 ],
[ "rs4477212", "72017", "1", {"AA"=>1}, {"A"=>2, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs6681105", "581938", "1", {"TT"=>1}, {"A"=>0, "T"=>2, "G"=>0, "C"=>0}, 0 ]]
[ [ "rs11240767", "718814", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0],
[ "rs2185539", "556738", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0],
[ "rs6681105", "581938", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0] ]

assert_equal expected, snp_data

Expand All @@ -80,31 +81,5 @@ class ParsingTest < ActiveSupport::TestCase
assert snp_names.include?(s.snp_name)
end
end

context "existing Snps" do
setup do
[ "rs11240777", "rs12124819", "rs3094315", "rs3131972", "rs4477212" ].each_with_index do |snp_name|
Factory(:snp, name: snp_name)
end
end

should "be updated" do
Parsing.perform('genotype' => @genotype_23andme.attributes)
snps = Snp.all
assert_equal 5, snps.size
expected =
[ [ "rs11240777", {"AA"=>1, "AG"=>1}, {"A"=>1, "T"=>0, "G"=>1, "C"=>0} ],
[ "rs12124819", {"AA"=>1, "AG"=>1}, {"A"=>1, "T"=>0, "G"=>1, "C"=>0} ],
[ "rs3094315", {"AA"=>2}, {"A"=>2, "T"=>0, "G"=>0, "C"=>0} ],
[ "rs3131972", {"AA"=>1, "GG"=>1}, {"A"=>0, "T"=>0, "G"=>2, "C"=>0} ],
[ "rs4477212", {"AA"=>2}, {"A"=>2, "T"=>0, "G"=>0, "C"=>0} ]]

# Snp
snp_data = snps.map do |s|
[ s.name, s.genotype_frequency, s.allele_frequency ]
end.sort_by { |s| s[0] }
assert_equal expected, snp_data
end
end
end
end

0 comments on commit 64fddf5

Please sign in to comment.