<s> """<STR_LIT>""" <EOL> import os <EOL> import os . path as op <EOL> import sys <EOL> import logging <EOL> from collections import defaultdict <EOL> from jcvi . formats . base import must_open <EOL> from jcvi . utils . cbook import percentage <EOL> from jcvi . apps . base import OptionParser , ActionDispatcher , sh , which <EOL> class Protocol ( object ) : <EOL> def __init__ ( self , outputDir , reference , reads , highqual = False ) : <EOL> self . outputDir = outputDir <EOL> self . reference = reference <EOL> self . reads = reads <EOL> oblasr = ( <NUM_LIT:16> , <NUM_LIT> ) if highqual else ( <NUM_LIT:8> , <NUM_LIT> ) <EOL> self . blasr = "<STR_LIT>" . format ( * oblasr ) <EOL> self . blasr += "<STR_LIT>" <EOL> self . blasr += "<STR_LIT>" <EOL> def write_xml ( self , filename = "<STR_LIT>" ) : <EOL> import xml . etree . cElementTree as ET <EOL> import xml . dom . minidom as XD <EOL> jellyProtocol = ET . Element ( "<STR_LIT>" ) <EOL> reference = ET . SubElement ( jellyProtocol , "<STR_LIT>" ) <EOL> reference . text = self . reference <EOL> outputDir = ET . SubElement ( jellyProtocol , "<STR_LIT>" ) <EOL> outputDir . text = self . outputDir <EOL> blasr = ET . SubElement ( jellyProtocol , "<STR_LIT>" ) <EOL> blasr . text = self . blasr <EOL> inp = ET . SubElement ( jellyProtocol , "<STR_LIT:input>" ) <EOL> baseDir , readsfile = op . split ( self . reads ) <EOL> inp . set ( "<STR_LIT>" , baseDir ) <EOL> job = ET . SubElement ( inp , "<STR_LIT>" ) <EOL> job . text = readsfile <EOL> s = ET . tostring ( jellyProtocol ) <EOL> s = XD . parseString ( s ) <EOL> fw = open ( filename , "<STR_LIT:w>" ) <EOL> print >> sys . stderr , s . toprettyxml ( ) <EOL> print >> fw , s . toprettyxml ( ) <EOL> logging . debug ( "<STR_LIT>" . format ( filename ) ) <EOL> fw . close ( ) <EOL> class M4Line ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sline ) : <EOL> args = sline . split ( ) <EOL> self . query = args [ <NUM_LIT:0> ] <EOL> self . subject = args [ <NUM_LIT:1> ] <EOL> self . score = int ( args [ <NUM_LIT:2> ] ) <EOL> self . qstrand = args [ <NUM_LIT:3> ] <EOL> self . qstart = int ( args [ <NUM_LIT:4> ] ) <EOL> self . qstop = int ( args [ <NUM_LIT:5> ] ) <EOL> self . qlen = int ( args [ <NUM_LIT:6> ] ) <EOL> self . sstrand = args [ <NUM_LIT:7> ] <EOL> self . sstart = int ( args [ <NUM_LIT:8> ] ) <EOL> self . sstop = int ( args [ <NUM_LIT:9> ] ) <EOL> self . slen = int ( args [ <NUM_LIT:10> ] ) <EOL> self . spaceusage = int ( args [ <NUM_LIT:11> ] ) <EOL> self . quality = int ( args [ <NUM_LIT:12> ] ) <EOL> def main ( ) : <EOL> actions = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> p = ActionDispatcher ( actions ) <EOL> p . dispatch ( globals ( ) ) <EOL> def filterm4 ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( filterm4 . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = <NUM_LIT:1> , type = "<STR_LIT:int>" , help = "<STR_LIT>" ) <EOL> p . set_outfile ( ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> m4file , = args <EOL> best = opts . best <EOL> fp = open ( m4file ) <EOL> fw = must_open ( opts . outfile , "<STR_LIT:w>" ) <EOL> seen = defaultdict ( int ) <EOL> retained = total = <NUM_LIT:0> <EOL> for row in fp : <EOL> r = M4Line ( row ) <EOL> total += <NUM_LIT:1> <EOL> if total % <NUM_LIT> == <NUM_LIT:0> : <EOL> logging . debug ( "<STR_LIT>" . format ( percentage ( retained , total ) ) ) <EOL> if seen . get ( r . query , <NUM_LIT:0> ) < best : <EOL> fw . write ( row ) <EOL> seen [ r . query ] += <NUM_LIT:1> <EOL> retained += <NUM_LIT:1> <EOL> fw . close ( ) <EOL> def spancount ( args ) : <EOL> """<STR_LIT>""" <EOL> import json <EOL> p = OptionParser ( spancount . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> fof , = args <EOL> fp = open ( fof ) <EOL> flist = [ row . strip ( ) for row in fp ] <EOL> spanCount = "<STR_LIT>" <EOL> avgSpanBases = "<STR_LIT>" <EOL> fw = open ( spanCount , "<STR_LIT:w>" ) <EOL> for f in flist : <EOL> fp = open ( f ) <EOL> j = json . load ( fp ) <EOL> sc = j . get ( spanCount , None ) <EOL> asb = j . get ( avgSpanBases , None ) <EOL> print >> fw , f , asb , sc <EOL> fw . flush ( ) <EOL> fw . close ( ) <EOL> def fake_quals ( fa ) : <EOL> faq = fa . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> if op . exists ( faq ) : <EOL> logging . debug ( "<STR_LIT>" . format ( faq ) ) <EOL> else : <EOL> sh ( "<STR_LIT>" . format ( fa , faq ) ) <EOL> return fa , faq <EOL> def patch ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . base import write_file <EOL> from jcvi . formats . fasta import format <EOL> p = OptionParser ( patch . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> p . set_home ( "<STR_LIT>" ) <EOL> p . set_cpus ( ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> ref , reads = args <EOL> cpus = opts . cpus <EOL> cmd = op . join ( opts . pbjelly_home , "<STR_LIT>" ) <EOL> setup = "<STR_LIT>" . format ( cmd ) <EOL> if not which ( "<STR_LIT>" ) : <EOL> sh ( setup ) <EOL> pf = ref . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> pr , px = reads . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> if opts . cleanfasta : <EOL> oref = pf + "<STR_LIT>" <EOL> oreads = pr + "<STR_LIT>" <EOL> format ( [ ref , oref ] ) <EOL> format ( [ reads , oreads ] ) <EOL> ref , reads = oref , oreads <EOL> ref , refq = fake_quals ( ref ) <EOL> convert_reads = not px in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if convert_reads : <EOL> reads , readsq = fake_quals ( reads ) <EOL> readsfiles = "<STR_LIT:U+0020>" . join ( ( reads , readsq ) ) <EOL> else : <EOL> readsfiles = reads <EOL> dref , dreads = "<STR_LIT>" , "<STR_LIT>" <EOL> cwd = os . getcwd ( ) <EOL> reference = op . join ( cwd , "<STR_LIT>" . format ( dref , ref ) ) <EOL> reads = op . join ( cwd , "<STR_LIT>" . format ( dreads , reads ) ) <EOL> if not op . exists ( reference ) : <EOL> sh ( "<STR_LIT>" . format ( dref ) ) <EOL> sh ( "<STR_LIT>" . format ( "<STR_LIT:U+0020>" . join ( ( ref , refq ) ) , dref ) ) <EOL> if not op . exists ( reads ) : <EOL> sh ( "<STR_LIT>" . format ( dreads ) ) <EOL> sh ( "<STR_LIT>" . format ( readsfiles , dreads ) ) <EOL> outputDir = cwd <EOL> p = Protocol ( outputDir , reference , reads , highqual = opts . highqual ) <EOL> p . write_xml ( ) <EOL> runsh = [ setup ] <EOL> for action in "<STR_LIT>" . split ( "<STR_LIT:|>" ) : <EOL> runsh . append ( "<STR_LIT>" . format ( action ) ) <EOL> runsh . append ( '<STR_LIT>' . format ( cpus ) ) <EOL> runsh . append ( "<STR_LIT>" ) <EOL> runfile = "<STR_LIT>" <EOL> contents = "<STR_LIT:\n>" . join ( runsh ) <EOL> write_file ( runfile , contents ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
