def _converter(self, assembly_name_or_alias): """ Create a Converter instance for a given genome assembly. """ assembly = Assembly.query \ .filter(or_(Assembly.name == assembly_name_or_alias, Assembly.alias == assembly_name_or_alias)) \ .one() return Converter(assembly, self.output)
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 lovd_variant_info(): """ The chromosomal to coding and vice versa conversion interface for LOVD. Search for an NM number in the database, if the version number matches, get the start and end positions in a variant and translate these positions to chromosomal notation if the variant is in coding notation and vice versa. - If no end position is present, the start position is assumed to be the end position. - If the version number is not found in the database, an error message is generated and a suggestion for an other version is given. - If the reference sequence is not found at all, an error is returned. - If no variant is present, the transcription start and end and CDS end in coding notation is returned. - If the variant is not accepted by the nomenclature parser, a parse error will be printed. Get variant info and return the result as plain text. Parameters: LOVD_ver The version of the calling LOVD. build The human genome build (hg19 assumed). acc The accession number (NM number). var A description of the variant. Returns: start_main The main coordinate of the start position in I{c.} (non-star) notation. start_offset The offset coordinate of the start position in I{c.} notation (intronic position). end_main The main coordinate of the end position in I{c.} (non-star) notation. end_offset The offset coordinate of the end position in I{c.} notation (intronic position). start_g The I{g.} notation of the start position. end_g The I{g.} notation of the end position. type The mutation type. Returns (alternative): trans_start Transcription start in I{c.} notation. trans_stop Transcription stop in I{c.} notation. CDS_stop CDS stop in I{c.} notation. """ lovd_version = request.args['LOVD_ver'] build = request.args['build'] accession = request.args['acc'] description = request.args.get('var') output = Output(__file__) output.addMessage(__file__, -1, 'INFO', 'Received request variantInfo(%s:%s (LOVD_ver %s, ' 'build %s)) from %s' % (accession, description, lovd_version, build, request.remote_addr)) try: assembly = Assembly.by_name_or_alias(build) except NoResultFound: response = make_response('invalid build') response.headers['Content-Type'] = 'text/plain; charset=utf-8' return response converter = Converter(assembly, output) result = '' # If no variant is given, return transcription start, transcription # end and CDS stop in c. notation. if description: ret = converter.mainMapping(accession, description) else: ret = converter.giveInfo(accession) if ret: result = '%i\n%i\n%i' % ret if not result and not getattr(ret, 'startmain', None): out = output.getOutput('LOVDERR') if out: result = out[0] else: result = 'Unknown error occured' output.addMessage(__file__, -1, 'INFO', 'Finished request variantInfo(%s:%s (LOVD_ver %s, ' 'build %s))' % (accession, description, lovd_version, build)) if not result and getattr(ret, 'startmain', None): result = '%i\n%i\n%i\n%i\n%i\n%i\n%s' % ( ret.startmain, ret.startoffset, ret.endmain, ret.endoffset, ret.start_g, ret.end_g, ret.mutationType) # Todo: Obsoleted error messages, remove soon. if lovd_version == '2.0-23': response = re.sub('^Error \(.*\):', 'Error:', result) response = make_response(result) response.headers['Content-Type'] = 'text/plain; charset=utf-8' return response
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)