-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_fabfile.py
759 lines (690 loc) · 30.2 KB
/
data_fabfile.py
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
"""Fabric deployment file to install genomic data on remote instances.
Designed to automatically download and manage biologically associated
data on cloud instances like Amazon EC2.
Fabric (http://docs.fabfile.org) is used to manage the automation of
a remote server.
Usage:
fab -i key_file -H servername -f data_fabfile.py install_data
"""
import os
import operator
from glob import glob
from contextlib import contextmanager
from fabric.api import *
from fabric.contrib.files import *
# -- bx-python bits
bx_script = '''#!/usr/bin/env python
__requires__ = 'bx-python'
import os, sys
sys.path.insert(0, '/mnt/galaxyTools/galaxy-central/lib')
from galaxy import eggs
import pkg_resources
pkg_resources.run_script('bx-python',os.path.basename(sys.argv[0]))
'''
twobit_length = '''#!/usr/bin/env python
import os, sys
sys.path.insert(0, '/mnt/galaxyTools/galaxy-central/lib')
from galaxy import eggs
import pkg_resources
pkg_resources.require('bx-python')
from bx.seq.twobit import TwoBitFile
build = os.path.basename(sys.argv[1]).replace('.2bit','')
lenfile = os.path.abspath(os.path.join(os.path.dirname(sys.argv[1]), '..', 'seq', '%s.len' % build))
if not os.path.exists(lenfile):
o = open(lenfile, 'w')
tb = TwoBitFile( open( sys.argv[1] ))
for key in tb:
o.write('%s %s\\n' % (key, len(tb[key])))
'''
# -- Host specific setup for various groups of servers.
env.remove_old_genomes = False
def amazon_ec2():
"""Setup for a ubuntu amazon ec2 share.
Need to pass in host and private key file on commandline:
-H hostname -i private_key_file
"""
env.user = 'ubuntu'
env.data_files = '/mnt/galaxyIndices'
env.galaxy_base = env.data_files + '/galaxy'
env.galaxy_tools = '/mnt/galaxyTools/tools'
env.shell = "/bin/bash -l -c"
def local_server():
"""Example setup for a local (non-cloud) server.
"""
env.user = 'chapman'
env.data_files = '/source/galaxy'
env.galaxy_base = os.path.join(env.data_files, 'web')
env.shell = "/usr/bin/zsh -l -i -c"
# -- Configuration for genomes to download and prepare
class _DownloadHelper:
def ucsc_name(self):
return None
def _exists(self, fname, seq_dir):
"""Check if a file exists in either download or final destination.
"""
return exists(fname) or exists(os.path.join(seq_dir, fname))
class UCSCGenome(_DownloadHelper):
def __init__(self, genome_name):
self._name = genome_name
self._url = "ftp://hgdownload.cse.ucsc.edu/goldenPath/%s/bigZips" % \
genome_name
def ucsc_name(self):
return self._name
def download(self, seq_dir):
for zipped_file in ["chromFa.tar.gz", "%s.fa.gz" % self._name,
"chromFa.zip"]:
if not self._exists(zipped_file, seq_dir):
with settings(warn_only=True):
result = run("wget %s/%s" % (self._url, zipped_file))
if not result.failed:
break
else:
break
genome_file = "%s.fa" % self._name
if not self._exists(genome_file, seq_dir):
if zipped_file.endswith(".tar.gz"):
run("tar -xzpf %s" % zipped_file)
elif zipped_file.endswith(".zip"):
run("unzip %s" % zipped_file)
elif zipped_file.endswith(".gz"):
run("gunzip -c %s > out.fa" % zipped_file)
else:
raise ValueError("Do not know how to handle: %s" % zipped_file)
tmp_file = genome_file.replace(".fa", ".txt")
with settings(warn_only=True):
result = run("ls *.fa")
# some UCSC downloads have the files in multiple directories
# mv them to the parent directory and delete the child directories
#ignore_random = " -a \! -name '*_random.fa' -a \! -name 'chrUn*'" \
# "-a \! -name '*hap*.fa'"
ignore_random = ""
if result.failed:
run("find . -name '*.fa'%s -exec mv {} . \;" % ignore_random)
run("find . -type d -a \! -name '\.' | xargs rm -rf")
result = run("find . -name '*.fa'%s" % ignore_random)
result = result.split("\n")
result.sort()
run("cat %s > %s" % (" ".join(result), tmp_file))
run("rm -f *.fa")
run("mv %s %s" % (tmp_file, genome_file))
return genome_file, [zipped_file]
class NCBIRest(_DownloadHelper):
"""Retrieve files using the TogoWS REST server pointed at NCBI.
"""
def __init__(self, name, refs):
self._name = name
self._refs = refs
self._base_url = "http://togows.dbcls.jp/entry/ncbi-nucleotide/%s.fasta"
def download(self, seq_dir):
genome_file = "%s.fa" % self._name
if not self._exists(genome_file, seq_dir):
for ref in self._refs:
run("wget %s" % (self._base_url % ref))
run("ls -l")
run("sed -i.bak -r -e '/1/ s/^>.*$/>%s/g' %s.fasta" % (ref,
ref))
# sed in Fabric does not cd properly?
#sed('%s.fasta' % ref, '^>.*$', '>%s' % ref, '1')
tmp_file = genome_file.replace(".fa", ".txt")
run("cat *.fasta > %s" % tmp_file)
run("rm -f *.fasta")
run("rm -f *.bak")
run("mv %s %s" % (tmp_file, genome_file))
return genome_file, []
class EnsemblGenome(_DownloadHelper):
"""Retrieve genome FASTA files from Ensembl.
ftp://ftp.ensemblgenomes.org/pub/plants/release-3/fasta/arabidopsis_thaliana/dna/Arabidopsis_thaliana.TAIR9.55.dna.toplevel.fa.gz
ftp://ftp.ensembl.org/pub/release-56/fasta/caenorhabditis_elegans/dna/Caenorhabditis_elegans.WS200.56.dna.toplevel.fa.gz
"""
def __init__(self, ensembl_section, release_number, release2, organism,
name, convert_to_ucsc=False):
if ensembl_section == "standard":
url = "ftp://ftp.ensembl.org/pub/"
else:
url = "ftp://ftp.ensemblgenomes.org/pub/%s/" % ensembl_section
url += "release-%s/fasta/%s/dna/" % (release_number, organism.lower())
self._url = url
release2 = ".%s" % release2 if release2 else ""
self._get_file = "%s.%s%s.dna.toplevel.fa.gz" % (organism, name,
release2)
self._name = name
self._convert_to_ucsc = convert_to_ucsc
def download(self, seq_dir):
genome_file = "%s.fa" % self._name
if not self._exists(self._get_file, seq_dir):
run("wget %s%s" % (self._url, self._get_file))
if not self._exists(genome_file, seq_dir):
run("gunzip -c %s > %s" % (self._get_file, genome_file))
if self._convert_to_ucsc:
#run("sed s/ / /g %s" % genome_file)
raise NotImplementedError("Replace with chr")
return genome_file, [self._get_file]
class BroadGenome(_DownloadHelper):
"""Retrieve genomes organized and sorted by Broad for use with GATK.
"""
def __init__(self, target_fasta):
self._target = target_fasta
self._ftp_url = "ftp://ftp.broadinstitute.org/pub/seq/references/"
def download(self, seq_dir):
if not self._exists(self._target, seq_dir):
run("wget %s/%s" % (self._ftp_url, self._target))
return self._target, []
genomes = [
("Amellifera_Honeybee", "apiMel3", UCSCGenome("apiMel3")),
("Athaliana", "araTha_tair9", EnsemblGenome("plants", "3", "55",
"Arabidopsis_thaliana", "TAIR9")),
("Btaurus", "bosTau4", UCSCGenome("bosTau4")),
("Celegans", "ce6", UCSCGenome("ce6")),
("Celegans", "WS200", EnsemblGenome("standard", "56", "56",
"Caenorhabditis_elegans", "WS200")),
("Cfamiliaris_Dog", "canFam2", UCSCGenome("canFam2")),
("Drerio_Zebrafish", "danRer6", UCSCGenome("danRer6")),
("Dmelanogaster", "dm3", UCSCGenome("dm3")),
("Ecoli", "eschColi_K12", NCBIRest("eschColi_K12", ["U00096.2"])),
("Fcatus_Cat", "felCat3", UCSCGenome("felCat3")),
("Ggallus_Chicken", "galGal3", UCSCGenome("galGal3")),
("Hsapiens", "hg18", UCSCGenome("hg18")),
#("Hsapiens", "hg18-broad", BroadGenome("Homo_sapiens_assembly18.fasta")),
#("Hsapiens", "GRCh39", BroadGenome("Homo_sapiens_assembly19.fasta")),
("Mmulatta", "rheMac2", UCSCGenome("rheMac2")),
("Mmusculus", "mm8", UCSCGenome("mm8")),
("Mmusculus", "mm9", UCSCGenome("mm9")),
("Msmegmatis", "92", NCBIRest("92", ["NC_008596.1"])),
("Mtuberculosis_H37Rv", "mycoTube_H37RV", NCBIRest("mycoTube_H37RV",
["NC_000962"])),
("Paeruginosa_UCBPP-PA14", "386", NCBIRest("386", ["CP000438.1"])),
("phiX174", "phix", NCBIRest("phix", ["NC_001422.1"])),
("Rnorvegicus", "rn4", UCSCGenome("rn4")),
("Scerevisiae", "sacCer2", UCSCGenome("sacCer2")),
("Spurpuratus", "strPur2", UCSCGenome("strPur2")),
("Sscrofa", "susScr2", UCSCGenome("susScr2")),
("Sscrofa", "Scrofa9.58", EnsemblGenome("standard", "58", "58",
"Sus_scrofa", "Sscrofa9")),
# No (or incomplete) PerM indexes in the current snapshot
("Tguttata", "taeGut1", UCSCGenome("taeGut1")),
("Xtropicalis", "xenTro2", UCSCGenome("xenTro2")),
("Ecaballus_Horse", "equCab2", UCSCGenome("equCab2")),
("Drerio_Zebrafish", "danRer5", UCSCGenome("danRer5")),
("Hsapiens", "hg19", UCSCGenome("hg19")),
]
lift_over_genomes = [g.ucsc_name() for (_, _, g) in genomes if g.ucsc_name()]
#lift_over_genomes = ['hg18', 'hg19', 'mm9', 'xenTro2', 'rn4']
# -- Fabric instructions
def install_data():
"""Main entry point for installing useful biological data.
"""
amazon_ec2()
#local_server()
_setup_bxpy()
#_data_uniref()
_data_ngs_genomes()
_data_liftover()
# NOTE: The pairwise method is incomplete (see below)
#_data_pairwise()
# == Decorators and context managers
def _if_installed(pname):
"""Run if the given program name is installed.
"""
def argcatcher(func):
def decorator(*args, **kwargs):
with settings(
hide('warnings', 'running', 'stdout', 'stderr'),
warn_only=True):
result = run(pname)
if result.return_code not in [127]:
return func(*args, **kwargs)
return decorator
return argcatcher
@contextmanager
def _make_tmp_dir():
work_dir = os.path.join(env.data_files, "tmp")
if not exists(work_dir):
run("mkdir %s" % work_dir)
yield work_dir
if exists(work_dir):
run("rm -rf %s" % work_dir)
# == NGS
def _data_ngs_genomes():
"""Download and create index files for next generation genomes.
"""
genome_dir = os.path.join(env.data_files, "genomes")
if not exists(genome_dir):
run('mkdir %s' % genome_dir)
for organism, genome, manager in genomes:
cur_dir = os.path.join(genome_dir, organism, genome)
if not exists(cur_dir):
run('mkdir -p %s' % cur_dir)
with cd(cur_dir):
if env.remove_old_genomes:
_clean_genome_directory()
seq_dir = 'seq'
ref_file, base_zips = manager.download(seq_dir)
ref_file = _move_seq_files(ref_file, base_zips, seq_dir)
bwa_index = _index_bwa(ref_file)
bowtie_index = _index_bowtie(ref_file)
bowtie_color_index = _index_bowtie_color(ref_file)
#maq_index = _index_maq(ref_file)
#_index_novoalign(ref_file)
perm_base_index = _index_perm(ref_file)
perm_color_index = _index_perm(ref_file, color=True)
twobit_index = _index_twobit(ref_file)
_chrom_length(ref_file)
#_index_eland(ref_file)
# other indexers not supported by default
if False:
bfast_index = _index_bfast(ref_file)
arachne_index = _index_arachne(ref_file)
with cd(seq_dir):
sam_index = _index_sam(ref_file)
# srma needs the sam index.
srma_index = _index_srma(ref_file)
for ref_index_file, cur_index, prefix, new_style in [
("sam_fa_indices.loc", sam_index, "index", False),
("srma_index.loc", srma_index, "", True),
("alignseq.loc", twobit_index, "seq", False),
("twobit.loc", twobit_index, "", False),
("bowtie_indices.loc", bowtie_index, "", True),
("bowtie_indices_color.loc", bowtie_color_index, "", True),
("bwa_index.loc", bwa_index, "", True),
("lastz_seqs.loc", twobit_index, "", True),
]:
if cur_index:
str_parts = [genome, os.path.join(cur_dir, cur_index)]
if new_style:
str_parts = [genome, genome] + str_parts
if prefix:
str_parts.insert(0, prefix)
_update_loc_file(ref_index_file, str_parts)
for ref_index_file, cur_index in [
("perm_base_index.loc", perm_base_index),
("perm_color_index.loc", perm_color_index),
]:
if cur_index:
for str_parts in [[e[0], e[0], os.path.join(cur_dir, e[1])] for e in cur_index]:
_update_loc_file(ref_index_file, str_parts)
def _clean_genome_directory():
"""Remove any existing sequence information in the current directory.
"""
remove = ["arachne", "bowtie", "bowtie_color", "bwa", "eland", "maq", "seq", "ucsc"]
for dirname in remove:
if exists(dirname):
run("rm -rf %s" % dirname)
def _move_seq_files(ref_file, base_zips, seq_dir):
if not exists(seq_dir):
run('mkdir %s' % seq_dir)
for move_file in [ref_file] + base_zips:
if exists(move_file):
run("mv %s %s" % (move_file, seq_dir))
path, fname = os.path.split(ref_file)
moved_ref = os.path.join(path, seq_dir, fname)
assert exists(moved_ref), moved_ref
return moved_ref
def _update_loc_file(ref_file, line_parts):
"""Add a reference to the given genome to the base index file.
"""
if env.galaxy_base is not None:
tools_dir = os.path.join(env.galaxy_base, "tool-data")
if not exists(tools_dir):
conf_file = "tool_data_table_conf.xml"
run("mkdir -p %s" % tools_dir)
put(os.path.join("installed_files", conf_file),
os.path.join(env.galaxy_base, conf_file))
add_str = "\t".join(line_parts)
with cd(tools_dir):
if not exists(ref_file):
run("touch %s" % ref_file)
if not contains(add_str, ref_file):
append(add_str, ref_file)
@_if_installed("faToTwoBit")
def _index_twobit(ref_file):
"""Index reference files using 2bit for random access.
"""
dir_name = "ucsc"
ref_base = os.path.splitext(os.path.split(ref_file)[-1])[0]
out_file = "%s.2bit" % ref_base
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
if not exists(out_file):
run("faToTwoBit %s %s" % (os.path.join(os.pardir, ref_file),
out_file))
return os.path.join(dir_name, out_file)
def _chrom_length(ref_file):
"""Calculate chromosome lengths for converting axt to maf"""
dir_name = "ucsc"
ref_base = os.path.splitext(os.path.split(ref_file)[-1])[0]
out_file = "%s.2bit" % ref_base
if not exists(os.path.join('seq', ref_base + '.len')):
run("/tmp/twobit_length.py %s" % os.path.join(dir_name, out_file))
def _index_bowtie(ref_file):
dir_name = "bowtie"
ref_base = os.path.splitext(os.path.split(ref_file)[-1])[0]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("bowtie-build -f %s %s" % (
os.path.join(os.pardir, ref_file),
ref_base))
return os.path.join(dir_name, ref_base)
def _index_bowtie_color(ref_file):
dir_name = "bowtie_color"
ref_base = os.path.splitext(os.path.split(ref_file)[-1])[0]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("bowtie-build -C -f %s %s" % (
os.path.join(os.pardir, ref_file),
ref_base))
return os.path.join(dir_name, ref_base)
def _index_bwa(ref_file):
dir_name = "bwa"
local_ref = os.path.split(ref_file)[-1]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
with settings(warn_only=True):
result = run("bwa index -a bwtsw %s" % local_ref)
# work around a bug in bwa indexing for small files
if result.failed:
run("bwa index %s" % local_ref)
run("rm -f %s" % local_ref)
return os.path.join(dir_name, local_ref)
def _index_perm(ref_file, color=False):
if color:
space = "color"
fastq = "csfastq"
else:
space = "base"
fastq = "fastq"
dir_name = "perm_%s" % space
local_ref = os.path.split(ref_file)[-1]
genome = local_ref.replace('.fa','')
read_length = 50
rval = []
if not exists(dir_name):
pass # don't actually index, just add existing indexes to the location file
'''
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
for seed in [ 'F3', 'F4' ]:
index = "%s_%s_%s_%s.index" % (genome, space, seed, read_length)
run("%s/perm/default/PerM %s %s --readFormat %s --seed %s -m -s %s" \
% (env.galaxy_tools, local_ref, read_length, fastq, seed, index))
rval.append(('%s_%s_%s' % (genome, seed, read_length), os.path.join(dir_name, index)))
'''
else:
for seed in [ 'F3', 'F4' ]:
index = "%s_%s_%s_%s.index" % (genome, space, seed, read_length)
if exists(os.path.join(dir_name, index)):
rval.append(('%s_%s_%s' % (genome, seed, read_length), os.path.join(dir_name, index)))
return rval
def _index_maq(ref_file):
dir_name = "maq"
local_ref = os.path.split(ref_file)[-1]
binary_out = "%s.bfa" % os.path.splitext(local_ref)[0]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
run("maq fasta2bfa %s %s" % (local_ref,
binary_out))
return os.path.join(dir_name, binary_out)
def _index_srma(ref_file):
dir_name = "srma"
local_ref = os.path.split(ref_file)[-1]
genome = local_ref.replace('.fa','')
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
run("ln -s %s.fai" % os.path.join(os.pardir, ref_file))
run("java -cp %s/srma/default/srma.jar net.sf.picard.sam.CreateSequenceDictionary R=%s O=%s/%s.dict URI=%s.fa" \
% (env.galaxy_tools, local_ref, os.curdir, genome, local_ref))
return os.path.join(dir_name, local_ref)
@_if_installed("novoindex")
def _index_novoalign(ref_file):
dir_name = "novoalign"
index_name = os.path.splitext(os.path.basename(ref_file))[0]
ref_file = os.path.join(os.pardir, ref_file)
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("novoindex %s %s" % (index_name, ref_file))
_index_novoalign_cs(ref_file, dir_name)
@_if_installed("novoalignCS")
def _index_novoalign_cs(ref_file, dir_name):
color_dir = os.path.join(dir_name, "colorspace")
ref_file = os.path.join(os.pardir, ref_file)
if not exists(color_dir):
run("mkdir %s" % color_dir)
with cd(color_dir):
run("novoindex -c %s %s" % (index_name, ref_file))
def _index_sam(ref_file):
(_, local_file) = os.path.split(ref_file)
if not exists("%s.fai" % local_file):
run("samtools faidx %s" % local_file)
return ref_file
@_if_installed("MakeLookupTable")
def _index_arachne(ref_file):
"""Index for Broad's Arachne aligner.
"""
dir_name = "arachne"
ref_base = os.path.splitext(os.path.split(ref_file)[-1])[0]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
ref_file = os.path.split(ref_file)[-1]
run("MakeLookupTable SOURCE=%s OUT_HEAD=%s" % (ref_file,
ref_base))
run("fastaHeaderSizes FASTA=%s HEADER_SIZES=%s.headerSizes" %
(ref_file, ref_file))
#run("rm -f %s" % ref_file)
return os.path.join(dir_name, ref_base)
@_if_installed("squashGenome")
def _index_eland(ref_file):
"""Index for Solexa's Eland aligner.
This is nasty since Eland will choke on large files like the mm9 and h18
genomes. It also has a restriction on only having 24 larger reference
files per directory. This indexes files with lots of shorter sequences (like
xenopus) as one file, and splits up other files, removing random and other
associated chromosomes to avoid going over the 24 file limit.
"""
dir_name = "eland"
if not exists(dir_name):
run("mkdir %s" % dir_name)
num_refs = run("grep '^>' %s | wc -l" % ref_file)
# For a lot of reference sequences, Eland needs them in 1 file
if int(num_refs) > 239:
run("squashGenome %s %s" % (dir_name, ref_file))
# For large reference sequences, squash fails and need them split up
else:
tmp_dir = "tmp_seqparts"
run("mkdir %s" % tmp_dir)
run("seqretsplit -sequence %s -osdirectory2 %s -outseq ." %
(ref_file, tmp_dir))
with cd(tmp_dir):
result = run("ls *.fasta")
result = result.split("\n")
seq_files = [os.path.join(tmp_dir, f) for f in result]
run("squashGenome %s %s" % (dir_name, " ".join(seq_files)))
run("rm -rf %s" % tmp_dir)
# Eland can only handle up to 24 reference files in a directory
# If we have more, remove any with *random* in the name to get
# below. This sucks, but seemingly no way around it because
# Eland will choke on large reference files
if int(num_refs) > 24:
with cd(dir_name):
for remove_re in ["*random*", "*_hap*", "chrun_*"]:
with settings(warn_only=True):
run("rm -f %s" % remove_re)
new_count = run("ls | wc -l")
# Human is still too big, need to remove chromosome M
if int(new_count) // 2 > 24:
with settings(warn_only=True):
run("rm -f chrm*")
# == Liftover files
def _data_liftover():
"""Download chain files for running liftOver.
Does not install liftOver binaries automatically.
"""
lo_dir = os.path.join(env.data_files, "liftOver")
if not exists(lo_dir):
run("mkdir %s" % lo_dir)
lo_base_url = "ftp://hgdownload.cse.ucsc.edu/goldenPath/%s/liftOver/%s"
lo_base_file = "%sTo%s.over.chain.gz"
for g1 in lift_over_genomes:
for g2 in [g for g in lift_over_genomes if g != g1]:
g2u = g2[0].upper() + g2[1:]
cur_file = lo_base_file % (g1, g2u)
non_zip = os.path.splitext(cur_file)[0]
worked = False
with cd(lo_dir):
if not exists(non_zip):
pass # don't actually fetch
'''
with settings(warn_only=True):
result = run("wget %s" % (lo_base_url % (g1, cur_file)))
# Lift over back and forths don't always exist
# Only move forward if we found the file
if not result.failed:
worked = True
run("gunzip %s" % cur_file)
'''
else:
worked = True
if worked:
ref_parts = [g1, g2, os.path.join(lo_dir, non_zip)]
_update_loc_file("liftOver.loc", ref_parts)
# == Set up bx_python scripts
def _setup_bxpy():
"""Setup bx-python scripts."""
open('/tmp/bx_script.py', 'w').write( bx_script )
put('/tmp/bx_script.py', '/tmp/bx_script.py', mode=0755)
for script in ( 'axt_to_maf', 'maf_build_index' ):
run('ln -sf bx_script.py /tmp/%s.py' % script)
open('/tmp/twobit_length.py', 'w').write( twobit_length )
put('/tmp/twobit_length.py', '/tmp/twobit_length.py', mode=0755)
# == Pairwise alignments
def _data_pairwise():
genome_dir = os.path.join(env.data_files, "genomes")
for organism, genome, manager in [(o, g, m) for (o, g, m) in genomes if m.ucsc_name()]:
cur_dir = os.path.join(genome_dir, organism, genome)
with cd(cur_dir):
_fetch_pairwise(manager.ucsc_name())
def _fetch_pairwise(ucsc_name):
"""Download pairwise alignments."""
dir_name = "pairwise"
base_url = "rsync://hgdownload.cse.ucsc.edu/goldenPath/%s/" % ucsc_name
if not exists(dir_name):
run("mkdir %s" % dir_name)
with settings(warn_only=True):
result = run("rsync %s" % base_url)
if not result.failed:
available = []
for line in result.split('\n'):
if line.split()[-1].startswith('vs'):
vs_name = line.split()[-1].replace('vs', '', 1)
vs_name = vs_name[0].lower() + vs_name[1:]
available.append( vs_name )
for org, gen, name in [(o, g, m.ucsc_name()) for (o, g, m) in genomes if m.ucsc_name()]:
subdir_name = os.path.join(dir_name, name)
if name in available and not exists(subdir_name):
url = base_url + 'vs' + name[0].upper() + name[1:] + '/axtNet'
with settings(warn_only=True):
result = run("rsync %s" % url)
if not result.failed:
run("mkdir %s" % subdir_name)
with cd(subdir_name):
url += '/'
run("rsync -avP %s ." % url)
run("gunzip *.gz")
for axt in glob("*.axt"):
maf = axt.replace('.axt','.maf')
b1_length = os.path.join('..', 'seq', '%s.len' % ucsc_name)
b2_length = os.path.join('..', '..', '..', org, gen, 'seq', '%s.len' % name)
run("/tmp/axt_to_maf.py %s:%s %s:%s < %s > %s" % (ucsc_name, b1_length, name, b2_length, axt, maf))
# TODO: finish writing this method...
# == UniRef
def _data_uniref():
"""Retrieve and index UniRef databases for protein searches.
http://www.ebi.ac.uk/uniref/
These are currently indexed for FASTA searches. Are other indexes desired?
Should this be separated out and organized by program like genome data?
This should also check the release note and automatically download and
replace older versions.
"""
site = "ftp://ftp.uniprot.org"
base_url = site + "/pub/databases/uniprot/" \
"current_release/uniref/%s/%s"
for uniref_db in ["uniref50", "uniref90", "uniref100"]:
work_dir = os.path.join(env.data_files, "uniref", uniref_db)
if not exists(work_dir):
run("mkdir -p %s" % work_dir)
base_work_url = base_url % (uniref_db, uniref_db)
fasta_url = base_work_url + ".fasta.gz"
base_file = os.path.splitext(os.path.basename(fasta_url))[0]
with cd(work_dir):
if not exists(base_file):
run("wget -c %s" % fasta_url)
run("gunzip %s" % os.path.basename(fasta_url))
run("wget %s" % (base_work_url + ".release_note"))
_index_blast_db(work_dir, base_file, "prot")
def _index_blast_db(work_dir, base_file, db_type):
"""Index a database using blast+ for similary searching.
"""
type_to_ext = dict(prot = ("phr", "pal"), nucl = ("nhr", "nal"))
db_name = os.path.splitext(base_file)[0]
with cd(work_dir):
if not reduce(operator.or_,
(exists("%s.%s" % (db_name, ext)) for ext in type_to_ext[db_type])):
run("makeblastdb -in %s -dbtype %s -out %s" %
(base_file, db_type, db_name))
# == Not used -- takes up too much space and time to index
def _index_bfast(ref_file):
"""Indexes bfast in color and nucleotide space for longer reads.
This preps for 40+bp sized reads, which is bfast's strength.
"""
dir_name = "bfast"
window_size = 14
bfast_nt_masks = [
"1111111111111111111111",
"1111101110111010100101011011111",
"1011110101101001011000011010001111111",
"10111001101001100100111101010001011111",
"11111011011101111011111111",
"111111100101001000101111101110111",
"11110101110010100010101101010111111",
"111101101011011001100000101101001011101",
"1111011010001000110101100101100110100111",
"1111010010110110101110010110111011",
]
bfast_color_masks = [
"1111111111111111111111",
"111110100111110011111111111",
"10111111011001100011111000111111",
"1111111100101111000001100011111011",
"111111110001111110011111111",
"11111011010011000011000110011111111",
"1111111111110011101111111",
"111011000011111111001111011111",
"1110110001011010011100101111101111",
"111111001000110001011100110001100011111",
]
local_ref = os.path.split(ref_file)[-1]
if not exists(dir_name):
run("mkdir %s" % dir_name)
with cd(dir_name):
run("ln -s %s" % os.path.join(os.pardir, ref_file))
# nucleotide space
run("bfast fasta2brg -f %s -A 0" % local_ref)
for i, mask in enumerate(bfast_nt_masks):
run("bfast index -d 1 -n 4 -f %s -A 0 -m %s -w %s -i %s" %
(local_ref, mask, window_size, i + 1))
# colorspace
run("bfast fasta2brg -f %s -A 1" % local_ref)
for i, mask in enumerate(bfast_color_masks):
run("bfast index -d 1 -n 4 -f %s -A 1 -m %s -w %s -i %s" %
(local_ref, mask, window_size, i + 1))