Thank you for the clarification! It seems that the gene names are stored in the /label qualifier rather than the /gene qualifier. We can modify the script to extract the gene names correctly from the /label qualifier. Here's the updated script:
Updated Python Script
python
from Bio import SeqIO
import csv
# 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:
if feature.type == "gene":
# Get the gene name from the /label qualifier
gene_name = feature.qualifiers.get('label', [''])[0] # Get gene name
start = feature.location.start + 1 # Convert to 1-based index
end = feature.location.end # End is already 1-based
csv_writer.writerow([gene_name, start, end])
print(f"Gene positions extracted to {output_file}")
Explanation of Changes
Gene Name Extraction: The script now looks for the label qualifier instead of the gene qualifier to get the gene name.
CSV Writing: The rest of the script remains the same, writing the gene name, starting position, and ending position to the CSV file.
Running the Script
You can run the updated script in the same way as before:
bash
python extract_genes.py
Output
After running the script, the gene_positions.csv file should now correctly contain the gene names along with their starting and ending base pair positions in the specified format:
code
Gene name;starting bp;ending bp
thrB;2801;3733
...
Make sure to check the output file to confirm that the gene names are now included as expected!