-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalignmentToGenes.py
More file actions
executable file
·74 lines (65 loc) · 2.44 KB
/
alignmentToGenes.py
File metadata and controls
executable file
·74 lines (65 loc) · 2.44 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env python
#Script written by Madison Youngblom
import sys
import subprocess
from collections import defaultdict
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
####
# - Takes a whole genome alignment file (RGA) and an annotation file (in bed format)
# and splits the alignment into individual gene alignments.
# - Will name gene alignments by "Name" if present in info column of annotation file
# otherwise will name them by "ID".
# - Gene alignments will be written to a directory called "indv_genes/"
# ** Note: features that are not of the type "CDS" will be written into a directory called "indv_genes/non_CDS/"
####
# check for correct arguments
if len(sys.argv) != 3:
print("Usage: alignmentToGenes.py genome_alignment.fasta annotation.bed")
sys.exit(0)
alnfile = sys.argv[1]
annotfile = sys.argv[2]
# make output directory
subprocess.call(["mkdir","indv_genes"])
subprocess.call(["mkdir","indv_genes/non_CDS"])
# read annotations into a dictionary with structure {"gene":[start,stop]}
genes = {}
with open(annotfile,"r") as f:
for line in f:
info = line.strip().split("\t")
start = int(info[1])
stop = int(info[2])
typ = info[7]
annot = info[9].split(";")
if "Name" not in info[9]:
for x in annot:
if x.startswith("ID"):
name = x.split("=")[1]
else:
for x in annot:
if x.startswith("Name"):
name = x.split("=")[1]
genes[name] = [start,stop,typ]
# parse genome alignment, for each sequence read through genes dictionary,
# create new Seq object with gene sequence and append gene sequence to new dictionary
# with structure {"gene":[Seq1,Seq2]}
indvalns = defaultdict(list)
for genome in SeqIO.parse(alnfile,"fasta"):
for gene,coord in genes.items():
alnname = genome.id+"_"+gene
geneseq = SeqRecord(Seq(str(genome.seq[coord[0]:coord[1]])),\
id=alnname,\
name=alnname,\
description=alnname)
indvalns[gene].append(geneseq)
# write individual alignments
for gene,alns in indvalns.items():
if genes[gene][2] == "CDS":
newfile = open("indv_genes/"+gene+".fasta","w")
SeqIO.write(alns,newfile,"fasta")
newfile.close()
else:
newfile = open("indv_genes/non_CDS/"+gene+".fasta","w")
SeqIO.write(alns,newfile,"fasta")
newfile.close()