Exceptions and bug handling
Jump to navigation
Jump to search
Previous: String manipulation | Next: Stateful parsing |
Required course material for the lesson
Powerpoint: Exceptions and bug handling
Resource: Example code - Exceptions
Subjects covered
How to structure your code in distinct parts.
Finding bugs in your program, using exceptions.
Exercises to be handed in
- In the file dna.fsa is some DNA. Construct a program that finds possible translation starts :-)
All proteins start with the amino acid methionine (at least when translating, Met might be removed in later processing states). Methionine is coded with ATG. The exercise is therefore; find the position of all ATG's in the sequence. The first position is 83 as humans count.
In some organisms different start codons are possible. If you really want to, you can make the program handle those cases too. - Connecting to the previous exercise; Assuming that the first Met at position 83 is translation start, find the corresponding translation stop (which is the first one in frame). Stop codon is coded by TAA, TAG, or TGA. Remember that the stop codon has to be in the same reading frame as ATG. Notice: There are two ways to solving this exercise. The primitive way is to start at the position given. The more general and better way is to find the first ATG and then find the corresponding stop codon, which is on position 128.
- Read the file orphans.sp and find all accession numbers (and only the accession numbers), save them in another file of your choosing. Hint: an accession number might look like this AB000114.CDS.1 or like this AB000114 or like this AB000114.CDS.3. CDS means CoDing Sequence followed by a number. If the accession number contains the CDS part, consider .CDS.1 as a part of the accession number. Accession numbers differ in length for historical reasons. You can assume that the accession number comes straight after the >, which is first on the line.
- Make a program that asks for an organism, like 'HUMAN' or 'RAT'. The program should then count the number of lines/times a SwissProt identifier in the file orphans.sp is present with said organism, ie. PARG_HUMAN and LUM_HUMAN are the two first for HUMAN. Hint: you should probably think twice in this exercise. The position is significant. Check what happens if you search for RAT.
- Count the ATCG's in the sequence in dna.fsa. Display the percentage of ATCG's with 2 digits after the dot. Make it look really structured. Just to be exceedingly clear; the percentage of A's is 100*(NumberOfA/LengthOfSequence).
- The file studentweight.txt contains both text lines and numbers. Make a program that can ask for a file name and sum the numbers and ignore the text lines using exceptions. Result is 1035.
Exercises for extra practice
- In the file mixedlines.txt how many 4 letter words exists? A word is here defined as consecutive visible characters, i.e. "12", "ipsum." are both words.
- Write a program that asks for an integer, and then determines if the number was a prime number or not.
- Write a program that prints a "bulls-eye" to the screen using characters like; '.', '+', '*', '#', etc.
You have to import the math library, in order to access the squareroot (sqrt), sine (sin), cosine (cos), and so forth functions. The square root of x is gotten like math.sqrt(x). I only used: for, if, elif, print, +, -, /, **, int, sqrt for this 40x40 example.
Hint: Think of a grid where one point is the center of the eye/circle. The positions in the grid will use different chars, depending how far away they are from the center. You might get an oval shape - think about why.