-
Notifications
You must be signed in to change notification settings - Fork 3
/
DAS_Tool-convert.py
executable file
·59 lines (50 loc) · 1.5 KB
/
DAS_Tool-convert.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
#!/usr/bin/env python3
# !/bin/sh
# Author: Arkadiy Garber
import textwrap
import argparse
import os
import re
from collections import defaultdict
import sys
parser = argparse.ArgumentParser(
prog="DAS_Toool-convert.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Program for creating TSV-formatted bin files for DAS_Tool;
Developed by Arkadiy Garber; [email protected]
'''))
parser.add_argument('-dir', help='Directory with bins. Please provide full path.')
parser.add_argument('-name', help='Basename of output file.')
parser.add_argument('-id', help='suffix or prefix of bins')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
def fasta(fasta_file):
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
seq = ''
else:
header = i[1:]
seq = ''
else:
seq += i
Dict[header]["seq"] = seq
return Dict
cwd = os.getcwd()
outfile = open(cwd + "/" + args.name, "w")
dir = os.listdir(args.dir)
for i in dir:
if args.id == i[len(i) - len(args.id): len(i)]:
binX = open(args.directory + "/" + i, "r")
binX = fasta(binX)
for j in binX.keys():
outfile.write(j + "\t" + i + "\n")