<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import collections <EOL> import pysam <EOL> import ga4gh . datamodel . reads as reads <EOL> import ga4gh . protocol as protocol <EOL> class AbstractConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , container , objectIterator , outputFile , binaryOutput ) : <EOL> self . _container = container <EOL> self . _objectIterator = objectIterator <EOL> self . _outputFile = outputFile <EOL> self . _binaryOutput = binaryOutput <EOL> class SamException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class SamConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , client , readGroupId = None , referenceId = None , <EOL> start = None , end = None , outputFileName = None , binaryOutput = False ) : <EOL> self . _client = client <EOL> self . _readGroup = self . _client . getReadGroup ( readGroupId ) <EOL> self . _reference = self . _client . getReference ( referenceId ) <EOL> self . _start = start <EOL> self . _end = end <EOL> self . _outputFileName = outputFileName <EOL> self . _binaryOutput = binaryOutput <EOL> def convert ( self ) : <EOL> header = self . _getHeader ( ) <EOL> targetIds = self . _getTargetIds ( header ) <EOL> if self . _binaryOutput : <EOL> flags = "<STR_LIT:wb>" <EOL> else : <EOL> flags = "<STR_LIT>" <EOL> fileString = "<STR_LIT:->" <EOL> if self . _outputFileName is not None : <EOL> fileString = self . _outputFileName <EOL> alignmentFile = pysam . AlignmentFile ( fileString , flags , header = header ) <EOL> iterator = self . _client . searchReads ( <EOL> [ self . _readGroup . id ] , self . _reference . id , self . _start , self . _end ) <EOL> for read in iterator : <EOL> alignedSegment = SamLine . toAlignedSegment ( read , targetIds ) <EOL> alignmentFile . write ( alignedSegment ) <EOL> alignmentFile . close ( ) <EOL> def _getHeader ( self ) : <EOL> header = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:1.0>' } , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : self . _reference . length , <EOL> '<STR_LIT>' : self . _reference . name <EOL> } ] <EOL> } <EOL> return header <EOL> def _getTargetIds ( self , header ) : <EOL> targetIds = collections . defaultdict ( int ) <EOL> targetId = <NUM_LIT:0> <EOL> if '<STR_LIT>' in header : <EOL> headerLines = header [ '<STR_LIT>' ] <EOL> for headerLine in headerLines : <EOL> refName = headerLine [ '<STR_LIT>' ] <EOL> targetIds [ refName ] = targetId <EOL> targetId += <NUM_LIT:1> <EOL> return targetIds <EOL> class SamLine ( object ) : <EOL> """<STR_LIT>""" <EOL> _encoding = '<STR_LIT:utf8>' <EOL> _tagReservedFieldPrefixes = set ( [ "<STR_LIT:X>" , "<STR_LIT:Y>" , "<STR_LIT>" , ] ) <EOL> _tagIntegerFields = set ( [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ] ) <EOL> _tagStringFields = set ( [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , ] ) <EOL> _tagIntegerArrayFields = set ( [ "<STR_LIT>" , ] ) <EOL> def __init__ ( self ) : <EOL> raise SamException ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def toAlignedSegment ( cls , read , targetIds ) : <EOL> ret = pysam . AlignedSegment ( ) <EOL> ret . query_name = read . fragmentName . encode ( cls . _encoding ) <EOL> ret . query_sequence = read . alignedSequence . encode ( cls . _encoding ) <EOL> ret . flag = cls . toSamFlag ( read ) <EOL> if read . alignment is not None : <EOL> refName = read . alignment . position . referenceName <EOL> ret . reference_id = targetIds [ refName ] <EOL> if read . alignment is None : <EOL> ret . reference_start = <NUM_LIT:0> <EOL> else : <EOL> ret . reference_start = int ( read . alignment . position . position ) <EOL> if read . alignment is not None : <EOL> ret . mapping_quality = read . alignment . mappingQuality <EOL> ret . cigar = cls . toCigar ( read ) <EOL> if read . nextMatePosition is None : <EOL> ret . next_reference_id = - <NUM_LIT:1> <EOL> else : <EOL> nextRefName = read . nextMatePosition . referenceName <EOL> ret . next_reference_id = targetIds [ nextRefName ] <EOL> if read . nextMatePosition is None : <EOL> ret . next_reference_start = - <NUM_LIT:1> <EOL> else : <EOL> ret . next_reference_start = int ( read . nextMatePosition . position ) <EOL> ret . template_length = read . fragmentLength <EOL> ret . query_qualities = read . alignedQuality <EOL> ret . tags = cls . toTags ( read ) <EOL> return ret <EOL> @ classmethod <EOL> def toSamFlag ( cls , read ) : <EOL> flag = <NUM_LIT:0> <EOL> if read . numberReads == <NUM_LIT:2> : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . READ_PAIRED ) <EOL> if not read . improperPlacement : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . READ_PROPER_PAIR ) <EOL> if read . alignment is None : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . READ_UNMAPPED ) <EOL> if read . nextMatePosition is None : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . MATE_UNMAPPED ) <EOL> if ( read . alignment is not None and <EOL> read . alignment . position . strand == <EOL> protocol . Strand . NEG_STRAND ) : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . READ_REVERSE_STRAND ) <EOL> if ( read . nextMatePosition is not None and <EOL> read . nextMatePosition . strand == protocol . Strand . NEG_STRAND ) : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . MATE_REVERSE_STRAND ) <EOL> if read . readNumber is None : <EOL> pass <EOL> elif read . readNumber == <NUM_LIT:0> : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . FIRST_IN_PAIR ) <EOL> elif read . readNumber == <NUM_LIT:1> : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . SECOND_IN_PAIR ) <EOL> else : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . FIRST_IN_PAIR ) <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . SECOND_IN_PAIR ) <EOL> if read . secondaryAlignment : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . SECONDARY_ALIGNMENT ) <EOL> if read . failedVendorQualityChecks : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . FAILED_QUALITY_CHECK ) <EOL> if read . duplicateFragment : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . DUPLICATE_READ ) <EOL> if read . supplementaryAlignment : <EOL> flag = reads . SamFlags . setFlag ( <EOL> flag , reads . SamFlags . SUPPLEMENTARY_ALIGNMENT ) <EOL> return flag <EOL> @ classmethod <EOL> def toCigar ( cls , read ) : <EOL> cigarTuples = [ ] <EOL> if read . alignment is not None : <EOL> for gaCigarUnit in read . alignment . cigar : <EOL> operation = reads . SamCigar . ga2int ( gaCigarUnit . operation ) <EOL> length = int ( gaCigarUnit . operationLength ) <EOL> cigarTuple = ( operation , length ) <EOL> cigarTuples . append ( cigarTuple ) <EOL> return tuple ( cigarTuples ) <EOL> @ classmethod <EOL> def _parseTagValue ( cls , tag , value ) : <EOL> if tag [ <NUM_LIT:0> ] in cls . _tagReservedFieldPrefixes : <EOL> return value [ <NUM_LIT:0> ] . encode ( cls . _encoding ) <EOL> elif tag in cls . _tagIntegerFields : <EOL> return int ( value [ <NUM_LIT:0> ] ) <EOL> elif tag in cls . _tagStringFields : <EOL> return value [ <NUM_LIT:0> ] . encode ( cls . _encoding ) <EOL> elif tag in cls . _tagIntegerArrayFields : <EOL> return [ int ( integerString ) for integerString in value ] <EOL> else : <EOL> raise SamException ( "<STR_LIT>" . format ( tag ) ) <EOL> @ classmethod <EOL> def toTags ( cls , read ) : <EOL> tags = [ ] <EOL> for tag , value in read . info . items ( ) : <EOL> val = cls . _parseTagValue ( tag , value ) <EOL> tags . append ( ( tag . encode ( cls . _encoding ) , val ) ) <EOL> retval = tuple ( tags ) <EOL> return retval <EOL> class VcfException ( Exception ) : <EOL> pass <EOL> class VcfConverter ( AbstractConverter ) : <EOL> """<STR_LIT>""" <EOL> def _writeHeader ( self ) : <EOL> variantSet = self . _container <EOL> print ( "<STR_LIT>" , variantSet . id ) <EOL> print ( "<STR_LIT>" , variantSet . datasetId ) <EOL> print ( "<STR_LIT>" ) <EOL> for metadata in variantSet . metadata : <EOL> print ( "<STR_LIT:\t>" , metadata ) <EOL> def _writeBody ( self ) : <EOL> for variant in self . _objectIterator : <EOL> print ( variant ) <EOL> break <EOL> def convert ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _writeHeader ( ) <EOL> self . _writeBody ( ) </s>
