from Bio import SeqIO
from Bio.SeqRecord import SeqRecord

# Function to convert FASTA file to GenBank format
def convert_fasta_to_genbank(fasta_file, genbank_file):
    # Parse the FASTA file and read sequences
    records = []
    for record in SeqIO.parse(fasta_file, "fasta"):
        # Extract the sequence and description from FASTA record
        sequence = record.seq
        description = record.description
        
        # Create SeqRecord object for GenBank with basic annotations
        genbank_record = SeqRecord(
            sequence,
            id=record.id,
            name="Example_Gene",
            description=description,
            annotations={
                "molecule_type": "DNA",  # Required for GenBank format
                "gene": "ExampleGene",
                "function": "Hypothetical protein"
            }
        )
        records.append(genbank_record)  # Add the record to the list
    
    # Write all SeqRecords to GenBank format at once
    with open(genbank_file, "w") as output_handle:
        SeqIO.write(records, output_handle, "genbank")
    
    print(f"All FASTA sequences converted to GenBank format and saved as {genbank_file}")

# Define input and output file paths
fasta_file = "fasta_1.fasta"  # Replace with your actual FASTA file path
genbank_file = "example_output.gb"  # Output GenBank file

# Call the function to convert FASTA to GenBank
convert_fasta_to_genbank(fasta_file, genbank_file)

# Example FASTA file (example.fasta):
# >seq1 Example sequence description
# ATGCGTACGTAGCTAGCTAG

# Expected Output in the GenBank file (example_output.gb):
# LOCUS       seq1                21 bp    DNA     linear   UNK 01-JAN-2025
# DEFINITION  Example sequence description
# 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
# //