-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRakefile
1145 lines (1042 loc) · 43 KB
/
Rakefile
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# TogoID
require 'time'
require 'yaml'
ENV['PATH'] = "bin:#{ENV['HOME']}/local/bin:#{ENV['PATH']}"
### Options
$verbose = true # Flag to enable verbose output for STDERR
# TogoID#file_older_than_days?()
$duration = 5 # Default number of days to force update
# TogoID#validate_tsv_output()
$chklines = 10 # Number of head and tail lines to be validated
$maxblank = 2 # Maximum number of acceptable empty lines in TSV files
$minratio = 0.5 # Minimum acceptable size ratio of new / old TSV file sizes
### Tasks
directory OUTPUT_TSV_DIR = "output/tsv/"
directory OUTPUT_TTL_DIR = "output/ttl/relation/"
directory OUTPUT_ID_LABEL_TTL_DIR = "output/ttl/label/"
CFG_FILES = FileList["config/*/config.yaml"]
TSV_FILES = CFG_FILES.pathmap("%-1d").sub(/^/, OUTPUT_TSV_DIR).sub(/$/, '.tsv')
TTL_FILES = CFG_FILES.pathmap("%-1d").sub(/^/, OUTPUT_TTL_DIR).sub(/$/, '.ttl')
datasets = YAML.load(File.read("config/dataset.yaml"))
id_label_files_strs = []
datasets.each do |dataset, hash|
if hash.has_key?("method")
id_label_files_strs.push("#{OUTPUT_ID_LABEL_TTL_DIR}#{dataset}.ttl")
end
end
ID_LABEL_FILES = FileList.new(id_label_files_strs)
# For update procedure on AWS
UPDATE_TXT = ENV['TOGOID_UPDATE_TXT'] || File.join(OUTPUT_TSV_DIR, "update.txt")
S3_BUCKET_NAME = ENV['S3_BUCKET_NAME'] || "togo-id-production"
desc "Default task (update & convert)"
#task :default => [ :pre, :update, :convert, :post ]
task :default => [ :pre, 'prepare:all', :update, :convert, :id_label, :post ]
desc "Update all TSV files"
task :update => TSV_FILES
desc "Update all TTL files"
task :convert => TTL_FILES
desc "Update all ID and label TTL files"
task :id_label => ID_LABEL_FILES
desc "Pre task"
task :pre do
$stderr.puts "*** Started: #{`date +%FT%T`.strip} ***"
$stderr.puts
end
desc "Post task"
task :post do
$stderr.puts
$stderr.puts "*** Finished: #{`date +%FT%T`.strip} ***"
end
desc "Show targets"
task :test do
p CFG_FILES
p TSV_FILES
p TTL_FILES
end
desc "Draw a diagram with graphviz"
task :draw do
sh "togoid-config-summary config/*/config.yaml > docs/dot/togoid.sum"
sh "togoid-config-summary-dot --id dot/togoid.sum > docs/dot/togoid.dot"
sh "dot -Nshape=box -Nstyle=filled,rounded -Nfontcolor=white -Ecolor=gray -Kdot -Tpng docs/dot/togoid.dot -odocs/dot/togoid.png"
end
### Methods
module TogoID
# Methods for update/convert
module Main
# Entry point for TSV update
def update_tsv(taskname)
if taskname[/#{OUTPUT_TSV_DIR}/]
pair = taskname.sub(/#{OUTPUT_TSV_DIR}/, '').sub(/\.tsv$/, '')
if $verbose
$verbose = false
$stderr.puts "### Update TSV for #{pair} if check_tsv_filesize #{check_tsv_filesize(pair)} or check_config_timestamp #{check_config_timestamp(pair)} or check_tsv_timestamp #{check_tsv_timestamp(pair)}"
$verbose = true
end
if check_tsv_filesize(pair) or check_config_timestamp(pair) or check_tsv_timestamp(pair)
$stderr.puts "## Update #{config_file_name(pair)} => #{tsv_file_name(pair)}"
$stderr.puts "< #{`date +%FT%T`.strip} #{pair}"
if File.exist?(tsv_file_name(pair))
# Backup previous TSV output
sh "mv #{tsv_file_name(pair)} #{tsv_file_name_old(pair)}", verbose: false
end
sh "togoid-config #{config_dir_name(pair)} update"
if validate_tsv_output(pair)
$stderr.puts "# Success: #{tsv_file_name(pair)} is updated"
if File.exist?(tsv_file_name_old(pair))
# Remove prevous TSV output
sh "rm #{tsv_file_name_old(pair)}", verbose: false
end
else
$stderr.puts "# Failure: #{tsv_file_name(pair)} is not updated"
if File.exist?(tsv_file_name_old(pair))
# Revert previous TSV output"
sh "mv #{tsv_file_name_old(pair)} #{tsv_file_name(pair)}", verbose: false
end
end
$stderr.puts "> #{`date +%FT%T`.strip} #{pair}"
else
$stderr.puts "# => Preserving #{tsv_file_name(pair)}"
end
end
return "config/dataset.yaml"
end
# Entry point for TTL convert
def update_ttl(taskname)
if taskname[/#{OUTPUT_TTL_DIR}/]
pair = taskname.sub(/#{OUTPUT_TTL_DIR}/, '').sub(/\.ttl$/, '')
if $verbose
$verbose = false
$stderr.puts "### Update TTL for #{pair} if check_ttl_filesize #{check_ttl_filesize(pair)} or check_ttl_timestamp #{check_ttl_timestamp(pair)}"
$verbose = true
end
if check_ttl_filesize(pair) or check_ttl_timestamp(pair)
$stderr.puts "## Convert #{tsv_file_name(pair)} => #{ttl_file_name(pair)}"
$stderr.puts "< #{`date +%FT%T`.strip} #{pair}"
sh "togoid-config #{config_dir_name(pair)} convert"
$stderr.puts "> #{`date +%FT%T`.strip} #{pair}"
else
$stderr.puts "# => Preserving #{ttl_file_name(pair)}"
end
end
return "config/dataset.yaml"
end
# Entry point for ID and Label TTL
def update_id_label(taskname)
if taskname[/#{OUTPUT_ID_LABEL_TTL_DIR}/]
name = taskname.sub(/#{OUTPUT_ID_LABEL_TTL_DIR}/, '').sub(/\.ttl$/, '')
if $verbose
$verbose = false
$stderr.puts "### Update ID and Label TTL for #{name} if check_id_label_filesize #{check_id_label_filesize(name)} or check_id_label_timestamp #{check_id_label_timestamp(name)}"
$verbose = true
end
if check_id_label_filesize(name) or check_id_label_timestamp(name)
$stderr.puts "## Update #{id_label_file_name(name)}"
$stderr.puts "< #{`date +%FT%T`.strip} #{name}"
sh "togoid-rdfize-id-label #{name}"
$stderr.puts "> #{`date +%FT%T`.strip} #{name}"
else
$stderr.puts "# => Preserving #{ttl_file_name(name)}"
end
end
return "config/dataset.yaml"
end
def config_dir_name(pair)
"config/#{pair}"
end
def config_file_name(pair)
"config/#{pair}/config.yaml"
end
def tsv_file_name(pair)
"#{OUTPUT_TSV_DIR}#{pair}.tsv"
end
def tsv_file_name_old(pair)
"#{OUTPUT_TSV_DIR}#{pair}.tsv.old"
end
def ttl_file_name(pair)
"#{OUTPUT_TTL_DIR}#{pair}.ttl"
end
def id_label_file_name(name)
"#{OUTPUT_ID_LABEL_TTL_DIR}#{name}.ttl"
end
# Return true (needs update) when the TSV file does not exist or the size is zero
def check_tsv_filesize(pair)
output = tsv_file_name(pair)
return ! (File.exist?(output) and File.size(output) > 0)
end
# Return true (needs update) when the TTL file does not exist or the size is zero
def check_ttl_filesize(pair)
output = ttl_file_name(pair)
return ! (File.exist?(output) and File.size(output) > 0)
end
# Return true (needs update) when the TTL file does not exist or the size is zero
def check_id_label_filesize(name)
output = id_label_file_name(name)
return ! (File.exists?(output) and File.size(output) > 0)
end
# Return true (needs udpate) when the TSV file is older than the config file
def check_config_timestamp(pair)
input = config_file_name(pair)
output = tsv_file_name(pair)
file_older_than_stamp?(output, input)
end
# Return true (needs update) when the TSV file is older than the timestamp file
def check_tsv_timestamp(pair)
source = pair.split('-').first
input = "input/#{source}/download.lock"
output = tsv_file_name(pair)
# If there is no timpestamp file (input), update the pair anyway
file_older_than_stamp?(output, input)
end
# Return true (needs update) when the TTL file is older than the TSV file
def check_ttl_timestamp(pair)
input = tsv_file_name(pair)
output = ttl_file_name(pair)
file_older_than_stamp?(output, input)
end
# Return true (needs update) when the TSV file is older than the timestamp file
def check_id_label_timestamp(name)
input = "input/#{name}/download.lock"
output = id_label_file_name(name)
# If there is no timpestamp file (input), update the pair anyway
file_older_than_stamp?(output, input)
end
# Return true (needs update) unless the output file exists and newer than the given timestamp file (if available)
def file_older_than_stamp?(file, stamp)
if File.exist?(file) && File.exist?(stamp) && File.mtime(file) > File.mtime(stamp)
$stderr.puts "# File #{file} is newer than #{stamp}" if $verbose
false
else
if File.exist?(stamp)
$stderr.puts "# File #{file} is older than #{stamp}" if $verbose
true
else
$stderr.puts "# File #{file} has no timestamp file" if $verbose
file_older_than_days?(file)
end
end
end
# Return true (needs update) when the file is older than $duration (or given) days
def file_older_than_days?(file, days = $duration)
if File.exist?(file)
age = (Time.now - File.mtime(file)) / (24*60*60)
$stderr.puts "# File #{file} is created #{age} days ago (will be updated when >#{days} days)" if $verbose
age > days
else
true
end
end
def validate_tsv_output(pair)
tsv = tsv_file_name(pair)
old = tsv_file_name_old(pair)
check = true
count = 0
if File.exist?(tsv) and File.exist?(old)
ratio = 1.0 * File.size(tsv) / File.size(old)
# New file is not smaller than a half of old file size
if ratio < $minratio
$stderr.puts "# Error: #{tsv} new file size per old #{File.size(tsv)} / #{File.size(old)} = #{ratio} < #{$minratio}" if $verbose
check = false
end
end
# Check if new TSV is valid (regardless of the previous TSV output exists or not)
if File.exist?(tsv) and File.size(tsv) > 0
head = `head -#{$chklines} #{tsv}`
tail = `tail -#{$chklines} #{tsv}`
[head, tail].each do |lines|
lines.each_line do |line|
line.strip!
if line[/^\S+\t\S+$/] # check ID tab ID
#$stderr.puts "# Pass: #{tsv} seems to be OK #{line}" if $verbose
# Do nothing
elsif line[/</] # check HTML tag
$stderr.puts "# Error: #{tsv} seems to contain HTML #{line}" if $verbose
check = false
elsif line.size == 0 # check empty line
count += 1
if count >= $maxblank
$stderr.puts "# Error: #{tsv} seems to contain >#{count} empty lines" if $verbose
check = false
end
else
$stderr.puts "# Error: #{tsv} seems to be malformed #{line}" if $verbose
check = false
end
end
end
else
$stderr.puts "# Error: Failed to create #{tsv} or created file was empty" if $verbose
check = false
end
return check
end
end
# Methods for preparation
module Prepare
# Entry point for preparation
def prepare_task(taskname)
case taskname
# when /#{OUTPUT_TSV_DIR}drugbank/
# return "prepare:drugbank"
when /#{OUTPUT_TSV_DIR}bioproject/
return "prepare:bioproject"
when /#{OUTPUT_TSV_DIR}biosample/
return "prepare:biosample"
when /#{OUTPUT_TSV_DIR}cellosaurus/
return "prepare:cellosaurus"
when /#{OUTPUT_TSV_DIR}clinvar/
return "prepare:clinvar"
when /#{OUTPUT_TSV_DIR}ensembl/
return "prepare:ensembl"
when /#{OUTPUT_TSV_DIR}glytoucan/
return "prepare:glytoucan"
when /#{OUTPUT_TSV_DIR}hgnc/
return "prepare:hgnc"
when /#{OUTPUT_TSV_DIR}hmdb/
return "prepare:hmdb"
when /#{OUTPUT_TSV_DIR}homologene/
return "prepare:homologene"
when /#{OUTPUT_TSV_DIR}hp_phenotype/
return "prepare:hp_phenotype"
when /#{OUTPUT_TSV_DIR}cog/
return "prepare:cog"
when /#{OUTPUT_TSV_DIR}interpro/
return "prepare:interpro"
when /#{OUTPUT_TSV_DIR}mgi_gene/
return "prepare:mgi_gene"
when /#{OUTPUT_TSV_DIR}mgi_genotype/
return "prepare:mgi_genotype"
when /#{OUTPUT_TSV_DIR}ncbigene/
return "prepare:ncbigene"
when /#{OUTPUT_TSV_DIR}oma_protein/
return "prepare:oma_protein"
when /#{OUTPUT_TSV_DIR}pmc/
return "prepare:pmc"
when /#{OUTPUT_TSV_DIR}prosite/
return "prepare:prosite"
when /#{OUTPUT_TSV_DIR}reactome/
return "prepare:reactome"
when /#{OUTPUT_TSV_DIR}refseq_protein/
return "prepare:refseq_protein"
when /#{OUTPUT_TSV_DIR}refseq_rna/
return "prepare:refseq_rna"
when /#{OUTPUT_TSV_DIR}rhea/
return "prepare:rhea"
when /#{OUTPUT_TSV_DIR}sra/
return "prepare:sra"
when /#{OUTPUT_TSV_DIR}swisslipids/
return "prepare:swisslipids"
when /#{OUTPUT_TSV_DIR}uniprot/
return "prepare:uniprot"
when /#{OUTPUT_TSV_DIR}taxonomy/
return "prepare:taxonomy"
else
File.open("input/update.txt", "w")
return "input/update.txt"
end
end
# Check if the file is updated or the sizes differ or the file doesn't exist
def update_input_file?(file, url)
if File.exist?(file)
# Both checks should be made as the local file can be newer than remote when the previous download fails
# and the local file can be smaller or size 0 even when it exists
check_remote_file_time(file, url) || check_remote_file_size(file, url)
else
true
end
end
# Create file lock to avoid simultaneous access
def download_lock(dir, &block)
$stderr.puts "# Checking lock file #{dir}/download.lock for download"
# File.open with "w" option immediately update the file's timestamp but "a" preserve the timestamp when not modified
File.open("#{dir}/download.lock", "a") do |lockfile|
if lockfile.flock(File::LOCK_EX)
# Each downloader needs to implement a block returning true when update procedure is executed (othewise false)
if yield block
$stderr.puts "# Overwriting timestamp of the #{dir}/download.lock"
lockfile.truncate(0)
lockfile.puts `date +%FT%T`
lockfile.flush
else
$stderr.puts "# Preserving timestamp of the #{dir}/download.lock"
end
end
end
end
# Download files with wget
def download_file(dir, url, glob = nil)
# When running Wget without -N, -nc, -r, or -p, downloading the same file in the same directory
# will result in the original copy of file being preserved and the second copy being named file.1
# The following opts are equivalent to "-q -r -np -nd -N"
#opts = "--quiet --recursive --no-parent --no-directories --timestamping"
# Certificate for reactome.org seems to be expired between 20211004 and 20211015
opts = "--quiet --recursive --no-parent --no-directories --timestamping --no-check-certificate"
# Also specify output directory by --directory-prefix (-P)
begin
if glob
sh "wget #{opts} --directory-prefix #{dir} --accept '#{glob}' #{url}"
else
sh "wget #{opts} --directory-prefix #{dir} #{url}"
end
rescue StandardError => e
$stderr.puts "Error: download_file(#{dir}, #{url}): #{e.message}"
end
end
# Return true (needs update) when the remote file size is different from the local one
def check_remote_file_size(file, url)
if File.exist?(file)
# The wget --timestamping (-N) option won't check the file size especially when
# previous download was interrupted and left broken files with newer dates.
local_file_size = File.size(file)
remote_file_size = `curl -sIL #{url} | grep -i '^content-length:' | tail -1 | awk '{print $2}'`.strip.to_i
$stderr.puts "# Local file size: #{local_file_size} (#{file})"
$stderr.puts "# Remote file size: #{remote_file_size} (#{url})"
if remote_file_size == 0
$stderr.puts "Error: Remote file is empty"
return false
end
return local_file_size != remote_file_size
else
return true
end
end
# Return true (needs update) when the remote file is newer than the local file
def check_remote_file_time(file, url)
if File.exist?(file)
local_file_time = File.mtime(file) # Time object
begin
remote_file_time = Time.parse(`curl -sIL #{url} | grep -i '^last-modified:' | tail -1 | sed -e 's/^[Ll]ast-[Mm]odified: //'`) # Time object
rescue ArgumentError => e
$stderr.puts "Error: check_remote_file_time(#{file}, #{url}): #{e.message}"
return false
end
$stderr.puts "# Local file time: #{local_file_time} (#{file})"
$stderr.puts "# Remote file time: #{remote_file_time} (#{url})"
return local_file_time < remote_file_time
else
return true
end
end
end
end
# Import defined methods to be used in the following rules/tasks (Rake's namespace doesn't separate methods from Object, unfortunatelly)
include TogoID::Main
include TogoID::Prepare
### Update/convert rules
# Note: Because of a quirk in Ruby syntax, parenthesis are required on rule when the first argument is a regular expression
# See https://ruby.github.io/rake/doc/rakefile_rdoc.html
# Dependency for TSV files (check dependency for preparation by target names)
rule(/#{OUTPUT_TSV_DIR}\S+\.tsv/ => [
OUTPUT_TSV_DIR,
method(:prepare_task)
]) do |t|
update_tsv(t.name)
$stderr.puts "Rule for TSV (#{t.name})"
$stderr.puts t.investigation if $verbose
end
# Dependency for TTL files (generate dependent filenames by pathmap notation)
rule(/#{OUTPUT_TTL_DIR}\S+\.ttl/ => [
OUTPUT_TTL_DIR,
"%{#{OUTPUT_TTL_DIR},#{OUTPUT_TSV_DIR}}X.tsv",
method(:update_ttl)
]) do |t|
$stderr.puts "Rule for TTL (#{t.name})"
$stderr.puts t.investigation if $verbose
end
# Dependency for ID and Label TTL files
rule(/#{OUTPUT_ID_LABEL_TTL_DIR}\S+\.ttl/ => [
OUTPUT_ID_LABEL_TTL_DIR,
method(:update_id_label)
]) do |t|
$stderr.puts "Rule for ID and Label TTL (#{t.name})"
$stderr.puts t.investigation if $verbose
end
### Preparatioin tasks
namespace :prepare do
desc "Prepare all"
task :all => [ :bioproject, :biosample, :cellosaurus, :clinvar, :ensembl, :glytoucan, :hmdb, :hgnc, :homologene, :hp_phenotype, :cog, :interpro, :mgi_gene, :mgi_genotype, :ncbigene, :oma_protein, :pmc, :prosite, :reactome, :refseq_protein, :refseq_rna, :rhea, :sra, :swisslipids, :uniprot, :taxonomy ]
directory INPUT_DRUGBANK_DIR = "input/drugbank"
directory INPUT_BIOPROJECT_DIR = "input/bioproject"
directory INPUT_BIOSAMPLE_DIR = "input/biosample"
directory INPUT_CELLOSAURUS_DIR = "input/cellosaurus"
directory INPUT_CLINVAR_DIR = "input/clinvar"
directory INPUT_ENSEMBL_DIR = "input/ensembl"
directory INPUT_HOMOLOGENE_DIR = "input/homologene"
directory INPUT_HP_PHENOTYPE_DIR = "input/hp_phenotype"
directory INPUT_COG_DIR = "input/cog"
directory INPUT_GLYTOUCAN_DIR = "input/glytoucan"
directory INPUT_HMDB_DIR = "input/hmdb"
directory INPUT_HGNC_DIR = "input/hgnc"
directory INPUT_INTERPRO_DIR = "input/interpro"
directory INPUT_MGI_GENE_DIR = "input/mgi_gene"
directory INPUT_MGI_GENOTYPE_DIR = "input/mgi_genotype"
directory INPUT_NCBIGENE_DIR = "input/ncbigene"
directory INPUT_OMA_PROTEIN_DIR = "input/oma_protein"
directory INPUT_PROSITE_DIR = "input/prosite"
directory INPUT_PMC_DIR = "input/pmc"
directory INPUT_REACTOME_DIR = "input/reactome"
directory INPUT_REFSEQ_PROTEIN_DIR = "input/refseq_protein"
directory INPUT_REFSEQ_RNA_DIR = "input/refseq_rna"
directory INPUT_RHEA_DIR = "input/rhea"
directory INPUT_SRA_DIR = "input/sra"
directory INPUT_SWISSLIPIDS_DIR = "input/swisslipids"
directory INPUT_UNIPROT_DIR = "input/uniprot"
directory INPUT_TAXONOMY_DIR = "input/taxonomy"
=begin
desc "Prepare required files for Drugbank"
task :drugbank => INPUT_DRUGBANK_DIR do
$stderr.puts "*** TODO ***: implement prepare:drugbank"
$stderr.puts "## Prepare input files for Drugbank"
download_lock(INPUT_DRUGBANK_DIR) do
updated = false
# https://go.drugbank.com/releases/5-1-8/downloads/all-full-database
updated
end
end
=end
desc "Prepare required files for BioProject"
task :bioproject => INPUT_BIOPROJECT_DIR do
$stderr.puts "## Prepare input files for BioProject"
download_lock(INPUT_BIOPROJECT_DIR) do
updated = false
input_file = "#{INPUT_BIOPROJECT_DIR}/bioproject.xml"
input_url = "https://ftp.ncbi.nlm.nih.gov/bioproject/bioproject.xml"
if update_input_file?(input_file, input_url)
download_file(INPUT_BIOPROJECT_DIR, input_url)
sh "python bin/bioproject_xml2tsv.py #{INPUT_BIOPROJECT_DIR}/bioproject.xml > #{INPUT_BIOPROJECT_DIR}/bioproject.tsv"
updated = true
end
input_file = "#{INPUT_BIOPROJECT_DIR}/bioproject2biosample.tsv"
input_url = "https://ddbj.nig.ac.jp/public/dblink/bioproject-biosample/bioproject2biosample.tsv"
if update_input_file?(input_file, input_url)
download_file(INPUT_BIOPROJECT_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for BioSample"
task :biosample => INPUT_BIOSAMPLE_DIR do
$stderr.puts "## Prepare input files for BioSample"
download_lock(INPUT_BIOSAMPLE_DIR) do
updated = false
input_file = "#{INPUT_BIOSAMPLE_DIR}/biosample_set.xml.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/biosample/biosample_set.xml.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_BIOSAMPLE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_BIOSAMPLE_DIR}/biosample_set.xml"
sh "python bin/biosample_xml2tsv.py #{INPUT_BIOSAMPLE_DIR}/biosample_set.xml > #{INPUT_BIOSAMPLE_DIR}/biosample_set.tsv"
updated = true
end
updated
end
end
desc "Prepare required files for Cellosaurus"
task :cellosaurus => INPUT_CELLOSAURUS_DIR do
$stderr.puts "## Prepare input files for Cellosaurus"
download_lock(INPUT_CELLOSAURUS_DIR) do
updated = false
input_file = "#{INPUT_CELLOSAURUS_DIR}/cellosaurus.txt"
input_url = "https://ftp.expasy.org/databases/cellosaurus/cellosaurus.txt"
if update_input_file?(input_file, input_url)
download_file(INPUT_CELLOSAURUS_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for ClinVar"
task :clinvar => INPUT_CLINVAR_DIR do
$stderr.puts "## Prepare input files for ClinVar"
download_lock(INPUT_CLINVAR_DIR) do
updated = false
input_file = "#{INPUT_CLINVAR_DIR}/variant_summary.txt.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/pub/clinvar/tab_delimited/variant_summary.txt.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_CLINVAR_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_CLINVAR_DIR}/variant_summary.txt"
updated = true
end
updated
end
end
desc "Prepare taxonomy ID list for Ensembl"
task :ensembl => INPUT_ENSEMBL_DIR do
$stderr.puts "## Prepare input files for Ensembl"
download_lock(INPUT_ENSEMBL_DIR) do
updated = false
taxonomy_file = "#{INPUT_ENSEMBL_DIR}/taxonomy.txt"
if file_older_than_days?(taxonomy_file)
sh "sparql_csv2tsv.sh #{INPUT_ENSEMBL_DIR}/taxonomy.rq https://rdfportal.org/ebi/sparql > #{taxonomy_file}"
updated = true
end
updated
end
end
desc "Prepare required files for GlyTouCan"
task :glytoucan => INPUT_GLYTOUCAN_DIR do
$stderr.puts "## Prepare input files for GlyTouCan"
download_lock(INPUT_GLYTOUCAN_DIR) do
updated = false
input_file = "#{INPUT_GLYTOUCAN_DIR}/glycosmos_ggdbs_pubmed.csv"
input_url = "https://glycosmos.org/download/glycosmos_ggdbs_pubmed.csv"
if update_input_file?(input_file, input_url)
download_file(INPUT_GLYTOUCAN_DIR, input_url)
sh "sparql_csv2tsv.sh bin/sparql/glycogene-uniprot.rq https://ts.glycosmos.org/sparql > #{INPUT_GLYTOUCAN_DIR}/glycogene-uniprot.tsv"
updated = true
end
updated
end
end
desc "Prepare required files for HGNC"
task :hgnc => INPUT_HGNC_DIR do
$stderr.puts "## Prepare input files for HGNC"
download_lock(INPUT_HGNC_DIR) do
updated = false
input_file = "#{INPUT_HGNC_DIR}/hgnc_complete_set.txt"
input_url = "https://storage.googleapis.com/public-download-files/hgnc/tsv/tsv/hgnc_complete_set.txt"
if update_input_file?(input_file, input_url)
download_file(INPUT_HGNC_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for HMDB"
task :hmdb => INPUT_HMDB_DIR do
$stderr.puts "## Prepare input files for HMDB"
download_lock(INPUT_HMDB_DIR) do
updated = false
input_file = "#{INPUT_HMDB_DIR}/hmdb_metabolites.zip"
input_url = "https://hmdb.ca/system/downloads/current/hmdb_metabolites.zip"
if update_input_file?(input_file, input_url)
download_file(INPUT_HMDB_DIR, input_url)
sh "unzip #{input_file} -d #{INPUT_HMDB_DIR}/"
sh "python bin/hmdb_xml2tsv_sax.py #{INPUT_HMDB_DIR}/hmdb_metabolites.xml > #{INPUT_HMDB_DIR}/hmdb_metabolites.tsv"
updated = true
end
updated
end
end
desc "Prepare required files for HomoloGene"
task :homologene => INPUT_HOMOLOGENE_DIR do
$stderr.puts "## Prepare input files for Homologene"
download_lock(INPUT_HOMOLOGENE_DIR) do
updated = false
input_file = "#{INPUT_HOMOLOGENE_DIR}/homologene.data"
input_url = "https://ftp.ncbi.nlm.nih.gov/pub/HomoloGene/current/homologene.data"
if update_input_file?(input_file, input_url)
download_file(INPUT_HOMOLOGENE_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for HP Phenotype"
task :hp_phenotype => INPUT_HP_PHENOTYPE_DIR do
$stderr.puts "## Prepare input files for HP Phenotype"
download_lock(INPUT_HP_PHENOTYPE_DIR) do
updated = false
input_file = "#{INPUT_HP_PHENOTYPE_DIR}/phenotype.hpoa"
input_url = "http://purl.obolibrary.org/obo/hp/hpoa/phenotype.hpoa"
if update_input_file?(input_file, input_url)
download_file(INPUT_HP_PHENOTYPE_DIR, input_url)
updated = true
end
input_file = "#{INPUT_HP_PHENOTYPE_DIR}/genes_to_phenotype.txt"
input_url = "http://purl.obolibrary.org/obo/hp/hpoa/genes_to_phenotype.txt"
if update_input_file?(input_file, input_url)
download_file(INPUT_HP_PHENOTYPE_DIR, input_url)
updated = true
end
sh "bin/sparql_csv2tsv.sh bin/sparql/hp_category.rq https://rdfportal.org/bioportal/sparql > #{INPUT_HP_PHENOTYPE_DIR}/hp_category.tsv"
updated
end
end
desc "Prepare required files for COG"
task :cog => INPUT_COG_DIR do
$stderr.puts "## Prepare input files for COG"
download_lock(INPUT_COG_DIR) do
updated = false
input_file = "#{INPUT_COG_DIR}/cog-20.cog.csv"
input_url = "https://ftp.ncbi.nlm.nih.gov/pub/COG/COG2020/data/cog-20.cog.csv"
if update_input_file?(input_file, input_url)
download_file(INPUT_COG_DIR, input_url)
updated = true
end
input_file = "#{INPUT_COG_DIR}/cog-20.def.tab"
input_url = "https://ftp.ncbi.nlm.nih.gov/pub/COG/COG2020/data/cog-20.def.tab"
if update_input_file?(input_file, input_url)
download_file(INPUT_COG_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for InterPro"
task :interpro => INPUT_INTERPRO_DIR do
$stderr.puts "## Prepare input files for InterPro"
download_lock(INPUT_INTERPRO_DIR) do
updated = false
input_file = "#{INPUT_INTERPRO_DIR}/interpro2go"
input_url = "https://ftp.ebi.ac.uk/pub/databases/interpro/current_release/interpro2go"
if update_input_file?(input_file, input_url)
download_file(INPUT_INTERPRO_DIR, input_url)
updated = true
end
input_file = "#{INPUT_INTERPRO_DIR}/protein2ipr.dat.gz"
input_url = "https://ftp.ebi.ac.uk/pub/databases/interpro/current_release/protein2ipr.dat.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_INTERPRO_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_INTERPRO_DIR}/protein2ipr.dat"
updated = true
end
input_file = "#{INPUT_INTERPRO_DIR}/interpro.xml.gz"
input_url = "https://ftp.ebi.ac.uk/pub/databases/interpro/current_release/interpro.xml.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_INTERPRO_DIR, input_url)
sh "gzip -dc #{input_file} | python bin/interpro_xml2tsv.py > #{INPUT_INTERPRO_DIR}/interpro.tsv"
updated = true
end
updated
end
end
desc "Prepare required files for MGI gene"
task :mgi_gene => INPUT_MGI_GENE_DIR do
$stderr.puts "## Prepare input files for MGI_GENE"
download_lock(INPUT_MGI_GENE_DIR) do
updated = false
filenames = ["MRK_List2.rpt",
"MGI_Gene_Model_Coord.rpt",
"MRK_SwissProt_TrEMBL.rpt",
"HGNC_AllianceHomology.rpt",
"MGI_PhenotypicAllele.rpt"]
filenames.each do |filename|
input_file = "#{INPUT_MGI_GENE_DIR}/#{filename}"
input_url = "https://www.informatics.jax.org/downloads/reports/#{filename}"
if update_input_file?(input_file, input_url)
download_file(INPUT_MGI_GENE_DIR, input_url)
updated = true
end
end
updated
end
end
desc "Prepare required files for MGI genotype"
task :mgi_genotype => INPUT_MGI_GENOTYPE_DIR do
$stderr.puts "## Prepare input files for MGI_GENOTYPE"
download_lock(INPUT_MGI_GENOTYPE_DIR) do
updated = false
filenames = ["MGI_DiseaseGeneModel.rpt"]
filenames.each do |filename|
input_file = "#{INPUT_MGI_GENOTYPE_DIR}/#{filename}"
input_url = "https://www.informatics.jax.org/downloads/reports/#{filename}"
if update_input_file?(input_file, input_url)
download_file(INPUT_MGI_GENOTYPE_DIR, input_url)
updated = true
end
end
if updated
sh "ruby bin/query_mousemine.rb > #{INPUT_MGI_GENOTYPE_DIR}/mousemine_genotype.tsv"
end
updated
end
end
desc "Prepare required files for NCBI Gene"
task :ncbigene=> INPUT_NCBIGENE_DIR do
$stderr.puts "## Prepare input files for NCBI Gene"
download_lock(INPUT_NCBIGENE_DIR) do
updated = false
input_file = "#{INPUT_NCBIGENE_DIR}/gene2refseq.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2refseq.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_NCBIGENE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_NCBIGENE_DIR}/gene2refseq"
updated = true
end
input_file = "#{INPUT_NCBIGENE_DIR}/gene2ensembl.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2ensembl.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_NCBIGENE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_NCBIGENE_DIR}/gene2ensembl"
updated = true
end
input_file = "#{INPUT_NCBIGENE_DIR}/gene2go.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/gene/DATA/gene2go.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_NCBIGENE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_NCBIGENE_DIR}/gene2go"
updated = true
end
input_file = "#{INPUT_NCBIGENE_DIR}/Homo_sapiens.gene_info.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/gene/DATA/GENE_INFO/Mammalia/Homo_sapiens.gene_info.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_NCBIGENE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_NCBIGENE_DIR}/Homo_sapiens.gene_info"
updated = true
end
input_file = "#{INPUT_NCBIGENE_DIR}/gene_info.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/gene/DATA/gene_info.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_NCBIGENE_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_NCBIGENE_DIR}/gene_info"
updated = true
end
updated
end
end
desc "Prepare required files for OMA protein"
task :oma_protein => INPUT_OMA_PROTEIN_DIR do
$stderr.puts "## Prepare input files for OMA protein"
download_lock(INPUT_OMA_PROTEIN_DIR) do
updated = false
filenames = ["oma-entrez.txt.gz",
"oma-species.txt",
"oma-uniprot.txt.gz"]
filenames.each do |filename|
input_file = "#{INPUT_OMA_PROTEIN_DIR}/#{filename}"
input_url = "https://omabrowser.org/All/#{filename}"
if update_input_file?(input_file, input_url)
download_file(INPUT_OMA_PROTEIN_DIR, input_url)
updated = true
end
end
if updated
sh "awk -F \"\t\" '$4==\"Ensembl\" && $5~/Ensembl (Vertebrates )?[0-9]+;/ && $1!=\"YEAST\"{print $2}' #{INPUT_OMA_PROTEIN_DIR}/oma-species.txt > #{INPUT_OMA_PROTEIN_DIR}/taxonomy.txt"
end
updated
end
end
desc "Prepare required files for PROSITE"
task :prosite => INPUT_PROSITE_DIR do
$stderr.puts "## Prepare input files for PROSITE"
download_lock(INPUT_PROSITE_DIR) do
updated = false
input_file = "#{INPUT_PROSITE_DIR}/prosite.dat"
input_url = "https://ftp.expasy.org/databases/prosite/prosite.dat"
if update_input_file?(input_file, input_url)
download_file(INPUT_PROSITE_DIR, input_url)
updated = true
end
input_file = "#{INPUT_PROSITE_DIR}/prorule.dat"
input_url = "https://ftp.expasy.org/databases/prosite/prorule.dat"
if update_input_file?(input_file, input_url)
download_file(INPUT_PROSITE_DIR, input_url)
updated = true
end
updated
end
end
desc "Prepare required files for PMC"
task :pmc => INPUT_PMC_DIR do
$stderr.puts "## Prepare input files for PMC"
download_lock(INPUT_PMC_DIR) do
updated = false
input_file = "#{INPUT_PMC_DIR}/PMC-ids.csv.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/pub/pmc/PMC-ids.csv.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_PMC_DIR, input_url)
sh "gzip -dc #{input_file} > #{INPUT_PMC_DIR}/PMC-ids.csv"
updated = true
end
updated
end
end
desc "Prepare required files for Reactome"
task :reactome => INPUT_REACTOME_DIR do
$stderr.puts "## Prepare input files for Reactome"
download_lock(INPUT_REACTOME_DIR) do
updated = false
files = [
"UniProt2ReactomeReactions.txt",
"ChEBI2ReactomeReactions.txt",
"Ensembl2ReactomeReactions.txt",
"miRBase2ReactomeReactions.txt",
"NCBI2ReactomeReactions.txt",
"GtoP2ReactomeReactions.txt",
"UniProt2Reactome_All_Levels.txt",
"ChEBI2Reactome_All_Levels.txt",
"Ensembl2Reactome_All_Levels.txt",
"miRBase2Reactome_All_Levels.txt",
"NCBI2Reactome_All_Levels.txt",
"GtoP2Reactome_All_Levels.txt"
]
for file in files do
input_file = "#{INPUT_REACTOME_DIR}/#{file}"
input_url = "https://reactome.org/download/current/#{file}"
if update_input_file?(input_file, input_url)
download_file(INPUT_REACTOME_DIR, input_url)
updated = true
end
end
updated
end
end
desc "Prepare required files for RefSeq RNA"
task :refseq_rna => INPUT_REFSEQ_RNA_DIR do
$stderr.puts "## Prepare input files for RefSeq RNA"
download_lock(INPUT_REFSEQ_RNA_DIR) do
updated = false
input_file = "#{INPUT_REFSEQ_RNA_DIR}/RELEASE_NUMBER"
input_url = "https://ftp.ncbi.nlm.nih.gov/refseq/release/RELEASE_NUMBER"
if update_input_file?(input_file, input_url)
# If the RELEASE_NUMBER file is updated, fetch it and then download required data.
download_file(INPUT_REFSEQ_RNA_DIR, input_url)
# The index number of the gbff files (e.g. '1000' of 'complete.1000.rna.gbff.gz') is not stable.
# To keep the input directory up-to-date, delete all previous files before downloading the current files.
sh "rm -f #{INPUT_REFSEQ_RNA_DIR}/complete.*.rna.gbff.gz"
# Unfortunately, NCBI http/https server won't accept wildcard or --accept option.
# However, NCBI ftp server is currently broken.. You've Been Warned.
# (It is reported that large files are contaminated by illegal bytes occationally)
input_file = "complete.*.rna.gbff.gz"
input_url = "ftp://ftp.ncbi.nlm.nih.gov:/refseq/release/complete/"
download_file(INPUT_REFSEQ_RNA_DIR, input_url, input_file)
# Parse the gbff files and output all relations to a single tsv.
# Each config extract columns from the tsv.
sh "gzip -dc #{INPUT_REFSEQ_RNA_DIR}/#{input_file} | parse_refseq_rna_gbff.pl --summary > #{INPUT_REFSEQ_RNA_DIR}/refseq_rna_summary.tsv"
updated = true
end
updated
end
end
desc "Prepare required files for RefSeq Protein"
task :refseq_protein => INPUT_REFSEQ_PROTEIN_DIR do
$stderr.puts "## Prepare input files for RefSeq Protein"
download_lock(INPUT_REFSEQ_PROTEIN_DIR) do
updated = false
input_file = "#{INPUT_REFSEQ_PROTEIN_DIR}/gene_refseq_uniprotkb_collab.gz"
input_url = "https://ftp.ncbi.nlm.nih.gov/refseq/uniprotkb/gene_refseq_uniprotkb_collab.gz"
if update_input_file?(input_file, input_url)
download_file(INPUT_REFSEQ_PROTEIN_DIR, input_url)
updated = true
end
updated
end
end