Classes

From 22113
Revision as of 15:01, 6 March 2024 by WikiSysop (talk | contribs) (Created page with "__NOTOC__ {| width=500 style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;" |Previous: Comprehension, Generators, Functions and Methods |Next: Unit test |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22113/22113_07-Classes.ppt Classes]<br> Resource: Example code - Classes<br> == Subjects covered == Classes<br> Object Oriented Programming == Exercises to be handed in == The ex...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Previous: Comprehension, Generators, Functions and Methods Next: Unit test

Required course material for the lesson

Powerpoint: Classes
Resource: Example code - Classes

Subjects covered

Classes
Object Oriented Programming

Exercises to be handed in

The exercises will be about the making of a class. A the exercises goes on the class can do more and more. Remember to add code outside the class, which test the new methods you make as you make them. The class methods need some decent input control and you need to think about what is required. Notice how versatile the class becomes, as you progress through the exercises.

  1. Create a class called Fasta. The purpose of the class is to do various kinds of manipulation of fasta files and sequences. Start with adding 3 class methods:
    load(filename), which gets a file name/path and loads the fasta file into internal instance lists with header and sequence.
    save(filename), which writes the internal instance header/sequence lists into a fasta file.
    content(), which returns two lists; the headers and the sequences.
    You can likely reuse part of your functions in exercise 3 and 4 from Making Functions.
    Example use of the Fasta class:
    myfasta = Fasta()
    myfasta.load("dna7.fsa")
    print(myfasta.content())
    myfasta.save("newfile.fsa")
  2. Add a method delete([start[,end]]), which deletes entries in the headers and sequences. If called with no arguments like delete() it deletes all headers/sequences. If called with one argument like delete(start) it deletes the header and sequence at position start. If called with two arguments, it deletes headers and sequences from position start up to but not including position end. start and end can also be negative numbers, and in such case we count from the end of the lists, just like it works in normal list manipulations.
  3. Modify the content() method, so it works similar to delete(). If called with no arguments like content() it returns 2 lists with all headers and sequences. If called with one argument like content(start) it returns the header and sequence at position start. If called with two arguments, it returns the headers and sequences from position start up to but not including position end as two lists. Be careful that you return a copy of the lists and not the lists themselves as then the headers and sequences in the instance can be modified unintended outside the instance.
  4. Add a method insert(header,sequence[,position]), which adds header and sequence to the instance lists. header and sequence can either be simple strings (single header and sequence) or lists of headers and sequences. If position is not given, then the addition takes place at the end of the existing headers/sequences. If position is given then insertion takes places at that position.
  5. Add a method verify(alphabet,[start[,end]]), which verifies sequence entries according to an alphabet. It works in a way similar to delete() and content() for the range of what should be verified. The method returns True if all entries in the range are verified, False otherwise.
    The alphabet could be a DNA alphabet (ATCG), a protein alphabet or something derived from those. You should put your alphabets into class variables, as they are common for all instances, and not subject to change.
  6. Add a method discard(alphabet,[start[,end]]), which discards sequence entries and corresponding headers according to an alphabet. It works in a way similar to delete() and content() for the range of what should be verified.
  7. Add iteration and length evaluation to the Fasta class using magic methods. Do this so you can write code like
    if len(MyFastaInstance) > 0:
        for header, sequence in MyFastaInstance:
            # Do something with header and sequence
            print(header, sequence)
  8. Add the methods deletethis(), insertthis(header, sequence), verifythis(alphabet) and discardthis(alphabet) to the Fasta class. The tricky part is that these methods should only work when iterating over an instance at the current item, like this:
    for header, sequence in MyFastaInstance:
        if not MyFastaInstance.verifythis("DNA"):
            MyFastaInstance.deletethis()
    As some may remember, it is normally impossible to successfully iterate straightforward through a list and delete and/or add elements to the list during the iteration. You have to make this possible, maybe by changing the way your iteration works in the previous exercise.

Exercises for extra practice