<s> """<STR_LIT>""" <EOL> import sys <EOL> from jcvi . formats . base import LineFile , must_open <EOL> from jcvi . formats . gff import valid_gff_type <EOL> from jcvi . apps . base import OptionParser , ActionDispatcher <EOL> class BtabLine ( object ) : <EOL> def __init__ ( self , row , aat_dialect = False ) : <EOL> args = row . strip ( ) . split ( "<STR_LIT:\t>" ) <EOL> self . nargs = len ( args ) <EOL> self . query = args [ <NUM_LIT:0> ] . split ( ) [ <NUM_LIT:0> ] <EOL> self . qLen = int ( args [ <NUM_LIT:2> ] ) <EOL> self . qStart = int ( args [ <NUM_LIT:6> ] ) <EOL> self . qStop = int ( args [ <NUM_LIT:7> ] ) <EOL> self . qFrame = int ( args [ <NUM_LIT:16> ] ) <EOL> self . qStrand = "<STR_LIT:->" if args [ <NUM_LIT> ] == "<STR_LIT>" else "<STR_LIT:+>" <EOL> self . subject = args [ <NUM_LIT:5> ] <EOL> self . sStart = int ( args [ <NUM_LIT:8> ] ) <EOL> self . sStop = int ( args [ <NUM_LIT:9> ] ) <EOL> self . sDesc = args [ <NUM_LIT:15> ] <EOL> self . sLen = int ( args [ <NUM_LIT> ] ) <EOL> if self . qStrand == "<STR_LIT:->" : <EOL> self . sStart , self . sStop = self . sStop , self . sStart <EOL> self . pctid = float ( args [ <NUM_LIT:10> ] ) <EOL> self . pctsim = float ( args [ <NUM_LIT:11> ] ) <EOL> self . date = args [ <NUM_LIT:1> ] <EOL> self . method = args [ <NUM_LIT:3> ] <EOL> self . database = args [ <NUM_LIT:4> ] <EOL> if aat_dialect : <EOL> self . score = float ( args [ <NUM_LIT:12> ] ) <EOL> self . chainNum = int ( args [ <NUM_LIT> ] ) <EOL> self . segmentNum = int ( args [ <NUM_LIT> ] ) <EOL> self . key = "<STR_LIT:->" . join ( str ( x ) for x in ( self . query , self . subject , self . chainNum ) ) <EOL> else : <EOL> self . score = float ( args [ <NUM_LIT> ] ) <EOL> self . evalue = float ( args [ <NUM_LIT> ] ) <EOL> self . pvalue = float ( args [ <NUM_LIT:20> ] ) if len ( args ) > <NUM_LIT:20> else None <EOL> def __getitem__ ( self , key ) : <EOL> return getattr ( self , key ) <EOL> @ property <EOL> def blastline ( self ) : <EOL> return "<STR_LIT:\t>" . join ( ( self . query , self . subject + "<STR_LIT:U+0020>" + self . sDesc , <EOL> "<STR_LIT>" % self . pctid , <EOL> "<STR_LIT:0>" , "<STR_LIT:0>" , "<STR_LIT:0>" , <EOL> "<STR_LIT>" % self . qStart , "<STR_LIT>" % self . qStop , <EOL> "<STR_LIT>" % self . sStart , "<STR_LIT>" % self . sStop , <EOL> "<STR_LIT>" % self . evalue , "<STR_LIT>" % self . score ) ) <EOL> def gffline ( self , source = None , type = "<STR_LIT>" , primary_tag = "<STR_LIT>" , id = None ) : <EOL> source = self . method if not source else source <EOL> if type not in valid_gff_type : <EOL> score = "<STR_LIT>" . format ( self . pctid ) <EOL> target = "<STR_LIT:U+0020>" . join ( str ( x ) for x in [ self . subject , self . sStart , self . sStop ] ) <EOL> attributes = "<STR_LIT:;>" . join ( str ( x ) for x in [ primary_tag + "<STR_LIT:=>" + id , "<STR_LIT>" + target ] ) <EOL> else : <EOL> score = "<STR_LIT:.>" <EOL> note = "<STR_LIT>" . format ( self . sDesc ) if "<STR_LIT:U+0020>" in self . sDesc else self . sDesc <EOL> attributes = "<STR_LIT:;>" . join ( str ( x ) for x in [ primary_tag + "<STR_LIT:=>" + id , "<STR_LIT>" + self . subject , "<STR_LIT>" + note ] ) <EOL> line = "<STR_LIT:\t>" . join ( str ( x ) for x in [ self . query , source , type , self . qStart , self . qStop , score , self . qStrand , "<STR_LIT:.>" , attributes ] ) <EOL> return line <EOL> class Btab ( LineFile ) : <EOL> def __init__ ( self , filename , aat_dialect = False ) : <EOL> super ( Btab , self ) . __init__ ( filename ) <EOL> for line in must_open ( filename ) : <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> continue <EOL> self . append ( BtabLine ( line , aat_dialect = aat_dialect ) ) <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 blast ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( blast . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> btabfile , = args <EOL> btab = Btab ( btabfile ) <EOL> for b in btab : <EOL> print b . blastline <EOL> def bed ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . blast import BlastLine <EOL> p = OptionParser ( bed . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> btabfile , = args <EOL> btab = Btab ( btabfile ) <EOL> for b in btab : <EOL> Bline = BlastLine ( b . blastline ) <EOL> print Bline . bedline <EOL> def gff ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . utils . range import range_minmax <EOL> from jcvi . formats . gff import valid_gff_parent_child , valid_gff_type <EOL> p = OptionParser ( gff . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = None , help = "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> p . add_option ( "<STR_LIT>" , default = "<STR_LIT>" , choices = valid_gff_type , <EOL> help = "<STR_LIT>" ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> btabfile , = args <EOL> btabdict = { } <EOL> btab = Btab ( btabfile , aat_dialect = True ) <EOL> osource = opts . source or "<STR_LIT>" <EOL> otype = opts . type <EOL> octype = valid_gff_parent_child [ otype ] <EOL> for b in btab : <EOL> nargs = b . nargs <EOL> id = b . query + "<STR_LIT:->" + otype + "<STR_LIT>" . format ( b . chainNum ) <EOL> key = b . key <EOL> if key not in btabdict : <EOL> btabdict [ key ] = { '<STR_LIT:id>' : id , <EOL> '<STR_LIT>' : b . method , <EOL> '<STR_LIT>' : b . query , <EOL> '<STR_LIT>' : b . subject , <EOL> '<STR_LIT>' : b . qStrand , <EOL> '<STR_LIT>' : b . sDesc , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> btabdict [ key ] [ '<STR_LIT>' ] . append ( ( b . qStart , b . qStop ) ) <EOL> btabdict [ key ] [ '<STR_LIT>' ] . append ( b . gffline ( source = osource , type = octype , id = id ) ) <EOL> for v in btabdict . itervalues ( ) : <EOL> b = BtabLine ( "<STR_LIT:\t>" . join ( str ( x ) for x in [ <NUM_LIT:0> ] * nargs ) , aat_dialect = True ) <EOL> id = v [ '<STR_LIT:id>' ] <EOL> b . query = v [ '<STR_LIT>' ] <EOL> b . method = v [ '<STR_LIT>' ] <EOL> b . subject = v [ '<STR_LIT>' ] <EOL> b . qStrand = v [ '<STR_LIT>' ] <EOL> b . sDesc = v [ '<STR_LIT>' ] <EOL> b . qStart , b . qStop = range_minmax ( v [ '<STR_LIT>' ] ) <EOL> print b . gffline ( source = osource , type = otype , primary_tag = "<STR_LIT>" , id = id ) <EOL> print "<STR_LIT:\n>" . join ( v [ '<STR_LIT>' ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
