Please enable JavaScript.
Coggle requires JavaScript to display documents.
Bioinformatics programming using Python Mitchel Model Part 2.! (Classes…
Bioinformatics programming using Python
Mitchel Model Part 2.!
Collections (continuing)
streams
file methods
fileobj.write(string)
writes string to fileobj
example
'>id1>id2>id3'.split('>')
['', 'id1', 'id2', 'id3']
to read FASTA file into a string and split it
to do this through out statement
def read_FASTA_strings(filename):
with open(filename) as file:
return file.read().split('>')[1:]
fileobj.read([count])
reads count bytes to the end of file
Generators
is an object that rerutns values from a series it computes
example:
random.randint
statement
yield
next(generator[, default])
gets the next value from the generator object if the generator has no more values to produce, returns default, raising an error if no default value was specified
Collection-Related Expression Feature
Comperhensions
it creates a set, list, or dict from the results of evaluating an expression for each element of another collection
List comperhension
statements not allowe, only must by expressions
[expression for item in collection]
this list(generated by comperhension) contains only boolean values
checking values whether they ar true occurs through out build-in funcition
all
Set and dictionary comprehensions
{expression for item in collection}
{key-expression: value-expression for key, value in collection}
Conditional comperehensions
[expression for element in collection if test]
filterring
Filtering out the underscore names from dir
def dr(name):
"""Return the result of dir(name), omitting any names beginning with an underscore"""
return [nm for nm in dir(name) if nm[0] != '_']
can be included into the other comperehensions
Nested comperhensions
more than one for in it
it's rare to see more than two sections in a comperehension
A nested comprehension for generating codons:
def generate_triples(chars='TCAG'):
"""Return a list of all three-character combinations of unique characters in chars"""
chars = set(chars)
Generator expressions
syntatically like a list or set comperhension
is useful when collections are very large
can generate infinite streams
(expression for item in collection)
generating amino acid translations of codons
def aa_generator(rnaseq):
"""Return a generator object that produces an amino acid by translating
the next three characters of rnaseq each time next is called on it"""
(translate_RNA_codon(rnaseq[n:n+3])
for n in range(0, len(rnaseq), 3))
Functional Parameters
for lowest GC content
min
with key = gc_content
Functions objects
functions and methods are objects, just like everything in Python
the parameter "key"
">>>" max(range(3, 7), key=abs)
7
Anonymous functions
lightwieghted functions: lambda expression
syntax: lambda args: expression-using-args
def fn (x, y):
return x
x + y
y
as the same:
fn = lambda x, y: x
x + y
y
Tips, Traps, Tracebacks
to know your directory
import os
os.getcwd()
one-element tuple is (value,)
TypeError: unhashable type 'list'
indicates a violation of the prohibition against using mutable built-in types as elements of sets or keys of dictionaries
Control Statements
All the statements before this chapter were compound (require at least one intended line afterthe first line)
structure
can ormust have more than one clause
suite of the clause - an intended series of statements
header of clause - from keyword till a colon
other compound statements (scaffold of the program)
Exception handlers
Raising exceptions
raise exception-expression
try statement
try:
try-statements
except ErrorClass:
except-statements
Iterations
begins with
for
work with so-called type
iterables
File iteration
with open(filename) as file:
for line in file:
do something with line
Dictionary Iteration
for key in dictionary.keys():
do something with key
for value in dictionary.values():
do something with value
Numberig Iterations
for n, value in enumerate(iterable):
do something with n and value
to generate numerical keys
enumerate
and tuple unpacking
Kinds of Iterations
Repeat template
for count in range(n):
statements
Do template
for item in collection:
do something with item
Collect
the results that get from each element (of the "something") through creating new list, dictionary, set
result = []
for item in collection:
statements using item
result.append(expression based on the statements)
return result
sometimes it is better to express it as comperehension
Combine
to process all elements of collection, in result, yielding a single value
result = identity-value
for item in collection:
result = result · item # One of these
result ·= item # three forms
result = fn(result, item) # is used.
return result
for looking for longst sequence in FASTA file
Collection Combine
result = []
for item in collection:
result += fn(item)
!# merge result with previous results
return result
combines colection results in a single collection
Count
reduced Combine
count = 0
for item in iterable:
count += 1
return count
far more efficient than converting it to a list and getting the length of the list
Search
(is a simple variaiton on Do)
for item in collection:
is much like search loop
if test item:
return item
useful for search of specific GenBankID in a big FASTA file
Filter
similiar to searching. but there is some operation with found element
Filter Do
for each item that meets specific condition
for item in collection:
if test item:
statements using item
Filtered Collect
the values for wich test is true are collected one at a time
result = []
for item in collection:
if test item:
statements using item
result.append(expression based on the statements)
return result
Other
Filtered Collect of Groups of Lines
Filtered Collect
Filtered Combine
Filtered Count
Nested Iteration
"do something" involves processing of a value from outer iteration and a value drom the inner iteration together
for outer in outer_collection:
for inner in inner_collection:
do something with inner and outer
three level iteration is useful for three bases codon
Loops
while expression:
statements
Loop with Final Clause
while expression:
statements1
else:xs
statements2
Loop Interupption (assoc. with both loop and iterations)
continue (rarely used) and break
Initialization of Loop Values
Looping Forever
Loops with guard conditions
Search loop
not end and not test
while not at-end and not at-target:
The
and operator is used to “protect” the second part of the test
Looping over lines of File
line = file.readline()
while line and not test(line):
do something with line
line = file.readline()
are used primarily to deal with external events and to process files by methods other than reading them line by line
Conditions
Simple Conditional
if expression:
statements
One-Alternative Conditional
if expression:
statements1
else:
statements2
Multi-Test Conditional
if expression1:
statements1
elif expression2:
statements2
'#' . . . any number of additional elif clauses
else:
statements
Criteria:
succinct
clear
accurate
"you don't often need them"
control statemen affect order of execution of other statements
control flow or flow of control
Extended Examples
Extracting Information from an HTMLfile
Grand Unified Bioinformatics File Parser
6 functions
4 are universal
Parsing FenBank Files
get_items is not defined?
https://www.oreilly.com/catalog/errata.csp?isbn=9780596154516
but other problem bubled up: item is undefined
Classes
define new classes new functions
Defining Classes
each instances sotores information in fields
atributes are methods and fields
accessed through dot = obj.name()
'AAU'.isupper # ACCESS the isupper ATTRIBUTE
<built-in method isupper of str object at 0x1c523e0>
'AAU'.isupper() # CALL the isupper METHOD
classes add nouns, the way functions add verbs
grouping and structuring (that what collections serve)
class name:
def method1(args...):
def method2(args...):
Instance Attributes
Access methods
Compute a value from the value of one or more fields
filter the collection that is the value if a field
Look up a value in a dictionary that is the of a field
Search for a targered value in a field that has a collection value or in some other collection outside the instance
Get the value of a field
Predicate method
boolean-value method or function
is essentially access method that also does comparison
for highlighting, a method's name should start from is (e.g. isupper)
underscore-lt-underscore method
instance is defined within method (not form the book)
class Shark:
def
init
(self, name, age):
self.name = name
self.age = age
In the Shark class example below, name and age are instances
Initialization methods
underscore-init-underscore
to take one rgument other than self
String methods
underscore-repr-underscore
define how Python prints instances of class
underscore-str-underscore
Modification methods
Define an access method to retrieve the notes
Define a modification method to add a note
Assign the field to a new list in init
Class Attributes
Instances = {} (Initialize the instance dictionary)
Traking a Class's instances (class field)
throgh list
class UsingList:
Instances = []
def
init
(self[, arg, ...]):
self.Instances.append(self)
through dictionary
class UsingDictionary:
Instances = {}
def
init
(self[, arg, ...]):
self.Instances[self.somefield] = self
generate a unique ID number for each instance and keeping track of all a class's instances
class fields also can be used for
A format string used in str and repr, adn additional output methods
Mnimun and maximum values
file path for input and output
Classes as objects
classes and instance have a few small differencces
to assign class field, an instance method must use the class name, exactly as if it were a function outside the class
It is much better to call a class method that does the assignment so that the name of the class is not part of the method definition)
when the value of a field is accessed - not assigned - through self in an instance method, Python looks first in the instance and then in the class
methods called through seld in an instance method can be either instance or class methods, whereas methods called through self in a class method are always class method
Class method definitions are preceded by
classmethod
classes and modules are both objects and their mechanisms are similar
Class methods
distinguiser is
classmethod
class GenBankEntry:
Instances = {} #. initialize the instance dictionary
classmethod
#.
def InstanceCount(self): make the method a class method
return len(self.Instances)
classmethod
def GetInstances(self): #. returning generator as discussed earlier
return (value for value in self.Instance.keys())
classmethod
def Get(self, target):
"""Return the instance whose GID is target"""
return self.Instances.get(target, None)
Trackin the Instance Count
class SomeClass:
InstanceCount = 0 #. initial assignment
classmethod
def IncrementCount(self):
self.InstanceCount += 1 #. reassignment
def
init
(self, args)
self.number = self.InstanceCount
.#. assign an instance field to
.#. the value of a class field
self.IncrementCount() #. class method call
Class and Method Relationships
part/whole relationships ammong the ojcets of the classes
taxonomy of types
class decompostion
dividing classes
file.gb is a list in which each feature is itself a rather elaborate data structure
class GenBankFeature (example of turning comples colllectons into a collextion of instances)
singleton classes
application needs only one instance
Inheritance
allows to define a new class that differs an old one in the methods it defines
subclasss and superclass
e.g.
class subclass(superclass):
generalization
several classes can be generelized under one superclass
subclass methods
compounding
a subclass can invoke several superclass methods
blocking
addition
extension
overriding
Tips, Traps, Tracebacks
don't forget about self in pararmeter
don;t capitalize the names of class files