Okay, you are right! The previous script assumed the gene name would be in the /gene qualifier. Your example shows it's in the /label qualifier, often formatted like /label="<gene_name> gene".
Let's modify the script to check the /label qualifier if the /gene qualifier is missing or empty, and extract the first part of the label as the gene name.
Here is the updated Python script:
python
from Bio import SeqIO
import csv
import re # Import regular expressions module (optional but potentially useful for more complex parsing)
# Specify the input and output files
input_file = "sega007.gb"
output_file = "gene_positions.csv"
# Open the output CSV file
with open(output_file, mode='w', newline='') as csvfile:
csv_writer = csv.writer(csvfile, delimiter=';')
# Write the header
csv_writer.writerow(['Gene name', 'starting bp', 'ending bp'])
# Parse the GenBank file
for record in SeqIO.parse(input_file, "genbank"):
for feature in record.features:
# We are only interested in features of type 'gene'
if feature.type == "gene":
gene_name = "" # Initialize gene name as empty
# 1. Try to get the gene name from the '/gene' qualifier (standard)
if 'gene' in feature.qualifiers:
gene_name = feature.qualifiers['gene'][0]
# 2. If '/gene' is not present or empty, try '/label'
# This handles cases like /label="thrB gene"
if not gene_name and 'label' in feature.qualifiers:
full_label = feature.qualifiers['label'][0]
# Extract the first word from the label as the potential gene name
# This assumes format like "thrB gene" or just "thrB"
if full_label:
gene_name = full_label.split()[0]
# 3. Optional Fallback: If still no name, try '/locus_tag'
# if not gene_name and 'locus_tag' in feature.qualifiers:
# gene_name = feature.qualifiers['locus_tag'][0]
# If no name found after trying common qualifiers, leave it blank or use a placeholder
if not gene_name:
gene_name = "N/A" # Or leave as ""
# Get start and end positions (adjusting start to 1-based)
start = feature.location.start + 1
end = feature.location.end
# Write the row to the CSV
csv_writer.writerow([gene_name, start, end])
print(f"Gene positions extracted to {output_file}")
Explanation of Changes:
Check /gene first: The code still prioritizes the standard /gene qualifier.
Fallback to /label: If gene_name is still empty after checking /gene (meaning /gene was absent or empty), it checks for the /label qualifier.
Extract from /label: If /label exists and is not empty (if full_label:), it takes the value (e.g., "thrB gene"), splits it by spaces (.split()), and takes the first element ([0]), which should be the gene name (e.g., "thrB").
Placeholder: If no name is found in either /gene or /label, it now assigns "N/A" as the gene name. You can change this back to "" (empty string) if you prefer.
Optional /locus_tag: I've added a commented-out section to check /locus_tag as another common identifier, which you can enable if needed.
Now, run this updated extract_genes.py script again. It should correctly extract gene names from features formatted like your example.