Remove locus tag, extract /label of the gene instead
- User
Remove locus tag, extract /label of the gene instead
Assistant
Okay, here's the modified Python script that removes the
locus_tagand extracts the/labelqualifier of the gene instead. If a/labelis not present, it will default to "N/A".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 = "Gene_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 information from the feature's qualifiers gene_label = feature.qualifiers.get('label', ['N/A'])[0] # Extract location information 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' # Format the data as a tab-separated string and write to file output_line = f"{gene_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:
- Header: The header row in the TSV file is now
Gene_Label\tStart\tEnd\tStrand. gene_label = feature.qualifiers.get('label', ['N/A'])[0]: This line now retrieves the value associated with thelabelqualifier. If thelabelqualifier is not found, it defaults to "N/A".- Output Line: The output line now uses
gene_labelinstead oflocus_tag.
How to Use:
- Save the script as
export_genes.py. - Make sure
sega007.gbis in the same directory. - Run the script from your terminal:
python export_genes.py
The resulting
sega007_genes.tsvfile will now contain the gene label (if available), start position, end position, and strand for each gene in your GenBank file. If a gene doesn't have a/labelqualifier, "N/A" will be used in its place.- Header: The header row in the TSV file is now
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.