import random


def shred(d, prefix=None, existing=False, contigs=10000, minsize=500,
          maxsize=10000):
    """
    Generate random shreds of input fasta file

    :param d: Dictionary of sequences
    :param prefix: Prefix string to append to random contigs
    :param existing: Use existing prefix string ('|' splits prefix)
    :param contigs: Number of contigs to generate
    :param minsize: Minimum size of contigs
    :param maxsize: Maximum size of contigs
    :return: Dictionary of randomly shredded contigs
    """
    random.seed(42)
    shreds = {}
    keys = list(d.keys())

    for i in range(0, contigs):
        # pick a random contig
        key = random.choice(keys)
        if existing:
            prefix = key.split("|")[0]
        if prefix is not None:
            contig_id = ">{}|contig{}".format(prefix, i)
        else:
            contig_id = ">contig{}".format(i)
        keylen = len(d[key])-1
        # pick a random length
        rand_len = random.randrange(minsize, maxsize)
        # if random length is bigger than contig, choose entire contig
        if rand_len >= keylen:
            shreds[contig_id] = str(d[key].seq)
            continue
        # choose whether to start from beginning or end
        if random.choice(["start", "end"]) == "start":
            # if choosing from beginning, pick a random position between
            # the first nucleotide and contig_length - rand_length
            rand_start = random.randrange(0, keylen-rand_len)
            rand_end = rand_start+rand_len
        else:
            rand_end = random.randrange(rand_len, keylen)
            rand_start = rand_end-rand_len
        rand_seq = d[key][rand_start:rand_end]
        shreds[contig_id] = rand_seq.seq
    return shreds