import string,os,sys,getopt def usage(): print """ dnda_2d.py [OPTIONS] OPTIONS can be -h,--help This message -f,--file The file containing the base sequence -s,--stdin Read the sequence from stdin -n,--nograph Do'nt generate a graphical plot """ def make2drep(seq): currx = 0 curry = 0 points = [] for base in seq: if base in [' ','\n']: continue if base in ['a','A']: currx += 1 if base in ['t','T']: currx = currx - 1 if base in ['c','C']: curry += 1 if base in ['g','G']: curry =curry - 1 points.append( (currx,curry) ) return(points) def main(): seqfile = '' stdin = 0 graph = 1 if len(sys.argv) == 1: usage() sys.exit(0) try: opt,args = getopt.getopt(sys.argv[1:], 'hf:ns',\ ['file=','nograph','help','stdin']) except getopt.GetoptError: usage() sys.exit(0) for o,a in opt: if o in ('-h','--help'): usage() sys.exit(0) if o in ('-f','--file'): seqfile = a if o in ('-n','--nograph'): graph = 0 if o in ('-s','--stdin'): stdin = 1 # Read in the sequence seq = '' if seqfile: try: f = open(seqfile,'r') for line in f: seq += line f.close() except IOError: print """ Could'nt open %s """ % (seqfile) sys.exit(0) else: seq = sys.stdin.readline() # Make the 2D rep points = make2drep(seq) f = open('rep.dat','w') for x,y in points: f.write('%d %d\n' % (x,y)) f.close() if graph: # show a plot import StringIO,time s = StringIO.StringIO() s.write('set size square\n') s.write('set term png\n') s.write('set out \'rep.png\'\n') s.write('plot \'rep.dat\' title \'%s\' with lines\n' % (seqfile)) s.write('set term x11\n') s.write('plot \'rep.dat\' title \'%s\' with lines\n' % (seqfile)) o,i,e = os.popen3('gnuplot -persist') o.write(s.getvalue()) o.close() time.sleep(1) if __name__ == '__main__': main()