<s> """<STR_LIT>""" <EOL> from pbcore . io . base import ReaderBase , WriterBase <EOL> class BLASRRecord ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , qID , qLength , qStart , qEnd , qStrand , <EOL> sID , sLength , sStart , sEnd , sStrand , <EOL> score , mapQV , <EOL> nMatch = None , nMismatch = None , nIns = None , nDel = None , <EOL> qAln = None , alnStr = None , sAln = None , <EOL> strand = None , identity = None ) : <EOL> self . qID = qID <EOL> self . qLength = qLength <EOL> self . qStart = qStart <EOL> self . qEnd = qEnd <EOL> def strandString ( s ) : <EOL> d = { '<STR_LIT:+>' : '<STR_LIT:+>' , '<STR_LIT:->' : '<STR_LIT:->' , <NUM_LIT:0> : '<STR_LIT:+>' , <NUM_LIT:1> : '<STR_LIT:->' , '<STR_LIT:0>' : '<STR_LIT:+>' , '<STR_LIT:1>' : '<STR_LIT:->' } <EOL> try : <EOL> return d [ s ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" . format ( s = s ) ) <EOL> self . qStrand = strandString ( qStrand ) <EOL> self . sID = sID <EOL> self . sLength = sLength <EOL> self . sStart = sStart <EOL> self . sEnd = sEnd <EOL> self . sStrand = strandString ( sStrand ) <EOL> self . score = score <EOL> self . nMatch = nMatch <EOL> self . nMismatch = nMismatch <EOL> self . nIns = nIns <EOL> self . nDel = nDel <EOL> self . mapQV = mapQV <EOL> self . qAln = qAln <EOL> self . alnStr = alnStr <EOL> self . sAln = sAln <EOL> self . strand = strandString ( strand ) <EOL> self . identity = identity <EOL> @ property <EOL> def alignment_length ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . alnStr ) if self . alnStr is not None else None <EOL> @ property <EOL> def mismatches ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . nMismatch <EOL> @ property <EOL> def gaps ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . nIns + self . nDel if self . nIns is not None and self . nDel is not None else None <EOL> @ property <EOL> def e ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def __eq__ ( self , another ) : <EOL> return self . __dict__ == another . __dict__ <EOL> def __str__ ( self ) : <EOL> msg = """<STR_LIT>""" . format ( qID = self . qID , sID = self . sID , <EOL> identity = self . identity , <EOL> qStart = self . qStart , qEnd = self . qEnd , <EOL> sStart = self . sStart , sEnd = self . sEnd , <EOL> e = self . e , score = self . score , strand = self . strand ) <EOL> padding = "<STR_LIT:U+0020>" * <NUM_LIT:8> <EOL> if self . alignment_length is not None : <EOL> msg += "<STR_LIT>" . format ( <EOL> p = padding , alignment_length = self . alignment_length ) <EOL> if None not in [ self . nMatch , self . nMismatch , <EOL> self . nIns , self . nDel , self . gaps ] : <EOL> msg += "<STR_LIT>" . format ( <EOL> mismatches = self . nMismatch , gaps = self . gaps , p = padding ) <EOL> return msg <EOL> @ classmethod <EOL> def fromM4String ( cls , line , delimiter = '<STR_LIT:U+0020>' , flipQS = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fields = line . rstrip ( ) . split ( delimiter ) <EOL> assert ( len ( fields ) == <NUM_LIT> ) <EOL> qName = fields [ <NUM_LIT:0> ] <EOL> if qName . count ( '<STR_LIT:/>' ) > <NUM_LIT:2> : <EOL> qName = "<STR_LIT:/>" . join ( qName . split ( "<STR_LIT:/>" ) [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> print qName <EOL> tName = fields [ <NUM_LIT:1> ] <EOL> score = int ( fields [ <NUM_LIT:2> ] ) <EOL> identity = float ( fields [ <NUM_LIT:3> ] ) <EOL> qStrand = fields [ <NUM_LIT:4> ] <EOL> qStart = int ( fields [ <NUM_LIT:5> ] ) <EOL> qEnd = int ( fields [ <NUM_LIT:6> ] ) <EOL> qLength = int ( fields [ <NUM_LIT:7> ] ) <EOL> tStrand = fields [ <NUM_LIT:8> ] <EOL> tStart = int ( fields [ <NUM_LIT:9> ] ) <EOL> tEnd = int ( fields [ <NUM_LIT:10> ] ) <EOL> tLength = int ( fields [ <NUM_LIT:11> ] ) <EOL> mapQV = int ( fields [ <NUM_LIT:12> ] ) <EOL> strand = '<STR_LIT:+>' if qStrand == tStrand else '<STR_LIT:->' <EOL> if flipQS : <EOL> return BLASRRecord ( <EOL> qID = tName , qLength = tLength , <EOL> qStart = tStart , qEnd = tEnd , qStrand = tStrand , <EOL> sID = qName , sLength = qLength , <EOL> sStart = qStart , sEnd = qEnd , sStrand = qStrand , <EOL> score = score , mapQV = mapQV , <EOL> strand = strand , identity = identity ) <EOL> else : <EOL> return BLASRRecord ( <EOL> qID = qName , qLength = qLength , <EOL> qStart = qStart , qEnd = qEnd , qStrand = qStrand , <EOL> sID = tName , sLength = tLength , <EOL> sStart = tStart , sEnd = tEnd , sStrand = tStrand , <EOL> score = score , mapQV = mapQV , <EOL> strand = strand , identity = identity ) <EOL> except ( AssertionError , ValueError ) : <EOL> errMsg = "<STR_LIT>" <EOL> raise ValueError ( errMsg ) <EOL> @ classmethod <EOL> def fromM5String ( cls , line ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fields = line . strip ( ) . split ( ) <EOL> assert ( len ( fields ) == <NUM_LIT> ) <EOL> qID = fields [ <NUM_LIT:0> ] <EOL> if qID . count ( '<STR_LIT:/>' ) > <NUM_LIT:2> : <EOL> qID = '<STR_LIT:/>' . join ( qID . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> qLength = int ( fields [ <NUM_LIT:1> ] ) <EOL> qStart = int ( fields [ <NUM_LIT:2> ] ) <EOL> qEnd = int ( fields [ <NUM_LIT:3> ] ) <EOL> qStrand = fields [ <NUM_LIT:4> ] <EOL> sID = fields [ <NUM_LIT:5> ] <EOL> sLength = int ( fields [ <NUM_LIT:6> ] ) <EOL> sStart = int ( fields [ <NUM_LIT:7> ] ) <EOL> sEnd = int ( fields [ <NUM_LIT:8> ] ) <EOL> sStrand = fields [ <NUM_LIT:9> ] <EOL> score = int ( fields [ <NUM_LIT:10> ] ) <EOL> nMatch = int ( fields [ <NUM_LIT:11> ] ) <EOL> nMismatch = int ( fields [ <NUM_LIT:12> ] ) <EOL> nIns = int ( fields [ <NUM_LIT> ] ) <EOL> nDel = int ( fields [ <NUM_LIT> ] ) <EOL> mapQV = int ( fields [ <NUM_LIT:15> ] ) <EOL> qAln = fields [ <NUM_LIT:16> ] <EOL> alnStr = fields [ <NUM_LIT> ] <EOL> sAln = fields [ <NUM_LIT> ] <EOL> identity = float ( "<STR_LIT>" . format ( nMatch * <NUM_LIT> / len ( alnStr ) ) ) <EOL> strand = '<STR_LIT:+>' if qStrand == sStrand else '<STR_LIT:->' <EOL> return BLASRRecord ( <EOL> qID = qID , qLength = qLength , <EOL> qStart = qStart , qEnd = qEnd , qStrand = qStrand , <EOL> sID = sID , sLength = sLength , <EOL> sStart = sStart , sEnd = sEnd , sStrand = sStrand , <EOL> score = score , mapQV = mapQV , <EOL> nMatch = nMatch , nMismatch = nMismatch , nIns = nIns , nDel = nDel , <EOL> qAln = qAln , alnStr = alnStr , sAln = sAln , <EOL> strand = strand , identity = identity ) <EOL> except ( AssertionError , ValueError ) as e : <EOL> errMsg = "<STR_LIT>" <EOL> raise ValueError ( errMsg + str ( e ) ) <EOL> class BLASRReaderBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fileName , className = "<STR_LIT>" ) : <EOL> self . fileName = fileName <EOL> self . className = className <EOL> try : <EOL> self . infile = open ( self . fileName , '<STR_LIT:r>' ) <EOL> except IOError as e : <EOL> errMsg = self . className + "<STR_LIT>" + fileName + "<STR_LIT:\n>" + str ( e ) <EOL> raise IOError ( errMsg ) <EOL> def __iter__ ( self ) : <EOL> raise NotImplementedError ( self . className + <EOL> "<STR_LIT>" ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . infile . close ( ) <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . close ( ) <EOL> class BLASRM4Reader ( BLASRReaderBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fileName , flipQS = False ) : <EOL> BLASRReaderBase . __init__ ( self , fileName = fileName , <EOL> className = "<STR_LIT>" ) <EOL> self . flipQS = flipQS <EOL> def __iter__ ( self ) : <EOL> try : <EOL> for line in self . infile : <EOL> line = line . strip ( ) <EOL> if len ( line ) == <NUM_LIT:0> or line [ <NUM_LIT:0> ] == '<STR_LIT:#>' or line . lower ( ) . endswith ( "<STR_LIT>" . lower ( ) ) : <EOL> continue <EOL> yield BLASRRecord . fromM4String ( line = line , <EOL> flipQS = self . flipQS ) <EOL> except ValueError as e : <EOL> raise ValueError ( self . className + "<STR_LIT>" <EOL> + self . fileName + "<STR_LIT:\n>" + str ( e ) ) <EOL> class BLASRM5Reader ( BLASRReaderBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fileName ) : <EOL> BLASRReaderBase . __init__ ( self , fileName = fileName , <EOL> className = "<STR_LIT>" ) <EOL> def __iter__ ( self ) : <EOL> try : <EOL> for line in self . infile : <EOL> line = line . strip ( ) <EOL> if len ( line ) == <NUM_LIT:0> or line [ <NUM_LIT:0> ] == '<STR_LIT:#>' or line . lower ( ) . endswith ( "<STR_LIT>" . lower ( ) ) : <EOL> continue <EOL> yield BLASRRecord . fromM5String ( line = line ) <EOL> except ValueError as e : <EOL> raise ValueError ( self . className + "<STR_LIT>" <EOL> + self . fileName + "<STR_LIT:\n>" + str ( e ) ) </s>
