from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio import SeqIO

# Step 1: Create a DNA sequence
dna_sequence = Seq("ATGCGTACGTAGCTAGCTAG")

# Step 2: Create a SeqRecord object with annotations
record = SeqRecord(
    dna_sequence,
    id="seq1",
    name="Example_Gene",
    description="Example gene sequence",
    annotations={
        "molecule_type": "DNA",  # Required for GenBank format
        "gene": "ExampleGene",
        "function": "Hypothetical protein"
    }
)

# Step 3: Write the SeqRecord object to a GenBank file
output_file_path = "q4_genbank.gb"  # Change path as needed
with open(output_file_path, "w") as output_file:
    SeqIO.write(record, output_file, "genbank")

print("GenBank file written successfully.")

# Step 4: Open and read the GenBank file
with open(output_file_path, "r") as input_file:
    record_read = SeqIO.read(input_file, "genbank")
    print("\nContents of the GenBank file:")
    print(record_read)

# Example of the output in the GenBank file:
# LOCUS       seq1                21 bp    DNA     linear   UNK 01-JAN-2025
# DEFINITION  Example gene sequence.
# ACCESSION   seq1
# VERSION     seq1.1
# KEYWORDS    .
# SOURCE      
#   ORGANISM  Synthetic construct
# REFERENCE   1  (bases 1 to 21)
#   AUTHORS   Example Author
#   TITLE     Direct submission
# FEATURES             Location/Qualifiers
#      gene            1..21
#                      /gene="ExampleGene"
#                      /function="Hypothetical protein"
# ORIGIN
#         1 atgcgtacgt agctagctag
# //