def combinetargets(targets, targetpath, mol_type='nt'):
    """
    Creates a set of all unique sequences in a list of supplied FASTA files. Properly formats headers and sequences
    to be compatible with local pipelines. Splits hybrid entries. Removes illegal characters.
    :param targets: fasta gene targets to combine
    :param targetpath: folder containing the targets
    """
    make_path(targetpath)
    with open(os.path.join(targetpath, 'combinedtargets.fasta'), 'w') as combined:
        idset = set()
        for target in targets:
            # Remove non-unicode characters present in the FASTA files
            cleanedstring = str()
            # Read in the file as binary
            with open(target, 'rb') as fasta:
                # Import all the text
                text = fasta.read()
                # Convert the binary variable to a string, ignoring non-UTF-8 characters
                cleanedstring += text.decode('utf-8', 'ignore')
            # Overwrite the file with the clean string
            with open(target, 'w') as fasta:
                fasta.write(cleanedstring)
            # Clean up each record
            for record in SeqIO.parse(target, 'fasta'):
                # In case FASTA records have been spliced together, allow for the splitting of
                # these records
                if '>' in record.seq:
                    # Split the two records apart on '>' symbols
                    record.seq, hybrid = record.seq.split('>')
                    # Split the header from the sequence e.g. sspC:6:CP003808.1ATGGAAAGTACATTAGA...
                    # will be split into sspC:6:CP003808.1 and ATGGAAAGTACATTAGA
                    hybridid, seq = re.findall('(.+\\d+\\.\\d)(.+)', str(hybrid))[0]
                    # Replace and dashes in the record.id with underscores
                    hybridid = hybridid.replace('-', '_')
                    # Convert the string to a seq object
                    if mol_type == 'nt':
                        hybridseq = Seq(seq, generic_dna)
                    else:
                        hybridseq = Seq(seq, generic_protein)
                    # Create a SeqRecord of the sequence - use the sequence object and id
                    hybridrecord = SeqRecord(hybridseq,
                                             description='',
                                             id=hybridid)

                    # Remove and dashes or 'N's from the sequence data - makeblastdb can't handle sequences
                    # with gaps
                    # noinspection PyProtectedMember
                    hybridrecord.seq._data = hybridrecord.seq._data.replace('-', '').replace('N', '')
                    # Write the original record to the file
                    # Extract the sequence record from each entry in the multifasta
                    # Replace and dashes in the record.id with underscores
                    record.id = record.id.replace('-', '_')
                    # Remove and dashes or 'N's from the sequence data - makeblastdb can't handle sequences
                    # with gaps
                    # noinspection PyProtectedMember
                    record.seq._data = record.seq._data.replace('-', '').replace('N', '')
                    # Clear the name and description attributes of the record
                    record.name = ''
                    record.description = ''
                    if record.id not in idset:
                        SeqIO.write(record, combined, 'fasta')
                    if hybridrecord.id not in idset:
                        # Write the second record to file
                        SeqIO.write(hybridrecord, combined, 'fasta')
                        idset.add(hybridrecord.id)

                else:
                    # Extract the sequence record from each entry in the multifasta
                    # Replace and dashes in the record.id with underscores
                    record.id = record.id.replace('-', '_')
                    # Remove and dashes or 'N's from the sequence data - makeblastdb can't handle sequences
                    # with gaps
                    # noinspection PyProtectedMember
                    record.seq._data = record.seq._data.replace('-', '').replace('N', '')
                    # Clear the name and description attributes of the record
                    record.name = ''
                    record.description = ''
                    if record.id not in idset:
                        SeqIO.write(record, combined, 'fasta')
                        idset.add(record.id)