def db( meta: Metadata, folder: str, rank: str = "genus", threads: int = 1 ) -> JSONDirectory: """Create a model database from a set of SBML files.""" meta = meta.to_dataframe() meta.columns = meta.columns.str.lower() if not REQ_FIELDS.isin(meta.columns).all(): raise ValueError( "Metadata File needs to have the following " "columns %s." % ", ".join(REQ_FIELDS) ) meta["id"] = meta.index files = os.listdir(folder) meta["file"] = meta.id + ".xml" bad = meta.file.apply(lambda x: x not in files) if any(bad): raise ValueError( "The following models are in the Metadata but not " "in the folder: %s" % meta.file[bad] ) meta = meta.groupby(rank).apply(reduce_group).reset_index(drop=True) meta.index = meta[rank] json_dir = JSONDirectory() args = [ (tid, row, str(json_dir.json_files.path_maker(model_id=tid)), folder) for tid, row in meta.iterrows() ] workflow(_summarize_models, args, threads) meta["file"] = meta.index + ".json" meta["id"] = meta.index meta["summary_rank"] = rank meta.to_csv(json_dir.manifest.path_maker(), index=False) return json_dir
def draw_interactive_map(output_dir: str, metadata: qiime2.Metadata, column: str = None, latitude: str = 'Latitude', longitude: str = 'Longitude', color_palette: str = 'rainbow', discrete: bool = False, missing_data: str = 'error'): metadata = _load_and_validate( metadata, [column, latitude, longitude], ['column', 'latitude', 'longitude'], missing_data) lat_0, lat_1, lon_0, lon_1 = get_max_extent( metadata[latitude], metadata[longitude]) loc_min, loc_max = [lon_0, lat_0], [lon_1, lat_1] cmap = plt.get_cmap(color_palette) data = [] # If column is numeric, color points by column if np.issubdtype(metadata[column].dtype, np.number) and not discrete: metadata[column] = metadata[column].astype(float) normalize = mcolors.Normalize( vmin=metadata[column].min(), vmax=metadata[column].max()) scalarmappaple = cm.ScalarMappable( norm=normalize, cmap=cmap) scalarmappaple.set_array(metadata[column]) fig, ax = plt.subplots() plt.colorbar(scalarmappaple).set_label(column) ax.remove() metadata.sort_values(by=column, ascending=False, inplace=True) for i, row in metadata.iterrows(): data.append({ 'sample_id': i, column: row[column], 'latitude': row[latitude], 'longitude': row[longitude], 'color': mcolors.to_hex(scalarmappaple.to_rgba(row[column])) }) # if column is not numeric, color discretely else: groups = metadata[column].unique() len_groups = len(groups) colors = {g: mcolors.to_hex(c) for g, c in zip( groups, cmap(np.linspace(0, 1, len(groups))))} for i, row in metadata.iterrows(): data.append({ 'sample_id': i, column: row[column], 'latitude': row[latitude], 'longitude': row[longitude], 'color': colors[row[column]] }) fig = plt.figure(figsize=[len_groups * 0.05, len_groups/2]) ax = fig.add_axes([0, 0, 1, 1]) for idx, (g, color) in enumerate(colors.items()): r = mpatch.Rectangle((0, idx), 1, 1, color=color) _ = ax.text(2, idx+.5, ' %s' % g, va='center', fontsize=10) ax.add_patch(r) ax.axhline(idx, color='k') ax.set_xlim(0, 3) ax.set_ylim(0, idx + 2) ax.axis('off') save_animated_map(output_dir, loc_min, loc_max, data, column)