%%html
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
<script src="https://bits.csb.pitt.edu/asker.js/lib/asker.js"></script>
<style>
.reveal pre { font-size: 100%; overflow-x: auto; overflow-y: auto;}
.reveal h1 { font-size: 2em}
.reveal ol {display: block;}
.reveal ul {display: block;}
.reveal .slides>section>section.present { max-height: 100%; overflow-y: auto;}
.jp-OutputArea-output { padding: 0; }
</style>
<script>
$3Dmolpromise = new Promise((resolve, reject) => {
require(['https://3Dmol.org/build/3Dmol.js'], function(){
resolve();});
});
require(['https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.js'], function(Ch){
Chart = Ch;
});
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
//the callback is provided a canvas object and data
var chartmaker = function(canvas, labels, data) {
var ctx = $(canvas).get(0).getContext("2d");
var dataset = {labels: labels,
datasets:[{
data: data,
backgroundColor: "rgba(150,64,150,0.5)",
fillColor: "rgba(150,64,150,0.8)",
}]};
var myBarChart = new Chart(ctx,{type:'bar',data:dataset,options:{legend: {display:false},
scales: {
yAxes: [{
ticks: {
min: 0,
}
}]}}});
};
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Types and Variables¶
value - your data
type - what kind of data it is
variable - name of your data, how you access it
Types¶
Built In:
- Numerical: integers, floating point, complex
- Boolean (True, False)
- None
- Sequences: strings, tuples, lists, sets, dictionaries
- Callable (functions)
type¶
type(3)
int
type(3.0)
float
type("Hello")
str
type(min)
builtin_function_or_method
Numbers¶
ints vs floats
%%html
<div id="types" style="width: 500px"></div>
<script>
jQuery('#types').asker({
id: "types",
question: "If <tt>a</tt> and <tt>b</tt> are of type <tt>int</tt> and <tt>x</tt> and <tt>y</tt> are of type <tt>float</tt> which of the following are true?",
answers: ["Speed","Associativity","Both of the above","Neither of the above"],
extra: ["<tt>a + b</tt> is faster than <tt>x + y</tt>",
"<tt>(a+b)-b</tt> will always equal <tt>a</tt> but <tt>(x+y)-y</tt> may not equal <tt>x</tt>"
],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Arithmetic Operators¶
+
, -
, *
, /
Hopefully self-explanatory
%
modulus (remainder after division)
**
exponentiation $x^y$ = x**y
//
integer (floor) division division is different in Python2 vs 3
5+1 * 3/2
%%html
<div id="aops" style="width: 500px"></div>
<script>
jQuery('#aops').asker({
id: "aops",
question: "What prints out?",
answers: ["6","6.5","7","7.5","9"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
5+1 * 3//2
%%html
<div id="aops2" style="width: 500px"></div>
<script>
jQuery('#aops2').asker({
id: "aops2",
question: "What prints out?",
answers: ["6","6.5","7","7.5","9"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
x = 10
x += 1
x
11
Strings¶
strings are a sequence of characters
String literals¶
- "you can use double quotes"
- 'you can using single quotes (more common python style)'
- "the difference is how easy it is to include a ' character" 'or a " character'
- special characters are escaped with a backslash
- so must always escape backslash itself
- '\n' newline
- '\\' backslash
- '\t' tab
- ''' single quote
Multiline string literals¶
"you can end a line with a slash\
and it will continue on the next line"
'you can end a line with a slashand it will continue on the next line'
Adjacent string literals are automatically concatenated
'hi ' 'bye'
'hi bye'
Triple quoted strings - for large blocks of text with newlines, commonly used as documentation:
'''There are three
quotes at the
start and
end'''
'There are three\nquotes at the\nstart and\nend'
print('"\\t'"'")
%%html
<div id="strq" style="width: 500px"></div>
<script>
var divid = '#strq';
jQuery(divid).asker({
id: divid,
question: "What prints out?",
answers: ["\ '\"",'"\ t"',
"\"\\t'\"'",'"\\t\'',"Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Variables¶
Data values are accessed through references to the value.
A reference is a name for the memory location of the value. Every value exists somewhere in memory and has an address.
A variable is created when it is bound to a value. It is impossible to have an uninitialized variable in python (but can be None).
The type of a variable is the type of the value it is bound to
x = 3
y = x
y = y + 1
print(x,y)
%%html
<div id="varsq" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#varsq';
jQuery(divid).asker({
id: divid,
question: "What prints out?",
answers: ["3 3","3 4","4 3","4 4"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Objects¶
Everything is an object!
An object is a value with set of attributes
Attributes that are callable are called methods and can work on the object data
Attributes are accessed with the '.' operator
The attributes of an object can be listed with dir
Strings are Objects¶
A string is an object that has several methods for manipulating the string data.
s = 'Hello World'
print(s.upper())
print(s.split())
HELLO WORLD ['Hello', 'World']
print(dir(s))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Numbers are Objects¶
Since everything is an object...
x = 3.0
x.is_integer()
True
print(dir(x))
['__abs__', '__add__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
Container Objects¶
A container object has items that are accessed with the [] operator
They hold an arbitrary number of item objects
The len method returns the number of items
Strings are an example of a container object
s = "Hello"
len(s)
5
s = "Hello"
%%html
<div id="sindex" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#sindex';
jQuery(divid).asker({
id: divid,
question: "What is the value of s[1]?",
answers: ["Hello","H","e","o","None"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Lists¶
A list is an ordered container of arbitrary objects.
A list is defined by comma separated items in square brackets []
.
mylist = [1,3.0,"cat", 9+2]
print (mylist)
[1, 3.0, 'cat', 11]
mylist[0]
1
Lists are objects and have a number of built-in methods:
print(dir(mylist))
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
l = []
l.append(5)
l.append(1)
l.append(3)
l.sort()
%%html
<div id="listq" style="width: 500px"></div>
<script>
var divid = '#listq';
jQuery(divid).asker({
id: divid,
question: "What is the value of l?",
answers: ["3","[3]","[5,1,3]","[1,3,5]","[3,1,5]"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Functions (callables)¶
A function is an encapsulation of code; a set of statements that can be executed on request and returns a value.
Functions are objects.
A method is a function that is an attribute of an object.
A function takes arguments and returns a result (maybe None)
The value of a callable type is the address of executable code
len("Luke, I am your father.") # this function takes one argument
divmod(13,4); # this function takes two arguments
Defining a function¶
def square(x):
return x * x
- def starts definition
- The function name is an identifier like a variable name
- good function names are a critical part of good coding style
- bad: foo, dostuff, process_data
- also bad: ReadInFromFileComputeCorrelationAndOutput
- good: ReadExpressionData, ComputeCorrelation, OutputCorrelationMatrix
- Parameters definine what arguments that function takes
- parameters are bound to the values passed to the function
- Statements are the body of the function; must be indented
- Return statement exits function and returns specified value
- if omitted, None is returned
- Function definition ends when no more indentation (whitespace significant!)
def twice(x):
return x*2
dbl = twice #functions are objects
print(dbl(4))
%%html
<div id="dbl" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#dbl';
jQuery(divid).asker({
id: divid,
question: "What prints out?",
answers: ["8","4","0","<function twice at 0x11115ea28>","None"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Function Scope¶
A function's parameters are bound to the passed value. That is, it's the same as if the parameter was set equal to the passed value (ex, x=4).
Parameters and variables bound (assigned to) in the function have local scope
global variables defined outside the function can only be read.
x=4
y=3
def incr(x):
x = x + 1
return x
print(x,incr(x),x)
4 5 4
%%html
<div id="func1" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#func1';
jQuery(divid).asker({
id: divid,
question: "What prints out?",
answers: ["4 4 4","4 5 5","4 5 4","4 5 6","Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
x=4
y=3
def incr():
y = y + 1
return y
print(y,incr())
%%html
<div id="func2" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#func2';
jQuery(divid).asker({
id: divid,
question: "What prints out?",
answers: ["3 3","3 4","4 4","Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Default Parameters¶
Default values for parameters can be given. This makes it easy to have optional arguments that take reasonable defaults if not specified.
def foo(x,y=0,z=1):
return (x+y)*z
foo(2)
2
foo(2,3)
5
foo(2,3,4)
20
Calling Functions¶
Functions are called using parens ().
It is an error to call a function with an incompatible number of arguments.
Named arguments allow you to specify arguments in a different order than defined.
Unnamed arguments (passed in the order defined) must all be specified before any named arguments.
foo(z=2,y=1,x=3)
8
foo(y=1,x=3)
4
def foo(x,y=0,z=1):
return (x+y)*z
%%html
<div id="func3" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#func3';
jQuery(divid).asker({
id: divid,
question: "What does <tt>foo(z=2,4)</tt> return?",
answers: ["8","4","5","10","An Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Lambda Functions¶
The lambda keyword can be used to generate an anonymous (unnamed) function object.
The return value of this anonymous lambda function is the value of the specified expression.
Lambda expressions tend to make your code more compact but less readable, so it's probably best to avoid their use for all but the simplest functions.
foo = lambda x: x**2 #this squares x
is equivalent to
def foo(x):
return x**2
Built-in Functions¶
There are a huge number of functions built into the python language and even more are included in standard modules. A few examples:
abs
- absolute valuelen
- length of a sequence (string, list, etc)min
,max
- returns smallest/largest item in a sequencesorted
- given a list, returns a sorted copy of that listtype
- returns type of an objectmap
- applies a function to every element of a sequence and returns a list of the resultfilter
- applies a function to every element of a sequence and returns a list of just those elements where the function evaluates to true
list(map(ord, "hello")) #ord returns ASCII code of string of length 1
[104, 101, 108, 108, 111]
list(filter(lambda x: x > 0, [1.0,0.4,-0.3,-1.3,4]))
[1.0, 0.4, 4]
What is truth?¶
Every object has a Boolean value
bool(None),bool(False),bool(True)
(False, False, True)
For numerical types 0 is false
bool(0),bool(0.0),bool(-100)
(False, False, True)
Empty collections are false
bool([]),bool(''),bool([False]),bool([0])
(False, False, True, True)
%%html
<div id="falseisfalse" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#falseisfalse';
jQuery(divid).asker({
id: divid,
question: "What is the value of bool('false')?",
answers: ["True","False","None","Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
1 < 3
True
"hello" != "hi"
True
[1,2,3] == [1,2,3], [1,2,3] == [1,2,3.14]
(True, False)
x = 3; y = 4;
x >= y
False
list(filter(round, [1.0,0.4,-0.3,-1.3,4]))
%%html
<div id="filterlam" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#filterlam';
jQuery(divid).asker({
id: divid,
question: "What is the result of this application of filter?",
answers: ["[1, 0, 0, -1, 4]","[1.0, 0.4, -0.3, -1.3, 4]","[1.0, -1.3, 4]","[1,-1,4]","[]","An Error"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Introspection¶
dir
and help
can be used to figure out what methods an object has and what they do.
help(round)
Help on built-in function round in module builtins: round(number, ndigits=None) Round a number to a given precision in decimal digits. The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as the number. ndigits may be negative.
File Objects¶
A file object provides an interface for reading and writing files.
Files, unlike memory, are accessed sequentially (like reading a book).
To create a file object use the open
function:
fileobject = open(filename, mode)
Where filename is a string that is either a relative path from the
current working directory (e.g., file.txt if file.txt is in the current
directory) or an absolute path (e.g. /home/user/dkoes/tmp/file.txt
).
File Mode¶
mode is a string that specifies what you are going to do with the file.
- 'r' - file must already exist and will only be read from (default)
- 'w' - file is created or truncated (delete what's already there) and can only be written to
- 'a' - file is open appended to (does not delete existing file) and can only be written to
It is also possible to open files for both read/write access ('r+') but this is tricky and generally not necessary.
Manipulating File Objects (Methods)¶
close
- closes the file when you are done with itread
- return the entire file as a string (can also specify optional size argument)readline
- return a single line from the file, returned string includes '\n'readlines
- return lists of all lineswrite
- writes a passed string to the fileseek
- set current position of the file; seek(0) starts back at beginning
f = open('../files/brca1.fasta')
f.readline()
'>lcl|NC_000017.10_cdsid_NP_009225.1 [gene=BRCA1] [protein=breast cancer type 1 susceptibility protein isoform 1] [protein_id=NP_009225.1] [location=complement(join(41197695..41197819,41199660..41199720,41201138..41201211,41203080..41203134,41209069..41209152,41215350..41215390,41215891..41215968,41219625..41219712,41222945..41223255,41226348..41226538,41228505..41228631,41234421..41234592,41242961..41243049,41243452..41246877,41247863..41247939,41249261..41249306,41251792..41251897,41256139..41256278,41256885..41256973,41258473..41258550,41267743..41267796,41276034..41276113))]\n'
f = open('../files/brca1.fasta')
f.read()
line = f.readline()
%%html
<div id="filer" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#filer';
jQuery(divid).asker({
id: divid,
question: "What is the value of line?",
answers: ["The first line in the file","The second line in the file",
"The last line in the file","An empty line","An error is generated"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
Exercise¶
What percent of this string consists of g or c?
atattaggtttttacctacccaggaaaagccaaccaacctcgatctcttgtagatctgttctctaaacgaactttaaaatctgtgtagctgtcgctcggctgcatgcctagtgcacctac
Create a .py
text file using Jupyter.
Exercise¶
!wget https://MSCBIO2025.github.io/files/brca1.fasta
How can you extract the gene name (second column) from the first line ofbrca1.fasta
?
How many As, Ts, Cs, and Gs are there on the second line of brca1.fasta
?
Write a function that takes a file name as an argument and prints out the gene name and percentage of G's and C's in the first line of the sequence.
Hint: Checkout split
, count
, and strip
methods of str