def split_by_fixed_size(infile, outfiles_prefix, chunk_size, tolerance, skip_if_all_Ns=False):
file_count = <NUM_LIT:1><EOL>coords = []<EOL>small_sequences = []  <EOL>seq_reader = sequences.file_reader(infile)<EOL>f_coords = utils.open_file_write(outfiles_prefix + '<STR_LIT>')<EOL>for seq in seq_reader:<EOL><INDENT>if skip_if_all_Ns and seq.is_all_Ns():<EOL><INDENT>continue<EOL><DEDENT>if len(seq) < chunk_size:<EOL><INDENT>small_sequences.append(copy.copy(seq))<EOL><DEDENT>elif len(seq) <= chunk_size + tolerance:<EOL><INDENT>f = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>print(seq, file=f)<EOL>utils.close(f)<EOL>file_count += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>chunks = [(x,x+chunk_size) for x in range(<NUM_LIT:0>, len(seq), chunk_size)]<EOL>if chunks[-<NUM_LIT:1>][<NUM_LIT:1>] - <NUM_LIT:1> > len(seq):<EOL><INDENT>chunks[-<NUM_LIT:1>] = (chunks[-<NUM_LIT:1>][<NUM_LIT:0>], len(seq))<EOL><DEDENT>if len(chunks) > <NUM_LIT:1> and (chunks[-<NUM_LIT:1>][<NUM_LIT:1>] - chunks[-<NUM_LIT:1>][<NUM_LIT:0>]) <= tolerance:<EOL><INDENT>chunks[-<NUM_LIT:2>] = (chunks[-<NUM_LIT:2>][<NUM_LIT:0>], chunks[-<NUM_LIT:1>][<NUM_LIT:1>])<EOL>chunks.pop()<EOL><DEDENT>offset = <NUM_LIT:0><EOL>for chunk in chunks:<EOL><INDENT>if not(skip_if_all_Ns and seq.is_all_Ns(start=chunk[<NUM_LIT:0>], end=chunk[<NUM_LIT:1>]-<NUM_LIT:1>)):<EOL><INDENT>f = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>chunk_id = seq.id + '<STR_LIT::>' + str(chunk[<NUM_LIT:0>]+<NUM_LIT:1>) + '<STR_LIT:->' + str(chunk[<NUM_LIT:1>])<EOL>print(sequences.Fasta(chunk_id, seq[chunk[<NUM_LIT:0>]:chunk[<NUM_LIT:1>]]), file=f)<EOL>print(chunk_id, seq.id, offset, sep='<STR_LIT:\t>', file=f_coords)<EOL>utils.close(f)<EOL>file_count += <NUM_LIT:1><EOL><DEDENT>offset += chunk[<NUM_LIT:1>] - chunk[<NUM_LIT:0>]<EOL><DEDENT><DEDENT><DEDENT>if len(small_sequences):<EOL><INDENT>f = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>file_count += <NUM_LIT:1><EOL>base_count = <NUM_LIT:0><EOL>for seq in small_sequences:<EOL><INDENT>if base_count > <NUM_LIT:0> and base_count + len(seq) > chunk_size + tolerance:<EOL><INDENT>utils.close(f)<EOL>f = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>file_count += <NUM_LIT:1><EOL>base_count = <NUM_LIT:0><EOL><DEDENT>print(seq, file=f)<EOL>base_count += len(seq)<EOL><DEDENT>utils.close(f)<EOL><DEDENT>
Splits  fasta/q file into separate files, with up to (chunk_size + tolerance) bases in each file