def _processSNP(self, batch_job, cmd, flags): """ Process an entry from the SNP converter Batch, write the results to the job-file. If an Exception is raised, catch and continue. Side-effect: - Output written to outputfile. @arg cmd: The SNP converter input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) O.addMessage(__file__, -1, "INFO", "Received SNP converter batch rs" + cmd) stats.increment_counter('snp-converter/batch') #Read out the flags # Todo: Do something with the flags? skip = self.__processFlags(O, flags) descriptions = [] if not skip: R = Retriever.Retriever(O) descriptions = R.snpConvert(cmd) # Todo: Is output ok? outputline = "%s\t" % cmd outputline += "%s\t" % "|".join(descriptions) outputline += "%s\t" % "|".join(O.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename): # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = [ 'Input Variant', 'HGVS description(s)', 'Errors and warnings' ] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else: handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s%s" % (outputline, separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finished SNP converter batch rs%s" % cmd)
def syntax_checker(): """ Parse the given variant and render the syntax checker HTML form. """ # Backwards compatibility. if 'variant' in request.args: return redirect(url_for('.syntax_checker', description=request.args['variant']), code=301) description = request.args.get('description') if not description: return render_template('syntax-checker.html') output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received request syntaxCheck(%s) from %s' % (description, request.remote_addr)) stats.increment_counter('syntax-checker/website') grammar = Grammar(output) grammar.parse(description) parse_error = output.getOutput('parseError') messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished request syntaxCheck(%s)' % description) return render_template('syntax-checker.html', description=description, messages=messages, parse_error=parse_error)
def back_translator(): """ Back translator. """ output = Output(__file__) output.addMessage( __file__, -1, 'INFO', 'Received Back Translate request from {}'.format(request.remote_addr)) stats.increment_counter('back-translator/website') description = request.args.get('description') variants = [] if description: variants = backtranslator.backtranslate(output, description) errors, warnings, summary = output.Summary() messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished Back Translate request') return render_template('back-translator.html', errors=errors, summary=summary, description=description or '', messages=messages, variants=variants)
def snp_converter(): """ SNP converter. Convert a dbSNP rs number to HGVS description(s) of the SNP specified on the reference sequence(s) used by dbSNP. """ # Backwards compatibility. if 'rsId' in request.args: return redirect(url_for('.snp_converter', rs_id=request.args['rsId']), code=301) rs_id = request.args.get('rs_id') if not rs_id: return render_template('snp-converter.html') output = Output(__file__) output.addMessage( __file__, -1, 'INFO', 'Received request snpConvert(%s) from %s' % (rs_id, request.remote_addr)) stats.increment_counter('snp-converter/website') descriptions = ncbi.rsid_to_descriptions(rs_id, output) messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished request snpConvert(%s)' % rs_id) return render_template('snp-converter.html', rs_id=rs_id, descriptions=descriptions, messages=messages, summary=output.Summary()[2])
def _processSNP(self, batch_job, cmd, flags): """ Process an entry from the SNP converter Batch, write the results to the job-file. If an Exception is raised, catch and continue. Side-effect: - Output written to outputfile. @arg cmd: The SNP converter input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) O.addMessage(__file__, -1, "INFO", "Received SNP converter batch rs" + cmd) stats.increment_counter('snp-converter/batch') #Read out the flags # Todo: Do something with the flags? skip = self.__processFlags(O, flags) descriptions = [] if not skip : R = Retriever.Retriever(O) descriptions = R.snpConvert(cmd) # Todo: Is output ok? outputline = "%s\t" % cmd outputline += "%s\t" % "|".join(descriptions) outputline += "%s\t" % "|".join(O.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename) : # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = ['Input Variant', 'HGVS description(s)', 'Errors and warnings'] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else : handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s%s" % (outputline, separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finished SNP converter batch rs%s" % cmd)
def _processSyntaxCheck(self, batch_job, cmd, flags): """ Process an entry from the Syntax Check, write the results to the job-file. Side-effect: - Output written to outputfile @arg cmd: The Syntax Checker input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ output = Output(__file__) grammar = Grammar(output) output.addMessage(__file__, -1, "INFO", "Received SyntaxChecker batchvariant " + cmd) stats.increment_counter('syntax-checker/batch') skip = self.__processFlags(output, flags) #Process if not skip: parsetree = grammar.parse(cmd) else: parsetree = None if parsetree: result = "OK" else: result = "|".join(output.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename): # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = ['Input', 'Status'] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else: handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s\t%s%s" % (cmd, result, separator)) handle.close() output.addMessage(__file__, -1, "INFO", "Finished SyntaxChecker batchvariant " + cmd)
def _processSyntaxCheck(self, batch_job, cmd, flags): """ Process an entry from the Syntax Check, write the results to the job-file. Side-effect: - Output written to outputfile @arg cmd: The Syntax Checker input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ output = Output(__file__) grammar = Grammar(output) output.addMessage(__file__, -1, "INFO", "Received SyntaxChecker batchvariant " + cmd) stats.increment_counter('syntax-checker/batch') skip = self.__processFlags(output, flags) #Process if not skip : parsetree = grammar.parse(cmd) else : parsetree = None if parsetree : result = "OK" else : result = "|".join(output.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename) : # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = ['Input', 'Status'] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else : handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s\t%s%s" % (cmd, result, separator)) handle.close() output.addMessage(__file__, -1, "INFO", "Finished SyntaxChecker batchvariant " + cmd)
def snp_converter(): """ SNP converter. Convert a dbSNP rs number to HGVS description(s) of the SNP specified on the reference sequence(s) used by dbSNP. """ # Backwards compatibility. if 'rsId' in request.args: return redirect(url_for('.snp_converter', rs_id=request.args['rsId']), code=301) rs_id = request.args.get('rs_id') if not rs_id: return render_template('snp-converter.html') output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received request snpConvert(%s) from %s' % (rs_id, request.remote_addr)) stats.increment_counter('snp-converter/website') try: descriptions = ncbi.rsid_to_descriptions(rs_id) except ncbi.ServiceError: output.addMessage(__file__, 4, 'EENTREZ', 'An error occured while communicating with dbSNP.') messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished request snpConvert(%s)' % rs_id) return render_template('snp-converter.html', rs_id=rs_id, descriptions=descriptions, messages=messages, summary=output.Summary()[2])
def snp_converter(): """ SNP converter. Convert a dbSNP rs number to HGVS description(s) of the SNP specified on the reference sequence(s) used by dbSNP. """ # Backwards compatibility. if 'rsId' in request.args: return redirect(url_for('.snp_converter', rs_id=request.args['rsId']), code=301) rs_id = request.args.get('rs_id') if not rs_id: return render_template('snp-converter.html') output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received request snpConvert(%s) from %s' % (rs_id, request.remote_addr)) stats.increment_counter('snp-converter/website') retriever = Retriever.Retriever(output) descriptions = retriever.snpConvert(rs_id) messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished request snpConvert(%s)' % rs_id) return render_template('snp-converter.html', rs_id=rs_id, descriptions=descriptions, messages=messages, summary=output.Summary()[2])
def back_translator(): """ Back translator. """ output = Output(__file__) output.addMessage( __file__, -1, 'INFO', 'Received Back Translate request from {}'.format(request.remote_addr)) stats.increment_counter('back-translator/website') description = request.args.get('description') variants = [] if description: variants = backtranslator.backtranslate(output, description) errors, warnings, summary = output.Summary() messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished Back Translate request') return render_template( 'back-translator.html', errors=errors, summary=summary, description=description or '', messages=messages, variants=variants)
def batch_jobs_submit(): """ Run batch jobs and render batch checker HTML form. The batch jobs are added to the database by the scheduler and ran by the BatchChecker daemon. """ job_type = request.form.get('job_type') email = request.form.get('email') # Note that this is always a seekable binary file object. batch_file = request.files.get('file') assemblies = Assembly.query \ .order_by(*Assembly.order_by_criteria) \ .all() assembly_name_or_alias = request.form.get('assembly_name_or_alias', settings.DEFAULT_ASSEMBLY) errors = [] if not email: email = '{}@website.mutalyzer'.format(request.remote_addr) if job_type not in BATCH_JOB_TYPES: errors.append('Invalid batch job type.') if not file: errors.append('Please select a local file for upload.') if job_type == 'position-converter': try: Assembly.by_name_or_alias(assembly_name_or_alias) except NoResultFound: errors.append('Not a valid assembly.') argument = assembly_name_or_alias else: argument = None output = Output(__file__) if not errors: stats.increment_counter('batch-job/website') scheduler = Scheduler.Scheduler() file_instance = File.File(output) job, columns = file_instance.parseBatchFile(batch_file) if job is None: errors.append('Could not parse input file, please check your ' 'file format.') else: result_id = scheduler.addJob(email, job, columns, job_type, argument=argument) # Todo: We now assume that the job was not scheduled if there are # messages, which is probably not correct. if not output.getMessages(): return redirect(url_for('.batch_job_progress', result_id=result_id)) for error in errors: output.addMessage(__file__, 3, 'EBATCHJOB', error) messages = map(util.message_info, output.getMessages()) return render_template('batch-jobs.html', assemblies=assemblies, assembly_name_or_alias=assembly_name_or_alias, job_type=job_type, max_file_size=settings.MAX_FILE_SIZE // 1048576, messages=messages)
def description_extractor_submit(): """ The Variant Description Extractor (experimental service). There multiple ways for the user to provide two sequences, corresponding to the values for the `reference_method` and `sample_method` fields, each requiring some additional fields to be defined: `raw_method` The reference and sample sequences are pasted into the form fields. - `reference_sequence`: The reference sequence. - `sample_sequence`: The sample sequence. `file_method` The reference and sample sequences are uploaded. - `reference_file`: The reference file. - `sample_file`: The sample file. `refseq_method` The reference and sample sequences are given by RefSeq accession numbers. - `reference_accession_number`: RefSeq accession number for the reference sequence. - `sample_accession_number`: RefSeq accession number for the sample sequence. """ output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received Description Extract request from %s' % request.remote_addr) stats.increment_counter('description-extractor/website') r = s = '' reference_method = request.form.get('reference_method') sample_method = request.form.get('sample_method') reference_sequence = request.form.get('reference_sequence') sample_sequence = request.form.get('sample_sequence') reference_file = request.files.get('reference_file') sample_file = request.files.get('sample_file') reference_filename = '' sample_filename = '' reference_accession_number = request.form.get('reference_accession_number') sample_accession_number = request.form.get('sample_accession_number') if reference_method == 'refseq_method': if reference_accession_number: retriever = Retriever.GenBankRetriever(output) genbank_record = retriever.loadrecord(reference_accession_number) if genbank_record: r = unicode(genbank_record.seq) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'Reference accession number input fields is empty.') elif reference_method == 'file_method': if reference_file: reference_filename = reference_file.filename r = util.read_dna(reference_file) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'No reference file provided.') else: # raw_method if reference_sequence: r = util.read_dna(StringIO.StringIO(reference_sequence)) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'Reference sequence number input fields is empty.') if sample_method == 'refseq_method': if sample_accession_number: retriever = Retriever.GenBankRetriever(output) genbank_record = retriever.loadrecord(sample_accession_number) if genbank_record: s = unicode(genbank_record.seq) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'Sample accession number input fields is empty.') elif sample_method == 'file_method': if sample_file: sample_filename = sample_file.filename s = util.read_dna(sample_file) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'No sample file provided.') else: # raw_method if sample_sequence: s = util.read_dna(StringIO.StringIO(sample_sequence)) else: output.addMessage(__file__, 3, 'EEMPTYFIELD', 'Sample sequence number input fields is empty.') # Todo: Move this to the describe module. if not r or not util.is_dna(r): output.addMessage(__file__, 3, 'ENODNA', 'Reference sequence is not DNA.') if not s or not util.is_dna(s): output.addMessage(__file__, 3, 'ENODNA', 'Sample sequence is not DNA.') raw_vars = None if r and s: if (len(r) > settings.EXTRACTOR_MAX_INPUT_LENGTH or len(s) > settings.EXTRACTOR_MAX_INPUT_LENGTH): output.addMessage(__file__, 3, 'EMAXSIZE', 'Input sequences are restricted to {:,} bp.' .format(settings.EXTRACTOR_MAX_INPUT_LENGTH)) else: raw_vars = extractor.describe_dna(r, s) errors, warnings, summary = output.Summary() messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished Description Extract request') return render_template('description-extractor.html', extractor_max_input_length=settings.EXTRACTOR_MAX_INPUT_LENGTH, reference_sequence=reference_sequence or '', sample_sequence=sample_sequence or '', reference_accession_number=reference_accession_number or '', sample_accession_number=sample_accession_number or '', reference_filename=reference_filename or '', sample_filename=sample_filename or '', raw_vars=raw_vars, errors=errors, summary=summary, messages=messages, reference_method=reference_method, sample_method=sample_method)
def position_converter(): """ Position converter. """ # Backwards compatibility. if 'variant' in request.args: return redirect(url_for('.position_converter', description=request.args['variant']), code=301) assemblies = Assembly.query \ .order_by(*Assembly.order_by_criteria) \ .all() assembly_name_or_alias = request.args.get('assembly_name_or_alias', settings.DEFAULT_ASSEMBLY) description = request.args.get('description') if not description: return render_template('position-converter.html', assemblies=assemblies, assembly_name_or_alias=assembly_name_or_alias) output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received request positionConverter(%s, %s) from %s' % (assembly_name_or_alias, description, request.remote_addr)) stats.increment_counter('position-converter/website') chromosomal_description = None transcript_descriptions = None try: assembly = Assembly.by_name_or_alias(assembly_name_or_alias) except NoResultFound: output.addMessage(__file__, 3, 'ENOASSEMBLY', 'Not a valid assembly.') else: converter = Converter(assembly, output) # Convert chromosome name to accession number. corrected_description = converter.correctChrVariant(description) if corrected_description: # Now we're ready to actually do position conversion. if not(':c.' in corrected_description or ':n.' in corrected_description or ':g.' in corrected_description or ':m.' in corrected_description): grammar = Grammar(output) grammar.parse(corrected_description) if (':c.' in corrected_description or ':n.' in corrected_description): corrected_description = converter.c2chrom( corrected_description) chromosomal_description = corrected_description if corrected_description and (':g.' in corrected_description or ':m.' in corrected_description): descriptions = converter.chrom2c(corrected_description, 'dict') if descriptions is None: chromosomal_description = None elif descriptions: transcript_descriptions = [ '%-10s:\t%s' % (key[:10], '\n\t\t'.join(value)) for key, value in descriptions.items()] messages = map(util.message_info, output.getMessages()) output.addMessage(__file__, -1, 'INFO', 'Finished request positionConverter(%s, %s)' % (assembly_name_or_alias, description)) return render_template('position-converter.html', assemblies=assemblies, assembly_name_or_alias=assembly_name_or_alias, description=description, chromosomal_description=chromosomal_description, transcript_descriptions=transcript_descriptions, messages=messages)
def name_checker(): """ Name checker. """ # For backwards compatibility with older LOVD versions, we support the # `mutationName` argument. If present, we redirect and add `standalone=1`. # # Also for backwards compatibility, we support the `name` argument as an # alias for `description`. if 'name' in request.args: return redirect(url_for('.name_checker', description=request.args['name'], standalone=request.args.get('standalone')), code=301) if 'mutationName' in request.args: return redirect(url_for('.name_checker', description=request.args['mutationName'], standalone=1), code=301) description = request.args.get('description') if not description: return render_template('name-checker.html') output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received variant %s from %s' % (description, request.remote_addr)) stats.increment_counter('name-checker/website') variantchecker.check_variant(description, output) errors, warnings, summary = output.Summary() parse_error = output.getOutput('parseError') record_type = output.getIndexedOutput('recordType', 0, '') reference = output.getIndexedOutput('reference', 0, '') if reference: if record_type == 'LRG': reference_filename = reference + '.xml' else : reference_filename = reference + '.gb' else: reference_filename = None genomic_dna = output.getIndexedOutput('molType', 0) != 'n' genomic_description = output.getIndexedOutput('genomicDescription', 0, '') # Create a link to the UCSC Genome Browser. browser_link = None raw_variants = output.getIndexedOutput('rawVariantsChromosomal', 0) if raw_variants: positions = [pos for descr, (first, last) in raw_variants[2] for pos in (first, last)] bed_url = url_for('.bed', description=description, _external=True) browser_link = ('http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&' 'position={chromosome}:{start}-{stop}&hgt.customText=' '{bed_file}'.format(chromosome=raw_variants[0], start=min(positions) - 10, stop=max(positions) + 10, bed_file=urllib.quote(bed_url))) # Experimental description extractor. if (output.getIndexedOutput('original', 0) and output.getIndexedOutput('mutated', 0)): allele = extractor.describe_dna(output.getIndexedOutput('original', 0), output.getIndexedOutput('mutated', 0)) extracted = '(skipped)' if allele: extracted = unicode(allele) else: extracted = '' # Todo: Generate the fancy HTML views for the proteins here instead of in # `mutalyzer.variantchecker`. arguments = { 'description' : description, 'messages' : map(util.message_info, output.getMessages()), 'summary' : summary, 'parse_error' : parse_error, 'errors' : errors, 'genomicDescription' : genomic_description, 'chromDescription' : output.getIndexedOutput( 'genomicChromDescription', 0), 'genomicDNA' : genomic_dna, 'visualisation' : output.getOutput('visualisation'), 'descriptions' : output.getOutput('descriptions'), 'protDescriptions' : output.getOutput('protDescriptions'), 'oldProtein' : output.getOutput('oldProteinFancy'), 'altStart' : output.getIndexedOutput('altStart', 0), 'altProtein' : output.getOutput('altProteinFancy'), 'newProtein' : output.getOutput('newProteinFancy'), 'transcriptInfo' : output.getIndexedOutput('hasTranscriptInfo', 0, False), 'transcriptCoding' : output.getIndexedOutput('transcriptCoding', 0, False), 'exonInfo' : output.getOutput('exonInfo'), 'cdsStart_g' : output.getIndexedOutput('cdsStart_g', 0), 'cdsStart_c' : output.getIndexedOutput('cdsStart_c', 0), 'cdsStop_g' : output.getIndexedOutput('cdsStop_g', 0), 'cdsStop_c' : output.getIndexedOutput('cdsStop_c', 0), 'restrictionSites' : output.getOutput('restrictionSites'), 'legends' : output.getOutput('legends'), 'reference_filename' : reference_filename, # Todo: Download link is not shown... 'browserLink' : browser_link, 'extractedDescription': extracted, 'standalone' : bool(request.args.get('standalone')) } output.addMessage(__file__, -1, 'INFO', 'Finished variant %s' % description) return render_template('name-checker.html', **arguments)
def _processNameBatch(self, batch_job, cmd, flags): """ Process an entry from the Name Batch, write the results to the job-file. If an Exception is raised, catch and continue. Side-effect: - Output written to outputfile. @arg cmd: The NameChecker input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) O.addMessage(__file__, -1, "INFO", "Received NameChecker batchvariant " + cmd) stats.increment_counter('name-checker/batch') #Read out the flags skip = self.__processFlags(O, flags) if not skip: #Run mutalyzer and get values from Output Object 'O' try: variantchecker.check_variant(cmd, O) except Exception: #Catch all exceptions related to the processing of cmd O.addMessage(__file__, 4, "EBATCHU", "Unexpected error occurred, dev-team notified") import traceback O.addMessage(__file__, 4, "DEBUG", unicode(repr(traceback.format_exc()))) #except finally: #check if we need to update the database self._updateDbFlags(O, batch_job.id) #if batchOutput = O.getOutput("batchDone") outputline = "%s\t" % cmd outputline += "%s\t" % "|".join(O.getBatchMessages(2)) if batchOutput: outputline += batchOutput[0] #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename): # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = [ 'Input', 'Errors and warnings', 'AccNo', 'Genesymbol', 'Variant', 'Reference Sequence Start Descr.', 'Coding DNA Descr.', 'Protein Descr.', 'GeneSymbol Coding DNA Descr.', 'GeneSymbol Protein Descr.', 'Genomic Reference', 'Coding Reference', 'Protein Reference', 'Affected Transcripts', 'Affected Proteins', 'Restriction Sites Created', 'Restriction Sites Deleted' ] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else: handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s%s" % (outputline, separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finished NameChecker batchvariant " + cmd)
def batch_jobs_submit(): """ Run batch jobs and render batch checker HTML form. The batch jobs are added to the database by the scheduler and ran by the BatchChecker daemon. """ job_type = request.form.get('job_type') email = request.form.get('email') # Note that this is always a seekable binary file object. batch_file = request.files.get('file') assemblies = Assembly.query \ .order_by(*Assembly.order_by_criteria) \ .all() assembly_name_or_alias = request.form.get('assembly_name_or_alias', settings.DEFAULT_ASSEMBLY) errors = [] if not email: errors.append('Please provide an email address.') if job_type not in BATCH_JOB_TYPES: errors.append('Invalid batch job type.') if not file: errors.append('Please select a local file for upload.') if job_type == 'position-converter': try: Assembly.by_name_or_alias(assembly_name_or_alias) except NoResultFound: errors.append('Not a valid assembly.') argument = assembly_name_or_alias else: argument = None output = Output(__file__) if not errors: stats.increment_counter('batch-job/website') scheduler = Scheduler.Scheduler() file_instance = File.File(output) job, columns = file_instance.parseBatchFile(batch_file) if job is None: errors.append('Could not parse input file, please check your ' 'file format.') else: # Creates the result download URL from a job result_id. def create_download_url(result_id): return url_for('.batch_job_result', result_id=result_id, _external=True) result_id = scheduler.addJob( email, job, columns, job_type, argument=argument, create_download_url=create_download_url) # Todo: We now assume that the job was not scheduled if there are # messages, which is probably not correct. if not output.getMessages(): return redirect(url_for('.batch_job_progress', result_id=result_id)) for error in errors: output.addMessage(__file__, 3, 'EBATCHJOB', error) messages = map(util.message_info, output.getMessages()) return render_template('batch-jobs.html', assemblies=assemblies, assembly_name_or_alias=assembly_name_or_alias, job_type=job_type, max_file_size=settings.MAX_FILE_SIZE // 1048576, messages=messages)
def _processConversion(self, batch_job, cmd, flags): """ Process an entry from the Position Converter, write the results to the job-file. The Position Converter is wrapped in a try except block which ensures that he Batch Process keeps running. Errors are caught and the user will be notified. Side-effect: - Output written to outputfile. @arg cmd: The Syntax Checker input @type cmd: unicode @arg i: The JobID @type i: integer @arg build: The build to use for the converter @type build: unicode @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) variant = cmd variants = None gName = "" cNames = [""] O.addMessage(__file__, -1, "INFO", "Received PositionConverter batchvariant " + cmd) stats.increment_counter('position-converter/batch') skip = self.__processFlags(O, flags) if not skip: try: #process try: assembly = Assembly.by_name_or_alias(batch_job.argument) except NoResultFound: O.addMessage(__file__, 3, 'ENOASSEMBLY', 'Not a valid assembly: ' + batch_job.argument) raise converter = Converter(assembly, O) #Also accept chr accNo variant = converter.correctChrVariant(variant) #TODO: Parse the variant and check for c or g. This is ugly if not (":c." in variant or ":n." in variant or ":g." in variant): #Bad name grammar = Grammar(O) grammar.parse(variant) #if if ":c." in variant or ":n." in variant: # Do the c2chrom dance variant = converter.c2chrom(variant) # NOTE: # If we received a coding reference convert that to the # genomic position variant. Use that variant as the input # of the chrom2c. # If the input is a genomic variant or if we converted a # coding variant to a genomic variant we try to find all # other affected coding variants. if variant and ":g." in variant: # Do the chrom2c dance variants = converter.chrom2c(variant, "dict") if variants: gName = variant # Due to the cyclic behavior of the Position Converter # we know for a fact that if a correct chrom name is # generated by the converter.c2chrom that we will at # least find one variant with chrom2c. Collect the # variants from a nested lists and store them. cNames = [cName for cName2 in variants.values() \ for cName in cName2] except Exception: #Catch all exceptions related to the processing of cmd O.addMessage(__file__, 4, "EBATCHU", "Unexpected error occurred, dev-team notified") #except #if error = "%s" % "|".join(O.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename): # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = [ 'Input Variant', 'Errors', 'Chromosomal Variant', 'Coding Variant(s)' ] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else: handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s\t%s\t%s\t%s%s" % (cmd, error, gName, "\t".join(cNames), separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finisehd PositionConverter batchvariant " + cmd)
def _processConversion(self, batch_job, cmd, flags): """ Process an entry from the Position Converter, write the results to the job-file. The Position Converter is wrapped in a try except block which ensures that he Batch Process keeps running. Errors are caught and the user will be notified. Side-effect: - Output written to outputfile. @arg cmd: The Syntax Checker input @type cmd: unicode @arg i: The JobID @type i: integer @arg build: The build to use for the converter @type build: unicode @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) variant = cmd variants = None gName = "" cNames = [""] O.addMessage(__file__, -1, "INFO", "Received PositionConverter batchvariant " + cmd) stats.increment_counter('position-converter/batch') skip = self.__processFlags(O, flags) if not skip : try : #process try: assembly = Assembly.by_name_or_alias(batch_job.argument) except NoResultFound: O.addMessage(__file__, 3, 'ENOASSEMBLY', 'Not a valid assembly: ' + batch_job.argument) raise converter = Converter(assembly, O) #Also accept chr accNo variant = converter.correctChrVariant(variant) #TODO: Parse the variant and check for c or g. This is ugly if not(":c." in variant or ":n." in variant or ":g." in variant) : #Bad name grammar = Grammar(O) grammar.parse(variant) #if if ":c." in variant or ":n." in variant : # Do the c2chrom dance variant = converter.c2chrom(variant) # NOTE: # If we received a coding reference convert that to the # genomic position variant. Use that variant as the input # of the chrom2c. # If the input is a genomic variant or if we converted a # coding variant to a genomic variant we try to find all # other affected coding variants. if variant and ":g." in variant : # Do the chrom2c dance variants = converter.chrom2c(variant, "dict") if variants : gName = variant # Due to the cyclic behavior of the Position Converter # we know for a fact that if a correct chrom name is # generated by the converter.c2chrom that we will at # least find one variant with chrom2c. Collect the # variants from a nested lists and store them. cNames = [cName for cName2 in variants.values() \ for cName in cName2] except Exception: #Catch all exceptions related to the processing of cmd O.addMessage(__file__, 4, "EBATCHU", "Unexpected error occurred, dev-team notified") #except #if error = "%s" % "|".join(O.getBatchMessages(2)) #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename) : # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = ['Input Variant', 'Errors', 'Chromosomal Variant', 'Coding Variant(s)'] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else : handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s\t%s\t%s\t%s%s" % (cmd, error, gName, "\t".join(cNames), separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finisehd PositionConverter batchvariant " + cmd)
def _processNameBatch(self, batch_job, cmd, flags): """ Process an entry from the Name Batch, write the results to the job-file. If an Exception is raised, catch and continue. Side-effect: - Output written to outputfile. @arg cmd: The NameChecker input @type cmd: @arg i: The JobID @type i: @arg flags: Flags of the current entry @type flags: """ O = Output(__file__) O.addMessage(__file__, -1, "INFO", "Received NameChecker batchvariant " + cmd) stats.increment_counter('name-checker/batch') #Read out the flags skip = self.__processFlags(O, flags) if not skip : #Run mutalyzer and get values from Output Object 'O' try : variantchecker.check_variant(cmd, O) except Exception: #Catch all exceptions related to the processing of cmd O.addMessage(__file__, 4, "EBATCHU", "Unexpected error occurred, dev-team notified") import traceback O.addMessage(__file__, 4, "DEBUG", unicode(repr(traceback.format_exc()))) #except finally : #check if we need to update the database self._updateDbFlags(O, batch_job.id) #if batchOutput = O.getOutput("batchDone") outputline = "%s\t" % cmd outputline += "%s\t" % "|".join(O.getBatchMessages(2)) if batchOutput : outputline += batchOutput[0] #Output filename = "%s/batch-job-%s.txt" % (settings.CACHE_DIR, batch_job.result_id) if not os.path.exists(filename) : # If the file does not yet exist, create it with the correct # header above it. The header is read from the config file as # a list. We need a tab delimited string. header = ['Input', 'Errors and warnings', 'AccNo', 'Genesymbol', 'Variant', 'Reference Sequence Start Descr.', 'Coding DNA Descr.', 'Protein Descr.', 'GeneSymbol Coding DNA Descr.', 'GeneSymbol Protein Descr.', 'Genomic Reference', 'Coding Reference', 'Protein Reference', 'Affected Transcripts', 'Affected Proteins', 'Restriction Sites Created', 'Restriction Sites Deleted'] handle = io.open(filename, mode='a', encoding='utf-8') handle.write("%s\n" % "\t".join(header)) #if else : handle = io.open(filename, mode='a', encoding='utf-8') if flags and 'C' in flags: separator = '\t' else: separator = '\n' handle.write("%s%s" % (outputline, separator)) handle.close() O.addMessage(__file__, -1, "INFO", "Finished NameChecker batchvariant " + cmd)