<s> import os <EOL> import random <EOL> import sys <EOL> from subprocess import Popen , PIPE , STDOUT <EOL> def message ( msg , msgtype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" % dict ( level = msgtype . upper ( ) , text = msg ) <EOL> sys . stderr . write ( message . strip ( ) + "<STR_LIT:\n>" ) <EOL> if msgtype == '<STR_LIT:error>' : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def random_ports ( port , n ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( min ( <NUM_LIT:5> , n ) ) : <EOL> yield port + i <EOL> for i in range ( n - <NUM_LIT:5> ) : <EOL> yield max ( <NUM_LIT:1> , port + random . randint ( - <NUM_LIT:2> * n , <NUM_LIT:2> * n ) ) <EOL> def ParseTargets ( targetfile ) : <EOL> """<STR_LIT>""" <EOL> x = [ ] <EOL> with open ( targetfile , "<STR_LIT:r>" ) as f : <EOL> for line in f : <EOL> items = line . strip ( ) . split ( "<STR_LIT:\t>" ) <EOL> if len ( items ) != <NUM_LIT:4> : <EOL> message ( "<STR_LIT>" , "<STR_LIT:error>" ) <EOL> chrom , start , end , name = items <EOL> region = "<STR_LIT>" % ( chrom , start ) <EOL> x . append ( { "<STR_LIT:name>" : name , "<STR_LIT>" : region , "<STR_LIT>" : ( chrom , int ( start ) , int ( end ) ) } ) <EOL> line = f . readline ( ) <EOL> with open ( targetfile , "<STR_LIT:r>" ) as f : <EOL> line = f . readline ( ) <EOL> return x <EOL> def WriteParamFile ( paramfile , jspath , filetype , reference_track , samples , alignments_by_sample , fromindex , toindex ) : <EOL> """<STR_LIT>""" <EOL> f = open ( paramfile , "<STR_LIT:w>" ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' % reference_track ) <EOL> f . write ( '<STR_LIT>' % str ( samples ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> for sample in alignments_by_sample : <EOL> f . write ( '<STR_LIT>' % ( sample , alignments_by_sample [ sample ] ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' % fromindex ) <EOL> f . write ( '<STR_LIT>' % toindex ) <EOL> f . write ( '<STR_LIT>' % jspath ) <EOL> if filetype in [ "<STR_LIT:html>" , "<STR_LIT>" ] : <EOL> f . write ( '<STR_LIT>' % filetype ) <EOL> elif filetype == "<STR_LIT>" : <EOL> f . write ( '<STR_LIT>' ) <EOL> else : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> def RunCommand ( cmd ) : <EOL> p = Popen ( cmd , shell = True , stdin = PIPE , stdout = PIPE , stderr = STDOUT , close_fds = True ) <EOL> ex = p . wait ( ) <EOL> if ex != <NUM_LIT:0> : <EOL> stdout , stderr = "<STR_LIT>" , "<STR_LIT>" <EOL> if p . stdout is not None : stdout = p . stdout . read ( ) <EOL> if p . stderr is not None : stderr = p . stderr . read ( ) <EOL> message ( "<STR_LIT>" % ( cmd , stdout , stderr ) ) <EOL> return ex <EOL> def CheckProgram ( program ) : <EOL> """<STR_LIT>""" <EOL> def is_exe ( fpath ) : <EOL> return os . path . isfile ( fpath ) and os . access ( fpath , os . X_OK ) <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( '<STR_LIT:">' ) <EOL> exe_file = os . path . join ( path , program ) <EOL> if is_exe ( exe_file ) : return True <EOL> return False <EOL> def CheckNodeJSPackage ( package ) : <EOL> """<STR_LIT>""" <EOL> cmd = "<STR_LIT>" % package <EOL> x = RunCommand ( cmd ) <EOL> return x == <NUM_LIT:0> </s>
