def correct_mapping_data(mapping_data, header, char_replace='_'):
 corrected_data = deepcopy(mapping_data)
 valid_sample_id_chars = ((letters + digits) + '.')
 valid_data_field_chars = ((letters + digits) + '+-%./ :,;_')
 sample_id_char_replace = '.'
 sample_id_field = 'SampleID'
 fields_to_skip = ['BarcodeSequence', 'LinkerPrimerSequence', 'ReversePrimer']
 try:
  sample_id_ix = header.index(sample_id_field)
 except ValueError:
  sample_id_ix = (-1)
 fields_to_skip_ixs = []
 for curr_field in fields_to_skip:
  try:
   fields_to_skip_ixs.append(header.index(curr_field))
  except ValueError:
   continue
 for curr_row in range(len(mapping_data)):
  for curr_col in range(len(mapping_data[curr_row])):
   if (curr_col in fields_to_skip_ixs):
    continue
   elif ((sample_id_ix != (-1)) and (curr_col == sample_id_ix)):
    curr_replacement = sample_id_char_replace
    curr_valid_chars = valid_sample_id_chars
   else:
    curr_replacement = char_replace
    curr_valid_chars = valid_data_field_chars
   curr_corrected_field = ''
   for curr_char in mapping_data[curr_row][curr_col].replace('\n', ''):
    if (curr_char not in curr_valid_chars):
     curr_corrected_field += curr_replacement
    else:
     curr_corrected_field += curr_char
   corrected_data[curr_row][curr_col] = curr_corrected_field
 return corrected_data

def correct_mapping_data ( mapping_data , header , char_replace = '_' ) : corrected_data = deepcopy ( mapping_data ) valid_sample_id_chars = ( ( letters + digits ) + '.' ) valid_data_field_chars = ( ( letters + digits ) + '+-%./ :,;_' ) sample_id_char_replace = '.' sample_id_field = 'SampleID' fields_to_skip = [ 'BarcodeSequence' , 'LinkerPrimerSequence' , 'ReversePrimer' ] try : sample_id_ix = header . index ( sample_id_field ) except ValueError : sample_id_ix = ( - 1 ) fields_to_skip_ixs = [ ] for curr_field in fields_to_skip : try : fields_to_skip_ixs . append ( header . index ( curr_field ) ) except ValueError : continue for curr_row in range ( len ( mapping_data ) ) : for curr_col in range ( len ( mapping_data [ curr_row ] ) ) : if ( curr_col in fields_to_skip_ixs ) : continue elif ( ( sample_id_ix != ( - 1 ) ) and ( curr_col == sample_id_ix ) ) : curr_replacement = sample_id_char_replace curr_valid_chars = valid_sample_id_chars else : curr_replacement = char_replace curr_valid_chars = valid_data_field_chars curr_corrected_field = '' for curr_char in mapping_data [ curr_row ] [ curr_col ] . replace ( '\n' , '' ) : if ( curr_char not in curr_valid_chars ) : curr_corrected_field += curr_replacement else : curr_corrected_field += curr_char corrected_data [ curr_row ] [ curr_col ] = curr_corrected_field return corrected_data
replaces invalid characters in mapping data mapping_data: list of lists of raw metadata mapping file data header: list of header strings char_replace: character used to replace invalid characters in data fields .