I'll modify the script to extract the /label qualifier instead of the locus tag. Here's the updated version:
from Bio import SeqIO
import os
# --- Configuration ---
# Input GenBank file
genbank_file = "sega007.gb"
# Output TSV file
output_tsv_file = "sega007_genes.tsv"
# --- End Configuration ---
# Check if the input file exists
if not os.path.exists(genbank_file):
print(f"Error: The input file '{genbank_file}' was not found.")
exit()
# Open the output file for writing
with open(output_tsv_file, 'w') as f_out:
# Write the header row for the TSV file
header = "Label\tStart\tEnd\tStrand\n"
f_out.write(header)
# Parse the GenBank file
for record in SeqIO.parse(genbank_file, "genbank"):
print(f"Processing record: {record.id}")
# Iterate through all the features in the record
for feature in record.features:
# We are only interested in features of type 'gene'
if feature.type == 'gene':
# Extract label, defaulting to 'N/A' if not present
label = feature.qualifiers.get('label', ['N/A'])[0]
# Extract location information
# GenBank positions are 1-based, Biopython handles this
# feature.location.start gives the 0-based start coordinate
# We add 1 to make it 1-based for standard reporting
start_pos = feature.location.start + 1
end_pos = feature.location.end
# Determine the strand
if feature.location.strand == 1:
strand = '+'
elif feature.location.strand == -1:
strand = '-'
else:
strand = 'N/A' # For ambiguous or non-stranded features
# Format the data as a tab-separated string and write to file
output_line = f"{label}\t{start_pos}\t{end_pos}\t{strand}\n"
f_out.write(output_line)
print(f"\nSuccess! Gene data has been exported to '{output_tsv_file}'")
Key changes:
- Removed
locus_tag extraction
- Changed to extract
/label qualifier using feature.qualifiers.get('label', ['N/A'])[0]
- Updated the header to remove the Locus Tag column
- Simplified the output line to use just the label
The script will now create a TSV file with columns:
If no /label is found for a gene, it will use 'N/A' as a placeholder.
Run the script the same way as before:
python export_genes.py
This will generate sega007_genes.tsv with the requested format.