User:ShawnDouglas/scripts/random-sequence.py

From OpenWetWare

Jump to: navigation, search

Generate random DNA sequence, length specified in argv[1] --smd 14:11, 4 November 2005 (EST)

#!/usr/bin/python

import random
import sys

# prints random sequence, length specified as argument

def randseq(l):
  s = []
  for i in range(l):
    s.append(random.choice(['a', 'c', 'g', 't']))
  return ''.join(s)

if len(sys.argv) > 1:
  a = int(sys.argv[1])
else:
  sys.exit("usage: ./random-sequence.py [length]")

print randseq(a).upper()


Include reverse complement --smd 15:58, 26 June 2006 (EDT)

#!/usr/bin/python

import random
import sys
import string

# prints random sequence, length specified as argument

def randseq(l):
  s = []
  for i in range(l):
    s.append(random.choice(['a', 'c', 'g', 't']))
  return ''.join(s)

def rev(s):
  return s[::-1]
complement = string.maketrans('ACGTacgt','TGCAtgca')
def comp(s):
  return rev(s.translate(complement))

if len(sys.argv) > 1:
  a = int(sys.argv[1])
else:
  sys.exit("usage: ./random-sequence.py [length]")

foo = randseq(a).upper()

print foo
print comp(foo)
Personal tools