Of course. To do this, we'll use a Python script with the Biopython library, which is the standard tool for parsing bioinformatics files like GenBank.
Step 1: Install Biopython
If you don't have Biopython installed, open your terminal or command prompt and run:
bash
pip install biopython
Step 2: The Python Script
Create a Python file named export_genes.py and paste the following code into it. This script will read your GenBank file (sega007.gb), find all features of type "gene", and write their details to a TSV file.
python
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
# We add Locus_Tag and Gene for better context
header = "Locus_Tag\tGene\tStart\tEnd\tStrand\n"
f_out.write(header)
# Parse the GenBank file
# SeqIO.parse is used for files with multiple records (e.g., contigs)
# For a file with a single record, SeqIO.read would also work
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 information from the feature's qualifiers
# .get() is used to avoid errors if a qualifier is missing
locus_tag = feature.qualifiers.get('locus_tag', ['N/A'])[0]
gene_name = feature.qualifiers.get('gene', ['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"{locus_tag}\t{gene_name}\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}'")
Step 3: How to Run the Script
Make sure the GenBank file sega007.gb is in the same directory as the export_genes.py script.
Open your terminal or command prompt.
Navigate to the directory where you saved the files.
Run the script using the following command:
bash
python export_genes.py
Step 4: Check the Output
After the script runs, a new file named sega007_genes.tsv will be created. It will look something like this (the data below is just an example):
from Bio import SeqIO: Imports the necessary module from Biopython for sequence input/output.
SeqIO.parse(genbank_file, "genbank"): This function reads the GenBank file and creates a SeqRecord object for each entry in the file.
for feature in record.features:: Each SeqRecord contains a list of its annotated features (like genes, CDS, mRNA, etc.). We loop through them.
if feature.type == 'gene':: This is the key filter. We only process features that are explicitly marked as a "gene".
feature.qualifiers.get(...): Qualifiers hold metadata like /gene="dnaA" or /locus_tag="SEGA_0001". We use .get() to safely retrieve these values without causing an error if they don't exist.
feature.location.start + 1: Biopython internally uses 0-based indexing for start positions (like Python lists). Biological sequence coordinates are almost always 1-based. So, we add 1 to the start position to conform to the standard convention.
feature.location.end: The end position is already correct for 1-based reporting.
feature.location.strand: This returns 1 for the forward strand, -1 for the reverse strand, and 0 or None for others. We convert this to a more readable + or -.
f"..." (f-string): This is a modern and clean way to format the output string, with \t creating the tabs for our TSV file.