dna sequence generator

A forum for general discussion of the Python programming language.

dna sequence generator

Postby dnic2693 » Tue Jan 21, 2014 8:53 pm

I'm just starting to learn python in a bioinformatics research lab. My first project was to generate a program that can spit out various DNA sequences with parameters of length and number of copies. The sequences would then need to be output in FASTA format.

For those unfamiliar a DNA sequence can be made up of four "letters": A,G,C,T. Example DNA sequence: ACGTTCCGTACGTACTCT

I am really new to this all and I would like some advice on how to go about this and how to learn python in general (rely on tutorials, do random projects, etc).

I am currently using someone else's program for my DNA sequence project and then I will go through line by line to see what's being done.

The first error I encountered when copying over the code was this:

Code: Select all
>>> import random
>>> import sys
>>> def simulate_sequence (length) :
   dna = ['A','G','C','T']
   sequence = ''
   for i in range (length) :
      sequence += random.choice (dna)
   return sequence

>>> setsize = int (sys.argv[1])
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    setsize = int (sys.argv[1])
IndexError: list index out of range
>>>



thank you.
Last edited by micseydel on Tue Jan 21, 2014 9:49 pm, edited 1 time in total.
Reason: First post lock.
dnic2693
 
Posts: 1
Joined: Tue Jan 21, 2014 8:48 pm

Re: dna sequence generator

Postby ochichinyezaboombwa » Tue Jan 21, 2014 9:55 pm

The traceback
Code: Select all
setsize = int (sys.argv[1])
IndexError: list index out of range
says: You (your code) are trying to access an element of the list which isn't there.In other words, you had to supply an argument to your program, and you didn't.

sys.argv is a list of program arguments, where [0]th element is the program itself, [1]...[N] are other arguments. For examplem if a command is
Code: Select all
wc.py -l agct.txt
, then sys.argv[1] is "-l" and sys.argv[2] is agct.txt.

Hope that helps; if it doesn't: you need to learn python first.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: dna sequence generator

Postby stranac » Wed Jan 22, 2014 10:40 am

Just in case it was not clear from ochichinyezaboombwa's post, you need to save the code in a .py file.
Then you run it from a terminal, and pass it arguments.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: dna sequence generator

Postby Kebap » Wed Jan 22, 2014 10:56 am

There are 2 different ways to use python: via interactive shell, or executing pre-written code files. You tried to use the first, but the sys.argv thing only makes sense in the second case, when you run your code files and pass additional arguments like so:

Code: Select all
 $ python mycode.py add additional arguments


You can just print sys.argv and see what is in there, by the way print is always a great way to debug in interactive mode, too.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 394
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests