import pandas as pd
from Bio import SeqIO
from Bio import SeqFeature
df = pd.read_csv('27SEPTtest.txt', sep='\t', names=['Cluster', 'Start', 'Stop'])
start_gene = df["Start"].tolist()
end_gene = df["Stop"].tolist()
cluster_name = df["Cluster"].tolist()
gbk = "antiSMASH.gbk"
record = next(SeqIO.parse("antiSMASH.gbk", "genbank"))
for x, y, z in zip(start_gene, end_gene, cluster_name):
for seq_record in SeqIO.parse(gbk, "genbank"):
for feat in seq_record.features:
if feat.type == "CDS":
if x in feat.qualifiers["gene_id"]:
cluster_start = feat.location.start
else:
if y in feat.qualifiers["gene_id"]:
cluster_end = feat.location.end
subrecord = record[cluster_start:cluster_end]
filename = "%s.gbk" % z
SeqIO.write(subrecord, filename, "gb")
Here is my attempt. I'd place myself at "just above noob level" and not used to using your style of for loops -- so I used this way instead.
I'm at a loss because the "cluster_start" and "cluster_end" strings correspond to the correct location in the genbank file. However when I call "record[cluster_start:cluster_end]" and write it to a file, it generates a genbank file where the captured genes are about 100 genes downstream of my actual target.
I feel like I am very close to solving this >:(
Output can be genbank or Fasta (protein seq) because its going to be used for multigeneblast.
Perhaps useful to note, I am parsing a Genbank file created from antiSMASH. Yes I know they have cluster'XX'.gbk files that can be downloaded, but I am trying to extract different Clusters (predicted by SMIPS/CASSIS).
Would it be possible to use start and end coordinates range instead of gene IDs to extract genes within a particular slice?
yep thats my plan, use the gene_id to then capture the corresponding location :)
Check out my answer to jrj.healey