-
Notifications
You must be signed in to change notification settings - Fork 3
/
reformat-fastas.py
executable file
·98 lines (86 loc) · 3.34 KB
/
reformat-fastas.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
#!/usr/bin/env python3
#!/bin/sh
from collections import defaultdict
import re, os
import textwrap
import argparse
import sys
parser = argparse.ArgumentParser(
prog="reformat-fastas",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Program for reformatting multiple fasta files alla Anvi'o
'''))
parser.add_argument('-i', help='input directory with fasta files')
parser.add_argument('-x', help='extension of fasta files (include the preceeding period)')
parser.add_argument('-min_length', help="minimum sequence length for fasta file (default = 1)", default=1)
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
def stabilityCounter(int):
if len(str(int)) == 1:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 2:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 3:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 4:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 5:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 6:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 7:
string = (str(0) + str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 8:
string = (str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 9:
string = (str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 10:
string = (str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 11:
string = (str(0) + str(int))
return (string)
dir = os.listdir(args.i)
def fasta(dir):
count = 0
for file in dir:
if re.findall(args.x, file):
ext = len(args.x)
infile = open(args.i + "/" + file, "r")
outfile = open(args.i + "/" + file[0:len(file)-ext] + "-fixed" + args.x, "w")
seq = ''
header = ''
for i in infile:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) >= int(args.min_length):
outfile.write(">" + header + "\n")
outfile.write(seq + "\n")
count += 1
headerNum = stabilityCounter(count)
header = ("c_" + str(headerNum))
seq = ''
else:
count += 1
headerNum = stabilityCounter(count)
header = ("c_" + str(headerNum))
seq = ''
else:
seq += i
outfile.write(">" + header + "\n")
outfile.write(seq + "\n")
infile.close()
outfile.close()
fasta(dir)