Assignment 10
Due date: April 6th at midnight (Thursday night).
In this assignment we will perform a clustering analysis on some codon data. Codons, for those unfamiliar, are sequences of three nucleotides that form a unit of genetic information. Since there are 4 nucleotide bases (A, C, T, G), there are 4*4*4=64 possible codons. You can read more about codons here.
The data for this assignment were originally taken from here, though the original source is here. I have modified the data to simplify it somewhat; the modified data can be found here. The data consists of codon relative-frequency data for 12,788 species of organisms. We will perform a clustering analysis on these data to determine if there are any clusters of significance and which 'Kingdoms' (types of organisms), are most-commonly in those clusters.
0) You must use version control ("git"), as you develop your code. We suggest you start, from the Linux command line, by creating a new directory, e.g. assignment9, cd into that directory and initialize a git repository ("git init"
) within it, and perform "git add ..., git commit"
repeatedly as you add to your code. You will hand in the output of "git log"
for your assignment repository as part of the assignment. You must have a significant number of commits representing the modifications, alterations and changes to your code. If your log does not show a significant number of commits with meaningful comments you will lose marks.
1) Create a file named codon_utilities.py
containing the following functions.
1a) Write a function which takes a single string argument, the name of the data file. The function should load the data, separate the data into the features (the codon columns) and the targets (the 'Kingdom' column), and return them. The pandas
package contains functionality for reading CSV files. The read_csv
and drop
functions might be useful. You may hard-code the column names involved.
1b) Write a function which, given some input data and a percentage, will perform a principle component analysis. The function should keep only those priciple components which explain the percentage of the data's variance indicated by the second argument. The function should then return the model.
1c) Write a function which, given some input data, will perform 10-fold cross validation on \(k\)-Means models, with \(k\) values ranging from 1 to 20. The function should generate a plot of cross-validation score versus \(k\).
1d) Write a function which takes some input data and a value of \(k\). The function will create a \(k\)-Means model for the given value of \(k\), train it on the data, and return the model.
1e) Write a function which takes as arguments a trained \(k\)-means model, some data, and a vector of targets. The function should determine the Kingdom with the highest population in each cluster. The function should print out the name of the Kingdom and the percentage of the population of the cluster that it represents. The collections
package's Counter
function is useful here. The predict
function, associated with the \(k\)-means model, is also needed.
2) Create a Python script called codon_analysis.py
that will perform the following steps:
- load the modified data file, linked above,
- run PCA on the features, keeping those principle components which explain 90% of the data's variance.
- create a transformed version of the features, using the principle components from the PCA.
- run 10-fold cross-validation on \(k\)-means models, using the transformed data, to determine which value of \(k\) would be the ideal number of clusters for this data set.
- At this point you should examine the figure which is generated by the function. Using your expertise, pick an ideal value of \(k\) for this data set. Put an explanation of your choice in the comments of your driver script.
- Create a new \(k\)-Means model, using your choice of ideal value of \(k\) and the transformed data.
- Print out the Kingdom in each cluster which has the largest population.
Your script should output something like this, when run from the shell terminal:
$
$ python codon_analysis.py
Cluster: 0 Kingdom: Vertebrate
Cluster: 1 Kingdom: Plant
Cluster: 2 Kingdom: Invertebrate
Cluster: 3 Kingdom: Bacteria
Cluster: 4 Kingdom: Bacteria
Cluster: 5 Kingdom: Virus
$
Be sure to comment and document your functions. Defensive programming is not needed for this assignment.
Submit your codon_utilities.py
, codon_analysis.py
files, your cross-validation figure, and the output of git log
from your assignment repository.
Both Python code files must be added and committed frequently to the repository. To capture the output of git log
use redirection (git log > git.log
, and hand in the git.log
file).
Assignments will be graded on a 10 point basis. Due date is April 6th 2023 (midnight), with 0.5 penalty point per day off for late submission until the cut-off date of April 13th 2023, at 11:00am.