Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 41 lines (33 sloc) 1.426 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/bin/env python
# Inspired by seqLib2.py (Nick Furlotte <nfurlott@cs.ucla.edu>)

import random
import sys
from optparse import OptionParser

def write_FASTA_file(seq, filename, title):
    """Dumps a sequence 'seq' in FASTA file format to 'filename'"""
    f = open(filename, 'w')
    write_FASTA(seq, f, title)
    f.close()

def write_FASTA(seq, f, title, n=78):
    """Dumps a sequence 'seq' in FASTA file format to 'f'"""
    f.write("> %s\n" % title)
    f.writelines(["%s\n" % seq[i:i+n-1] for i in xrange(0,len(seq),n-1)])

def gen_seq(l, letters=['A', 'T', 'C', 'G']):
    """Generates a sequence of length 'l'"""
    return ''.join(random.choice(letters) for _ in xrange(l))

if __name__ == "__main__":
    parser = OptionParser(usage="%prog length [options]")
    parser.add_option("-f", "--file", dest="file",
                      help="write generated sequence to FILE", metavar="FILE")
    parser.add_option("-t", "--title", dest="title", help="title for sequence",
                      metavar="TITLE", default="")

    (options, args) = parser.parse_args(sys.argv[1:])
    if len(args) != 1:
        parser.error("incorrect number of args")
    if not args[0].isdigit():
        parser.error("length not an integer")

    if options.file == None:
        options.file = sys.stdout
    else:
        options.file = open(options.file, 'w')

    write_FASTA(gen_seq(int(args[0])), options.file, options.title)
Something went wrong with that request. Please try again.