-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample_fasta.py
More file actions
26 lines (22 loc) · 994 Bytes
/
Copy pathsample_fasta.py
File metadata and controls
26 lines (22 loc) · 994 Bytes
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
#!python3
import helper
import argparse
import os
import random
def sample(file, n, outfile, seed):
seqs = helper.ReadFasta(file)
if seed is not None:
random.seed(seed)
take = random.sample(seqs.keys(), n)
out = {}
for id in take:
out[id] = seqs[id]
helper.WriteFasta(out, outfile)
if __name__=="__main__":
parser = argparse.ArgumentParser(description="Randomly sample sequences in a fasta file, sampling without replacement.")
parser.add_argument('infile', metavar='i', type=str, help='Input fasta file.')
parser.add_argument('sample_size', metavar='n', type=int, help='Number of sequences to sample.')
parser.add_argument('outlabel', metavar='o', type=str, help='Name for the output fasta.')
parser.add_argument('-seed', metavar='-s', type=float, help='Seed for the random number generator.')
args = parser.parse_args()
sample(args.infile, args.sample_size, args.outlabel, args.seed)