%%html
<script src="https://requirejs.org/docs/release/2.3.6/minified/require.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.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>
console.log(jQuery.asker);
$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>
if
statement conditionally executes a block of codeif..elif..else
statement, only one block of code will be executedif False:
print(1)
elif True:
print(2)
else:
print(3)
2
val = 0
if val >= 0:
val += 1
elif val < 1:
val += 2
elif True:
val += 3
else:
val += 5
val += 7
%%html
<div id="if1" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="https://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');
var divid = '#if1';
jQuery(divid).asker({
id: divid,
question: "What is the value of val?",
answers: ["1","4","6","8","11","16"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
pass
does nothing
if True:
pass
else:
print("I will never print")
and
, or
, and not
not
has higher precedence than and
which has higher precedence than or
%%html
<div id="logic1" style="width: 600px"></div>
<script>
jQuery("#logic1").asker({
id: "logic1",
question: "Which of the following is equivalent to <tt>not (x and y)</tt>",
answers: ["<tt>not x and not y</tt>","<tt>not x and y</tt>","<tt>x or y</tt>","<tt>not x or not y</tt>"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
x = 3; y = 4; z = 5
val = x != 3 or not y < 0 and z < 10 or not y == 0 and z < 0
%%html
<div id="cond1" style="width: 500px"></div>
<script>
var divid = '#cond1';
jQuery(divid).asker({
id: divid,
question: "What is the value of val?",
answers: ["True","False"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
in and not in can be used to see if an object is in a collection
l = [1,2,3]
1 in l
True
345 in l
False
"1" not in l
True
"good" in "goodness"
True
val = 'cat' in 'catalog'
%%html
<div id="catin" style="width: 500px"></div>
<script>
var divid = '#catin';
jQuery(divid).asker({
id: divid,
question: "What is val?",
answers: ["True","False","None"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
val = 'cat' in ['catalog','catastrophe']
%%html
<div id="catin2" style="width: 500px"></div>
<script>
var divid = '#catin2';
jQuery(divid).asker({
id: divid,
question: "What is val?",
answers: ["True","False","None"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
i = 0
while i < 3: #as long as this condition is true...
print(i) #...execute this block of code
i += 1
0 1 2
letters = ['a','b','c','d','e','f','g']
for i in letters: #for every item in this collection...
print(i) #...execute this block of code with i set to the object
a b c d e f g
The preferred method of looping is to use a for loop. There are a number of builtin functions to help create collections to iterate over.
list(range(3))
[0, 1, 2]
list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(1,10,3))
[1, 4, 7]
val = 0
for i in range(100):
val += i
%%html
<div id="summer" style="width: 500px"></div>
<script>
var divid = '#summer';
jQuery(divid).asker({
id: divid,
question: "What is the value of val?",
answers: ["0","99","100","101","4950","5050"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
for will loop over any iterable object
In addition to collections, this includes any object that can provide items one at a time
f = open("../files/brca1.fasta")
cnt = 0
for line in f.readlines():
cnt += len(line)
cnt
6258
f = open("../files/brca1.fasta")
cnt = 0
for line in f:
cnt += len(line)
cnt
6258
break will exit out of the entire loop while continue will skip to the next iteration
i = 0
while True:
i += 1
if i < 6:
continue
print(i)
if i > 4:
break
6
x = 0
while x < 100:
if x < 50:
pass
elif x == 5:
break
x += 1
%%html
<div id="breakq" style="width: 500px"></div>
<script>
var divid = '#breakq';
jQuery(divid).asker({
id: divid,
question: "What is the value of x?",
answers: ["5","50","100","101","Nothing"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
What if you don't want to iterate over the full list?
s = "Hello"
s[0:2]
'He'
s[2:] # empty indices indicate remainder of string/list
'llo'
s[:-2] #negative indices count from the back
'Hel'
s2 = "Hello, world"
s2[:-2]
'Hello, wor'
l = ["cat","dog","fish","mouse"]
val = len(l[2:3])
%%html
<div id="slicer" style="width: 500px"></div>
<script>
var divid = '#slicer';
jQuery(divid).asker({
id: divid,
question: "What is the value of val?",
answers: ["0","1","2","3","4","5"],
server: "https://bits.csb.pitt.edu/asker.js/example/asker.cgi",
charter: chartmaker})
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();
</script>
import matplotlib.pyplot as plt
# next is a python notebook command to embed plot output in the notebook
%matplotlib inline
plt has hundreds of functions for creating and manipulating plots
plt.plot creates line/point graphs
plt.plot([0,1,2,3],[0,1,2,3],"-b",[0,1,2,3],[0,1,4,9],"--r")
plt.ylabel("Y")
plt.xlabel("X")
plt.show() #not actually necessary in notebook
plt.plot([1,2,4,9,16],label="one") #if only one data set is provide, assume uniform x
plt.plot([0.5,1,2,4.5,8],'^-.',label="two") #keep adding to current graph until show is called
plt.legend() #uses labels
plt.show()
plt.show() # this doesn't show anything since we haven't created another plot
plt.close() #another way to finish with a plot
If you use the %matplotlib command in ipython, the plot will be updated with every command (can skip show)
marker description
”.” point
”,” pixel
“o” circle
“v” triangle_down
“^” triangle_up
“<” triangle_left
“>” triangle_right
“1” tri_down
“2” tri_up
“3” tri_left
“4” tri_right
“8” octagon
“s” square
“p” pentagon
“*” star
“h” hexagon1
“H” hexagon2
“+” plus
“x” x
“D” diamond
“d” thin_diamond
“|” vline
“_” hline
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
Can also specify colors with full names or hexadecimal
plt.plot([1,2,4,6,7,4,2,2],'orange',linewidth=6)
plt.show()
f = open('../files/Spellman.txt')
header = f.readline().rstrip().split(',')#rstrip removes newline
data = []
for line in f:
data.append(line.rstrip().split(','))
len(data)
4381
for row in data[:10]: #first 10 only
values = list(map(float, row[1:]))
times = list(map(int, header[1:]))
plt.plot(times,values,label=row[0])
plt.legend(loc='upper center',ncol=5,prop={'size':7})
plt.xlabel("Time");
Data conversion...
for i in range(1,len(header)):
header[i] = int(header[i])
for r in range(len(data)):
data[r][i] = float(data[r][i])
plt.bar([1.1,2.1,3.1],[30,40,50],0.5,yerr=[2,10,12],ecolor="black",alpha=0.5,label="One")
plt.bar([1.4,2.4,3.4],[60,50,20],0.5,color='r',yerr=[10,4,1],ecolor="black",alpha=0.5,label="Two")
plt.xticks([1.4,2.4,3.4], ['A','B','C'])
plt.ylim([0,100])
plt.legend();
import random
rdata = []
for i in range(1000):
rdata.append(random.normalvariate(0,1))
plt.hist(rdata,10);
Just accumulate plots
plt.hist(rdata,10)
plt.plot([-3,-1,0,1,2,3],[20,30,50,60,80,90],'-r',linewidth=3)
plt.show()
fig, axes = plt.subplots(2,1)
plt.sca(axes[0]) #set current axis to first
plt.plot(header[1:],data[0][1:])
plt.ylabel(data[0][0])
plt.sca(axes[1])
plt.plot(header[1:],data[1][1:],'g')
plt.ylabel(data[1][0])
plt.show()
See also GridSpec
numgenes = 3
fig, axes = plt.subplots(numgenes,1,sharex=True,sharey=True)
colors = ['b','g','purple']
for i in range(numgenes):
#most plot methods have equivalents on axes objects
axes[i].plot(header[1:],data[i][1:],color=colors[i])
axes[i].set_ylabel(data[i][0])
Instead of plt.show use plt.savefig
bbox_inches='tight'
tells matplotlib to try harder to fit everything nicely.
plt.axes(aspect=1)
plt.pie([8,2,1],labels=["Novice","Beginner","Proficient"])
plt.savefig("pie.png",bbox_inches='tight')
ls *png
pie.png
Other formats: pdf, svg, eps...
https://MSCBIO2025.github.io/files/Spellman.csv