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 _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)