/
workflow.rb
773 lines (637 loc) · 23.1 KB
/
workflow.rb
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
require 'rbbt-util'
require 'rbbt/workflow'
require 'rbbt/sources/organism'
require 'rbbt/sources/uniprot'
Misc.add_libdir if __FILE__ == $0
require 'rbbt/sources/TFCheckpoint'
module TFCheckpoint
extend Workflow
input :organism, :string, "Organism code", "Hsa"
input :remove_unknown, :boolean, "Remove genes that cannot be identified", false
task :join => :tsv do |organism, remove_unknown|
join = TSV.setup({}, :key_field => "Associated Gene Name", :fields => [], :type => :double)
organism_short = organism.split("/").first
TFCheckpoint.root.glob("*").each do |file|
log :join_source, file
tsv = TSV.open file, :type => :double
fields = tsv.fields
db = File.basename(file)
tsv.fields = [db + ": ID used (#{fields.first})"] + fields[1..-1].collect{|f| [db, f] * ": "}
next unless tsv.namespace.include? organism_short
tsv = tsv.change_key "Associated Gene Name", :identifiers => Organism.identifiers(organism), :persist_identifiers => true unless tsv.key_field == "Associated Gene Name"
join = join.attach tsv, :complete => true, :fields => tsv.fields, :one2one => false
end
name2entrez = Organism.identifiers(organism).index :target => "Entrez Gene ID", :persist => true
name2entrez2 = UniProt.identifiers[organism_short].index :target => "Entrez Gene ID", :persist => true
adhoc = Rbbt.data.adhoc_entrez.tsv
join = join.add_field "Entrez Gene ID" do |k,values|
name2entrez[k] || name2entrez2[k] || adhoc[k]
end unless join.fields.include?("Entrez Gene ID")
name2uni = Organism.identifiers(organism).index :target => "UniProt/SwissProt Accession", :persist => true
name2uni2 = UniProt.identifiers[organism_short].index :target => "UniProt/SwissProt Accession", :persist => true
join = join.add_field "UniProt/SwissProt Accession" do |k,values|
name2uni[k] || name2uni2[k]
end
name2ensembl = Organism.identifiers(organism).index :target => "Ensembl Gene ID", :persist => true
uni2ensembl2 = UniProt.identifiers[organism_short].index :fields => ["UniProt/SwissProt Accession"], :target => "Ensembl Gene ID", :persist => true
join = join.add_field "Ensembl Gene ID" do |k,values|
if e = name2ensembl[k]
e
elsif u = name2uni2[k]
uni2ensembl2[u]
end
end unless join.fields.include?("Ensembl Gene ID")
join.namespace = organism_short
join = join.select("Ensembl Gene ID"){|i| i.first } if remove_unknown
new = join.annotate({})
uni2name = UniProt.identifiers[organism_short].index :target => "Associated Gene Name", :persist => true, :order => true
join.each do |name,list|
uni = list[-2].first
new_name = uni2name[uni]
next if new_name.nil?
if new[new_name].nil?
new[new_name] = list
else
current = new[new_name]
new_list = current.zip(list).collect do |v1,v2|
(v1 + v1).compact
res = [v1, v2].compact.reject{|v| v.empty? }.first
res || []
end
new[new_name] = new_list
end
end
new
end
dep :join, :organism => "Hsa/feb2021", :jobname => "Hsa"
dep :join, :organism => "Mmu", :jobname => "Mmu"
dep :join, :organism => "Rno", :jobname => "Rno"
task :all_orgs => :tsv do
hsa, rno, mmu = dependencies.collect do |dep|
tsv = dep.load
organism = tsv.namespace
tsv.add_field "Organism" do
organism
end
tsv.add_field "Entrez Taxa ID" do
Organism.entrez_taxids(organism).produce.read
end
end
hsa = hsa.attach rno, :complete => true, :fields => rno.fields
hsa = hsa.attach mmu, :complete => true, :fields => mmu.fields
hsa
end
dep :all_orgs
task :ortho => :tsv do
tsv = step(:all_orgs).load.to_double
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
orthologs = {}
TSV.traverse Rbbt.data["gene_orthologs"], :type => :array, :bar => true do |line|
taxid, gene, rel, otaxid, ogene = line.split("\t")
next unless taxas.include?(taxid) && taxas.include?(otaxid) && [taxid, otaxid].include?('9606')
if taxid == '9606'
taxid, otaxid = otaxid, taxid
gene, ogene = ogene, gene
end
source = [taxid, gene] * ":"
target = [otaxid, ogene] * ":"
orthologs[source] = []
orthologs[source] << target
end
new = tsv.annotate({})
TSV.traverse tsv, :into => new do |name, values|
organism, taxa, entrez = values.values_at "Organism", "Entrez Taxa ID", "Entrez Gene ID"
ortho_key = [taxa, entrez] * ":"
ovalues = orthologs[ortho_key]
res = []
res.extend MultipleResult
if taxa != '9606' && ovalues
ovalues.each do |k|
t, e = k.split(":")
name = entrez_index[e]
next if name.nil?
res << [name, values]
end
else
res << [name, values]
end
res
end
end
dep :all_orgs
task :ortho2 => :tsv do
tsv = step(:all_orgs).load.to_double
ensembl_index = tsv.index :fields => ["Ensembl Gene ID"]
new = tsv.annotate({})
organism_orthologs_all = {}
organism_orthologs_all["Hsa"] = {}
TSV.traverse tsv, :into => new do |name, values|
organism, ensembl = values.values_at "Organism", "Ensembl Gene ID"
organism = organism.first if Array === organism
organism_orthologs = organism_orthologs_all[organism] ||= Organism.ortholog_Hsa(organism).tsv
orthologs = organism_orthologs.values_at(*ensembl).compact.flatten
res = []
res.extend MultipleResult
if ! organism.include?("Hsa") && orthologs.any?
orthologs.each do |e|
name = ensembl_index[e]
next if name.nil?
res << [name, values]
end
else
res << [name, values]
end
res
end
end
dep :all_orgs
task :ortho3 => :tsv do
tsv = step(:all_orgs).load.to_double
ensembl_index = tsv.index :fields => ["Ensembl Gene ID"]
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
new = tsv.annotate({})
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
ortholog_entrez = {}
TSV.traverse Rbbt.data["gene_orthologs"], :type => :array, :bar => true do |line|
taxid, gene, rel, otaxid, ogene = line.split("\t")
next unless taxas.include?(taxid) && taxas.include?(otaxid) && [taxid, otaxid].include?('9606')
if taxid == '9606'
taxid, otaxid = otaxid, taxid
gene, ogene = ogene, gene
end
source = [taxid, gene] * ":"
target = [otaxid, ogene] * ":"
ortholog_entrez[source] = []
ortholog_entrez[source] << target
end
organism_orthologs_all = {}
organism_orthologs_all["Hsa"] = {}
TSV.traverse tsv, :into => new do |name, values|
organism, ensembl, taxa, entrez = values.values_at "Organism", "Ensembl Gene ID", "Entrez Taxa ID", "Entrez Gene ID"
organism = organism.first if Array === organism
organism_orthologs = organism_orthologs_all[organism] ||= Organism.ortholog_Hsa(organism).tsv
orthologs = organism_orthologs.values_at(*ensembl).compact.flatten
ortho_keys = Misc.zip_fields([taxa, entrez]).collect{|p| p * ":"}
ovalues = ortholog_entrez.values_at(*ortho_keys).flatten.uniq.compact
res = []
res.extend MultipleResult
if ! organism.include?("Hsa") && orthologs.any?
orthologs.each do |e|
name = ensembl_index[e]
next if name.nil?
res << [name, values]
end
elsif taxa != '9606' && ovalues.any?
ovalues.each do |k|
t, e = k.split(":")
name = entrez_index[e]
next if name.nil?
res << [name, values]
end
else
res << [name, values]
end
res
end
end
dep :all_orgs
task :ortho4 => :tsv do
tsv = step(:all_orgs).load.to_double
ensembl_index = tsv.index :fields => ["Ensembl Gene ID"]
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
new = tsv.annotate({})
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
ortholog_entrez = {}
TSV.traverse Rbbt.data["gene_orthologs"], :type => :array, :bar => true do |line|
taxid, gene, rel, otaxid, ogene = line.split("\t")
next unless taxas.include?(taxid) && taxas.include?(otaxid) && [taxid, otaxid].include?('9606')
if taxid == '9606'
taxid, otaxid = otaxid, taxid
gene, ogene = ogene, gene
end
source = [taxid, gene] * ":"
target = [otaxid, ogene] * ":"
ortholog_entrez[source] = []
ortholog_entrez[source] << target
end
organism_orthologs_all = {}
organism_orthologs_all["Hsa"] = {}
TSV.traverse tsv, :into => new do |name, values|
organism, ensembl, taxa, entrez = values.values_at "Organism", "Ensembl Gene ID", "Entrez Taxa ID", "Entrez Gene ID"
organism = organism.first if Array === organism
organism_orthologs = organism_orthologs_all[organism] ||= Organism.ortholog_Hsa(organism).tsv
orthologs = organism_orthologs.values_at(*ensembl).compact.flatten
ortho_keys = Misc.zip_fields([taxa, entrez]).collect{|p| p * ":"}
ovalues = ortholog_entrez.values_at(*ortho_keys).flatten.uniq.compact
res = []
res_ensembl = []
res_entrez = []
if ! organism.include?("Hsa") && orthologs.any?
orthologs.each do |e|
name = ensembl_index[e]
next if name.nil?
res_ensembl << [name, values]
end
elsif taxa != '9606' && ovalues.any?
ovalues.each do |k|
t, e = k.split(":")
name = entrez_index[e]
next if name.nil?
res_entrez << [name, values]
end
else
res << [name, values]
end
if res.empty?
if res_entrez.empty?
res = res_ensembl
elsif res_ensembl.empty?
res = res_entrez
else
if res_ensembl.first.downcase == name.downcase
res = res_ensembl
else
res = res_entrez
end
end
end
res.extend MultipleResult
res
end
end
dep :all_orgs
task :ortho5 => :tsv do
tsv = step(:all_orgs).load.to_double
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
orthologs = {}
mmu2hsa = Rbbt.data["human_mouse_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'mouse')
rno2hsa = Rbbt.data["human_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
rno2mmu = Rbbt.data["mouse_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
i = 0
new = tsv.annotate({})
field_pos = ["Organism", "Entrez Taxa ID", "Entrez Gene ID"].collect{|f| tsv.fields.index(f) }
TSV.traverse tsv, :into => new do |name, values|
res = []
res.extend MultipleResult
used_names = []
organismsl = values["Organism"]
entrezl = values["Entrez Gene ID"]
Misc.zip_fields([organismsl, entrezl]).each do |organism,entrez|
next if organism.nil?
case organism
when 'Mmu'
oe = (mmu2hsa[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
when 'Rno'
oe = (rno2hsa[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
oe = (rno2mmu[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
end
else
res << [name, values]
end
end
res.uniq!
res
end
end
dep :all_orgs
task :ortho6 => :tsv do
tsv = step(:all_orgs).load.to_double
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
orthologs = {}
mmu2hsa = {}
rno2hsa = {}
rno2mmu = {}
TSV.traverse Rbbt.data["orthodb_hsa_mmu_rno_orthologs_mammals_level.tsv"], :type => :array do |line|
hsa = []
rno = []
mmu = []
line.split("\t").each do |entry|
taxa, id = entry.partition("_")
case taxa
when "9606"
hsa << id
when "10116"
rno << id
when "10090"
mmu << id
else
raise "Unkown taxa: #{taxa}"
end
end
mmu.each do |source|
hsa.each do |target|
mmu2hsa[source] ||= []
mmu2hsa[source] << target
end
end
rno.each do |source|
hsa.each do |target|
rno2hsa[source] ||= []
rno2hsa[source] << target
end
mmu.each do |target|
rno2mmu[source] ||= []
rno2mmu[source] << target
end
end
end
#mmu2hsa = Rbbt.data["human_mouse_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'mouse')
#rno2hsa = Rbbt.data["human_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
#rno2mmu = Rbbt.data["mouse_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
manual_orthologs = TSV.xlsx(Rbbt.data["TFcheckpoint_orthologs_manual.xlsx"].find)
manual_orthologs.process "mouse NCBI_D" do |v|
v.first.to_i.to_s
end
manual_orthologs.process "rat NCBI_ID" do |v|
v.first.to_i.to_s
end
manual_orthologs_mmu = manual_orthologs.index :target => 'Ortholog', :fields => ["mouse NCBI_D"]
manual_orthologs_rno = manual_orthologs.index :target => 'Ortholog', :fields => ["rat NCBI_ID"]
i = 0
new = tsv.annotate({})
field_pos = ["Organism", "Entrez Taxa ID", "Entrez Gene ID"].collect{|f| tsv.fields.index(f) }
TSV.traverse tsv, :into => new do |name, values|
res = []
res.extend MultipleResult
used_names = []
organismsl = values["Organism"]
entrezl = values["Entrez Gene ID"]
Misc.zip_fields([organismsl, entrezl]).each do |organism,entrez|
next if organism.nil?
case organism
when 'Mmu'
oe = (mmu2hsa[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
new_oname = manual_orthologs_mmu[entrez]
onames << new_oname if new_oname
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
when 'Rno'
oe = (rno2hsa[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
new_oname = manual_orthologs_rno[entrez]
onames << new_oname if new_oname
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
oe = (rno2mmu[entrez] || []) - entrezl
onames = entrez_index.values_at(*oe)
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
end
else
res << [name, values]
end
end
res.uniq!
res
end
end
dep :all_orgs
task :ortho7 => :tsv do
tsv = step(:all_orgs).load.to_double
entrez_index = tsv.index :fields => ["Entrez Gene ID"]
taxas = tsv.column("Entrez Taxa ID").values.flatten.uniq.compact.sort
orthologs = {}
mmu2hsa = Rbbt.data["human_mouse_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'mouse')
rno2hsa = Rbbt.data["human_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
rno2mmu = Rbbt.data["mouse_rat_orthologs.tsv"].tsv(:header_hash => "", :merge => true, :type => :flat, :key_field => 'rat')
manual_orthologs = TSV.xlsx(Rbbt.data["TFcheckpoint_orthologs_manual.xlsx"].find)
manual_orthologs.process "mouse NCBI_D" do |v|
[v.first.to_i.to_s]
end
manual_orthologs.process "rat NCBI_ID" do |v|
[v.first.to_i.to_s]
end
manual_orthologs_mmu = manual_orthologs.index :target => 'Ortholog', :fields => ["mouse NCBI_D"]
manual_orthologs_rno = manual_orthologs.index :target => 'Ortholog', :fields => ["rat NCBI_ID"]
i = 0
new = tsv.annotate({})
field_pos = ["Organism", "Entrez Taxa ID", "Entrez Gene ID"].collect{|f| tsv.fields.index(f) }
TSV.traverse tsv, :into => new do |name, values|
res = []
res.extend MultipleResult
used_names = []
organismsl = values["Organism"]
entrezl = values["Entrez Gene ID"]
Misc.zip_fields([organismsl, entrezl]).each do |organism,entrez|
next if organism.nil?
case organism
when 'Mmu'
oe = (mmu2hsa[entrez] || []).uniq - entrezl
onames = entrez_index.values_at(*oe)
new_oname = manual_orthologs_mmu[entrez]
onames << new_oname if new_oname
onames.compact!
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
when 'Rno'
oe = (rno2hsa[entrez] || []).uniq - entrezl
onames = entrez_index.values_at(*oe)
new_oname = manual_orthologs_rno[entrez]
onames << new_oname if new_oname
onames.compact!
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
oe = (rno2mmu[entrez] || []).uniq - entrezl
onames = entrez_index.values_at(*oe)
if onames.any?
onames.each do |oname|
next if used_names.include? oname
used_names << oname
res << [oname, values]
end
else
res << [name, values]
end
end
else
res << [name, values]
end
end
res.uniq!
res
end
end
dep :ortho7
task :tidy => :tsv do
tsv = dependencies.first.load
fields = tsv.fields
new_fields =<<-EOF.split("\n")
Entrez Taxa ID
Entrez Gene ID
UniProt/SwissProt Accession
Ensembl Gene ID
EOF
go_fields = {}
fields.each do |f|
go, num, org = f.split("_")
next unless go == "go"
go_fields[num] ||= []
go_fields[num] << f
end
go_fields.each do |num,fields|
term = fields.select{|f| f.include? "TERM"}
evidence = fields.select{|f| f.include? "EVIDENCE"}
tsv = tsv.add_field "GO #{num} terms" do |k,values|
values.values_at(*term).flatten.compact
end
tsv = tsv.add_field "GO #{num} evidence" do |k,values|
values.values_at(*evidence).flatten.compact
end
end
fields = tsv.fields
ids = fields.select{|f| f.include? "ID used"}
go_fields = fields.select{|f| f=~/^GO /}
old_go_fields = fields.select{|f| f=~/^go_/}
features = fields.select{|f| f.include? ":"} - ids - go_fields - old_go_fields
rest = fields - new_fields - ids - features - go_fields - old_go_fields
tsv = tsv.reorder :key, new_fields + features + go_fields + ids
tsv.each do |k,v|
raise if v.flatten.include? nil
end
ids.sort.each do |field|
db = field.split(": ").first
next if db =~ /^go_/
tsv.add_field "#{db} present" do |k,values|
if values[field].any?
[db]
else
[]
end
end
end
tsv = tsv.reorder :key, tsv.fields - ids
# Remove GO duplicates
tsv.through do |k,values|
%w(140223 3700 3712 43565 6355 6357 981).each do |goid|
fterm = "GO #{goid} terms"
fevidence = "GO #{goid} evidence"
terms = values[fterm]
evidence = values[fevidence]
uniq = Misc.zip_fields([terms, evidence]).uniq
terms = uniq.collect{|p| p[0] }
evidence = uniq.collect{|p| p[1] }
values[fterm] = terms
values[fevidence] = evidence
end
id_fields = ["Entrez Taxa ID", "Entrez Gene ID", "UniProt/SwissProt Accession", "Ensembl Gene ID"]
id_values = values.values_at *id_fields
uniq_id_values = Misc.zip_fields(Misc.zip_fields(id_values).uniq)
id_fields.zip(uniq_id_values).each do |field, value|
values[field] = value
end
end
tsv
end
dep :tidy
task :filter_go => :tsv do
tsv = step(:tidy).load
present_fields = tsv.fields.select{|f| f.include?(" present") }
removed = []
tsv = tsv.select do |k,values|
present = values.values_at(*present_fields).flatten.compact
good = present.any? ||
values["GO 3712 terms"].any? ||
values["GO 3700 terms"].any? ||
values["GO 981 terms"].any? ||
(values["GO 43565 terms"].any? && (values["GO 6357 terms"].any?))
removed << k unless good
good
end
Open.write(file('removed'), removed * "\n")
tsv
end
dep :filter_go
task :process_go_fields => :tsv do
tsv = step(:filter_go).load
tsv.add_field "Has GO:0006355" do |k,values|
values["GO 6355 terms"].any? ? "Yes" : "No"
end
tsv.add_field "Has GO:0140223" do |k,values|
values["GO 140223 terms"].any? ? "Yes" : "No"
end
removed_go = ["GO 6355 terms", "GO 6355 evidence", "GO 140223 terms", "GO 140223 evidence"]
good_fields = tsv.fields - removed_go
tsv.slice good_fields
end
dep :process_go_fields
task :cleanup => :tsv do
tsv = dependencies.first.load
#tsv.select{|k,v| p = v["UniProt/SwissProt Accession"]; ! (p.nil? || p.empty?) }
tsv.select{|k,v| e = v["Entrez Gene ID"]; p = v["UniProt/SwissProt Accession"]; ! (e.nil? || e.empty?) && ! (p.nil? || p.empty?)}
end
task :orthologs => :tsv do
organisms = %w(Hsa Mmu Rno)
organisms.each do |target|
organisms.each do |source|
next if target == source
Organism.send("ortholog_#{target}", source).produce
end
end
end
end
#require 'TFCheckpoint/tasks/basic.rb'
#require 'rbbt/knowledge_base/TFCheckpoint'
#require 'rbbt/entity/TFCheckpoint'