# CLASS 8

The lecture slides and the relevant code for the class are here: http://goo.gl/CCgs0V

In this class we covered basic image processing to locate the position of a rat in a 3 chamber environment. We used this to estimate the time spent by the rat in each compartments (by processing many pictures / video). We also covered a list of resources that will be useful for further learning (Class8_2.pdf)

We first subtract the image of the cage with rat, from the image of just the case. Then we used a basic thresholding algorithm to fetch the position of the animal more precisely. This is now marked in a red dot.

Top to bottom, Background, image with rat, Subtracted image (output of images2.py)

Top to bottom, Abs difference, Abs difference and thresholding of 40 intensity (output of images3.py)

Localizing the rat as a red dot (output of images4.py)

Trace of the rat movement (output of images6.py)

Use the script images7.py to quantify the time spent in each chamber

# Class 7

In this class we learn how to handle strings. Specifically we looked at how DNA style strings can be manipulated.

All the slides and material used in the class is here http://goo.gl/R1vlrF

Class 7 first half was based on this tutorial.

We learn how to count length of DNA strands. To make their complements. To make its pre-mRNA, and then prune its introns. We translate it to the respective protein sequence.

We also see advanced plotting using animation for the grid cells example from Class 6. See grid_cells_2.py We use a colored line to trace the movement of the rat in the arena, while simultaneously plotting the spikes of the cell.

NOTES

1) How to split strings while retaining the delimiter string?
– There is no simple operation like dna.split(‘A’) that will accomplish this.

dna = ‘GATTACA’
delimiter = ‘A’
words = [ ]
for x in dna.split( delimiter ):
words.append( x + delimiter )
words_2 = [ x + delimiter for x in dna.split( delimiter ) ] # This is the relevant line

One has to re-create the list over again. Also note words and words_2 do the exact same thing. But words_2 is more elegant.

2) How to change a particular nucleotide at some position? (Emulating a single nucleotide mutation.)
– Generally it is not advised (or even possible) to change strings.
– The solution we arrived in the Class is correct.

dna = ‘GATTACA’
mutation = dna[:4] + ‘C’ + dna[5:] #mutation at the second A

HOMEWORK

1) Read, and do – Slide 17 and 18 – Python pickling and unpicling. Read codontable dictionary from parameters.py and dump this as codontable.pkl, and then read from it.

2) Slide 16, Read ‘protein.txt’, and find the corresponding location of mRNA in ‘mRNA.txt’ as shown in the class.

3) Using DNA sequence in parameters.py print the original DNA sequence with coding bases in uppercase and non-coding bases in lowercase. Introns are stated in the slides.

# Class 6 Homework

The lecture slides for Class 6 the code used, are available here: http://goo.gl/NWylEy

Homework (Please write if you need clarification or help)

1)

2)
The guessing game programmed in the class (see: /code/python_game.py) has a bug – when you guess the correct number in the last trial, the program says, ‘You guessed right!’, and yet the program exists stating that ‘You lost! No soup for you!’, Identify where this bug is, and correct it.

3)
Write a program that takes a filename for input, and gives the following:

A) The number of words in this file.
B) The number of occurrences of ‘and’, ‘a’, ‘an’, and ‘the’ in the text.
C) Make a bar plot of values from B, with appropriate x labels, and file name as the plot title.

4)
On the meadows of Austria, live wild sheep, with no predators or external threats. The population of the sheep can takes values between 0.0 and 1.0 – representing the ratio between existing population and the maximum theoretical possible population.

The population of sheep, the next year is given by,

x(n+1) = R * x(n) * ( 1 – x(n) )

Where x(n) is the current year’s population.
Where R is a rate of reproduction.

The equation above incorporates, that the next years population depends on
– current years population (reproduction) when populations are small and
– over crowding (starvation) when population is reaching maximum possible population : theoretical “carrying capacity” of the environment.

Part1
Plot as points, the population (y axis) for the first 53 years (x axis). For the following cases:
(Subplot1) starting population = 0.2, R = 2.8, color = ‘k’
(Subplot1) starting population = 0.2, R = 3.3, color = ‘g’
(Subplot1) starting population = 0.2, R = 3.5, color = ‘b’
(Subplot1) starting population = 0.2, R = 3.8. color = ‘r’

