def retrieve_sequences(self, sqldb, protein_db, dest="./",
                             shared_namespace=None, outfile=None):
        """
        :param sqldb: srting. Path to sqlite database file
        :param protein_db: string. Path to protein database file
        :param dest: string. Directory where sequences will be exported
        :param shared_namespace: Namespace object to communicate with
        TriFusion's main process
        :param outfile: If set, all sequeces will be instead saved in a
        single output file. This is used for the nucleotide sequence export
        :return:
        """

        if not os.path.exists(dest) and not outfile:
            os.makedirs(dest)

        if not os.path.exists(join(dest, "header_correspondance")):
            os.makedirs(join(dest, "header_correspondance"))

        if shared_namespace:
            shared_namespace.act = shared_namespace.msg = "Creating database"
            # Stores sequences that could not be retrieved
            shared_namespace.missed = shared_namespace.counter = 0
            shared_namespace.progress = 0
            # Get number of lines of protein database
            p = 0
            with open(protein_db) as fh:
                for p, _ in enumerate(fh):
                    pass
            shared_namespace.max_pb = shared_namespace.total = p + 1

        # Connect to database
        con = sqlite3.connect(sqldb)
        c = con.cursor()

        table_name = "".join([x for x in protein_db if x.isalnum()]).encode(
            "utf8")

        # Create table if it does not exist
        if not c.execute("SELECT name FROM sqlite_master WHERE type='table' "
                         "AND name='{}'".format(table_name)).fetchall():

            c.execute("CREATE TABLE [{}] (seq_id text PRIMARY KEY, seq text)".
                      format(table_name))

            # Populate database
            with open(protein_db) as ph:
                seq = ""
                for line in ph:
                    # Kill switch
                    if shared_namespace:
                        if shared_namespace.stop:
                            con.close()
                            raise KillByUser("")
                        shared_namespace.progress += 1
                        shared_namespace.counter += 1

                    if line.startswith(">"):
                        if seq != "":
                            c.execute("INSERT INTO [{}] VALUES (?, ?)".
                                      format(table_name), (seq_id, seq))
                        seq_id = line.strip()[1:]
                        seq = ""
                    else:
                        seq += line.strip()

            con.commit()

        if shared_namespace:
            shared_namespace.act = shared_namespace.msg = "Fetching sequences"
            shared_namespace.good = shared_namespace.counter = 0
            shared_namespace.progress = 0
            shared_namespace.max_pb = shared_namespace.total = \
                self.all_compliant

        # Set single output file, if option is set
        if outfile:
            output_handle = open(join(dest, outfile), "w")

        # Fetching sequences
        for line, cl in zip(self.groups(), self.iter_species_frequency()):

            # Kill switch
            if shared_namespace:
                if shared_namespace.stop:
                    con.close()
                    raise KillByUser("")

            # Filter sequences
            if self._get_compliance(cl) == (1, 1):

                if shared_namespace:
                    shared_namespace.good += 1
                    shared_namespace.progress += 1
                    shared_namespace.counter += 1

                # Retrieve sequences from current cluster
                if self.excluded_taxa:
                    line = self._remove_tx(line)
                fields = line.split(":")

                # Open file
                if not outfile:
                    cl_name = fields[0]
                    oname = join(dest, cl_name)
                    mname = join(dest, "header_correspondance", cl_name)
                    output_handle = open(oname + ".fas", "w")
                    map_handle = open(mname + "_headerMap.csv", "w")

                seqs = fields[-1].split()
                for i in seqs:
                    # Query database
                    c.execute("SELECT * FROM [{}] WHERE seq_id = ?".
                              format(table_name), (i,))
                    vals = c.fetchone()
                    # Handles cases where the sequence could not be retrieved
                    # If outfile is set, output_handle will be a single file
                    # for all groups. If not, it will represent an individual
                    # group file
                    try:
                        if not outfile:
                            tx_name = vals[0].split("|")[0]
                            output_handle.write(">{}\n{}\n".format(tx_name,
                                                                   vals[1]))
                            map_handle.write("{}; {}\n".format(vals[0],
                                                               tx_name))
                        else:
                            output_handle.write(">{}\n{}\n".format(vals[0],
                                                                   vals[1]))
                    except TypeError:
                        pass

                if not outfile:
                    output_handle.close()

        if outfile:
            output_handle.close()

        con.close()