-
Notifications
You must be signed in to change notification settings - Fork 3
/
break-seq.py
executable file
·101 lines (70 loc) · 1.98 KB
/
break-seq.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
#!/usr/bin/env python3
from collections import defaultdict
import re, os
import textwrap
import argparse
import sys
def fasta(fasta_file):
count = 0
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
count += 1
if count % 1000000 == 0:
print(count)
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
# header = header.split(" ")[0]
seq = ''
else:
header = i[1:]
# header = header.split(" ")[0]
seq = ''
else:
seq += i
Dict[header] = seq
# print(count)
return Dict
def allButTheFirst(iterable, delim):
x = ''
length = len(iterable.split(delim))
for i in range(1, length):
x += iterable.split(delim)[i]
x += delim
return x[0:len(x)]
parser = argparse.ArgumentParser(
prog="break-seq.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Script for splitting contig sequences into 2000 bp chunks
Developed by Arkadiy Garber: [email protected]
'''))
parser.add_argument('-i', help='input fasta file')
parser.add_argument('-o', help="output fasta file")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
file = open(args.i)
file = fasta(file)
out = open(args.o, "w")
for i in file.keys():
print(i)
seq = file[i]
contig = i
firstItem = contig.split(" ")[0]
rest = allButTheFirst(contig, " ")
count = 0
counter = 0
for j in range(2000, len(seq), 2000):
start = counter
end = j
segment = seq[start:end]
out.write(">" + firstItem + "_" + str(count) + " " + rest + "\n")
out.write(segment + "\n")
counter += 2000
count += 1