<s> """<STR_LIT>""" <EOL> import os . path as op <EOL> import sys <EOL> import string <EOL> import logging <EOL> from collections import defaultdict <EOL> from Bio import SeqIO <EOL> from jcvi . utils . cbook import memoized <EOL> from jcvi . utils . orderedcollections import parse_qs <EOL> from jcvi . formats . base import DictFile <EOL> from jcvi . apps . base import OptionParser , ActionDispatcher , sh , mkdir , glob <EOL> """<STR_LIT>""" <EOL> Title = """<STR_LIT>""" <EOL> Authors = """<STR_LIT>""" <EOL> Libname = "<STR_LIT>" <EOL> Contact = "<STR_LIT>" <EOL> PublicationTemplate = """<STR_LIT>""" <EOL> LibraryTemplate = """<STR_LIT>""" <EOL> ContactTemplate = """<STR_LIT>""" <EOL> Directions = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> Primers = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> GSSTemplate = """<STR_LIT>""" <EOL> Nrows , Ncols = <NUM_LIT:16> , <NUM_LIT> <EOL> vars = globals ( ) <EOL> def main ( ) : <EOL> actions = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <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 fcs ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( fcs . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = <NUM_LIT:200> , <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> fcsfile , = args <EOL> cutoff = opts . cutoff <EOL> fp = open ( fcsfile ) <EOL> for row in fp : <EOL> if row [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> continue <EOL> sep = "<STR_LIT:\t>" if "<STR_LIT:\t>" in row else None <EOL> atoms = row . rstrip ( ) . split ( sep , <NUM_LIT:3> ) <EOL> contig , length = atoms [ : <NUM_LIT:2> ] <EOL> length = int ( length ) <EOL> label = atoms [ - <NUM_LIT:1> ] <EOL> label = label . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> if len ( atoms ) == <NUM_LIT:3> : <EOL> ranges = "<STR_LIT>" . format ( <NUM_LIT:1> , length ) <EOL> else : <EOL> assert len ( atoms ) == <NUM_LIT:4> <EOL> ranges = atoms [ <NUM_LIT:2> ] <EOL> for ab in ranges . split ( "<STR_LIT:U+002C>" ) : <EOL> a , b = ab . split ( "<STR_LIT:..>" ) <EOL> a , b = int ( a ) , int ( b ) <EOL> assert a <= b <EOL> ahang = a - <NUM_LIT:1> <EOL> bhang = length - b <EOL> if ahang < cutoff : <EOL> a = <NUM_LIT:1> <EOL> if bhang < cutoff : <EOL> b = length <EOL> print "<STR_LIT:\t>" . join ( str ( x ) for x in ( contig , a - <NUM_LIT:1> , b , label ) ) <EOL> def asn ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . base import must_open <EOL> p = OptionParser ( asn . __doc__ ) <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> fw = must_open ( opts . outfile , "<STR_LIT:w>" ) <EOL> for asnfile in args : <EOL> fp = open ( asnfile ) <EOL> ingeneralblock = False <EOL> ingenbankblock = False <EOL> gb , name = None , None <EOL> for row in fp : <EOL> if row . strip ( ) == "<STR_LIT>" : <EOL> continue <EOL> tag = row . split ( ) [ <NUM_LIT:0> ] <EOL> if tag == "<STR_LIT>" : <EOL> ingeneralblock = True <EOL> if ingeneralblock and tag == "<STR_LIT:str>" : <EOL> if name is None : <EOL> name = row . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] <EOL> ingeneralblock = False <EOL> if tag == "<STR_LIT>" : <EOL> ingenbankblock = True <EOL> if ingenbankblock and tag == "<STR_LIT>" : <EOL> if gb is None : <EOL> gb = row . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] <EOL> ingenbankblock = False <EOL> assert gb and name <EOL> print >> fw , "<STR_LIT>" . format ( gb , name ) <EOL> def verify_sqn ( sqndir , accession ) : <EOL> valfile = "<STR_LIT>" . format ( sqndir , accession ) <EOL> contents = open ( valfile ) . read ( ) . strip ( ) <EOL> assert not contents , "<STR_LIT>" . format ( contents ) <EOL> cmd = "<STR_LIT>" . format ( sqndir , accession ) <EOL> outfile = "<STR_LIT>" . format ( sqndir , accession ) <EOL> sh ( cmd , outfile = outfile ) <EOL> def htgnew ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . fasta import sequin <EOL> p = OptionParser ( htgnew . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:3> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> fastafile , phasefile , sbtfile = args <EOL> comment = opts . comment <EOL> fastadir = "<STR_LIT>" <EOL> sqndir = "<STR_LIT>" <EOL> mkdir ( fastadir ) <EOL> mkdir ( sqndir ) <EOL> cmd = "<STR_LIT>" . format ( fastafile , fastadir ) <EOL> sh ( cmd , outfile = "<STR_LIT>" , errfile = "<STR_LIT>" ) <EOL> acmd = '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> nupdated = <NUM_LIT:0> <EOL> for row in open ( phasefile ) : <EOL> name , phase = row . split ( ) [ : <NUM_LIT:2> ] <EOL> fafile = op . join ( fastadir , name + "<STR_LIT>" ) <EOL> cloneopt = "<STR_LIT>" . format ( name ) <EOL> splitfile , gaps = sequin ( [ fafile , cloneopt ] ) <EOL> splitfile = op . basename ( splitfile ) <EOL> accession = accession_nv = name <EOL> phase = int ( phase ) <EOL> assert phase in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> cmd = acmd . format ( accession_nv = accession_nv , sqndir = sqndir , <EOL> sbtfile = sbtfile , splitfile = splitfile , phase = phase , <EOL> comment = comment ) <EOL> sh ( cmd ) <EOL> verify_sqn ( sqndir , accession ) <EOL> nupdated += <NUM_LIT:1> <EOL> print >> sys . stderr , "<STR_LIT>" . format ( nupdated ) <EOL> def htg ( args ) : <EOL> """<STR_LIT>""" <EOL> from jcvi . formats . fasta import sequin , ids <EOL> from jcvi . formats . agp import phase <EOL> from jcvi . apps . fetch import entrez <EOL> p = OptionParser ( htg . __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> p . add_option ( "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> fastafile , sbtfile = args <EOL> pf = fastafile . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> idsfile = pf + "<STR_LIT>" <EOL> phasefile = pf + "<STR_LIT>" <EOL> namesfile = pf + "<STR_LIT>" <EOL> ids ( [ fastafile , "<STR_LIT>" . format ( idsfile ) ] ) <EOL> asndir = "<STR_LIT>" <EOL> mkdir ( asndir ) <EOL> entrez ( [ idsfile , "<STR_LIT>" , "<STR_LIT>" . format ( asndir ) ] ) <EOL> asn ( glob ( "<STR_LIT>" . format ( asndir ) ) + [ "<STR_LIT>" . format ( namesfile ) ] ) <EOL> if opts . phases is None : <EOL> gbdir = "<STR_LIT>" <EOL> mkdir ( gbdir ) <EOL> entrez ( [ idsfile , "<STR_LIT>" , "<STR_LIT>" . format ( gbdir ) ] ) <EOL> phase ( glob ( "<STR_LIT>" . format ( gbdir ) ) + [ "<STR_LIT>" . format ( phasefile ) ] ) <EOL> else : <EOL> phasefile = opts . phases <EOL> assert op . exists ( namesfile ) and op . exists ( phasefile ) <EOL> newphasefile = phasefile + "<STR_LIT>" <EOL> newphasefw = open ( newphasefile , "<STR_LIT:w>" ) <EOL> comment = opts . comment <EOL> fastadir = "<STR_LIT>" <EOL> sqndir = "<STR_LIT>" <EOL> mkdir ( fastadir ) <EOL> mkdir ( sqndir ) <EOL> from jcvi . graphics . histogram import stem_leaf_plot <EOL> names = DictFile ( namesfile ) <EOL> assert len ( set ( names . keys ( ) ) ) == len ( set ( names . values ( ) ) ) <EOL> phases = DictFile ( phasefile ) <EOL> ph = [ int ( x ) for x in phases . values ( ) ] <EOL> stem_leaf_plot ( ph , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , title = "<STR_LIT>" ) <EOL> logging . debug ( "<STR_LIT>" . format ( len ( phases ) ) ) <EOL> assert len ( names ) == len ( phases ) <EOL> newph = [ ] <EOL> cmd = "<STR_LIT>" . format ( fastafile , fastadir ) <EOL> sh ( cmd , outfile = "<STR_LIT>" , errfile = "<STR_LIT>" ) <EOL> acmd = '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> acmd += '<STR_LIT>' <EOL> qq = "<STR_LIT>" <EOL> nupdated = <NUM_LIT:0> <EOL> for row in open ( phasefile ) : <EOL> atoms = row . rstrip ( ) . split ( "<STR_LIT:\t>" ) <EOL> accession , phase , clone = atoms [ <NUM_LIT:0> ] , atoms [ <NUM_LIT:1> ] , atoms [ - <NUM_LIT:1> ] <EOL> fafile = op . join ( fastadir , accession + "<STR_LIT>" ) <EOL> accession_nv = accession . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> newid = names [ accession_nv ] <EOL> newidopt = "<STR_LIT>" . format ( newid ) <EOL> cloneopt = "<STR_LIT>" . format ( clone ) <EOL> splitfile , gaps = sequin ( [ fafile , newidopt , cloneopt ] ) <EOL> splitfile = op . basename ( splitfile ) <EOL> phase = int ( phase ) <EOL> assert phase in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> oldphase = phase <EOL> if gaps == <NUM_LIT:0> and phase != <NUM_LIT:3> : <EOL> phase = <NUM_LIT:3> <EOL> if gaps != <NUM_LIT:0> and phase == <NUM_LIT:3> : <EOL> phase = <NUM_LIT:2> <EOL> print >> newphasefw , "<STR_LIT>" . format ( accession_nv , oldphase , phase ) <EOL> newph . append ( phase ) <EOL> qualifiers = qq . format ( phase = phase ) <EOL> if "<STR_LIT:;>" in clone : <EOL> qualifiers += "<STR_LIT>" <EOL> cmd = acmd . format ( accession = accession , accession_nv = accession_nv , <EOL> sqndir = sqndir , sbtfile = sbtfile , splitfile = splitfile , <EOL> qualifiers = qualifiers , comment = comment ) <EOL> sh ( cmd ) <EOL> verify_sqn ( sqndir , accession ) <EOL> nupdated += <NUM_LIT:1> <EOL> stem_leaf_plot ( newph , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> , title = "<STR_LIT>" ) <EOL> print >> sys . stderr , "<STR_LIT>" . format ( nupdated ) <EOL> @ memoized <EOL> def get_rows_cols ( nrows = Nrows , ncols = Ncols ) : <EOL> rows , cols = string . ascii_uppercase [ : nrows ] , range ( <NUM_LIT:1> , ncols + <NUM_LIT:1> ) <EOL> return rows , cols <EOL> @ memoized <EOL> def get_plate ( nrows = Nrows , ncols = Ncols ) : <EOL> rows , cols = get_rows_cols ( nrows , ncols ) <EOL> plate = [ [ "<STR_LIT>" ] * ncols for x in xrange ( nrows ) ] <EOL> n = <NUM_LIT:0> <EOL> for i in xrange ( <NUM_LIT:0> , nrows , <NUM_LIT:2> ) : <EOL> for j in xrange ( <NUM_LIT:0> , ncols , <NUM_LIT:2> ) : <EOL> n += <NUM_LIT:1> <EOL> prefix = "<STR_LIT>" . format ( n ) <EOL> plate [ i ] [ j ] = prefix + '<STR_LIT:A>' <EOL> plate [ i ] [ j + <NUM_LIT:1> ] = prefix + '<STR_LIT:B>' <EOL> plate [ i + <NUM_LIT:1> ] [ j ] = prefix + '<STR_LIT:C>' <EOL> plate [ i + <NUM_LIT:1> ] [ j + <NUM_LIT:1> ] = prefix + '<STR_LIT:D>' <EOL> splate = { } <EOL> for i in xrange ( nrows ) : <EOL> for j in xrange ( ncols ) : <EOL> c = plate [ i ] [ j ] <EOL> splate [ c ] = "<STR_LIT>" . format ( rows [ i ] , j + <NUM_LIT:1> ) <EOL> return plate , splate <EOL> def convert_96_to_384 ( c96 , quad , nrows = Nrows , ncols = Ncols ) : <EOL> """<STR_LIT>""" <EOL> rows , cols = get_rows_cols ( ) <EOL> plate , splate = get_plate ( ) <EOL> n96 = rows . index ( c96 [ <NUM_LIT:0> ] ) * ncols / <NUM_LIT:2> + int ( c96 [ <NUM_LIT:1> : ] ) <EOL> q = "<STR_LIT>" . format ( n96 , "<STR_LIT>" [ quad - <NUM_LIT:1> ] ) <EOL> return splate [ q ] <EOL> def t384 ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( t384 . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> plate , splate = get_plate ( ) <EOL> fw = sys . stdout <EOL> for i in plate : <EOL> for j , p in enumerate ( i ) : <EOL> if j != <NUM_LIT:0> : <EOL> fw . write ( '<STR_LIT:|>' ) <EOL> fw . write ( p ) <EOL> fw . write ( '<STR_LIT:\n>' ) <EOL> def parse_description ( s ) : <EOL> """<STR_LIT>""" <EOL> s = "<STR_LIT>" . join ( s . split ( ) [ <NUM_LIT:1> : ] ) . replace ( "<STR_LIT:/>" , "<STR_LIT:;>" ) <EOL> a = parse_qs ( s ) <EOL> return a <EOL> def gss ( args ) : <EOL> """<STR_LIT>""" <EOL> p = OptionParser ( gss . __doc__ ) <EOL> opts , args = p . parse_args ( args ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> sys . exit ( p . print_help ( ) ) <EOL> fastafile , mappingfile = args <EOL> seen = defaultdict ( int ) <EOL> clone = defaultdict ( set ) <EOL> plateMapping = DictFile ( mappingfile ) <EOL> fw = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> print >> fw , PublicationTemplate . format ( ** vars ) <EOL> print >> fw , LibraryTemplate . format ( ** vars ) <EOL> print >> fw , ContactTemplate . format ( ** vars ) <EOL> logging . debug ( "<STR_LIT>" . format ( fw . name ) ) <EOL> fw = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> fw_log = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> for rec in SeqIO . parse ( fastafile , "<STR_LIT>" ) : <EOL> description = rec . description <EOL> a = parse_description ( description ) <EOL> direction = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_barcode = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_well_coordinates = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_96well_quadrant = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_96well_coordinates = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> w96 = sequencer_plate_96well_coordinates <EOL> w96quad = int ( sequencer_plate_96well_quadrant ) <EOL> w384 = sequencer_plate_well_coordinates <EOL> assert convert_96_to_384 ( w96 , w96quad ) == w384 <EOL> plate = sequencer_plate_barcode <EOL> assert plate in plateMapping , "<STR_LIT>" . format ( plate , mappingfile ) <EOL> plate = plateMapping [ plate ] <EOL> d = Directions [ direction ] <EOL> cloneID = "<STR_LIT>" . format ( plate , w384 ) <EOL> gssID = "<STR_LIT>" . format ( cloneID , d ) <EOL> seen [ gssID ] += <NUM_LIT:1> <EOL> if seen [ gssID ] > <NUM_LIT:1> : <EOL> gssID = "<STR_LIT>" . format ( gssID , seen [ gssID ] ) <EOL> seen [ gssID ] += <NUM_LIT:1> <EOL> clone [ cloneID ] . add ( gssID ) <EOL> seen = defaultdict ( int ) <EOL> for rec in SeqIO . parse ( fastafile , "<STR_LIT>" ) : <EOL> description = rec . description <EOL> a = parse_description ( description ) <EOL> direction = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_barcode = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> sequencer_plate_well_coordinates = a [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> w384 = sequencer_plate_well_coordinates <EOL> plate = sequencer_plate_barcode <EOL> plate = plateMapping [ plate ] <EOL> d = Directions [ direction ] <EOL> cloneID = "<STR_LIT>" . format ( plate , w384 ) <EOL> gssID = "<STR_LIT>" . format ( cloneID , d ) <EOL> seen [ gssID ] += <NUM_LIT:1> <EOL> if seen [ gssID ] > <NUM_LIT:1> : <EOL> logging . error ( "<STR_LIT>" . format ( gssID ) ) <EOL> gssID = "<STR_LIT>" . format ( gssID , seen [ gssID ] ) <EOL> othergss = clone [ cloneID ] - set ( [ gssID ] ) <EOL> othergss = "<STR_LIT:U+002CU+0020>" . join ( sorted ( othergss ) ) <EOL> vars . update ( locals ( ) ) <EOL> print >> fw , GSSTemplate . format ( ** vars ) <EOL> print >> fw_log , "<STR_LIT>" . format ( gssID , description ) <EOL> print >> fw_log , "<STR_LIT:=>" * <NUM_LIT> <EOL> logging . debug ( "<STR_LIT>" . format ( len ( seen ) , fw . name ) ) <EOL> fw . close ( ) <EOL> fw_log . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> main ( ) </s>
