def extract_per_individual_states_from_sample_metadata(sample_metadata, state_category, state_values, individual_identifier_category, filter_missing_data=True):
 len_state_values = len(state_values)
 def inner_dict_constructor():
  return ([None] * len_state_values)
 results = defaultdict(inner_dict_constructor)
 for (sample_id, metadata) in sample_metadata.items():
  try:
   individual_id = metadata[individual_identifier_category]
  except KeyError:
   raise KeyError(('%s is not a sample metadata category.' % individual_identifier_category))
  try:
   state_value = metadata[state_category]
  except KeyError:
   raise KeyError(('%s is not a sample metadata category.' % state_category))
  try:
   state_index = state_values.index(state_value)
  except ValueError:
   continue
  results[individual_id][state_index] = sample_id
 if filter_missing_data:
  for (individual_id, sample_ids) in results.items():
   if (None in sample_ids):
    del results[individual_id]
 return results

def extract_per_individual_states_from_sample_metadata ( sample_metadata , state_category , state_values , individual_identifier_category , filter_missing_data = True ) : len_state_values = len ( state_values ) def inner_dict_constructor ( ) : return ( [ None ] * len_state_values ) results = defaultdict ( inner_dict_constructor ) for ( sample_id , metadata ) in sample_metadata . items ( ) : try : individual_id = metadata [ individual_identifier_category ] except KeyError : raise KeyError ( ( '%s is not a sample metadata category.' % individual_identifier_category ) ) try : state_value = metadata [ state_category ] except KeyError : raise KeyError ( ( '%s is not a sample metadata category.' % state_category ) ) try : state_index = state_values . index ( state_value ) except ValueError : continue results [ individual_id ] [ state_index ] = sample_id if filter_missing_data : for ( individual_id , sample_ids ) in results . items ( ) : if ( None in sample_ids ) : del results [ individual_id ] return results
sample_metadata : 2d dictionary mapping sample ids to metadata state_category: metadata category name describing state of interest as a string state_values: ordered list of values of interest in the state_category metadata entry individual_identifier_category: metadata category name describing the individual as a string filter_missing_data: if true .