def truncate_rev_primers(fasta_f, output_fp, reverse_primers, truncate_option='truncate_only', primer_mismatches=2):
 log_data = {'sample_id_not_found': 0, 'reverse_primer_not_found': 0, 'total_seqs': 0, 'seqs_written': 0}
 for (label, seq) in parse_fasta(fasta_f):
  curr_label = label.split('_')[0]
  log_data['total_seqs'] += 1
  try:
   curr_rev_primer = reverse_primers[curr_label]
  except KeyError:
   log_data['sample_id_not_found'] += 1
   output_fp.write(('>%s\n%s\n' % (label, seq)))
   log_data['seqs_written'] += 1
   continue
  mm_tests = {}
  for rev_primer in curr_rev_primer:
   (rev_primer_mm, rev_primer_index) = local_align_primer_seq(rev_primer, seq)
   mm_tests[rev_primer_mm] = rev_primer_index
  rev_primer_mm = min(mm_tests.keys())
  rev_primer_index = mm_tests[rev_primer_mm]
  if (rev_primer_mm > primer_mismatches):
   if (truncate_option == 'truncate_remove'):
    log_data['reverse_primer_not_found'] += 1
   else:
    log_data['reverse_primer_not_found'] += 1
    log_data['seqs_written'] += 1
    output_fp.write(('>%s\n%s\n' % (label, seq)))
  elif (rev_primer_index > 0):
   log_data['seqs_written'] += 1
   output_fp.write(('>%s\n%s\n' % (label, seq[0:rev_primer_index])))
 return log_data

def truncate_rev_primers ( fasta_f , output_fp , reverse_primers , truncate_option = 'truncate_only' , primer_mismatches = 2 ) : log_data = { 'sample_id_not_found' : 0 , 'reverse_primer_not_found' : 0 , 'total_seqs' : 0 , 'seqs_written' : 0 } for ( label , seq ) in parse_fasta ( fasta_f ) : curr_label = label . split ( '_' ) [ 0 ] log_data [ 'total_seqs' ] += 1 try : curr_rev_primer = reverse_primers [ curr_label ] except KeyError : log_data [ 'sample_id_not_found' ] += 1 output_fp . write ( ( '>%s\n%s\n' % ( label , seq ) ) ) log_data [ 'seqs_written' ] += 1 continue mm_tests = { } for rev_primer in curr_rev_primer : ( rev_primer_mm , rev_primer_index ) = local_align_primer_seq ( rev_primer , seq ) mm_tests [ rev_primer_mm ] = rev_primer_index rev_primer_mm = min ( mm_tests . keys ( ) ) rev_primer_index = mm_tests [ rev_primer_mm ] if ( rev_primer_mm > primer_mismatches ) : if ( truncate_option == 'truncate_remove' ) : log_data [ 'reverse_primer_not_found' ] += 1 else : log_data [ 'reverse_primer_not_found' ] += 1 log_data [ 'seqs_written' ] += 1 output_fp . write ( ( '>%s\n%s\n' % ( label , seq ) ) ) elif ( rev_primer_index > 0 ) : log_data [ 'seqs_written' ] += 1 output_fp . write ( ( '>%s\n%s\n' % ( label , seq [ 0 : rev_primer_index ] ) ) ) return log_data
locally aligns reverse primers .