<s> """<STR_LIT>""" <EOL> import sys <EOL> import logging <EOL> from jcvi . formats . base import BaseFile , read_block <EOL> from jcvi . apps . base import OptionParser , ActionDispatcher <EOL> class ReadLine ( object ) : <EOL> def __init__ ( self , row , contig ) : <EOL> assert row [ <NUM_LIT:0> ] == '<STR_LIT:#>' <EOL> self . id = row . strip ( "<STR_LIT:#>" ) . split ( '<STR_LIT:(>' ) [ <NUM_LIT:0> ] <EOL> coords = row . split ( "<STR_LIT:<>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:>>" ) [ <NUM_LIT:0> ] <EOL> start , end = coords . split ( ) <EOL> self . contig = contig <EOL> self . start = int ( start ) <EOL> self . end = int ( end ) <EOL> if self . start > self . end : <EOL> self . start , self . end = self . end , self . start <EOL> self . orientation = "<STR_LIT:->" if "<STR_LIT>" in row else "<STR_LIT:+>" <EOL> def __str__ ( self ) : <EOL> return self . id <EOL> @ property <EOL> def bedline ( self ) : <EOL> return "<STR_LIT:\t>" . join ( str ( x ) for x in ( self . contig , self . start - <NUM_LIT:1> , self . end , <EOL> self . id , "<STR_LIT:0>" , self . orientation ) ) <EOL> __repr__ = __str__ <EOL> class ContigLine ( object ) : <EOL> def __init__ ( self , row ) : <EOL> assert row [ : <NUM_LIT:2> ] == '<STR_LIT>' <EOL> self . id = row . strip ( "<STR_LIT:#>" ) . split ( ) [ <NUM_LIT:0> ] <EOL> self . reads = [ ] <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT::>" . join ( ( self . id , str ( self . reads ) ) ) <EOL> __repr__ = __str__ <EOL> class ContigFile ( BaseFile ) : <EOL> def __init__ ( self , filename ) : <EOL> super ( ContigFile , self ) . __init__ ( filename ) <EOL> self . fp = open ( filename ) <EOL> def iter_records ( self ) : <EOL> c = None <EOL> for a , b in read_block ( self . fp , "<STR_LIT:#>" ) : <EOL> if a [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> if c : <EOL> yield c <EOL> c = ContigLine ( a ) <EOL> else : <EOL> c . reads . append ( ReadLine ( a , c . id ) ) <EOL> if c : <EOL> yield c <EOL> def main ( ) : <EOL> actions = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> p = ActionDispatcher ( actions ) <EOL> p . dispatch ( globals ( ) ) <EOL> def frombed ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . fasta import Fasta <EOL> from jcvi . formats . bed import Bed <EOL> from jcvi . utils . cbook import fill <EOL> p = OptionParser ( frombed . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:3> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> bedfile , contigfasta , readfasta = args <EOL> prefix = bedfile . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> contigfile = prefix + "<STR_LIT>" <EOL> idsfile = prefix + "<STR_LIT>" <EOL> contigfasta = Fasta ( contigfasta ) <EOL> readfasta = Fasta ( readfasta ) <EOL> bed = Bed ( bedfile ) <EOL> checksum = "<STR_LIT>" <EOL> fw_ids = open ( idsfile , "<STR_LIT:w>" ) <EOL> fw = open ( contigfile , "<STR_LIT:w>" ) <EOL> for ctg , reads in bed . sub_beds ( ) : <EOL> ctgseq = contigfasta [ ctg ] <EOL> ctgline = "<STR_LIT>" . format ( ctg , len ( reads ) , len ( ctgseq ) , checksum ) <EOL> print >> fw_ids , ctg <EOL> print >> fw , ctgline <EOL> print >> fw , fill ( ctgseq . seq ) <EOL> for b in reads : <EOL> read = b . accn <EOL> strand = b . strand <EOL> readseq = readfasta [ read ] <EOL> rc = "<STR_LIT>" if strand == "<STR_LIT:->" else "<STR_LIT>" <EOL> readlen = len ( readseq ) <EOL> rstart , rend = <NUM_LIT:1> , readlen <EOL> if strand == "<STR_LIT:->" : <EOL> rstart , rend = rend , rstart <EOL> readrange = "<STR_LIT>" . format ( rstart , rend ) <EOL> conrange = "<STR_LIT>" . format ( b . start , b . end ) <EOL> readline = "<STR_LIT>" . format ( read , rc , readlen , checksum , readrange , conrange ) <EOL> print >> fw , readline <EOL> print >> fw , fill ( readseq . seq ) <EOL> logging . debug ( "<STR_LIT>" . format ( contigfile ) ) <EOL> logging . debug ( "<STR_LIT>" . format ( idsfile ) ) <EOL> def bed ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( main . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> contigfile , = args <EOL> bedfile = contigfile . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + "<STR_LIT>" <EOL> fw = open ( bedfile , "<STR_LIT:w>" ) <EOL> c = ContigFile ( contigfile ) <EOL> for rec in c . iter_records ( ) : <EOL> for r in rec . reads : <EOL> print >> fw , r . bedline <EOL> logging . debug ( "<STR_LIT>" . format ( bedfile ) ) <EOL> return bedfile <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
