This repository was archived by the owner on Jan 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpreparsing.rb
More file actions
139 lines (127 loc) · 4.72 KB
/
Copy pathpreparsing.rb
File metadata and controls
139 lines (127 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
require 'zip'
require 'digest'
class Preparsing
include Sidekiq::Worker
# only retry 10 times - after that, the genotyping probably has already been deleted
sidekiq_options queue: :preparse, retry: 10, unique: true
def perform(genotype_id)
genotype = Genotype.find(genotype_id)
logger.info "Starting preparse"
biggest = ''
biggest_size = 0
begin
Zip::File.open(genotype.genotype.path) do |zipfile|
# find the biggest file, since that's going to be the genotyping
zipfile.each do |entry|
if entry.size > biggest_size
biggest = entry
biggest_size = entry.size
end
end
zipfile.extract(biggest,"#{Rails.root}/tmp/#{genotype.fs_filename}.csv")
system("mv #{Rails.root}/tmp/#{genotype.fs_filename}.csv #{Rails.root}/public/data/#{genotype.fs_filename}")
logger.info "copied file"
end
rescue
logger.info "nothing to unzip, seems to be a text-file in the first place"
end
# now that they are unzipped, check if they're actually proper files
file_is_ok = false
fh = File.open(genotype.genotype.path)
l = fh.readline()
# some files, for some reason, start with the UTF-BOM-marker
l = l.sub("\uFEFF","")
# iterate as long as there's commenting going on
while l.start_with?("#")
l = fh.readline()
l = l.sub("\uFEFF","")
end
if genotype.filetype == "23andme"
# first non-comment line is of length 4 after split
if l.strip.split("\t").length == 4
logger.info "file is 23andme and is ok!"
file_is_ok = true
end
elsif genotype.filetype == "genes-for-good"
# first non-comment line is of length 4 after split
if l.strip.split("\t").length == 4
logger.info "file is genes-for-good and is ok!"
file_is_ok = true
end
elsif genotype.filetype == "ancestry"
# first line is of length 5
if l.strip.split("\t").length == 5
file_is_ok = true
logger.info "file is ancestry and is ok!"
end
elsif genotype.filetype == "decodeme"
# first line is of length 6
if l.strip.split(",").length == 6
file_is_ok = true
logger.info "file is decodeme and is ok!"
end
elsif genotype.filetype == "ftdna-illumina"
# first line is of length 4
if l.strip.split(",").length == 4
file_is_ok = true
logger.info "file is ftdna and is ok!"
end
elsif genotype.filetype == "23andme-exome-vcf"
#first line is
if l.strip.split("\t").length == 10
file_is_ok = true
logger.info "file is 23andme-exome and is ok!"
end
elsif genotype.filetype == "IYG"
if l.strip.split("\t").length == 2
file_is_ok = true
logger.info "file is IYG and is ok!"
end
end
logger.info "Checking whether genotyping is duplicate"
md5 = Digest::MD5.file("#{Rails.root}/public/data/#{genotype.fs_filename}").to_s
file_is_duplicate = false
if Genotype.unscoped.where(md5sum: md5).where.not(id: genotype.id).count.positive?
file_is_duplicate = true
logger.info "Genotyping #{genotype.genotype.path} is already uploaded!\n"
logger.info "Genotyping #{genotype.fs_filename} has the same md5sum.\n"
file_is_ok = false
file_is_duplicate = true
end
logger.info "Checking whether genotyping contains email addresses"
# this should be the fastest way to do it
cmd = "LANG=C grep -F '@' #{Rails.root}/public/data/#{genotype.fs_filename}"
matches = system( cmd )
file_has_mails = false
if matches
logger.info "Genotyping #{genotype.genotype.path} contains email addresses!"
file_is_ok = false
file_has_mails = true
end
ensure
# not proper file!
if not file_is_ok
if file_is_duplicate
UserMailer.duplicate_file(genotype.user_id).deliver_later
system("rm #{Rails.root}/public/data/#{genotype.fs_filename}")
Genotype.find_by_id(genotype.id).delete
elsif file_has_mails
UserMailer.file_has_mails(genotype.user_id).deliver
system("rm #{Rails.root}/public/data/#{genotype.fs_filename}")
Genotype.find_by_id(genotype.id).delete
else
UserMailer.parsing_error(genotype.user_id).deliver_later
logger.info "file is not ok, sending email"
system("rm #{Rails.root}/public/data/#{genotype.fs_filename}")
Genotype.find_by_id(genotype.id).delete
end
else
logger.info "Updating genotype with md5sum #{md5}"
logger.info "Updating genotype #{genotype.id}"
status = genotype.update_attributes(md5sum: md5)
logger.info "Md5-updating-status is #{status}"
Parsing.perform_async(genotype.id)
end
end
end