-
Notifications
You must be signed in to change notification settings - Fork 3
/
sub-sample.py
executable file
·57 lines (47 loc) · 1.39 KB
/
sub-sample.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
#!/usr/bin/env python3
# !/bin/sh
# Author: Arkadiy Garber
from collections import defaultdict
import statistics
import os
import re
import textwrap
import argparse
import sys
parser = argparse.ArgumentParser(
prog="sub-sample.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Script for subsampling from a FASTA file
Developed by Arkadiy Garber: [email protected]
'''))
parser.add_argument('-fasta', help='input FASTA sequence file')
parser.add_argument('-num_seqs', help="number of sequences you would like to pull out from the fasta file")
parser.add_argument('-output', help="name output file")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
def fasta(fasta_file):
out = open(args.output, "w")
seq = ''
header = ''
count = 0
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if count < int(args.num_seqs):
if len(seq) > 0:
out.write(">" + header + "\n")
out.write(seq + "\n")
count += 1
print(count)
header = i[1:]
seq = ''
else:
header = i[1:]
seq = ''
else:
seq += i
file = open(args.fasta, "r")
fasta(file)