(Subplot2) same as Subplot1, but with starting population = 0.4
(Subplot3) same as Subplot1, but with starting population = 0.6
(Subplot4) same as Subplot1, but with starting population = 0.8

Part2
For 300 random starting populations, find the population of sheep at the end of 87 years.

Make a plot for R (x-axis) versus population at the end of 87 years (300 points on Y-axis for each R value), when is R varying between 2.4 and 4 at a 0.01 resolution. [R = 2.4, 2.41, 2.42, … 3.99, 4.0]

# CLASS 6

The lecture slides are available here: http://goo.gl/8rYGoH
Material necessary for this class are available here: http://goo.gl/JusZ2d (same as for Class5)

Grid cells plot.

# CLASS 5

The lecture slides from the Class 5 here: http://goo.gl/kIjnGk

Homework
(See Homework folder above)

1)
i) Complete the Exercise in ‘Spreadsheets.pdf’ slide number 16, See folder code, file analyze_imgdata1.py for help

ii) Use above, to generate a scatter plots corresponding to all the datasets,
– Age versus size
– Age versus intensity
(Homework / imgdata_result1.pdf is how your result will look like)

2)
In Homework folder, find ‘Phonebook.csv’ file. (This has been copied from http://www.nencki.gov.pl/ksiazka-telefoniczna)

Part1. Write a function that returns a list of persons in a given room. HINT: Use for loop and string matching.

Part2. Write a function that returns a dictionary with room numbers as keys and a list of persons in that room as its value.

HINT:
Use dict.has_key()
For example:
test_dict = { ‘1’ : [‘James Bond’, ‘006’], ‘2’ : [‘Her Majesty’], ‘3’ : [‘Moneypenny’, ‘M’] }
test_dict.has_key(‘1′) returns True
test_dict.has_key(’10’) returns False

Part3. Write a function, that returns the telephone number, and room number, give a given full name of person.

In all the above, account for corner cases – when invalid room numbers are sent to the function, it should not break your program – but instead should print ‘Room does not exist!’

Assignment – In case you have not already submitted your assignment, please do so before Wednesday (11th March) 5PM. Please submit how much ever you manage to complete.

To remind you, the assignment problem is the homework assignment – Bacteria growth from Class4 [https://nenckipython.wordpress.com/2015/03/02/class-4/].
Major hints to this assignment were provided in Class 5 and the slides for this are titled ‘Class4_HW.pdf’ in the above folder.

# Can you fly ?*

* Don’t try this at home.

# Class 4

Please find the slides from the class 4 here: http://goo.gl/LUBoC1

(Also included are programs written in Class 4)

This class was based on Software Carpentry’s lessons:

– Analyzing Data from Multiple Files (v 5.2, v5.3)
– Making Choices (v5.3)

Homework:

1) Complete exercise on slide 9 of Loops,  Class4_part1.pdf
2) Complete exercise on slide 9 of Conditionals, Class4_part2.pdf
– A certain strain of bacteria has a lag time of 5 minutes.
– Its exponential phase lasts for 12 minutes. In this time, each cell doubles after every minute.
– Stationary phase lasts for 1 hour, The cells maintain their population numbers.
– In the death phase, 7% of all surviving cells die after every minute.
Part 1.
Write a function that returns the highest number of cells the colony will ever have (This is equal to number of cells in stationary phase), for a given initial number (population count) of seed bacteria?
Part 2.
Write a function that accepts initial number (population count) of seed bacteria, and time of measurement in minutes, and returns
i) The phase of bacterial growth the colony is at the time of measurement? (Lag versus Exponential versus Stationary versus Death) and
ii) The number of cells alive at the time of the measurement.
HINT: Use if conditions for part 1, and use for loop in death phase of part 2.
Part 3.
Write a function, that for a given initial number (population) of cells, returns the time in minutes at which the colony dies? HINT: read documentation on ‘while loops
Part 4.
Make a plot similar to that shown below. With, X-axis as time versus Y-axis number of cells in log scale, for a given initial number (population) of seed cells. (Refer documentation on Log scale plots)
(From wiki page)
Cheers!
Chaitanya

# CLASS 3

