<s> """<STR_LIT>""" <EOL> import sys <EOL> from collections import OrderedDict as dict <EOL> for line in sys . stdin : <EOL> if line . startswith ( '<STR_LIT:#>' ) : <EOL> print line , <EOL> continue <EOL> print line , <EOL> continue <EOL> fields = line . rstrip ( '<STR_LIT:\n>' ) . split ( '<STR_LIT:\t>' ) <EOL> info = dict ( ( kv [ <NUM_LIT:0> ] , ( kv [ <NUM_LIT:1> ] if len ( kv ) > <NUM_LIT:1> else None ) ) for kv in ( x . split ( '<STR_LIT:=>' ) for x in fields [ <NUM_LIT:7> ] . split ( '<STR_LIT:;>' ) ) ) <EOL> ref , alt = fields [ <NUM_LIT:3> : <NUM_LIT:5> ] <EOL> if '<STR_LIT>' in info : <EOL> om = info [ '<STR_LIT>' ] <EOL> alleles = om . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] . split ( '<STR_LIT:/>' ) <EOL> assert ref == alleles [ <NUM_LIT:0> ] <EOL> else : <EOL> if '<STR_LIT:U+002C>' in info [ '<STR_LIT>' ] : <EOL> idxs = map ( int , info [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> alleles = [ [ ref , alt ] [ i ] for i in idxs ] <EOL> else : <EOL> print line , <EOL> continue <EOL> alt_i = alleles . index ( alt ) <EOL> allele_idxs = map ( int , info [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> info0 = { } <EOL> for pull_idx , ai in enumerate ( allele_idxs ) : <EOL> if ai == <NUM_LIT:0> : <EOL> for k in ( key for key in info if key . startswith ( '<STR_LIT>' ) ) : <EOL> val = info [ k ] . split ( '<STR_LIT:U+002C>' ) <EOL> if k in info0 : info0 [ k ] += '<STR_LIT:U+002C>' <EOL> info0 [ k ] = val [ pull_idx ] <EOL> if not alt_i in allele_idxs : <EOL> for k in ( key for key in info if key . startswith ( '<STR_LIT>' ) ) : <EOL> info [ k ] = "<STR_LIT:.>" <EOL> else : <EOL> pull_idx = allele_idxs . index ( alt_i ) <EOL> assert pull_idx >= <NUM_LIT:0> , ( allele_idxs , alt_i ) <EOL> for k in ( key for key in info if key . startswith ( '<STR_LIT>' ) ) : <EOL> val = info [ k ] . split ( '<STR_LIT:U+002C>' ) <EOL> assert len ( val ) == len ( allele_idxs ) , ( info , k , info [ k ] ) <EOL> info [ k ] = val [ pull_idx ] <EOL> vv = info [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> info [ '<STR_LIT>' ] = "<STR_LIT:U+002C>" . join ( v if v == '<STR_LIT:0>' else '<STR_LIT:1>' for v in vv ) <EOL> for k in info0 : <EOL> if info [ k ] == "<STR_LIT:.>" : info [ k ] = info0 [ k ] <EOL> else : info [ k ] = info0 [ k ] + "<STR_LIT:U+002C>" + info [ k ] <EOL> fields [ <NUM_LIT:7> ] = "<STR_LIT:;>" . join ( kv [ <NUM_LIT:0> ] + ( ( '<STR_LIT:=>' + kv [ <NUM_LIT:1> ] ) if kv [ <NUM_LIT:1> ] is not None else '<STR_LIT>' ) for kv in info . items ( ) ) <EOL> print "<STR_LIT:\t>" . join ( fields ) </s>
