# Path: unicycler/misc.py
# def load_fasta(filename):
#     """
#     Returns a list of tuples (name, seq) for each record in the fasta file.
#     """
#     fasta_seqs = []
#     open_func = get_open_function(filename)
#     with open_func(filename, 'rt') as fasta_file:
#         name = ''
#         sequence = ''
#         for line in fasta_file:
#             line = line.strip()
#             if not line:
#                 continue
#             if line[0] == '>':  # Header line = start of new contig
#                 if name:
#                     fasta_seqs.append((name.split()[0], sequence))
#                     sequence = ''
#                 name = line[1:]
#             else:
#                 sequence += line
#         if name:
#             fasta_seqs.append((name.split()[0], sequence))
#     return fasta_seqs


import os
import subprocess
from .misc import load_fasta
from . import log
replicons to a standard starting point.

This file is part of Unicycler. Unicycler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version. Unicycler is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details. You should have received a copy of the GNU General Public License along with Unicycler. If
not, see <http://www.gnu.org/licenses/>.
"""



class CannotFindStart(Exception):
    pass


def find_start_gene(sequence, start_genes_fasta, identity_threshold, coverage_threshold, blast_dir,
                    makeblastdb_path, tblastn_path):
    """
    This function uses tblastn to look for start genes in the sequence. It returns the first gene
    (using the order in the file) which meets the identity and coverage thresholds, as well as
    the position of that gene (including which strand it is on).
    This function assumes that the sequence is circular with no overlap.
    """
    # Prepare the replicon sequence. In order to get a solid, single BLAST hit in cases where the
    # gene overlaps from the end to the start, we have to duplicate some of the replicon sequence
    # for the BLAST database.
    seq_len = len(sequence)
    start_genes_fasta = os.path.abspath(start_genes_fasta)

# Path: unicycler/misc.py
# def load_fasta(filename):
#     """
#     Returns a list of tuples (name, seq) for each record in the fasta file.
#     """
#     fasta_seqs = []
#     open_func = get_open_function(filename)
#     with open_func(filename, 'rt') as fasta_file:
#         name = ''
#         sequence = ''
#         for line in fasta_file:
#             line = line.strip()
#             if not line:
#                 continue
#             if line[0] == '>':  # Header line = start of new contig
#                 if name:
#                     fasta_seqs.append((name.split()[0], sequence))
#                     sequence = ''
#                 name = line[1:]
#             else:
#                 sequence += line
#         if name:
#             fasta_seqs.append((name.split()[0], sequence))
#     return fasta_seqs


# Path: unicycler/blast_func.py
import os
import subprocess
from .misc import load_fasta
from . import log

replicons to a standard starting point.

This file is part of Unicycler. Unicycler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version. Unicycler is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details. You should have received a copy of the GNU General Public License along with Unicycler. If
not, see <http://www.gnu.org/licenses/>.
"""



class CannotFindStart(Exception):
    pass


def find_start_gene(sequence, start_genes_fasta, identity_threshold, coverage_threshold, blast_dir,
                    makeblastdb_path, tblastn_path):
    """
    This function uses tblastn to look for start genes in the sequence. It returns the first gene
    (using the order in the file) which meets the identity and coverage thresholds, as well as
    the position of that gene (including which strand it is on).
    This function assumes that the sequence is circular with no overlap.
    """
    # Prepare the replicon sequence. In order to get a solid, single BLAST hit in cases where the
    # gene overlaps from the end to the start, we have to duplicate some of the replicon sequence
    # for the BLAST database.
    seq_len = len(sequence)
    start_genes_fasta = os.path.abspath(start_genes_fasta)

    queries = load_fasta(start_genes_fasta)