def trim5p3p(records, output_prefix, seq_5p, seq_3p_rev):
    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')
    writer = DictWriter(f_csv, fieldnames=['id', 'end5', 'end3', 'endA', 'strand'])
    writer.writeheader()
    for r in records:
        r2 = r.reverse_complement()
        r2.id = r.id
        t1 = trim5p3p_helper(r, seq_5p, seq_3p_rev)
        t2 = trim5p3p_helper(r2, seq_5p, seq_3p_rev)
        is_fl_flag1 = ((t1.score5 >= MINSCORE_5P) and (t1.score3 >= MINSCORE_3P) and ((MIN_A_LEN == 0) or (t1.endA != t1.end3)))
        is_fl_flag2 = ((t2.score5 >= MINSCORE_5P) and (t2.score3 >= MINSCORE_3P) and ((MIN_A_LEN == 0) or (t2.endA != t2.end3)))
        if is_fl_flag1:
            if is_fl_flag2:
                if ((t1.score5 + t1.score3) > (t2.score5 + t2.score3)):
                    strand = '+'
                else:
                    strand = '-'
            else:
                strand = '+'
        elif is_fl_flag2:
            strand = '-'
        else:
            strand = 'NA'
        info = {'id': r.id, 'end5': 'NA', 'end3': 'NA', 'endA': 'NA', 'strand': 'NA'}
        if (strand == '+'):
            info['strand'] = '+'
            info['end5'] = t1.end5
            info['end3'] = t1.end3
            info['endA'] = t1.endA
            f_FL.write('>{0}\n{1}\n'.format(r.id, r.seq[t1.end5:t1.endA]))
            f_FL_clips.write('>{0}_5p strand:+ score:{1}\n{2}\n'.format(r.id, t1.score5, r.seq[:t1.end5]))
            f_FL_clips.write('>{0}_3p strand:+ score:{1}\n{2}\n'.format(r.id, t1.score3, r.seq[t1.endA:]))
        elif (strand == '-'):
            info['strand'] = '-'
            info['end5'] = t2.end5
            info['end3'] = t2.end3
            info['endA'] = t2.endA
            f_FL.write('>{0}\n{1}\n'.format(r2.id, r2.seq[t2.end5:t2.endA]))
            f_FL_clips.write('>{0}_5p strand:- score:{1}\n{2}\n'.format(r.id, t2.score5, r2.seq[:t2.end5]))
            f_FL_clips.write('>{0}_3p strand:- score:{1}\n{2}\n'.format(r.id, t2.score3, r2.seq[t2.endA:]))
        elif ((t1.score5 + t1.score3) > (t2.score5 + t2.score3)):
            f_nFL.write('>{0} strand:+?\n{1}\n'.format(r.id, r.seq[t1.end5:t1.endA]))
        else:
            f_nFL.write('>{0} strand:-?\n{1}\n'.format(r2.id, r2.seq[t2.end5:t2.endA]))
        writer.writerow(info)
    f_csv.close()
    f_FL.close()
    f_FL_clips.close()
    f_nFL.close()
def trim5p3p(records, output_prefix, seq_5p, seq_3p_rev):
    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')
    writer = DictWriter(f_csv, fieldnames=['id', 'end5', 'end3', 'endA', 'strand'])
    writer.writeheader()
    for r in records:
        r2 = r.reverse_complement()
        r2.id = r.id
        t1 = trim5p3p_helper(r, seq_5p, seq_3p_rev)
        t2 = trim5p3p_helper(r2, seq_5p, seq_3p_rev)
        is_fl_flag1 = ((t1.score5 >= MINSCORE_5P) and (t1.score3 >= MINSCORE_3P) and ((MIN_A_LEN == 0) or (t1.endA != t1.end3)))
        is_fl_flag2 = ((t2.score5 >= MINSCORE_5P) and (t2.score3 >= MINSCORE_3P) and ((MIN_A_LEN == 0) or (t2.endA != t2.end3)))
        if is_fl_flag1:
            if is_fl_flag2:
                if ((t1.score5 + t1.score3) > (t2.score5 + t2.score3)):
                    strand = '+'
                else:
                    strand = '-'
            else:
                strand = '+'
        elif is_fl_flag2:
            strand = '-'
        else:
            strand = 'NA'
        info = {'id': r.id, 'end5': 'NA', 'end3': 'NA', 'endA': 'NA', 'strand': 'NA'}
        if (strand == '+'):
            info['strand'] = '+'
            info['end5'] = t1.end5
            info['end3'] = t1.end3
            info['endA'] = t1.endA
            f_FL.write('>{0}\n{1}\n'.format(r.id, r.seq[t1.end5:t1.endA]))
            f_FL_clips.write('>{0}_5p strand:+ score:{1}\n{2}\n'.format(r.id, t1.score5, r.seq[:t1.end5]))
            f_FL_clips.write('>{0}_3p strand:+ score:{1}\n{2}\n'.format(r.id, t1.score3, r.seq[t1.endA:]))
        elif (strand == '-'):
            info['strand'] = '-'
            info['end5'] = t2.end5
            info['end3'] = t2.end3
            info['endA'] = t2.endA
            f_FL.write('>{0}\n{1}\n'.format(r2.id, r2.seq[t2.end5:t2.endA]))
            f_FL_clips.write('>{0}_5p strand:- score:{1}\n{2}\n'.format(r.id, t2.score5, r2.seq[:t2.end5]))
            f_FL_clips.write('>{0}_3p strand:- score:{1}\n{2}\n'.format(r.id, t2.score3, r2.seq[t2.endA:]))
        elif ((t1.score5 + t1.score3) > (t2.score5 + t2.score3)):
            f_nFL.write('>{0} strand:+?\n{1}\n'.format(r.id, r.seq[t1.end5:t1.endA]))
        else:
            f_nFL.write('>{0} strand:-?\n{1}\n'.format(r2.id, r2.seq[t2.end5:t2.endA]))
        writer.writerow(info)
    f_csv.close()
    f_FL.close()
    f_FL_clips.close()
    f_nFL.close()