from pathlib import Path


def extract_sequence(fasta: Path, target_contig: str) -> str:
    """
    Given an input FASTA file and a target contig, will extract the nucleotide/amino acid sequence
    and return as a string. Will break out after capturing 1 matching contig.
    """
    sequence = ""
    write_flag = False
    write_counter = 0
    with open(str(fasta), 'r') as infile:
        for line in infile:
            if write_counter > 1:
                break
            if line[0] == ">":  # This is marginally faster than .startswith()
                if target_contig in line:
                    write_counter += 1
                    write_flag = True
                else:
                    write_flag = False
            elif write_flag:
                sequence += line
            else:
                continue
    return sequence.replace("\n", "")