def generate_barcodes(nIds, codeLen=<NUM_LIT:12>):
def next_code(b, c, i):<EOL><INDENT>return c[:i] + b + (c[i+<NUM_LIT:1>:] if i < -<NUM_LIT:1> else '<STR_LIT>')<EOL><DEDENT>def rand_base():<EOL><INDENT>return random.choice(['<STR_LIT:A>', '<STR_LIT:T>', '<STR_LIT:C>', '<STR_LIT>'])<EOL><DEDENT>def rand_seq(n):<EOL><INDENT>return '<STR_LIT>'.join([rand_base() for _ in range(n)])<EOL><DEDENT>hpf = re.compile('<STR_LIT>', re.IGNORECASE)<EOL>while True:<EOL><INDENT>codes = [rand_seq(codeLen)]<EOL>if (hpf.search(codes[<NUM_LIT:0>]) is None):<EOL><INDENT>break<EOL><DEDENT><DEDENT>idx = <NUM_LIT:0><EOL>while len(codes) < nIds:<EOL><INDENT>idx -= <NUM_LIT:1><EOL>if idx < -codeLen:<EOL><INDENT>idx = -<NUM_LIT:1><EOL>codes.append(rand_seq(codeLen))<EOL><DEDENT>else:<EOL><INDENT>nc = next_code(rand_base(), codes[-<NUM_LIT:1>], idx)<EOL>if hpf.search(nc) is None:<EOL><INDENT>codes.append(nc)<EOL><DEDENT><DEDENT>codes = list(set(codes))<EOL><DEDENT>return codes<EOL>
Given a list of sample IDs generate unique n-base barcodes for each.
Note that only 4^n unique barcodes are possible.