def make_html(rarefaction_legend_mat, rarefaction_data_mat, xaxisvals, \ imagetype): rarefaction_legend_mat legend_td = [ '<b>Legend</b><div STYLE="border: thin black solid; height: 300px; width: 200px; font-size: 12px; overflow: auto;"><table>' ] summarized_table = [] metric_select_html = [] category_select_html = [] data_table_html = [] metrics = [] category_colors = {} cat_iter = 0 #iterate the legend dictionary for m in natsort(rarefaction_legend_mat): #Create the metric select box options metric_select_html.append('<option value="%s">%s</option>' % (m, m)) metrics.append(m) #iterate through the categories in the legend dictionary for category in natsort(rarefaction_legend_mat[m]['groups']): #Create the select box options if cat_iter == 0: cat_links = [] for i in rarefaction_legend_mat[m]['groups'][category]: cat_links.append(i) category_select_html.append('<option value="%s">%s</option>' % \ (category+'$#!'+'$#!'.join(cat_links),category)) plot_iterator = 0 #iterate through the groups in the legend dictionary and create #the html formatted rows for each category and group for group in natsort( rarefaction_legend_mat[m]['groups'][category]): sample_list = [] category_colors[group]=\ rarefaction_legend_mat[m]['groups'][category][group]['groupcolor'] for sample in natsort(rarefaction_legend_mat[m]['groups'] [category][group]['groupsamples']): sample_list.append('\'' + sample + '\'') plot_iterator = plot_iterator + 1 legend_td.append( '<tr id="%s" name="%s" style="display: none;"><td class="data" onmouseover="document.body.style.cursor=\'pointer\'" onmouseout="document.body.style.cursor=\'default\'" onclick="toggle(%s)" id="%s" name="%s">▶</td><td><input name="%s" type="checkbox" checked="True" onclick="show_hide_category(this)"></td><td style="color:%s">■ </td><td class="data"><b>%s</b></td></tr>' % (m + category, m + category, "'" + m + category + group + "'", m + category + group, ','.join(sample_list), m + category + group + '_raw.' + imagetype, rarefaction_legend_mat[m]['groups'][category][group] ['groupcolor'], group)) for sample in natsort(rarefaction_legend_mat[m]['groups'] [category][group]['groupsamples']): sample = str(sample) legend_td.append( '<tr id="%s" name="%s" style="display: none;"><td class="data" align="right">∟</td><td></td><td style="color:%s">◆</td><td class="data" align="left"><b>%s</b></td></tr>' % (m + category + group + '_raw', m + category + group, rarefaction_legend_mat[m]['samples'][sample]['color'], sample)) cat_iter = 1 #iterate through the data dictionary and format the rows for the html #data table for category in rarefaction_data_mat: data_table_html.append( '<tr name="%s" style="display: none;"><td class="headers">%s</td><td class="headers">Seqs/Sample</td>' % (category, category)) for j in metrics: data_table_html.append( '<td class="headers">%s Ave.</td><td class="headers">%s Err.</td>' % (j, j)) data_table_html.append('</tr>') #data_table_html.append('<tr name="%s" style="display: none;"></tr>' % (category)) for g in natsort(rarefaction_data_mat[category]): for i in range(len(xaxisvals)): data_table_html.append( '<tr name="%s" style="display: none;">' % (category)) data_table_html.append( '<td class="data" bgcolor="%s">%s</td><td class="data">%s</td>' % (category_colors[g], g, xaxisvals[i])) for m in metrics: #bugfix, was rarefaction_data_mat[category][g] data_table_html.append( '<td class="data">%s</td><td class="data">%s</td>' % (rarefaction_data_mat[category][g][m]['ave'][i], rarefaction_data_mat[category][g][m]['err'][i])) data_table_html.append('</tr>') legend_td.append('</table></div></div>') #Create the table that contains the plots and table plot_html = '%s' % ('\n'.join(legend_td)) #insert the formatted rows into the html string at the bottom of this file html_output=HTML % ('.'+imagetype, '\n'.join(metric_select_html), \ '\n'.join(category_select_html), \ plot_html, \ '\n'.join(data_table_html)) return html_output
def make_plots(background_color, label_color, rares, ymax, xmax,\ output_dir, resolution, imagetype,groups,colors,data_colors, \ metric_name,labelname,rarefaction_data_mat,\ rarefaction_legend_mat,sample_dict,sample_data_colors, sample_colors): '''This is the main function for generating the rarefaction plots and html file.''' #Get the alpha rare data raredata = rares #generate the filepath for the image file file_path = os.path.join(output_dir, \ splitext(split(raredata['headers'][0])[1])[0]) #Sort and iterate through the groups for i in natsort(groups): #for k in groups[i]: for j in range(len(raredata['xaxis'])): group_field = i seq_per_sample_field = int(raredata['xaxis'][j]) color_field = data_colors[colors[group_field]].toHex() #If a field is missing, then it means that one of the #samples did not contain enough sequences. #For this case, we will assign the value as n.a. try: average_field = raredata['series'][i][j] error_field = raredata['error'][i][j] if isnan(average_field): error_field = nan except: average_field = nan error_field = nan #Add context to the data dictionary, which will be used in the html if rarefaction_data_mat[labelname].has_key(i): if rarefaction_data_mat[labelname][i].has_key(metric_name): rarefaction_data_mat[labelname][i][metric_name][ 'ave'].append(''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name][ 'err'].append(''.join('%10.3f' % ((raredata['error'][i][j])))) else: rarefaction_data_mat[labelname][i][metric_name] = {} rarefaction_data_mat[labelname][i][metric_name]['ave'] = [] rarefaction_data_mat[labelname][i][metric_name]['err'] = [] rarefaction_data_mat[labelname][i][metric_name][ 'ave'].append(''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name][ 'err'].append(''.join('%10.3f' % ((raredata['error'][i][j])))) else: rarefaction_data_mat[labelname][i] = {} rarefaction_data_mat[labelname][i][metric_name] = {} rarefaction_data_mat[labelname][i][metric_name]['ave'] = [] rarefaction_data_mat[labelname][i][metric_name]['err'] = [] rarefaction_data_mat[labelname][i][metric_name]['ave'].append( ''.join('%10.3f' % ((raredata['series'][i][j])))) rarefaction_data_mat[labelname][i][metric_name]['err'].append( ''.join('%10.3f' % ((raredata['error'][i][j])))) #Create raw plots for each group in a category fpath = os.path.join(output_dir, metric_name + labelname + i) rarefaction_legend_mat=save_single_rarefaction_plots( \ sample_dict, \ imagetype,metric_name, \ sample_data_colors,sample_colors, \ fpath,background_color, \ label_color,resolution,ymax,xmax, rarefaction_legend_mat,groups[i], labelname,i) categories = [k for k in groups] #Create the rarefaction average plot and get updated legend information rarefaction_legend_mat=\ save_single_ave_rarefaction_plots(raredata['xaxis'], raredata['series'], \ raredata['error'], xmax, ymax, categories, \ labelname, imagetype, resolution, data_colors, \ colors, file_path, background_color, label_color, \ rarefaction_legend_mat, metric_name) # return rarefaction_data_mat, rarefaction_legend_mat
def make_averages(color_prefs, data, background_color, label_color, rares, \ output_dir,resolution,imagetype,ymax,make_webpage): '''This is the main function, which takes the rarefaction files, calls the functions to make plots and formatting the output html.''' rarelines = [] rarefaction_legend_mat = {} if ymax: user_ymax = True else: user_ymax = False if make_webpage: #Create the directories, where plots and data will be written all_output_dir = os.path.join(output_dir, 'html_plots') create_dir(all_output_dir) ave_output_dir = os.path.join(output_dir, 'average_plots') create_dir(ave_output_dir) ave_data_file_path = os.path.join(output_dir, 'average_tables') create_dir(ave_data_file_path, False) metric_num = 0 rarefaction_legend_mat = {} rarefaction_data_mat = {} rare_num = 0 #Iterate through the rarefaction files for r in natsort(rares): raredata = rares[r] metric_name = r.split('.')[0] #convert the rarefaction data into variables col_headers, comments, rarefaction_fn, rarefaction_data = rares[r] #Here we only need to perform these steps once, since the data is #the same for all rarefaction files if rare_num == 0: #Remove samples from the mapping file, which contain no data after #rarefaction updated_mapping = [] for j in data['map']: #Add the mapping header if j[0] == 'SampleID': updated_mapping.append(j) #Determine if the sample exists in the rarefaction file for i in col_headers[3:]: if j[0] == i: updated_mapping.append(j) #Get the groups and colors for the updated mapping file groups_and_colors = iter_color_groups(updated_mapping, color_prefs) groups_and_colors = list(groups_and_colors) #parse the rarefaction data rare_mat_trans, seqs_per_samp, sampleIDs = \ get_rarefaction_data(rarefaction_data, col_headers) rarefaction_legend_mat[metric_name] = {} #Create dictionary variables and get the colors for each Sample sample_colors = None rarefaction_legend_mat[metric_name]['groups'] = {} for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] #Create a legend dictionary for html output rarefaction_legend_mat[metric_name]['groups'][labelname] = {} #If this is the first time iterating through the rarefaction data #create a data dictionary for html output if rare_num == 0: rarefaction_data_mat[labelname] = {} #If the labelname is SampleID, use the colors assigned if labelname == 'SampleID': sample_colors = groups_and_colors[i][2] sample_data_colors = groups_and_colors[i][3] rare_num = 1 #If sample colors were not assigned, create a list of sample colors if not sample_colors: samples_and_colors=iter_color_groups(updated_mapping, \ {'SampleID': {'column': 'SampleID', 'colors': \ (('red', (0, 100, 100)), ('blue', (240, 100, 100)))}}) samples_and_colors = list(samples_and_colors) sample_colors = samples_and_colors[0][2] sample_data_colors = samples_and_colors[0][3] sample_dict = {} #Create a dictionary containing the samples for i, sid in enumerate(sampleIDs): if sid in (i[0] for i in updated_mapping): sample_dict[sid] = {} for j, seq in enumerate(seqs_per_samp): try: sample_dict[sid][seq].append(rare_mat_trans[i][j]) except (KeyError): sample_dict[sid][seq] = [] sample_dict[sid][seq].append(rare_mat_trans[i][j]) #convert xvals to float xaxisvals = [float(x) for x in set(seqs_per_samp)] xaxisvals.sort() #get the rarefaction averages rare_mat_ave = ave_seqs_per_sample(rare_mat_trans, seqs_per_samp, \ sampleIDs) #calculate the max xval xmax = max(xaxisvals) + (xaxisvals[len(xaxisvals)-1] - \ xaxisvals[len(xaxisvals)-2]) ''' #get the overall average #overall_average = get_overall_averages(rare_mat_ave, sampleIDs) rarelines.append("#" + r + '\n') for s in sampleIDs: rarelines.append('%f'%overall_average[s] + '\n') ''' if not user_ymax: ymax = 0 for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] groups = groups_and_colors[i][1] colors = groups_and_colors[i][2] data_colors = groups_and_colors[i][3] ave_file_path = os.path.join(ave_data_file_path, metric_name) #save the rarefaction averages rare_lines=save_rarefaction_data(rare_mat_ave, xaxisvals, xmax,\ labelname, colors, r, data_colors, groups) #write out the rarefaction average data open(ave_file_path + labelname + '.txt', 'w').writelines(rare_lines) #take the formatted rarefaction averages and format the results rares_data = parse_rarefaction_data( \ ''.join(rare_lines[:]).split('\n')) #determine the ymax based on the average data #multiple the ymax, since the dots can end up on the border new_ymax=(max([max(v) for v in rares_data['series'].values()])+\ max([max(e) for e in rares_data['error'].values()])) * 1.15 if isnan(new_ymax): new_ymax=(max([max(v) for v in \ rares_data['series'].values()])) * 1.15 if new_ymax > ymax: ymax = new_ymax iterator_num = 0 #iterate through the groups for i in range(len(groups_and_colors)): labelname = groups_and_colors[i][0] groups = groups_and_colors[i][1] colors = groups_and_colors[i][2] data_colors = groups_and_colors[i][3] data_color_order = groups_and_colors[i][4] #save the rarefaction averages rare_lines=save_rarefaction_data(rare_mat_ave, xaxisvals, xmax, \ labelname, colors, r, data_colors, groups) #take the formatted rarefaction averages and format the results rares_data = parse_rarefaction_data( \ ''.join(rare_lines[:]).split('\n')) if make_webpage: if iterator_num == 0: rarefaction_legend_mat[metric_name]['samples'] = {} for o in sample_dict: rarefaction_legend_mat[metric_name]['samples'][o] = {} #Add values to the legend dictionary rarefaction_legend_mat[metric_name]['samples'][o][ 'color'] = sample_data_colors[ sample_colors[o]].toHex() iterator_num = 1 #Iterate through the groups and create the legend dictionary for g in groups: #generate the filepath for the image file file_path = os.path.join(all_output_dir, \ metric_name+labelname+g) #create a dictionary of samples and their colors rarefaction_legend_mat[metric_name]['groups'][labelname][ g] = {} rarefaction_legend_mat[metric_name]['groups'][labelname][ g]['groupsamples'] = groups[g] rarefaction_legend_mat[metric_name]['groups'][labelname][g]['groupcolor']=\ data_colors[colors[g]].toHex() #Create the individual category average plots rarefaction_data_mat,rarefaction_legend_mat=make_plots(\ background_color, label_color, \ rares_data, ymax, xmax,all_output_dir, \ resolution, imagetype,groups, colors, \ data_colors,metric_name,labelname, \ rarefaction_data_mat,rarefaction_legend_mat, sample_dict,sample_data_colors, sample_colors) #generate the filepath for the image file file_path = os.path.join(ave_output_dir, \ splitext(split(rares_data['headers'][0])[1])[0]) #Create the average plots categories = [k for k in groups] save_ave_rarefaction_plots(rares_data['xaxis'], rares_data['series'], \ rares_data['error'], xmax, ymax, categories, \ labelname, imagetype, resolution, data_colors, \ colors, file_path, background_color, label_color, \ metric_name) else: #generate the filepath for the image file file_path = os.path.join(ave_output_dir, \ splitext(split(rares_data['headers'][0])[1])[0]) categories = [k for k in groups] save_ave_rarefaction_plots(rares_data['xaxis'], rares_data['series'], \ rares_data['error'], xmax, ymax, categories, \ labelname, imagetype, resolution, data_colors, \ colors, file_path, background_color, label_color, \ metric_name) if make_webpage: #format the html output html_output=make_html(rarefaction_legend_mat, \ rarefaction_data_mat,xaxisvals,imagetype) else: html_output = None return html_output
def make_mage_output(groups, colors, coord_header, coords, pct_var, \ background_color,label_color,data_colors, \ taxa=None, custom_axes=None,name='', \ radius=None, alpha=.75, num_coords=10,scaled=False, \ coord_scale=1.05, edges=None, coords_low=None, \ coords_high=None, ellipsoid_prefs=None, user_supplied_edges=False): """Convert groups, colors, coords and percent var into mage format""" result = [] #Scale the coords and generate header labels if scaled: scalars = pct_var if custom_axes: # create a dummy vector of ones to avoid scaling custom axes custom_scalars = scalars[0] * np.ones(len(custom_axes)) scalars = np.append(custom_scalars, scalars) coords = scale_pc_data_matrix(coords, scalars) if not coords_low is None: coords_low = scale_pc_data_matrix(coords_low, scalars) if not coords_high is None: coords_high = scale_pc_data_matrix(coords_high, scalars) header_suffix = '_scaled' else: header_suffix = '_unscaled' if radius is None: radius = auto_radius(coords) maxes = coords.max(0)[:num_coords] mins = coords.min(0)[:num_coords] pct_var = pct_var[:num_coords] #scale from fraction #check that we didn't get fewer dimensions than we wanted if len(mins) < num_coords: num_coords = len(mins) min_maxes = flatten(zip(mins, maxes)) if custom_axes: axis_names = [ 'PC%s' % (i + 1) for i in xrange(num_coords - len(custom_axes)) ] axis_names = custom_axes + axis_names else: axis_names = ['PC%s' % (i + 1) for i in xrange(num_coords)] #Write the header information result.append('@kinemage {%s}' % (name + header_suffix)) result.append('@dimension ' + ' '.join(['{%s}' % (name) for name in axis_names])) result.append('@dimminmax ' + ' '.join(map(str, min_maxes))) result.append('@master {points}') result.append('@master {labels}') if edges: result.append('@master {edges}') if not taxa is None: result.append('@master {taxa_points}') result.append('@master {taxa_labels}') for name, color in sorted(data_colors.items()): result.append(color.toMage()) if background_color == 'white': result.append('@whitebackground') result.append('@hsvcolor {black} 0.0 0.0 0.0') else: result.append('@hsvcolor {white} 180.0 0.0 100.0') #Write the groups, colors and coords coord_dict = dict(zip(coord_header, coords)) if not coords_low is None: coord_low_dict = dict(zip(coord_header, coords_low)) if not coords_high is None: coord_high_dict = dict(zip(coord_header, coords_high)) for group_name in natsort(groups): ids = groups[group_name] result.append('@group {%s (n=%s)} collapsible' % (group_name, len(ids))) color = colors[group_name] coord_lines = [] for id_ in sorted(ids): if id_ in coord_dict: coord_lines.append('{%s} %s' % \ (id_, ' '.join(map(str, coord_dict[id_][:num_coords])))) # create list of balls, one for each sample result.append('@balllist color=%s radius=%s alpha=%s dimension=%s \ master={points} nobutton' % (color, radius, alpha, num_coords)) result.append('\n'.join(coord_lines)) # make ellipsoids if low and high coord bounds were received if (not coords_low is None) and (not coords_high is None): # create one trianglelist for each sample to define ellipsoids result += make_mage_ellipsoids(ids, coord_dict, coord_low_dict, coord_high_dict, color, ellipsoid_prefs) # create list of labels result.append('@labellist color=%s radius=%s alpha=%s dimension=%s \ master={labels} nobutton' % (color, radius, alpha, num_coords)) result.append('\n'.join(coord_lines)) if not taxa is None: result += make_mage_taxa(taxa, num_coords, pct_var, scaled=scaled, scalars=None, radius=radius) #Write the axes on the bottom of the graph result.append('@group {axes} collapsible') state = 'on' axis_mins = mins * coord_scale axis_maxes = maxes * coord_scale if not custom_axes: custom_axes = [] # draw each axis for i in xrange(num_coords): if i == 3: state = 'off' result.append('@vectorlist {%s line} dimension=%s %s' % \ (axis_names[i], num_coords, state)) result.append(' '.join(map(str, axis_mins)) + ' ' + label_color) end = axis_mins.copy() end[i] = axis_maxes[i] result.append(' '.join(map(str, end)) + ' ' + label_color) end[i] *= coord_scale #add scale factor to offset labels a little # custom axes come first, no "percent variance" shown if i < len(custom_axes): result.append('@labellist {%s} dimension=%s %s' % \ (axis_names[i], num_coords, state)) result.append( ('{%s}' % (axis_names[i])) + \ ' '.join(map(str, end)) + ' ' + label_color) # if all custom axes have been drawn, draw normal PC axes else: pct = pct_var[i - len(custom_axes)] result.append('@labellist {%s (%0.2g%%)} dimension=%s %s' % \ (axis_names[i], pct, num_coords, state)) result.append( ('{%s (%0.2g%%)}' % (axis_names[i], pct)) + \ ' '.join(map(str, end)) + ' ' + label_color) #Write edges if requested if edges: result += make_edges_output(coord_dict, edges, num_coords, label_color, user_supplied_edges=user_supplied_edges) return result
def get_selected_column_values_old(controlled_col,col,table,user_id): # if the column is not controlled, then we must look in the database # for the public values provided in that column if not controlled_col: # Get the user details user_details = data_access.getUserDetails(user_id) if not user_details: raise ValueError('No details found for this user') is_admin = user_details['is_admin'] # Handle the different table names, since the inner joins depend on which # table is being traversed if is_admin: if str(table)=='STUDY': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.study_id=s.study_id inner join study st on s.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (col,table) elif str(table)=='SAMPLE': statement='select distinct t."%s" from "%s" t inner join study st on t.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (col,table) elif str(table)=='SEQUENCE_PREP': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (col,table) elif str(table)=='HOST_ASSOC_VERTIBRATE' or table=='HOST_ASSOC_PLANT' or table=='HOST_SAMPLE' or table=='HUMAN_ASSOCIATED': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (str(col),str(table)) elif str(table)=='HOST': statement='select distinct t."%s" from "%s" t inner join "HOST_SAMPLE" h on t.host_id=h.host_id inner join "SAMPLE" s on h.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (str(col),str(table)) else: statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join sff.analysis an on st.study_id=an.study_id where st.metadata_complete=\'y\'' % (col,table) else: if str(table)=='STUDY': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.study_id=s.study_id inner join study st on s.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (s."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (col,table,user_id) elif str(table)=='SAMPLE': statement='select distinct t."%s" from "%s" t inner join study st on t.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (t."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (col,table,user_id) elif str(table)=='SEQUENCE_PREP': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (s."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (col,table,user_id) elif str(table)=='HOST_ASSOC_VERTIBRATE' or table=='HOST_ASSOC_PLANT' or table=='HOST_SAMPLE' or table=='HUMAN_ASSOCIATED': statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (s."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (str(col),str(table),user_id) elif str(table)=='HOST': statement='select distinct t."%s" from "%s" t inner join "HOST_SAMPLE" h on t.host_id=h.host_id inner join "SAMPLE" s on h.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (s."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (str(col),str(table),user_id) else: statement='select distinct t."%s" from "%s" t inner join "SAMPLE" s on t.sample_id=s.sample_id inner join study st on s.study_id=st.study_id inner join user_study us on st.study_id=us.study_id inner join sff.analysis an on st.study_id=an.study_id where (s."PUBLIC" = \'y\' or us.web_app_user_id=%s) and st.metadata_complete=\'y\'' % (col,table,user_id) # Run the statement con = data_access.getMetadataDatabaseConnection() cur = con.cursor() results = cur.execute(statement) #put the column values into a dictionary so we can run natural sort on the list col_values={} for i in results: if i[0] <> None: col_values[str(i[0])]=str(i[0]) else: # get the controlled terms results=data_access.getValidControlledVocabTerms(col) #put the column values into a dictionary so we can run natural sort on the list col_values={} for i in results: if i[0] <> None: col_values[str(i[1])]=str(i[1]) # sort the column values col_values=natsort(col_values) return col_values
def test_natsort(self): """natsort should perform numeric comparisons on strings""" s = 'sample1 sample2 sample11 sample12'.split() self.assertEqual(natsort(s), 'sample1 sample2 sample11 sample12'.split())