def extract_fasta(<EOL>partition_file,<EOL>fasta_file,<EOL>output_dir,<EOL>chunk_size=DEFAULT_CHUNK_SIZE,<EOL>max_cores=DEFAULT_MAX_CORES,<EOL>):
genome = {<EOL>record.id: record.seq for record in SeqIO.parse(fasta_file, "<STR_LIT>")<EOL>}<EOL>data_chunks = list(<EOL>zip(*np.genfromtxt(partition_file, usecols=(<NUM_LIT:0>, <NUM_LIT:1>), dtype=None))<EOL>)<EOL>chunk_names = np.array(data_chunks[<NUM_LIT:0>], dtype=object)<EOL>cores = np.array(data_chunks[<NUM_LIT:1>])<EOL>for core in set(cores):<EOL><INDENT>if core > max_cores:<EOL><INDENT>continue<EOL><DEDENT>chunks_to_keep = chunk_names[cores == core]<EOL>core_name = "<STR_LIT>".format(core)<EOL>core_file = os.path.join(output_dir, core_name)<EOL>with open(core_file, "<STR_LIT:w>") as core_handle:<EOL><INDENT>for name in chunks_to_keep:<EOL><INDENT>fields = name.split("<STR_LIT:_>")<EOL>header_name = "<STR_LIT:_>".join(fields[:-<NUM_LIT:1>])<EOL>chunk = int(fields[-<NUM_LIT:1>])<EOL>pos_start = chunk * chunk_size<EOL>pos_end = min(<EOL>(chunk + <NUM_LIT:1>) * chunk_size, len(genome[header_name])<EOL>)<EOL>sequence = str(genome[header_name][pos_start:pos_end])<EOL>core_handle.write("<STR_LIT>".format(name))<EOL>core_handle.write("<STR_LIT>".format(sequence))<EOL><DEDENT><DEDENT><DEDENT>
Extract sequences from bins

    Identify bins, extract chunks belonging to each bins and gather them
    in a single FASTA file.

    Parameters
    ----------
    partition_file : file, str or pathlib.Path
        The file containing, for each chunk, the communities it was
        assigned to at each iteration.
    fasta_file : file, str or pathlib.Path
        The initial assembly from which chunks were initialized.
    output_dir : str or pathlib.Path
        The output directory to write the FASTA chunks into.
    chunk_size : int, optional
        The size of the chunks (in bp) used in the pipeline. Default is 1000.
    max_cores : int, optional
        How many bins to extract FASTA sequences from. Default is 100.