-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmcif.rake
362 lines (310 loc) · 10.5 KB
/
mmcif.rake
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require "rubygems"
require "logger"
require "date"
require "fileutils"
require "pathname"
require "net/smtp"
include FileUtils
$config = {
:mmcif_mirror_dir => Pathname.new("/tlbnas/mirror/pdb/data/structures/all/mmCIF"),
:schema_map_file => Pathname.new("/usr/local/db-loader-v4.2.0/db-loader/test/schema_map_pdbx_na.cif"),
:db_loader_bin => Pathname.new("/usr/local/db-loader-v4.2.0/bin/db-loader"),
:db_host => "localhost",
:db_name => "MMCIF_DEV",
:db_dbms => "mysql",
:db_user => "bernardo",
:db_pass => "",
:db_manager => "bernardo",
:my_email => "[email protected]",
:gloria_email => "[email protected]",
:schema_load_sql_file => "DB_LOADER_SCHEMA.sql",
#:schema_load_mod_sql_file => "DB_LOADER_SCHEMA_MOD.sql",
:schema_drop_sql_file => "DB_LOADER_SCHEMA_DROP.sql",
:data_load_sql_file => "DB_LOADER_LOAD.sql",
#:field_delimiter => "'@\\t'",
:field_delimiter => "'\\t'",
#:row_delimiter => "'#\\n'",
:row_delimiter => "'\\n'",
:temp_dir => Pathname.new("/spunky/temp/mmCIF"),
:log_file => "mmcif_rake.log",
:nthreads => 4
}
#$logger_formatter = Logger::Formatter.new
#class Logger
# def format_message(severity, timestamp, progname, msg)
# $logger_formatter.call(severity, timestamp, progname, msg)
# end
#end
class MultiIO
def initialize(*targets)
@targets = targets
end
def write(*args)
@targets.each {|t| t.write(*args) }
end
def close
@targets.each(&:close)
end
end
#$log_file = STDOUT
$logger = Logger.new MultiIO.new(STDOUT, File.open($config[:log_file], 'a'))
$logger.level = Logger::DEBUG
def send_email(from = $config[:my_email],
from_alias = 'Alicia Higueruelo',
to = $config[:gloria_email],
to_alias = 'Gloria',
subject = "[Gloria] MMCIF on spunky updated",
message = "MMCIF database has been updated.")
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
Net::SMTP.start('localhost') do |smtp|
smtp.send_message msg, from, to
end
end
$logger.debug "Initializing..."
# Tasks
desc "Simply just build MMCIF database!"
task :default => [
#"check:week",
#"prepare:temp_dir",
:req_passwd,
"prepare:files",
"create:list",
"create:revised_schema_mapping",
"create:updated_schema_mapping",
"create:schema",
"create:dumps",
"drop:tables",
"create:tables",
"modify:load_sql",
"import:dumps",
#"cleanup:tmpdir",
"send:email"
]
desc "Request SQL password"
task :req_passwd do
`stty -echo`
print "Input SQL Password for user '#{$config[:db_user]}': "
$config[:db_pass] = $stdin.gets.chomp
`stty echo`
puts ""
end
namespace :check do
desc "Check if this week is for MMCIF"
task :week do
if Date.today.cweek % 2 == 0
$logger.debug "This week is not mine."
puts "This week is not mine."
exit
else
$logger.debug "This week is mine"
puts "This week is mine."
end
end
end
namespace :prepare do
desc "Prepare a scratch directory"
task :temp_dir do
dir = $config[:temp_dir]
if File.exists? dir
rm_rf dir, :secure => true
$logger.info "Removing #{dir}: done"
end
mkdir_p dir
$logger.info "Creating #{dir}: done"
end
desc "Uncompress and copy mmCIF files to working directory"
task :files do
zipped_files = Dir[$config[:mmcif_mirror_dir].join("*.gz").to_s]
pidhash = Hash.new
zipped_files.each_with_index do |zipped_file, i|
unzipped_file = $config[:temp_dir].join(File.basename(zipped_file, ".gz")).to_s
if File.size? unzipped_file
$logger.debug "File #{unzipped_file} already exists. Skipping"
next
end
$logger.debug "Processing #{zipped_file} > #{unzipped_file}"
pidhash[spawn("gzip -cd #{zipped_file}", :out=>unzipped_file)] = [ zipped_file, unzipped_file ]
#system "gzip -cd #{zipped_file} > #{unzipped_file}"
while pidhash.size >= $config[:nthreads] do
finished, status = Process.wait2
$logger.debug "Uncompressed '#{pidhash[finished][0]}' to '#{pidhash[finished][1]}': done (#{i+1}/#{zipped_files.size})"
#$logger.debug "Uncompressing '#{zipped_file}' to '#{unzipped_file}': done (#{i+1}/#{zipped_files.size})"
pidhash.delete finished
end
end
Process.waitall
$logger.info "Uncompressing #{zipped_files.size} PDB mmCIF files to #{$config[:temp_dir]}: done"
end
end
namespace :create do
desc "Create a LIST file"
task :list do
File.open($config[:temp_dir].join("LIST"), 'w') do |file|
file.puts Dir[$config[:temp_dir].join("*.cif").to_s].map { |f| File.basename(f) }
end
$logger.info "Creating a LIST file: done"
end
desc "Create a revised schema mapping file"
task :revised_schema_mapping do
$logger.debug "Staring creation of revised schema mapping file: #{$config[:temp_dir]}/revised_schema_mapping.cif"
Dir.chdir $config[:temp_dir] do
success = system "#{$config[:db_loader_bin]} " +
"-map #{$config[:schema_map_file]} " +
"-list LIST " +
"-revise revised_schema_mapping.cif"
if success
$logger.info "Creating a revised schema mapping file: done"
else
$logger.error "Error creating revised schema mapping file!"
break
end
end
end
desc "Create an updated schema mapping file"
task :updated_schema_mapping do
$logger.debug "Staring creation of updated schema mapping file: #{$config[:temp_dir]}/updated_schema_mapping.cif"
Dir.chdir $config[:temp_dir] do
success = system "#{$config[:db_loader_bin]} " +
"-map #{$config[:schema_map_file]} " +
"-update updated_schema_mapping.cif " +
"-revise revised_schema_mapping.cif"
if success
$logger.info "Creating an updated schema mapping file: done"
else
$logger.error "Error creating updated schema mapping file!"
break
end
end
end
desc "Create MMCIF RDB schema"
task :schema do
Dir.chdir $config[:temp_dir] do
success = system "#{$config[:db_loader_bin]} " +
#"-map #{$config[:schema_map_file]} " +
"-map updated_schema_mapping.cif " +
"-server #{$config[:db_dbms]} " +
"-db #{$config[:db_name]} " +
"-schema"
if success
$logger.info "Creating MMCIF schema: done"
else
$logger.error "Error creating MMCIF schema!"
break
end
end
end
desc "Create MMCIF dump files"
task :dumps do
Dir.chdir $config[:temp_dir] do
success = system "#{$config[:db_loader_bin]} " +
#"-map #{$config[:schema_map_file]} " +
"-map updated_schema_mapping.cif " +
"-server #{$config[:db_dbms]} " +
"-db #{$config[:db_name]} " +
"-ft #{$config[:field_delimiter]} " +
"-rt #{$config[:row_delimiter]} " +
"-bcp " +
"-list LIST " +
"-revise revised_schema_mapping.cif"
if success
$logger.info "Creating MMCIF dump files: done"
else
$logger.error "Error creating MMCIF dump files!"
break
end
end
end
desc "Create MMCIF tables"
task :tables => [:req_passwd] do
success = system "mysql -f " +
"-h #{$config[:db_host]} " +
"-u #{$config[:db_user]} " +
"-p#{$config[:db_pass]} " +
"< #{$config[:temp_dir].join($config[:schema_load_sql_file])}"
if success
$logger.info "Creating MMCIF tables: done"
else
$logger.error "Error creating MMCIF tables!"
break
end
end
end
namespace :drop do
desc "Drop MMCIF tables"
task :tables => [:req_passwd] do
success = system "mysql -f " +
"-h #{$config[:db_host]} " +
"-u #{$config[:db_user]} " +
"-p#{$config[:db_pass]} " +
"< #{$config[:temp_dir].join($config[:schema_drop_sql_file])}"
if success
$logger.info "Dropping MMCIF tables: done"
else
$logger.error "Error dropping MMCIF tables!"
break
end
end
end
namespace :modify do
desc "Modify DB_LOADER_LOAD.sql (to prevent the loading of the massive atom_site table)"
task :load_sql do
sql_file = $config[:temp_dir].join($config[:data_load_sql_file])
FileUtils.copy_file sql_file, sql_file.sub('.sql', '.sql_orig'), :verbose => true
load_sql = IO.readlines(sql_file)
#atom_site_sql = load_sql.slice!(3..6)
#load_sql << atom_site_sql
File.open(sql_file, 'w') do |f|
skip_flag = false
load_sql.each do |l|
$logger.debug l
if l.strip.end_with? '"atom_site.bcp"' or l.strip.end_with? '"atom_site_anisotrop.bcp"'
skip_flag = true
end
f.print "-- " if skip_flag
f.puts l
if l.strip.end_with? ';'
skip_flag = false
end
end
#f.puts load_sql.join
end
$logger.info "Finished modifying #{$config[:data_load_sql_file]}"
end
end
namespace :import do
desc "Import MMCIF dump files to database"
task :dumps => [:req_passwd] do
Dir.chdir $config[:temp_dir] do
my_cmdline = "mysql --local-infile -v -f " +
"-h #{$config[:db_host]} " +
"-u #{$config[:db_user]} " +
"-p#{$config[:db_pass]} " +
"< #{$config[:data_load_sql_file]}"
#$logger.debug my_cmdline
success = system my_cmdline
if success
$logger.info "Importing mmCIF dump files to #{$config[:db_host]}.#{$config[:db_name]}: done"
else
$logger.error "Error importing mmCIF dump files to #{$config[:db_host]}.#{$config[:db_name]}!"
break
end
end
end
end
namespace :cleanup do
desc "Clean up temporary mmCIF files"
task :tmpdir do
rm_rf($config[:temp_dir], :secure => true) if Dir.exists? $config[:temp_dir]
end
end
namespace :send do
desc "Send log to Gloria team"
task :email do
send_email
end
end