def test_regulatory_graph_layout_remains() -> None: """ Testing if the new graph gets the correct layout of the old graph. Returns: None Raises: AssertionError: If the node coordinates of the new graph differ from expected coordinates. """ book = ExcelBook(PHEROMONE_XLS) reg_graph = RegulatoryGraph(book.rxncon_system) gml_system = XGMML(reg_graph.to_graph(), "pheromone_layout") mapped_layout = map_layout2xgmml(gml_system.to_string(), PHEROMONE_XGMML) xmldoc_no_layout = minidom.parseString(mapped_layout) xmldoc_no_layout_info = _get_labels_and_coordinates_dict(xmldoc_no_layout) xmldoc_expected_layout = minidom.parse(PHEROMONE_XGMML) xmldoc_expected_layout_info = _get_labels_and_coordinates_dict( xmldoc_expected_layout) assert all(xmldoc_no_layout_info[no_layout_node] == xmldoc_expected_layout_info[no_layout_node] for no_layout_node in xmldoc_no_layout_info) assert all(xmldoc_no_layout_info[expected_layout_node] == xmldoc_expected_layout_info[expected_layout_node] for expected_layout_node in xmldoc_expected_layout_info)
def write_bngl(excel_filename: str, base_name=None): if not base_name: base_name = os.path.splitext(os.path.basename(excel_filename))[0] base_path = os.path.dirname(excel_filename) bngl_model_filename = os.path.join(base_path, '{0}.bngl'.format(base_name)) print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) rxncon_system = excel_book.rxncon_system print( 'Constructed rxncon system: [{} reactions], [{} contingencies]'.format( len(rxncon_system.reactions), len(rxncon_system.contingencies))) print('Generating BNGL output ...') rbm = rule_based_model_from_rxncon(rxncon_system) print( 'Constructed rule-based model: [{} molecule types], [{} rules], [{} observables]' .format(len(rbm.mol_defs), len(rbm.rules), len(rbm.observables))) model_str = bngl_from_rule_based_model(rbm) print('Writing BNGL model file [{}] ...'.format(bngl_model_filename)) with open(bngl_model_filename, mode='w') as f: f.write(model_str)
def file_detail(request, id, compare_dict=None): instance = File.objects.get(id=id) slug = File.objects.filter(id=id).values("slug") project_files = File.objects.filter(slug=slug).order_by("-updated") # if instance.rxncon_system: # pickled_rxncon_system = Rxncon_system.objects.get(project_id=id, project_type="File") # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) # else: # rxncon_system = None book = ExcelBook(instance.get_absolute_path()) rxncon_system = book.rxncon_system context_data = { "project_files": project_files, "title": instance.project_name, "instance": instance, "loaded": instance.loaded, } if rxncon_system: context_data["nr_reactions"] = len(rxncon_system.reactions) context_data["nr_contingencies"] = len(rxncon_system.contingencies) if compare_dict: context_data.update(compare_dict) return render(request, "file_detail.html", context_data)
def quick_compare(request, id): loaded = File.objects.filter(loaded=True) if not loaded: # Quick loaded = Quick.objects.filter(loaded=True)[0] # pickled_rxncon_system = Rxncon_system.objects.get(project_id=loaded[0].id, project_type="Quick") book = RxnconQuick(loaded.quick_input) else: # File # pickled_rxncon_system = Rxncon_system.objects.get(project_id=loaded[0].id, project_type="File") loaded = loaded[0] book = ExcelBook(loaded.get_absolute_path()) # loaded_rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) loaded_rxncon_system = book.rxncon_system differences = compare_systems(request, id, loaded_rxncon_system, called_from="Quick") compare_dict = { "compare_nr_reactions": len(loaded_rxncon_system.reactions), "compare_nr_contingencies": len(loaded_rxncon_system.contingencies), "nr_different_reactions": differences["rxns"], "nr_different_contingencies": differences["cnts"], } return quick_detail(request, id, compare_dict)
def test_shuffled_columns() -> None: rxncon_system = ExcelBook(SHUFFLED_COLUMNS_XLS).rxncon_system expected_reactions = ['A_[x]_ppi+_B_[y]', 'A_[x]_ppi-_B_[y]', 'C_p+_A_[(z)]'] for rxn in expected_reactions: assert reaction_from_str(rxn) in rxncon_system.reactions
def test_missing_unnecessary_sheet() -> None: rxncon_system = ExcelBook(MISSING_UNNECESSARY_SHEET_XLS).rxncon_system expected_reactions = ['A_[x]_ppi+_B_[y]', 'A_[x]_ppi-_B_[y]', 'C_p+_A_[(z)]'] for rxn in expected_reactions: assert reaction_from_str(rxn) in rxncon_system.reactions
def create_rxncon_system(request, system_type, system_id): if system_type == "File": system = File.objects.filter(id=system_id)[0] book = ExcelBook(system.get_absolute_path()) else: system = Quick.objects.filter(id=system_id)[0] book = RxnconQuick(system.quick_input) return book.rxncon_system
def write_xgmml(excel_filename: str, output=None, layout_template_file=None): """ creating the xgmml file from an excel input and writing it into a new file. Args: excel_filename: Name of the excel input file. output: Name of the new output. layout_template_file: Name of the layout template file. Returns: None """ if not output: output = os.path.splitext(os.path.basename(excel_filename))[0] base_path = os.path.dirname(excel_filename) suffix = '_srg' if not output.endswith('.xgmml'): output = '{0}{1}.xgmml'.format(output, suffix) else: base_name = output.split('.xgmml')[0] output = "{0}{1}.{2}".format(base_name, suffix, 'xgmml') graph_filename = os.path.join(base_path, '{0}'.format(output)) print('graph_filename: ', graph_filename) _file_path_existence(graph_filename) print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) rxncon_system = excel_book.rxncon_system print( 'Constructed rxncon system: [{} reactions], [{} contingencies]'.format( len(rxncon_system.reactions), len(rxncon_system.contingencies))) print('Generating reaction species graph output...') reg_system = SpeciesReactionGraph(rxncon_system) graph = reg_system.to_graph() if layout_template_file: print('Writing layout information from [{0}] to graph file [{1}] ...'. format(layout_template_file, graph_filename)) gml_system = XGMML(graph, "{}".format(output)) graph = map_layout2xgmml(gml_system.to_string(), layout_template_file) print('Writing reaction species graph file [{}] ...'.format( graph_filename)) with open(graph_filename, "w") as graph_handle: graph_handle.write(graph) else: print('Writing reaction species graph file [{}] ...'.format( graph_filename)) gml_system = XGMML(graph, "{}".format(output)) gml_system.to_file(graph_filename)
def test_additional_modifiers() -> None: with pytest.raises(ValueError): state_from_str('A_[(x)]-{bladiebla}') rxncon_system = ExcelBook(ADDITIONAL_MODIFIERS_XLS).rxncon_system assert str(state_from_str('A_[(x)]-{bladiebla}')) == 'A_[(x)]-{bladiebla}' initialize_state_modifiers() with pytest.raises(ValueError): state_from_str('A_[(x)]-{bladiebla}')
def post(self, request, system_id=None): self.system_id = system_id self.request = request self.form = RuleForm(self.request.POST or None) if self.form.is_valid(): media_url = settings.MEDIA_URL media_root = settings.MEDIA_ROOT loaded = File.objects.filter(loaded=True) if not loaded: system_type = "Quick" system = Quick.objects.filter(id=system_id)[0] project_name = system.name book = RxnconQuick(system.quick_input) else: system_type = "File" system = File.objects.filter(id=system_id)[0] project_name = system.project_name book = ExcelBook(system.get_absolute_path()) bngl_model_filename = system.slug + "_model.bngl" model_path = os.path.join(media_root, system.slug, "rule_based", bngl_model_filename) if not check_filepath(request, model_path, system, media_root): if system_type == "Quick": return quick_detail(request, system_id) else: return file_detail(request, system_id) # pickled_rxncon_system = Rxncon_system.objects.get(project_id=system_id, project_type=system_type) # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) rxncon_system = book.rxncon_system rbm = rule_based_model_from_rxncon(rxncon_system) model_str = bngl_from_rule_based_model(rbm) if not os.path.exists(os.path.join(media_root, system.slug, "rule_based")): os.mkdir(os.path.join(media_root, system.slug, "rule_based")) with open(model_path, mode='w') as f: f.write(model_str) r = Rule_based_from_rxnconsys(project_name=project_name, model_path=model_path, comment=request.POST.get('comment')) r.save() messages.info(request, "BoolNet files for project '" + r.project_name + "' successfully created.") if system_type == "Quick": Quick.objects.filter(id=system_id).update(rule_based_model=r) return quick_detail(request, system_id) else: File.objects.filter(id=system_id).update(rule_based_model=r) return file_detail(request, system_id)
def test_additional_reactions() -> None: with pytest.raises(SyntaxError): reaction_from_str('A_smurf+_B_[(r)]') rxncon_system = ExcelBook(ADDITIONAL_REACTIONS_XLS).rxncon_system rxn = reaction_from_str('A_smurf+_B_[(r)]') assert rxn.produced_states == [state_from_str('B_[(r)]-{smurf}')] assert rxn.consumed_states == [state_from_str('B_[(r)]-{0}')] initialize_reaction_defs() with pytest.raises(SyntaxError): reaction_from_str('A_smurf+_B_[(r)]')
def write_boolnet(excel_filename: str, smoothing_strategy: SmoothingStrategy, knockout_strategy: KnockoutStrategy, overexpression_strategy: OverexpressionStrategy, k_plus_strategy: QuantitativeContingencyStrategy, k_minus_strategy: QuantitativeContingencyStrategy, base_name: Optional[str] = None): if not base_name: base_name = os.path.splitext(os.path.basename(excel_filename))[0] base_path = os.path.dirname(excel_filename) boolnet_model_filename = os.path.join(base_path, '{0}.boolnet'.format(base_name)) boolnet_symbol_filename = os.path.join(base_path, '{0}_symbols.csv'.format(base_name)) boolnet_initial_val_filename = os.path.join( base_path, '{0}_initial_vals.csv'.format(base_name)) print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) rxncon_system = excel_book.rxncon_system print( 'Constructed rxncon system: [{} reactions], [{} contingencies]'.format( len(rxncon_system.reactions), len(rxncon_system.contingencies))) print('Generating BoolNet output using smoothing strategy [{}] ...'.format( smoothing_strategy.name)) model_str, symbol_str, initial_val_str = boolnet_strs_from_rxncon( rxncon_system, smoothing_strategy, knockout_strategy, overexpression_strategy, k_plus_strategy, k_minus_strategy) print('Writing BoolNet model file [{}] ...'.format(boolnet_model_filename)) with open(boolnet_model_filename, mode='w') as f: f.write(model_str) print( 'Writing BoolNet symbol file [{}] ...'.format(boolnet_symbol_filename)) with open(boolnet_symbol_filename, mode='w') as f: f.write(symbol_str) print('Writing BoolNet initial value file [{}] ...'.format( boolnet_initial_val_filename)) with open(boolnet_initial_val_filename, mode='w') as f: f.write(initial_val_str)
def regGraphFile(request, system_id=None): form = regGraphFileForm(request.POST or None) if form.is_valid(): media_url = settings.MEDIA_URL media_root = settings.MEDIA_ROOT system = File.objects.get(id=system_id) graph_file_name = system.slug + "_" + system.get_filename().split( ".")[0] + "_regGraph.xgmml" graph_file_path = os.path.join(media_root, system.slug, "graphs", graph_file_name) if not check_filepath(request, graph_file_path, system, media_root): return file_detail(request, system_id) # pickled_rxncon_system = Rxncon_system.objects.get(project_id=system_id, project_type="File") # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) book = ExcelBook(system.get_absolute_path()) rxncon_system = book.rxncon_system graph = regulatory_graph.RegulatoryGraph(rxncon_system).to_graph() xgmml_graph = graphML.XGMML(graph, system.slug) graph_file = xgmml_graph.to_file(graph_file_path) graph_string = xgmml_graph.to_string() if request.FILES.get('template'): graph_string = apply_template_layout(request, graph_file_path, graph_string) g = Graph_from_File(project_name=system.project_name, graph_file=graph_file_path, graph_string=graph_string, comment=request.POST.get('comment')) g.save() File.objects.filter(id=system_id).update(reg_graph=g) messages.info( request, "Regulatory graph for project '" + g.project_name + "' successfully created.") return file_detail(request, system_id)
def extract_modules(excel_filename: str, output=None, modules=[], min_quality=0): """ Extracts modules from an excel input, adds any reactions necessary to satisfy said contingency, and then writes ouput to a new file Args: excel_filename: Name of the excel input file. output: Name of the new output. modules: Comma-separated list of modules to be extracted min_quality: Minimum quality for a line to be kept """ logger.debug("modules: {}".format(modules)) logger.debug("min quality: {}".format(min_quality)) if not output: output = os.path.dirname(excel_filename) + 'modules.xlsx' if not output.endswith('.xlsx'): output += '.xlsx' print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) rxncon_system = excel_book.rxncon_system print( 'Constructed rxncon system: [{} reactions], [{} contingencies]'.format( len(rxncon_system.reactions), len(rxncon_system.contingencies))) ### STEP 1: Get all the reactions and contingencies and filter down to only those with module tag + minimum quality # Get the sheets con_sheet = excel_book._xlrd_book.sheet_by_name('ContingencyList') rxn_sheet = excel_book._xlrd_book.sheet_by_name('ReactionList') # Get module and quality column numbers for reactions column_rxn_module = None column_rxn_quality = None rxn_header_row = list(rxn_sheet.get_rows())[1] for num, header in enumerate(rxn_header_row): if header.value == '!Module': column_rxn_module = num elif header.value == '!Quality': column_rxn_quality = num if column_rxn_module is None: raise ValueError( 'You must define a !Module column in the ReactionList sheet.') elif column_rxn_quality is None: raise ValueError( 'You must define a !Quality column in the ReactionList sheet.') # Get module and quality column numbers for contingencies column_con_module = None column_con_quality = None con_header_row = list(con_sheet.get_rows())[1] for num, header in enumerate(con_header_row): if header.value == '!Module': column_con_module = num elif header.value == '!Quality': column_con_quality = num if column_con_module is None: raise ValueError( 'You must define a !Module column in the ContingencyList sheet.') elif column_con_quality is None: raise ValueError( 'You must define a !Quality column in the ContingencyList sheet.') # Filter rxn rows by module tag and min quality rxn_rows = [row for row in rxn_sheet.get_rows()][2:] rxn_filtered = [] for num, row in enumerate(rxn_rows): row_modules = list( map(str.strip, row[column_rxn_module].value.split( ','))) # Split modules into list and trim whitespace if row[excel_book._column_reaction_full_name].value.strip( ) == '': # Skip empty rows continue if row[column_rxn_quality].value == '': # If quality missing, assume quality = 0 row_quality = 0 else: try: # Try and parse quality to an int row_quality = int(row[column_rxn_quality].value) except ValueError: raise TypeError( 'Reaction {}\'s quality must be an int.'.format( row[excel_book._column_reaction_full_name].value)) if row_quality < min_quality: # If less than minimum quality skip row logger.debug( 'Skipping reaction row {} (reaction quality: {}, min quality: {})' .format(num, row_quality, min_quality)) continue if not modules == [] and not any( item in row_modules for item in modules): # If row module isnt in modules list, skip row logger.debug( 'Skipping reaction row {} (module not present)'.format(num)) continue else: rxn_filtered.append({ 'row_num': num, 'name': row[excel_book._column_reaction_full_name].value }) # Filter rxn rows by module tag and min quality con_rows = [row for row in con_sheet.get_rows()][2:] con_filtered = [] for num, row in enumerate(con_rows): row_modules = list( map(str.strip, row[column_con_module].value.split( ','))) # Split modules into list and trim whitespace if row[excel_book._column_contingency_target].value.strip( ) == '': # Skip empty rows continue if row[column_con_quality].value == '': # If quality missing, assume quality = 0 row_quality = 0 else: try: # Try and parse quality to an int row_quality = int(row[column_con_quality].value) except ValueError: raise TypeError( 'Contingency {}\'s quality must be an int.'.format(num)) if row_quality < min_quality: # If less than minimum quality skip row logger.debug( 'Skipping contingency row {} (reaction quality: {}, min quality: {})' .format(num, row_quality, min_quality)) continue if not modules == [] and not any( item in row_modules for item in modules): # If row module isnt in modules list, skip row logger.debug( 'Skipping contingency row {} (module not present)'.format(num)) continue else: con_filtered.append({ 'row_num': num, 'target': row[excel_book._column_contingency_target].value.strip(), 'modifier': row[excel_book._column_contingency_modifier].value.strip(), 'type': row[excel_book._column_contingency_type].value.strip() }) ### STEP 1.5: Make sure boolean contingencies have inputs; if not, raise an error boolean_modifiers = [ con['modifier'] for con in con_filtered if re.match(BOOLEAN_CONTINGENCY_REGEX, con['modifier']) ] boolean_targets = [ con['modifier'] for con in con_filtered if re.match(BOOLEAN_CONTINGENCY_REGEX, con['modifier']) ] for modifier in boolean_modifiers: if not modifier in boolean_targets: raise ValueError( 'Boolean gate {} has no inputs in filtered contingencies'. format(modifier)) ### STEP 2 (New method): Map modifiers to rxncon states required_components = [] for con in con_filtered: con['modifier'] = re.sub('#.*', '', con['modifier']) if re.match(BOOLEAN_CONTINGENCY_REGEX, con['modifier'] ): # If contingency modifier is a boolean state, skip it con['modifier_states'] = [] continue orig_modifier_state = state_from_str( con['modifier']) # Get the state based on the string modifier_states = [ state for state in rxncon_system.states if state.is_subset_of(orig_modifier_state) ] # Find the states in the system that state refers to if not modifier_states: raise ValueError( 'Could not match contingency {} to a state in the rxncon system' .format(con['modifier'])) con['modifier_states'] = modifier_states for state in modifier_states: required_components.extend(state.components) ### STEP 3 (New method): For each filtered contingency (ie a state), add reaction(s) that produce(s) state to list (also add target reactions) def unsplit_bidirectional_reaction_str(rxn_str: str) -> str: for verb in BIDIRECTIONAL_REACTIONS: if ('_{}+_'.format(verb).lower() in rxn_str.lower() or '_{}-_'.format(verb).lower() in rxn_str.lower() ): # If reaction is in format _verb+_ or _verb-_ new_rxn_str = re.sub('(?i)_{}._'.format(verb), '_{}_'.format(verb), rxn_str) # Replace with _verb_ return new_rxn_str return rxn_str required_components = list(set( required_components)) # Make sure there are no duplicate components required_reactions = [] for con in con_filtered: con_reactions = [ ] # List to store reactions producing/synthesizing contingency reactions con_required_reactions = [ ] # List to store which reactions will be added to satisfy contingency # If target not a global state or boolean node (i.e. target is a reaction), add it to list of required reactions if not (re.match(BOOLEAN_CONTINGENCY_REGEX, con['target']) or re.match(GLOBAL_STATE_REGEX, con['target'])): required_reactions.append( unsplit_bidirectional_reaction_str(con['target'])) for state in con[ 'modifier_states']: # For each state contingency modifier matched to for reaction in rxncon_system.reactions: if (any([ produced_state.is_subset_of(state) for produced_state in reaction.produced_states ]) or # If a reaction produces the state any([ synthesised_state.is_subset_of(state) for synthesised_state in reaction.synthesised_states ])): # or synthesizes the state con_reactions.append( reaction ) # Add it to the list of reactions for the contingency # Try and find if reaction producing state already in filtered reactions for reaction in con_reactions: rxn_name = unsplit_bidirectional_reaction_str( reaction.name ) # Turn bidirectional reactions like ppi+ or ppi- into ppi rxn_name_no_domains = re.sub( r'_\[.*?\]', '', rxn_name) # Also check name of reaction without domains if [ filtered_reaction for filtered_reaction in rxn_filtered if (filtered_reaction['name'] == rxn_name or filtered_reaction['name'] == rxn_name_no_domains) ]: con_required_reactions.append(rxn_name) # If an already filtered reaction wasn't found, first try adding reaction involving only required components if not con_required_reactions: for reaction in con_reactions: rxn_name = unsplit_bidirectional_reaction_str( reaction.name ) # Turn bidirectional reactions like ppi+ or ppi- into ppi if all( component in required_components for component in reaction.components ): # Check that all reaction components are in required_components logger.warning( 'Adding reaction {} to satisfy contingency {} {} {}'. format(rxn_name, con['target'], con['type'], con['modifier'])) con_required_reactions.append(rxn_name) # If a reaction still wasn't found, add all reactions producing the contingency modifier if not con_required_reactions: for reaction in con_reactions: rxn_name = unsplit_bidirectional_reaction_str(reaction.name) logger.warning( 'Adding reaction {} to satisfy contingency {} {} {}'. format(rxn_name, con['target'], con['type'], con['modifier'])) con_required_reactions.append(rxn_name) required_reactions.extend(con_required_reactions) ### STEP 4: Check if required reactions already in rxn_filtered and add them if missing all_rxn_names = rxn_sheet.col_values( excel_book._column_reaction_full_name)[ 2:] # Get list of all reaction names rxn_filtered_names = [d['name'] for d in rxn_filtered] required_reactions = list( set(required_reactions)) # Make sure there are no duplicate reactions added_reactions = [] for required_rxn_name in required_reactions: # Check if reaction exists in rxn_filtered and skip it if it does required_rxn_name_no_domains = re.sub( r'_\[.*?\]', '', required_rxn_name) # Also check name of reaction without domains if required_rxn_name in rxn_filtered_names or required_rxn_name_no_domains in rxn_filtered_names: continue # If node not in rxn_filtered, try and find the matching reaction and add it to rxn_filtered for num, rxn_name in enumerate(all_rxn_names): if required_rxn_name == rxn_name or required_rxn_name_no_domains == rxn_name: logger.warning( 'Added required reaction {} to filtered reactions.'.format( rxn_name)) added_reactions.append({'name': rxn_name, 'row_num': num}) # If still not found, try stripping domains from both sides for num, rxn_name in enumerate(all_rxn_names): if required_rxn_name_no_domains == re.sub(r'_\[.*?\]', '', rxn_name): logger.warning( 'Added required reaction {} to filtered reactions.'.format( rxn_name)) added_reactions.append({'name': rxn_name, 'row_num': num}) if not added_reactions: raise ValueError( 'Unable to find reaction {}'.format(required_rxn_name)) rxn_filtered.extend(added_reactions) ### STEP 5: Write updated lists to excel file. This has to be done with openpyxl as xlrd/xlwt don't really support xlsx # NOTE: openpyxl is 1-indexed, not 0-indexed like xlrd wb = load_workbook(filename=excel_filename, data_only=True) rxn_open_sheet = wb['ReactionList'] con_open_sheet = wb['ContingencyList'] # Delete the old unfiltered rows. This is more efficient than the delete_rows method for row in rxn_open_sheet.iter_rows(min_row=3, max_row=(3 + len(rxn_rows))): for col in range(0, 12): row[col].value = None for row in con_open_sheet.iter_rows(min_row=3, max_row=(3 + len(con_rows))): for col in range(0, 8): row[col].value = None # Add the filtered rows to workbook rxn_filtered_row_nums = [d['row_num'] for d in rxn_filtered] for row_num, filtered_row_num in enumerate(rxn_filtered_row_nums, 3): for col_num, val in enumerate( rxn_sheet.row_values(filtered_row_num + 2), 1): rxn_open_sheet.cell(row_num, col_num).value = val con_filtered_row_nums = [d['row_num'] for d in con_filtered] for row_num, filtered_row_num in enumerate(con_filtered_row_nums, 3): for col_num, val in enumerate( con_sheet.row_values(filtered_row_num + 2), 1): con_open_sheet.cell(row_num, col_num).value = val # Write to file wb.save(output) print('Wrote filtered rxncon file to {}'.format(output))
def post(self, request, system_id=None): self.system_id = system_id self.request = request self.form = reaGraphFileForm(self.request.POST or None) if self.form.is_valid(): media_url = settings.MEDIA_URL media_root = settings.MEDIA_ROOT loaded = File.objects.filter(loaded=True) if not loaded: system_type = "Quick" system = Quick.objects.filter(id=system_id)[0] project_name = system.name book = RxnconQuick(system.quick_input) else: system_type = "File" system = File.objects.filter(id=system_id)[0] project_name = system.project_name book = ExcelBook(system.get_absolute_path()) graph_file_name = system.slug + "_" + system.get_filename().split( ".")[0] + "_sReaGraph.xgmml" graph_file_path = os.path.join(media_root, system.slug, "graphs", graph_file_name) if not check_filepath(request, graph_file_path, system, media_root): if system_type == "Quick": return quick_detail(request, system_id) else: return file_detail(request, system_id) # pickled_rxncon_system = Rxncon_system.objects.get(project_id=system_id, project_type=system_type) # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) rxncon_system = book.rxncon_system graph = regulatory_graph.SpeciesReactionGraph( rxncon_system=rxncon_system).to_graph() xgmml_graph = graphML.XGMML(graph, system.slug) graph_file = xgmml_graph.to_file(graph_file_path) graph_string = xgmml_graph.to_string() if request.FILES.get('template'): graph_string = apply_template_layout(request, graph_file_path, graph_string) g = Graph_from_File(project_name=project_name, graph_file=graph_file_path, graph_string=graph_string, comment=request.POST.get('comment')) g.save() messages.info( request, "Species reaction graph for project '" + g.project_name + "' successfully created.") if system_type == "Quick": Quick.objects.filter(id=system_id).update(sRea_graph=g) return quick_detail(request, system_id) else: File.objects.filter(id=system_id).update(sRea_graph=g) return file_detail(request, system_id)
def post(self, request, system_id=None): self.system_id = system_id self.request = request self.form = BoolForm(self.request.POST or None) if self.form.is_valid(): media_url = settings.MEDIA_URL media_root = settings.MEDIA_ROOT loaded = File.objects.filter(loaded=True) if not loaded: system_type = "Quick" system = Quick.objects.filter(id=system_id)[0] project_name = system.name book = RxnconQuick(system.quick_input) else: system_type = "File" system = File.objects.filter(id=system_id)[0] project_name = system.project_name book = ExcelBook(system.get_absolute_path()) boolnet_model_filename = system.slug + "_model.boolnet" boolnet_symbol_filename = system.slug + "_symbols.csv" boolnet_initial_val_filename = system.slug + "_initial_vals.csv" model_path = os.path.join(media_root, system.slug, "boolnet", boolnet_model_filename) symbol_path = os.path.join(media_root, system.slug, "boolnet", boolnet_symbol_filename) init_path = os.path.join(media_root, system.slug, "boolnet", boolnet_initial_val_filename) for path in [model_path, symbol_path, init_path]: if not check_filepath(request, path, system, media_root): if system_type == "Quick": return quick_detail(request, system_id) else: return file_detail(request, system_id) # pickled_rxncon_system = Rxncon_system.objects.get(project_id=system_id, project_type=system_type) # rxncon_system = pickle.loads(pickled_rxncon_system.pickled_system) rxncon_system = book.rxncon_system smoothing = SmoothingStrategy(request.POST.get('smoothing')) knockout = KnockoutStrategy(request.POST.get('knockout')) overexpr = OverexpressionStrategy(request.POST.get('overexpr')) k_plus = QuantitativeContingencyStrategy(request.POST.get('k_plus')) k_minus = QuantitativeContingencyStrategy(request.POST.get('k_minus')) model_str, symbol_str, initial_val_str = boolnet_strs_from_rxncon(rxncon_system, smoothing_strategy=smoothing, knockout_strategy=knockout, overexpression_strategy=overexpr, k_plus_strategy=k_plus, k_minus_strategy=k_minus) if not os.path.exists(os.path.join(media_root, system.slug, "boolnet")): os.mkdir(os.path.join(media_root, system.slug, "boolnet")) with open(model_path, mode='w') as f: f.write(model_str) with open(symbol_path, mode='w') as f: f.write(symbol_str) with open(init_path, mode='w') as f: f.write(initial_val_str) b = Bool_from_rxnconsys(project_name=project_name, model_path=model_path, symbol_path=symbol_path, init_path=init_path, comment=request.POST.get('comment')) b.save() messages.info(request, "BoolNet files for project '" + b.project_name + "' successfully created.") if system_type == "Quick": Quick.objects.filter(id=system_id).update(boolean_model=b) return quick_detail(request, system_id) else: File.objects.filter(id=system_id).update(boolean_model=b) return file_detail(request, system_id)
def test_missing_necessary_sheet() -> None: with pytest.raises(SyntaxError): ExcelBook(MISSING_NECESSARY_SHEET_XLS)
def write_reaction_mapping(excel_filename: str, output=None): """ Creates reaction string to full reaction name mapping Args: excel_filename: Name of the excel input file. output: Name of the new output. Returns: None """ if not output: output = './reaction_mapping.csv' print('Reading in Excel file [{}] ...'.format(excel_filename)) excel_book = ExcelBook(excel_filename) sheet = excel_book._xlrd_book.sheet_by_name( SHEET_REACTION_LIST) # type: ignore reaction_rows = [row for row in sheet.get_rows()][DATA_ROW:] header_row = list(sheet.get_rows())[HEADER_ROW] rate_column = None for num, header in enumerate(header_row): if header.value == '!Rate': rate_column = num strs_to_reactions = {} strs_to_rates = {} for row in reaction_rows: if not row[excel_book._column_reaction_full_name].value: logger.debug('_load_reaction_list: Empty row') continue raw_str = row[excel_book._column_reaction_full_name].value logger.debug('_load_reaction_list: {}'.format(raw_str)) # When a verb such as 'ppi' is encountered, the function 'preprocessed_reaction_strs' # will split it into 'ppi+' and 'ppi-'. reaction_strs = split_bidirectional_reaction_str(raw_str) strs_to_reactions[raw_str] = [ reaction_from_str(x) for x in reaction_strs ] rate = 1 if rate_column is not None: rate = row[rate_column].value if rate == "": rate = 1 strs_to_rates[raw_str] = rate with open(output, mode='w') as output_file: writer = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow(['original', 'rxnconName', 'rate']) for key in strs_to_reactions: if isinstance(strs_to_reactions[key], list): for x in strs_to_reactions[key]: writer.writerow([key, x, strs_to_rates[key]]) else: writer.writerow( [key, strs_to_reactions[key], strs_to_rates[key]])