|
1 | 1 | #!/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> |
3 | 3 | # |
4 | 4 | # This module is free software. You can redistribute it and/or modify it under |
5 | 5 | # the terms of the MIT License, see the file COPYING included with this |
6 | 6 | # distribution. |
7 | 7 |
|
8 | 8 | import sys |
9 | 9 | import os |
| 10 | +from tempfile import NamedTemporaryFile |
| 11 | + |
| 12 | +import pybedtools |
10 | 13 |
|
11 | 14 | from gimmemotifs.genome_index import GenomeIndex |
| 15 | +from gimmemotifs.shutils import find_by_ext |
| 16 | +from gimmemotifs.config import FASTA_EXT |
| 17 | + |
| 18 | + |
12 | 19 |
|
13 | 20 | def index(args): |
14 | 21 |
|
15 | 22 | 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) |
18 | 25 |
|
19 | 26 | fasta_dir = args.fastadir |
20 | 27 | index_dir = os.path.join(args.indexdir, args.indexname) |
21 | 28 |
|
22 | 29 | 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 | + |
0 commit comments