Skip to content

Commit ab1f1c9

Browse files
committed
fixed gc background
1 parent f11e2c7 commit ab1f1c9

5 files changed

Lines changed: 514 additions & 463 deletions

File tree

gimmemotifs/background.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,16 @@ def _weighted_random(self, l):
177177

178178
def matched_gc_bedfile(bedfile, matchfile, genome, number):
179179
N_FRACTION = 0.1
180-
genome_size = "/data/genomes/{0}/bwa/{0}.fa.sizes".format(genome)
181-
genome_fa = "/data/genomes/{0}/bwa/{0}.fa".format(genome)
180+
181+
config = MotifConfig()
182+
index = os.path.join(config.get_index_dir(), genome)
183+
184+
genome_size = os.path.join(index, "genome.size")
185+
genome_fa = os.path.join(index, "genome.fa")
186+
187+
if not os.path.exists(genome_size) or not os.path.exists(genome_fa):
188+
raise RuntimeError, "genome files not found, please re-index {} " \
189+
"with a recent version of gimme index".format(genome)
182190

183191
try:
184192
fa = Fasta(matchfile)
@@ -222,7 +230,7 @@ def matched_gc_bedfile(bedfile, matchfile, genome, number):
222230
#sys.stderr.write("{}\n".format(number))
223231

224232
r = rnd.random(l=length, n=number * 15, g=genome_size).nucleotide_content(fi=genome_fa)
225-
#sys.stderr.write("Retrieving\n")
233+
#sys.stderr.write("Retrieving\n")
226234
features = [f[:3] + [float(f[7])] for f in r if float(f[12]) <= length * N_FRACTION]
227235
gc = [f[3] for f in features]
228236

gimmemotifs/commands/index.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
#!/usr/bin/env python
2-
# Copyright (c) 2009-2013 Simon van Heeringen <s.vanheeringen@ncmls.ru.nl>
2+
# Copyright (c) 2009-2015 Simon van Heeringen <s.vanheeringen@ncmls.ru.nl>
33
#
44
# This module is free software. You can redistribute it and/or modify it under
55
# the terms of the MIT License, see the file COPYING included with this
66
# distribution.
77

88
import sys
99
import os
10+
from tempfile import NamedTemporaryFile
11+
12+
import pybedtools
1013

1114
from gimmemotifs.genome_index import GenomeIndex
15+
from gimmemotifs.shutils import find_by_ext
16+
from gimmemotifs.config import FASTA_EXT
17+
18+
1219

1320
def index(args):
1421

1522
if not os.path.exists(args.indexdir):
16-
print "Index_dir %s does not exist!" % (args.indexdir)
17-
sys.exit(1)
23+
print "Index_dir %s does not exist!" % (args.indexdir)
24+
sys.exit(1)
1825

1926
fasta_dir = args.fastadir
2027
index_dir = os.path.join(args.indexdir, args.indexname)
2128

2229
g = GenomeIndex()
23-
g = g.create_index(fasta_dir, index_dir)
30+
g.create_index(fasta_dir, index_dir)
31+
32+
# Create genome FASTA file for use with bedtools
33+
with open(os.path.join(index_dir, "genome.fa"), 'w') as out:
34+
for f in find_by_ext(fasta_dir, FASTA_EXT):
35+
for line in open(f):
36+
out.write(line)
37+
38+
test_chr = g.get_chromosomes()[0]
39+
tmp = NamedTemporaryFile()
40+
tmp.write("{}\t1\t2\n".format(test_chr))
41+
tmp.flush()
42+
43+
b = pybedtools.BedTool(tmp.name)
44+
try:
45+
b.nucleotide_content(fi=os.path.join(index_dir, "genome.fa"))
46+
except pybedtools.helpers.BEDToolsError as e:
47+
if str(e).find("generating") == -1:
48+
raise
49+

gimmemotifs/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
FA_VALID_BGS = ["random", "promoter", "gc", "user"]
1616
BED_VALID_BGS = ["random", "genomic", "gc", "promoter", "user"]
1717
BG_RANK = {"user":1, "promoter":2, "gc":3, "random":4, "genomic":5}
18+
FASTA_EXT = [".fasta", ".fa", ".fsa"]
19+
1820

1921
class MotifConfig:
2022
__shared_state = {}

0 commit comments

Comments
 (0)