-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasta_split_by_group.py
78 lines (53 loc) · 2.22 KB
/
fasta_split_by_group.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
#!/usr/bin/env python
from Bio import SeqIO
import argparse
import os
import pandas as pd
from jakomics import colors
import jak_utils
# OPTIONS #####################################################################
parser = argparse.ArgumentParser(
description='Give a fasta file and a grouping file, and it will split into smaller fasta files with group as file name')
parser.add_argument('--fasta',
help="Fasta file",
required=True)
parser.add_argument('-g', '--group_file',
help="Paths to tsv file with sequence group info. Column 1 is fasta header, column 2 is the group",
required=True)
parser.add_argument('--out_dir',
help="Directory to write individual fasta files to",
required=True)
args = parser.parse_args()
# FUNCTIONS ###################################################################
def get_fasta():
seqs = {}
for seq_record in SeqIO.parse(args.fasta, "fasta"):
seqs[seq_record.id] = seq_record
return seqs
def parse_group(header_col=0, group_col=1):
groups = {}
df = pd.read_csv(args.group_file, sep="\t", header=None)
for index, row in df.iterrows():
if row[group_col] in groups:
groups[row[group_col]].append(row[header_col])
else:
groups[row[group_col]] = [row[header_col]]
return groups
# MAIN ########################################################################
if __name__ == "__main__":
jak_utils.header()
args.out_dir = os.path.abspath(args.out_dir) + '/'
if not os.path.exists(args.out_dir):
print(f"{colors.bcolors.BLUE}Creating directory {args.out_dir}{colors.bcolors.END}")
os.makedirs(args.out_dir)
original_seqs = get_fasta()
groups = parse_group()
for group in groups:
group_seqs = []
for header in groups[group]:
if header in original_seqs:
group_seqs.append(original_seqs[header])
else:
raise Exception(
f"{colors.bcolors.RED}ERROR: {header} is not found in {args.fasta}{colors.bcolors.END}")
SeqIO.write(group_seqs, os.path.join(args.out_dir, group + '.fa'), "fasta")