def test_open_wrong_database(): pycyc.open('_long_name_that_should_really_not_be_a_valid_pgdb')
will cause tests to fail even when the code is performing correctly, but short of installing a purpose-built test PGDB I am not sure how to avoid this. """ import pycyc from nose.tools import raises, assert_is_instance, assert_raises from pycyc.frame import Frame # Specify the values which should be returned for true and false queries # (which may change in the future) _NIL = None _T = True ec = pycyc.open('ecoli') sample_reaction = 'trypsyn-rxn' # '1.8.4.13-RXN', 'oropribtrans-rxn' pathway_info = ('TRPSYN-PWY','RXN0-2381','OROPRIBTRANS-RXN') (sample_pathway, pathway_reaction, pathway_non_reaction) = pathway_info sample_compound = 'trp' sample_gene = 'EG11024' # should encode at least one enzyme in at least one pwy # should lie in at least one transcription unit # and have at least one regulator sample_enzyme = 'TRYPSYN' # should be of types chemical-change, small-molecule, # non-transport sample_protein = 'TRYPSYN' sample_transport_reaction='TRANS-RXN-157' # The sample transport protein should NOT be an enzyme of type non-transport sample_transport_protein='CPLX-165' sample_tf = 'PHOSPHO-CREB'
""" Test interface object behavior not directly related to ptools interaction. These tests require ecocyc and metacyc. As with the core Pathway Tools API tests, they will fail if the specific objects used for testing purposes change in certain ways due to database updates. """ import pycyc ec1 = pycyc.open("ecoli") ec2 = pycyc.open("ecoli") meta = pycyc.open("meta") def test_interface_to_str(): assert str(ec1) == "ecoli" def test_interface_eq(): assert ec1 == ec2 assert not ec1 == meta def test_interface_neq(): assert not ec1 != ec2 assert ec1 != meta
def main(args): """Run program Args: args (NameSpace): ArgParse arguments controlling program flow """ def shutdown(pid): print('>>> Shutdown sequence initiated.') print('>>> Terminating Pathway Tools LISP Daemon') pid.terminate() pid.wait() print('>>> Daemon destroyed.') print('>>> Until next time. :)') print('>>> Hi, I am DPS (Derive Pathway Steps).') print('>>> I will be analyzing pathways for you today.') print('>>> I am using the {0} database as per your command.' .format(args.database)) if args.database == 'metacyc': # Obtain executable pathway_tools = which('pathway-tools', path=args.executable) if pathway_tools is None: raise EnvironmentError('I cannot find pathway-tools: please ' 'specify -e.') else: print('>>> I found pathway-tools: {0}.'.format(pathway_tools)) # Start pathway-tools daemon while True: print('>>> Summoning Pathway Tools LISP Daemon.') pid = Popen([pathway_tools, '-lisp', '-api'], stderr=open(os.devnull, 'w'), stdout=open(os.devnull, 'w')) print('>>> Let\'s give it five seconds to spawn.') time.sleep(5) if os.path.exists('/tmp/ptools-socket') and \ stat.S_ISSOCK(os.stat('/tmp/ptools-socket').st_mode): print('>>> The daemon is is up!') break else: print('>>> The daemon took too long to boot. :(') print('>>> This makes me sad, so I will kill it.') pid.kill() print('>>> Let\'s wait five seconds for it to die!') time.sleep(5) pid.poll() if pid.returncode is None: raise CalledProcessError('Pathway Tools won\'t die!') else: print('>>> The daemon is dead!') print('>>> I miss it. :( I\'m going to try again. :)') atexit.register(shutdown, pid) # Connect to daemon try: metacyc = pycyc.open('meta') except IOError: print('>>> I cannot connect to Pathway Tools Daemon.') print('>>> Here is the original error message:') raise else: print('>>> I have connected to the Pathway Tools Daemon.') print('>>> Phenomenal cosmic powers! Itty bitty memory footprint!') # Index genes file print('>>> Indexing {0}.'.format(args.reactions_file.name)) reactions_to_genes = {} start_time = time.time() for line in args.reactions_file: parts = line.strip().split() reactions_to_genes[parts[0]] = (parts[1], parts[2:]) end_time = time.time() print('>>> I indexed {0} reactions in {1} seconds.' .format(str(len(reactions_to_genes)), str(end_time - start_time))) print('>>> I\'m so fast.') # Index all pathways by name print('>>> Time to index all the pathways from Metacyc.') pathways = {} start_time = time.time() for frame in metacyc.all_pathways(): pathways[frame.common_name] = frame end_time = time.time() print('>>> I indexed {0} pathways in {1} seconds.' .format(str(len(pathways)), str(end_time - start_time))) print('>>> Aren\'t you proud of me?') # Index gene abundance print('>>> Recording gene abundances from {0}.' .format(args.abundance_file.name)) abundances = {} start_time = time.time() for line in args.abundance_file: gene, abundance = line.strip().split('\t') abundances[gene] = abundance end_time = time.time() print('>>> I indexed {0} gene abundances in {1} seconds.' .format(str(len(abundances)), str(end_time - start_time))) # Obtain pathway of interest print('>>> Time to do some science!') print('>>> Note: you can input all or part of a pathway name.') print('>>> Type "q" for input at any time to exit the program.') while True: # Rest of program runs in a loop until user ends it possibilities = {} user_input = raw_input('>>> Enter a pathway: ') if user_input.lower() == 'q': break for name, frame in pathways.items(): if user_input in name: possibilities[name] = frame if len(possibilities) == 0: print('>>> I couldn\'t find any pathways matching your ' 'request.') print('>>> Try an alternative name for the pathway.') continue print('>>> I found {0} pathways matching your request.' .format(str(len(possibilities)))) shutdown = False restart = False pathway = None while True: print('>>> Here are possible pathways:') max_entry = len(possibilities) - 1 for possibility in enumerate(possibilities.items()): print('{0}: {1}'.format(str(possibility[0]), possibility[1][1].common_name)) path_num = raw_input('>>> Select a pathway ("r" to restart): ') if path_num.lower() == 'q': shutdown = True break elif path_num.lower() == 'r': restart = True break else: try: path_num = int(path_num) except ValueError: print('>>> Your answer is not an integer.') print('>>> I only understand integers.') print('>>> Please correct.') continue if path_num > max_entry or path_num < 0: print('>>> {0} is not a valid pathway.' .format(str(path_num))) print('>>> Valid pathways are: {0}.'.format(' '.join( [str(i) for i in range(max_entry + 1)]))) print('>>> Try again.') continue pathway = possibilities[possibilities.keys()[path_num]] print('>>> You selected: {0}.'.format(pathway.common_name)) print('>>> Neat! I\'ll analyze it now.') break if restart is True: continue if shutdown is True: break # Add genes and abundances to pathway reactions print('>>> Collecting reactions in pathway.') try: if type(pathway.reaction_list) is list: rxns = [str(rxn) for rxn in pathway.reaction_list] else: rxns = [str(pathway.reaction_list)] except KeyError: print('>>> I cannot access the reactions for this pathway. :(') print('>>> I\'m sorry I\'ve failed you. :(') print('>>> Please have me analyze something else.') continue print('>>> Analyzing pathway for key reactions.') if hasattr(pathway, 'key_reactions') is True and\ pathway.key_reactions is not None: key_rxns = [str(key) for key in pathway.key_reactions] for rxn in enumerate(rxns): if rxn[1] in key_rxns: rxns[rxn[0]] = rxn[1] + '*' print('>>> Acquiring gene families for each reaction from {0}.' .format(args.reactions_file.name)) reactions = {} for rxn in rxns: rxn_name = re.sub('\*$', '', rxn) if rxn_name in reactions_to_genes.keys(): ec, uniref_list = reactions_to_genes[rxn_name] rxn_name = rxn + ' (' + ec + ')' reactions[rxn_name] = {} for uniref in uniref_list: reactions[rxn_name][uniref] = 0.0 print('>>> Adding abundances from {0}.' .format(args.abundance_file.name)) for rxn in reactions.keys(): for gene in reactions[rxn]: if gene in abundances.keys(): reactions[rxn][gene] = abundances[gene] print('>>> Removing unused gene families.') for rxn in reactions.keys(): for uniref in reactions[rxn].keys(): if reactions[rxn][uniref] == 0.0: del reactions[rxn][uniref] for rxn in reactions.keys(): if reactions[rxn] == {}: reactions[rxn] = 'None\tN/A' continue # Format reactions for printing rxn_list = [pathway.common_name] for rxn in reactions.keys(): if reactions[rxn] == 'None\tN/A': temp = [rxn, ['None\tN/A']] rxn_list.append(temp) elif type(reactions[rxn]) is dict: temp = [rxn] for uniref in reactions[rxn].keys(): temp.append('{0}\t{1}'.format(uniref, str(reactions[rxn][uniref]))) rxn_list.append(temp) # Print output print('>>> I\'ve finished analyzing everything!') print('>>> Here it is (asterisks represent key reactions):') rxn_print = [rxn for rxn in print_nested_list(rxn_list)] for rxn in rxn_print: print(rxn) # Save output print('>>> What file would you like me to save this to?') print('>>> Type "n" if you don\'t want to save this output.') while True: out_file = raw_input('>>> File: ') if out_file.lower() != 'n' and out_file.lower() != 'q': try: with open(out_file, 'w') as out_handle: for rxn in rxn_print: out_handle.write(rxn + os.linesep) print('>>> Output written to {0}.'.format(out_file)) break except IOError as error: print('>>> I could not write to {0}.'.format(out_file)) print('>>> Original error:') print(error) print('>>> Let\'s try again (enter "n" to skip).') elif out_file.lower() == 'q': shutdown = True break else: break if shutdown is True: break print('>>> All done!') print('>>> Let\'s do more science (enter "q" to exit program)!')
p_ignored_set = {} # dictionary of ignored pathways and blocking metabolites r_ignored_set = set() # set of ignored reactions m_generic_set = {} # dictionary of generic metabolites and how often they lead to a ignored reaction p_generic_set = {} # dictionary of ignored pathways and how much ignored generic metabolites are responsible r_generic = 0 # count generic reactions r_total = 0 # count reactions # # I. input # print pycyc.all_orgs() answer_org = raw_input("Which Organism shoulb be exportet to sbml? ") org = pycyc.open(answer_org) print "Loading", answer_org, "with", len(org.all_rxns(":all")), "reactions" formula_dic = cyc.get_formula("./conf/formula.txt") # dictionary additional chemical formula for compounds ec_dic = cyc.get_ec_dic("db/ec-names_brenda.txt") # dictionary with ec numbers and reaction names answer_generic = raw_input( "\nDo you want to take care of generic metabolites (e.g. tRNA, carboxylates)?\n All subclasses of a generic metabolite will be searched for instances (that is specific metabolites) and specific reactions (could be much more!) will be added instead of the generic one\n [y/n] " ) care_generics = True if answer_generic == "y" else False if care_generics: answer_exceptions = raw_input( "\nAre some metabolites (./conf/exceptions.txt) to be kept even if they are generic metabolites? This could be usefull e.g. to summarize lipid metabolism.\n [y/n] " ) generic_exceptions = cyc.read_generic_exceptions("./conf/exceptions.txt") if answer_exceptions == "y" else [] else:
API tests, they will fail if the specific objects used for testing purposes change in certain ways due to database updates. """ import re import pycyc from nose.tools import raises, assert_is_instance from pycyc.frame import Frame # This differs slightly from the attribute pattern defined in frame.py # as here we are validating python attribute names, not finding LISP slot # names which may be made pythonic attribute_pattern = re.compile(r'[a-z_][a-z0-9_]*') ec = pycyc.open('ecoli') meta = pycyc.open('meta') # Test frame equality trp_ecoli_1 = Frame('trp',ec) trp = trp_ecoli_1 trp_ecoli_2 = Frame('trp',ec) trp_meta = Frame('trp',meta) trp_single_value_slot = 'COMMON-NAME' trp_many_value_slot = 'SYNONYMS' trp_no_value_slot = 'SUPERATOMS' trp_untranslatable_slot = 'SCHEMA?' trp_empty_slot = 'SUPERATOMS' class_ = Frame('|Genes|',ec) instance = trp class_member = 'EG11219'