<s> import sys <EOL> from collections import namedtuple <EOL> from math import fabs <EOL> from nucio import getDeltaAlignmentIterator , deltaRecordToOriginalFormat <EOL> from nucio import deltaRecordHeaderToString , deltaAlignmentHeaderToString <EOL> if not len ( sys . argv ) == <NUM_LIT:6> : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> def alignment_is_good ( record , a , arange ) : <EOL> trimmed_slen = arange . end - arange . start <EOL> if trimmed_slen < <NUM_LIT:0> : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> qury_wiggle = record . qlen * WIGGLE_PCT <EOL> if not fabs ( a . send - a . sstart ) > MIN_ALIGNMENT_SIZE : <EOL> return False <EOL> if ( fabs ( a . send - a . sstart ) >= ( trimmed_slen * CONTAINED_PCT ) and <EOL> record . qlen > record . slen ) : <EOL> return True <EOL> if ( fabs ( a . qend - a . qstart ) > ( record . qlen * CONTAINED_PCT ) and <EOL> record . slen > record . qlen ) : <EOL> return True <EOL> if ( ( a . sstart == arange . start or arange . end == a . send ) and <EOL> ( a . qstart < qury_wiggle or a . qend < qury_wiggle or <EOL> ( record . qlen - a . qstart ) < qury_wiggle or ( record . qlen - a . qend ) < qury_wiggle ) ) : <EOL> return True <EOL> return False <EOL> AlignmentRange = namedtuple ( '<STR_LIT>' , [ "<STR_LIT:start>" , "<STR_LIT:end>" ] ) <EOL> WIGGLE_PCT = float ( sys . argv [ <NUM_LIT:2> ] ) <EOL> CONTAINED_PCT = float ( sys . argv [ <NUM_LIT:3> ] ) <EOL> MIN_ALIGNMENT_SIZE = int ( sys . argv [ <NUM_LIT:4> ] ) <EOL> outfilename = sys . argv [ <NUM_LIT:5> ] <EOL> fh = open ( sys . argv [ <NUM_LIT:1> ] ) <EOL> delta_header = fh . readline ( ) + fh . readline ( ) . strip ( ) <EOL> deltaHash = { } <EOL> for record in getDeltaAlignmentIterator ( fh ) : <EOL> if not deltaHash . has_key ( record . sname ) : <EOL> deltaHash [ record . sname ] = [ ] <EOL> deltaHash [ record . sname ] . append ( record ) <EOL> fh . close ( ) <EOL> readRange = { } <EOL> for readname , drecord_list in deltaHash . iteritems ( ) : <EOL> ( start , end ) = ( drecord_list [ <NUM_LIT:0> ] . slen , <NUM_LIT:0> ) <EOL> for drecord in drecord_list : <EOL> for alignment in drecord . alignments : <EOL> l = min ( alignment . sstart , alignment . send ) <EOL> if l < start : <EOL> start = l <EOL> r = max ( alignment . sstart , alignment . send ) <EOL> if r > end : <EOL> end = r <EOL> readRange [ readname ] = AlignmentRange ( start , end ) <EOL> ofh = open ( outfilename , "<STR_LIT:w>" ) <EOL> efh = open ( outfilename + "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> ofh . write ( delta_header ) <EOL> ofh . write ( "<STR_LIT:\n>" ) <EOL> for readname , drecord_list in deltaHash . iteritems ( ) : <EOL> alignment_range = readRange [ readname ] <EOL> for record in drecord_list : <EOL> pfalignments = [ ] <EOL> for alnmt in record . alignments : <EOL> if alignment_is_good ( record , alnmt , alignment_range ) : <EOL> pfalignments . append ( alnmt ) <EOL> else : <EOL> efh . write ( "<STR_LIT>" ) <EOL> efh . write ( deltaRecordHeaderToString ( record ) + "<STR_LIT:\n>" ) <EOL> efh . write ( deltaAlignmentHeaderToString ( alnmt ) + "<STR_LIT:\n>" ) <EOL> if len ( pfalignments ) > <NUM_LIT:1> : <EOL> efh . write ( "<STR_LIT>" ) <EOL> efh . write ( deltaRecordHeaderToString ( record ) + "<STR_LIT:\n>" ) <EOL> for a in pfalignments : <EOL> efh . write ( deltaAlignmentHeaderToString ( a ) + "<STR_LIT:\n>" ) <EOL> record . alignments [ : ] = [ ] <EOL> for al in pfalignments : <EOL> record . alignments . append ( al ) <EOL> if ( len ( record . alignments ) > <NUM_LIT:0> ) : <EOL> ofh . write ( deltaRecordToOriginalFormat ( record ) ) <EOL> ofh . write ( "<STR_LIT:\n>" ) <EOL> efh . close ( ) <EOL> ofh . close ( ) </s>
