def add_neighborhood_to_network(context, input_path, output_path, name, range_rule, expression_rule, ratio_rule): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network if name in graph.neighborhoods: click.secho( "This network already has a neighborhood called %s, overwriting" % name, fg='yellow', err=True) rules = [] for rule in range_rule: expr, lo, hi, req = rule lo = int(lo) hi = int(hi) req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionRangeRule(expr, lo, hi, req)) for rule in expression_rule: expr, req = rule req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionExpressionRule(expr, req)) for rule in ratio_rule: numer, denom, threshold, req = rule threshold = float(threshold) req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionRatioRule(numer, denom, threshold, req)) graph.neighborhoods.add(CompositionRuleClassifier(name, rules)) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def export_identified_glycans_from_glycopeptides(database_connection, analysis_identifier, output_path): database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() # pylint: disable=not-callable analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % (str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() glycans = session.query(GlycanComposition).join( GlycanCombinationGlycanComposition).join(GlycanCombination).join( Glycopeptide, Glycopeptide.glycan_combination_id == GlycanCombination.id).join( IdentifiedGlycopeptide, IdentifiedGlycopeptide.structure_id == Glycopeptide.id).filter( IdentifiedGlycopeptide.analysis_id == analysis.id).all() if output_path is None: output_stream = ctxstream(click.get_binary_stream('stdout')) else: output_stream = open(output_path, 'wb') with output_stream: job = ImportableGlycanHypothesisCSVSerializer(output_stream, glycans) job.run()
def glycopeptide_hypothesis(database_connection, hypothesis_identifier, output_path, multifasta=False): '''Write each theoretical glycopeptide in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() hypothesis = get_by_name_or_id(session, GlycopeptideHypothesis, hypothesis_identifier) def generate(): interval = 100000 i = 0 while True: session.expire_all() chunk = hypothesis.glycopeptides.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide i += interval if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = GlycopeptideHypothesisCSVSerializer(output_stream, generate()) job.run()
def glycopeptide_hypothesis(database_connection, hypothesis_identifier, output_path, multifasta=False): '''Write each theoretical glycopeptide in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() hypothesis = get_by_name_or_id(session, GlycopeptideHypothesis, hypothesis_identifier) def generate(): interval = 100000 i = 0 while True: session.expire_all() chunk = hypothesis.glycopeptides.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide i += interval if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = GlycopeptideHypothesisCSVSerializer(output_stream, generate()) job.run()
def glycan_composition_identification(database_connection, analysis_identifier, output_path=None, threshold=0, report=False): '''Write each glycan chromatogram in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycan_lc_ms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') if report: with output_stream: job = GlycanChromatogramReportCreator( database_connection._original_connection, analysis_id, output_stream, threshold=threshold) job.run() else: def generate(): i = 0 interval = 100 query = session.query(GlycanCompositionChromatogram).filter( GlycanCompositionChromatogram.analysis_id == analysis_id, GlycanCompositionChromatogram.score > threshold) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for gcs in chunk: yield gcs.convert() i += interval i = 0 query = session.query(UnidentifiedChromatogram).filter( UnidentifiedChromatogram.analysis_id == analysis_id, UnidentifiedChromatogram.score > threshold) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for gcs in chunk: yield gcs.convert() i += interval with output_stream: job = GlycanLCMSAnalysisCSVSerializer(output_stream, generate()) job.run()
def remove_neighborhood(context, input_path, output_path, name): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network try: graph.neighborhoods.remove(name) except KeyError: click.secho( "No neighborhood with name %r was found" % name, err=True, fg='yellow') if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def glycan_composition_identification(database_connection, analysis_identifier, output_path=None, threshold=0, report=False): '''Write each glycan chromatogram in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycan_lc_ms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') if report: with output_stream: job = GlycanChromatogramReportCreator( database_connection._original_connection, analysis_id, output_stream, threshold=threshold) job.run() else: def generate(): i = 0 interval = 100 query = session.query(GlycanCompositionChromatogram).filter( GlycanCompositionChromatogram.analysis_id == analysis_id, GlycanCompositionChromatogram.score > threshold) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for gcs in chunk: yield gcs.convert() i += interval i = 0 query = session.query(UnidentifiedChromatogram).filter( UnidentifiedChromatogram.analysis_id == analysis_id, UnidentifiedChromatogram.score > threshold) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for gcs in chunk: yield gcs.convert() i += interval with output_stream: job = GlycanLCMSAnalysisCSVSerializer(output_stream, generate()) job.run()
def add_prebuild_neighborhoods_to_network(context, input_path, output_path, name): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network if name == 'n-glycan': neighborhoods = make_n_glycan_neighborhoods() else: raise LookupError(name) for n in neighborhoods: graph.neighborhoods.add(n) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def remove_neighborhood(context, input_path, output_path, name): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network try: graph.neighborhoods.remove(name) except KeyError: click.secho("No neighborhood with name %r was found" % name, err=True, fg='yellow') if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def add_prebuild_neighborhoods_to_network(context, input_path, output_path, name): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network if name == 'n-glycan': neighborhoods = make_n_glycan_neighborhoods() else: raise LookupError(name) for n in neighborhoods: graph.neighborhoods.add(n) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def glycopeptide_identification(database_connection, analysis_identifier, output_path=None, report=False, mzml_path=None, threshold=0): '''Write each distinct identified glycopeptide in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') if report: with output_stream: if mzml_path is None: mzml_path = analysis.parameters['sample_path'] if not os.path.exists(mzml_path): raise click.ClickException( ("Sample path {} not found. Pass the path to" " this file as `-m/--mzml-path` for this command.").format( mzml_path)) GlycopeptideDatabaseSearchReportCreator( database_connection._original_connection, analysis_id, stream=output_stream, threshold=threshold, mzml_path=mzml_path).run() else: query = session.query(Protein.id, Protein.name).join(Protein.glycopeptides).join( IdentifiedGlycopeptide).filter( IdentifiedGlycopeptide.analysis_id == analysis.id) protein_index = dict(query) def generate(): i = 0 interval = 100 query = session.query(IdentifiedGlycopeptide).filter( IdentifiedGlycopeptide.analysis_id == analysis_id) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide.convert() i += interval with output_stream: job = GlycopeptideLCMSMSAnalysisCSVSerializer(output_stream, generate(), protein_index) job.run()
def glycopeptide_identification(database_connection, analysis_identifier, output_path=None, report=False, mzml_path=None, threshold=0): '''Write each distinct identified glycopeptide in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') if report: with output_stream: if mzml_path is None: mzml_path = analysis.parameters['sample_path'] if not os.path.exists(mzml_path): raise click.ClickException( ("Sample path {} not found. Pass the path to" " this file as `-m/--mzml-path` for this command.").format( mzml_path)) GlycopeptideDatabaseSearchReportCreator( database_connection._original_connection, analysis_id, stream=output_stream, threshold=threshold, mzml_path=mzml_path).run() else: query = session.query(Protein.id, Protein.name).join(Protein.glycopeptides).join( IdentifiedGlycopeptide).filter( IdentifiedGlycopeptide.analysis_id == analysis.id) protein_index = dict(query) def generate(): i = 0 interval = 100 query = session.query(IdentifiedGlycopeptide).filter( IdentifiedGlycopeptide.analysis_id == analysis_id) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide.convert() i += interval with output_stream: job = GlycopeptideLCMSMSAnalysisCSVSerializer(output_stream, generate(), protein_index) job.run()
def add_neighborhood_to_network(context, input_path, output_path, name, range_rule, expression_rule, ratio_rule): if input_path is None: input_stream = ctxstream(sys.stdin) else: input_stream = open(input_path, 'r') with input_stream: graph = GraphReader(input_stream).network if name in graph.neighborhoods: click.secho( "This network already has a neighborhood called %s, overwriting" % name, fg='yellow', err=True) rules = [] for rule in range_rule: expr, lo, hi, req = rule lo = int(lo) hi = int(hi) req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionRangeRule(expr, lo, hi, req)) for rule in expression_rule: expr, req = rule req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionExpressionRule(expr, req)) for rule in ratio_rule: numer, denom, threshold, req = rule threshold = float(threshold) req = req.lower().strip() in ('true', 'yes', '1') rules.append(CompositionRatioRule(numer, denom, threshold, req)) graph.neighborhoods.add(CompositionRuleClassifier(name, rules)) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'w') with output_stream: GraphWriter(graph, output_stream)
def glycopeptide_training_mgf(database_connection, analysis_identifier, output_path=None, mzml_path=None, threshold=None): database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: TrainingMGFExporter.from_analysis( database_connection, analysis.id, output_stream, mzml_path, threshold).run()
def glycan_hypothesis(database_connection, hypothesis_identifier, output_path=None, importable=False): '''Write each theoretical glycan composition in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) hypothesis = get_by_name_or_id(database_connection, GlycanHypothesis, hypothesis_identifier) if importable: task_type = ImportableGlycanHypothesisCSVSerializer else: task_type = GlycanHypothesisCSVSerializer if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = task_type(output_stream, hypothesis.glycans) job.run()
def glycopeptide_training_mgf(database_connection, analysis_identifier, output_path=None, mzml_path=None, threshold=None): database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: TrainingMGFExporter.from_analysis( database_connection, analysis.id, output_stream, mzml_path, threshold).run()
def glycan_hypothesis(database_connection, hypothesis_identifier, output_path=None, importable=False): '''Write each theoretical glycan composition in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) hypothesis = get_by_name_or_id(database_connection, GlycanHypothesis, hypothesis_identifier) if importable: task_type = ImportableGlycanHypothesisCSVSerializer else: task_type = GlycanHypothesisCSVSerializer if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = task_type(output_stream, hypothesis.glycans) job.run()
def glycan_network(context, database_connection, hypothesis_identifier, edge_strategy, output_path): conn = DatabaseBoundOperation(database_connection) hypothesis = get_by_name_or_id(conn, GlycanHypothesis, hypothesis_identifier) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: db = GlycanCompositionDiskBackedStructureDatabase( database_connection, hypothesis.id) glycans = list(db) graph = CompositionGraph(glycans) if edge_strategy == 'manhattan': graph.create_edges(1) else: raise click.ClickException( "Could not find edge strategy %r" % (edge_strategy,)) GraphWriter(graph, output_stream)
def glycopeptide_spectrum_matches(database_connection, analysis_identifier, output_path=None): '''Write each matched glycopeptide spectrum in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() # pylint: disable=not-callable analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % (str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id query = session.query(Protein.id, Protein.name).join( Protein.glycopeptides).join(GlycopeptideSpectrumMatch).filter( GlycopeptideSpectrumMatch.analysis_id == analysis.id) protein_index = dict(query) def generate(): i = 0 interval = 100000 query = session.query(GlycopeptideSpectrumMatch).filter( GlycopeptideSpectrumMatch.analysis_id == analysis_id).order_by( GlycopeptideSpectrumMatch.scan_id) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide.convert() i += interval if output_path is None: output_stream = ctxstream(click.get_binary_stream('stdout')) else: output_stream = open(output_path, 'wb') with output_stream: job = GlycopeptideSpectrumMatchAnalysisCSVSerializer( output_stream, generate(), protein_index) job.run()
def glycan_network(context, database_connection, hypothesis_identifier, edge_strategy, output_path): conn = DatabaseBoundOperation(database_connection) hypothesis = get_by_name_or_id(conn, GlycanHypothesis, hypothesis_identifier) if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: db = GlycanCompositionDiskBackedStructureDatabase( database_connection, hypothesis.id) glycans = list(db) graph = CompositionGraph(glycans) if edge_strategy == 'manhattan': graph.create_edges(1) else: raise click.ClickException("Could not find edge strategy %r" % (edge_strategy, )) GraphWriter(graph, output_stream)
def export_identified_glycans_from_glycopeptides(database_connection, analysis_identifier, output_path): database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() glycans = session.query(GlycanComposition).join( GlycanCombinationGlycanComposition).join(GlycanCombination).join( Glycopeptide, Glycopeptide.glycan_combination_id == GlycanCombination.id).join( IdentifiedGlycopeptide, IdentifiedGlycopeptide.structure_id == Glycopeptide.id).filter( IdentifiedGlycopeptide.analysis_id == analysis.id).all() if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = ImportableGlycanHypothesisCSVSerializer(output_stream, glycans) job.run()
def glycopeptide_spectrum_matches(database_connection, analysis_identifier, output_path=None): '''Write each matched glycopeptide spectrum in CSV format ''' database_connection = DatabaseBoundOperation(database_connection) session = database_connection.session() analysis = get_by_name_or_id(session, Analysis, analysis_identifier) if not analysis.analysis_type == AnalysisTypeEnum.glycopeptide_lc_msms: click.secho("Analysis %r is of type %r." % ( str(analysis.name), str(analysis.analysis_type)), fg='red', err=True) raise click.Abort() analysis_id = analysis.id query = session.query(Protein.id, Protein.name).join(Protein.glycopeptides).join( GlycopeptideSpectrumMatch).filter( GlycopeptideSpectrumMatch.analysis_id == analysis.id) protein_index = dict(query) def generate(): i = 0 interval = 100000 query = session.query(GlycopeptideSpectrumMatch).filter( GlycopeptideSpectrumMatch.analysis_id == analysis_id).order_by( GlycopeptideSpectrumMatch.scan_id) while True: session.expire_all() chunk = query.slice(i, i + interval).all() if len(chunk) == 0: break for glycopeptide in chunk: yield glycopeptide.convert() i += interval if output_path is None: output_stream = ctxstream(sys.stdout) else: output_stream = open(output_path, 'wb') with output_stream: job = GlycopeptideSpectrumMatchAnalysisCSVSerializer(output_stream, generate(), protein_index) job.run()