# Path: mirtop/libs/fastq.py
# def open_fastq(in_file):
# def is_fastq(in_file):
# def splitext_plus(fn):


import os
import mirtop.libs.logger as mylog
from collections import defaultdict
from mirtop.libs import fastq
"""This code helps to work with spikeins"""


logger = mylog.getLogger(__name__)


def convert(args):
    out_dir = args.out if args.out else os.path.dirname(args.file)
    gff_file = os.path.join(out_dir, "spikeins_pre.gff")
    fasta_file = os.path.join(out_dir, "spikeins_pre.fasta")
    spikes = read_spikeins(args.file)
    write_gff(spikes, gff_file)
    logger.info("GFF file is at %s" % gff_file)
    write_precursors(spikes, fasta_file)
    logger.info("FASTA file is at %s" % fasta_file)


def read_spikeins(in_file):
    """Read FASTA file containing small spikeins

        Args:
            *in_file(str)*: file name with sequences.

        Returns:
            *spikeins(dict)*: dictionary with keys being names
             and values being: given spike-in, synthetic precursor and
             position on the synthetic precursor.
    """
    spikeins = defaultdict(dict)

# Path: mirtop/libs/fastq.py
# def open_fastq(in_file):
# def is_fastq(in_file):
# def splitext_plus(fn):


# Path: mirtop/libs/spikeins.py
import os
import mirtop.libs.logger as mylog
from collections import defaultdict
from mirtop.libs import fastq

"""This code helps to work with spikeins"""


logger = mylog.getLogger(__name__)


def convert(args):
    out_dir = args.out if args.out else os.path.dirname(args.file)
    gff_file = os.path.join(out_dir, "spikeins_pre.gff")
    fasta_file = os.path.join(out_dir, "spikeins_pre.fasta")
    spikes = read_spikeins(args.file)
    write_gff(spikes, gff_file)
    logger.info("GFF file is at %s" % gff_file)
    write_precursors(spikes, fasta_file)
    logger.info("FASTA file is at %s" % fasta_file)


def read_spikeins(in_file):
    """Read FASTA file containing small spikeins

        Args:
            *in_file(str)*: file name with sequences.

        Returns:
            *spikeins(dict)*: dictionary with keys being names
             and values being: given spike-in, synthetic precursor and
             position on the synthetic precursor.
    """
    spikeins = defaultdict(dict)

    with fastq.open_fastq(in_file) as inh: