def trim5p3p_multithreaded(fastq_filename, output_prefix, seq_5p, seq_3p_rev, chunks):
    num_lines = 0
    for line in open(fastq_filename, 'r'):
        num_lines += 1
    num_records = (num_lines // 4)
    chunk_size = ((num_records // chunks) + ((num_records % chunks) > 0))
    print('{0} has {1} records, {2} per chunk'.format(fastq_filename, num_records, chunk_size))
    pools = []
    records = []
    count = 0
    i = 1
    for r in SeqIO.parse(open(fastq_filename), 'fastq'):
        count += 1
        records.append(r)
        if (count >= chunk_size):
            p = Process(target=trim5p3p, args=(records, ((output_prefix + '.') + str(i)), seq_5p, seq_3p_rev))
            p.start()
            print('Starting worker {0}...'.format(i))
            pools.append(p)
            records = []
            count = 0
            i += 1
    p = Process(target=trim5p3p, args=(records, ((output_prefix + '.') + str(i)), seq_5p, seq_3p_rev))
    p.start()
    print('Starting worker {0}...'.format(i))
    pools.append(p)
    for p in pools:
        p.join()
    f_FL = open((output_prefix + '.fl.fasta'), 'w')
    f_FL_clips = open((output_prefix + '.fl.clips'), 'w')
    f_nFL = open((output_prefix + '.nfl.fasta'), 'w')
    f_csv = open((output_prefix + '.csv'), 'w')
    for j in range(1, (i + 1)):
        p = ((output_prefix + '.') + str(j))
        with open((p + '.fl.fasta')) as h:
            f_FL.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_FL.name))
        with open((p + '.fl.clips')) as h:
            f_FL_clips.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_FL_clips.name))
        with open((p + '.nfl.fasta')) as h:
            f_nFL.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_nFL.name))
        with open((p + '.csv')) as h:
            f_csv.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_csv.name))
        os.remove((p + '.fl.fasta'))
        os.remove((p + '.fl.clips'))
        os.remove((p + '.nfl.fasta'))
        os.remove((p + '.csv'))
    f_csv.close()
    f_FL.close()
    f_FL_clips.close()
    f_nFL.close()
def trim5p3p_multithreaded(fastq_filename, output_prefix, seq_5p, seq_3p_rev, chunks):
    num_lines = 0
    for line in open(fastq_filename, 'r'):
        num_lines += 1
    num_records = (num_lines // 4)
    chunk_size = ((num_records // chunks) + ((num_records % chunks) > 0))
    print('{0} has {1} records, {2} per chunk'.format(fastq_filename, num_records, chunk_size))
    pools = []
    records = []
    count = 0
    i = 1
    for r in SeqIO.parse(open(fastq_filename), 'fastq'):
        count += 1
        records.append(r)
        if (count >= chunk_size):
            p = Process(target=trim5p3p, args=(records, ((output_prefix + '.') + str(i)), seq_5p, seq_3p_rev))
            p.start()
            print('Starting worker {0}...'.format(i))
            pools.append(p)
            records = []
            count = 0
            i += 1
    p = Process(target=trim5p3p, args=(records, ((output_prefix + '.') + str(i)), seq_5p, seq_3p_rev))
    p.start()
    print('Starting worker {0}...'.format(i))
    pools.append(p)
    for p in pools:
        p.join()
    f_FL = open((output_prefix + '.fl.fasta'), 'w')
    f_FL_clips = open((output_prefix + '.fl.clips'), 'w')
    f_nFL = open((output_prefix + '.nfl.fasta'), 'w')
    f_csv = open((output_prefix + '.csv'), 'w')
    for j in range(1, (i + 1)):
        p = ((output_prefix + '.') + str(j))
        with open((p + '.fl.fasta')) as h:
            f_FL.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_FL.name))
        with open((p + '.fl.clips')) as h:
            f_FL_clips.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_FL_clips.name))
        with open((p + '.nfl.fasta')) as h:
            f_nFL.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_nFL.name))
        with open((p + '.csv')) as h:
            f_csv.write(h.read())
            print('writing {0} into {1}...'.format(h.name, f_csv.name))
        os.remove((p + '.fl.fasta'))
        os.remove((p + '.fl.clips'))
        os.remove((p + '.nfl.fasta'))
        os.remove((p + '.csv'))
    f_csv.close()
    f_FL.close()
    f_FL_clips.close()
    f_nFL.close()