Please find the slides for class 3 here: http://goo.gl/b4DWCV
This class is based on SoftwareCarpentry course (5.2), 2nd lecture : http://software-carpentry.org/v5/novice/python/02-func.html
NOTE: In ‘Python class 3.pdf’, we did not cover the slides 16-24 yet.
Home work,
1) Finish the Exercise on slide 13. (write a function analyze) in ‘Python class 3.pdf’
2) Complete the Exercise on slide 14 and 15
3)
The larvae of a certain insect, grow linearly in mass for the first 48 Hrs.
Write a function, named insect_weight, which returns the weight of the larva after a certain time.
The function accepts starting_weight, and growth_rate, and time as inputs.
For example, an 8 gram insect larvae growing at 1gram/ Hr, will weigh 9 grams after 1 hour and 10 grams after 2 hrs and so on.
So the function,
insect_weight(starting_weight = 8, growth_rate = 1, time=1) should return 9 grams, and
insect_weight(starting_weight = 8, growth_rate = 1, time=2) should return 10 grams.

# Class 2

In this class, we dive into details of plotting using matplotlib. This was prepared to be a filler class between 1st and 2nd classes from SoftwareCarpentry python course (5.2 version)

The slides and the code we used in the class  – with detailed comments are available here: Class2.pdf & codes folder http://goo.gl/xJVEQO

1. Output from bar_plot.py, showing mean inflammation data for 60 patients, as a bar plot, with error bars corresponding to std dev.

2. Output from patch_clamp.py showing the voltage trace from the Soma of a neuron, with spikes plotted as red points

3. Output from graph1.py illustrating drawing simple lines (and a curve) – using matplotlib as a graphing calculator

4. Output from fitting.py, showing the weights versus heights scatter plot for males, also  a linear fit to these points is shows, this is obtained using polyfit.

5. Output from graph2.py illustrating a sin and cos versus angle – using matplotlib as a graphing calculator

6. Output from graph3.py illustrating drawing of simple shapes – using matplotlib as a graphing calculator. Equal axis. [Paczki, as in dough nut in Polish]

7. Some fun graphs, output from cardioid.py, also showing xkcd style plotting.

Home work,
1) Programming 1. Complete the exercise on Slide 23, (add female weight height and fit scatter plot to the existing male weight height plot), data in codes folder here: http://goo.gl/xJVEQO
2) Programming 2.
In a behavioral experiment, a mouse (mouse1) is put in a box for an hour. The box has sensors at two of its corners, which record the time (in seconds) at which the mouse has entered that corner. In one corner (corner1) is water and in the other corner (corner2) is sugar water. The experiment is repeated with another mouse (mouse2). These time points are saved as 4 files and are available in HomeworkDataFiles folder here: http://goo.gl/xJVEQO
i)  In a bar plot, show the number of visits to each corner, for each mice.
ii) As a scatter plot, show the instances when each mouse was in the corner. [Time as X axis versus mouse1_corner1, mouse1_corner2, mouse2_corner1 and mouse2_corner2] Each visit is marked as a “dot” on the plot.
HINT: Like plotting spike times, there is no Y coordinate information.
3) Programming 3.
In an experiment, the potential from an electrode is recorded for 100ms following a stimulus. This is repeated 5000 times in all. This is available as potential.csv in HomeworkDataFiles folder here: http://goo.gl/xJVEQOIn 3 subplots, plot the following,
i) potential recorded in the first trial (subplot 1)
ii) averaged potential from the first 100 trials. (subplot 2)
iii) averaged potential from the 5000 trials. (subplot 3)
4) Reading: “Ten Simple Rules for Better Figures” http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833

Chaitanya

# Class 1

Please find the lecture slides, and python script from the first class on 5th February here.
Homework:

1) (Reading) Understand the material from the first class. The class was based on Software Carpentry’s first lesson: http://software-carpentry.org/v5/novice/python/01-numpy.html

2) Complete the exercises on page 35 from the slides.
3) (Reading) “Programming: Pick up Python”: http://www.nature.com/news/programming-pick-up-python-1.16833?WT.ec_id=NATURE-20150206 (Thanks to Marzena Stefaniuk for the link)

(Plot obtained at the end of the first lesson)