-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit_fasta.py
More file actions
executable file
·51 lines (40 loc) · 2.05 KB
/
Copy pathsplit_fasta.py
File metadata and controls
executable file
·51 lines (40 loc) · 2.05 KB
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
#!/usr/bin/env python
import argparse
def parse_args():
parser = argparse.ArgumentParser("Produces BED file of bam where read depth is at or above a threshold")
parser.add_argument('--input_fasta', '-i', dest='input_fasta', required=True, type=str,
help='Input fasta file')
parser.add_argument('--output_prefix', '-o', dest='output_prefix', default="output_", type=str,
help="Output will be of form ${OUTPUT_PREFIX}${IDX}${OUTPUT_SUFFIX}")
parser.add_argument('--output_suffix', '-O', dest='output_suffix', default=".fasta", type=str,
help="Output will be of form ${OUTPUT_PREFIX}${IDX}${OUTPUT_SUFFIX}")
parser.add_argument('--minimum_lines', '-l', dest='minimum_lines', required=True, type=int,
help='Minimum number of fasta lines to keep per file')
parser.add_argument('--index_format', '-d', dest='index_format', default="%03d",
help='Format string for chunk index')
args = parser.parse_args()
return args
def main():
# prep
args = parse_args()
with open(args.input_fasta, 'r') as infile:
current_line = 0
current_idx = 0
current_outfile = None
try:
for line in infile:
if current_outfile is None or (line.startswith(">") and current_line >= args.minimum_lines):
if current_outfile is not None:
current_outfile.close()
outfile_idx = args.index_format % current_idx
outfilename = args.output_prefix + outfile_idx + args.output_suffix
print("Writing to file {} after writing {} lines".format(outfilename, current_line))
current_outfile = open(outfilename, 'w')
current_line = 0
current_idx += 1
current_outfile.write(line)
current_line += 1
finally:
if current_outfile is not None: current_outfile.close()
if __name__ == "__main__":
main()