# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="n4mm8D0Hs085"
# ## Explorating the Tree of Life
#
# <table><tr><td>
# <img src="https://i.guim.co.uk/img/static/sys-images/Guardian/Pix/pictures/2008/04/17/DarwinSketch.article.jpg?width=445&quality=85&auto=format&fit=max&s=c7f89552d12b8495b2b4eb4d7a5bc391" width=200><a href="https://i.guim.co.uk/img/static/sys-images/Guardian/Pix/pictures/2008/04/17/DarwinSketch.article.jpg?width=445&quality=85&auto=format&fit=max&s=c7f89552d12b8495b2b4eb4d7a5bc391">source</a></td><td><img src="http://www.greennature.ca/greennature/taxonomy/tree_of_life.png" width=300><a href="http://www.greennature.ca/greennature/taxonomy/tree_of_life.png">source</a></td></tr></table>

# + [markdown] id="OMVslK4zs09B"
# In this hands-on exercise, you answer the following questions by using pandas data structures and methods to analyze the eukaryote genome data store in the following tab-delimited file. https://raw.githubusercontent.com/csbfx/advpy122-data/master/euk.tsv

# + id="gtuyCFJps09B" colab={"base_uri": "https://localhost:8080/", "height": 419} executionInfo={"status": "ok", "timestamp": 1616673686451, "user_tz": -330, "elapsed": 1319, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="a2d9572c-4a54-45bf-a9ca-70b59be86567"
import pandas as pd
# %matplotlib inline

# this input file is tab-delimited instead of comma-delimited
# Load the csv file into a dataframe
# Your code here
tsvFile = "https://raw.githubusercontent.com/csbfx/advpy122-data/master/euk.tsv"
euk = pd.read_csv(tsvFile, sep = '\t')
euk

# + [markdown] id="Uh1vDQw6s09C"
# ## Q1. How many Mammals have at least 20,000 genes? What are their scientific names? 
# *Note:* 
# - *Mammals are under Class*
# - *Scientific names are under Species*
#

# + id="bWZB42HAs09C" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1616673686941, "user_tz": -330, "elapsed": 1798, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="3dd82049-a61f-4f6a-86d3-f6db429f10e4"
# Your code here . . .
euk['Number of genes'] = pd.to_numeric(euk["Number of genes"], errors= 'coerce')
mammals_with_genes = euk[(euk.Class == 'Mammals') & (euk['Number of genes'] >= (20000))]
mammals_with_genes.Species.tolist()

# + [markdown] id="_3_x34els09D"
# ### Q2. Animals are a part of Kingdom. How many genomes are there for each Class of Animals?

# + id="o-v0tKXZs09D" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1616673686943, "user_tz": -330, "elapsed": 1794, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="6a36ad6d-2e33-4162-e745-d6bebb8025b1"
# Your code here . . .
animals = euk[euk["Kingdom"] == "Animals"]
animals.Class.value_counts()

# + [markdown] id="zmFL0cwjs09D"
# ### Q3. Animals are a part of Kingdom. How many unique Species are there for each Class of Animals?

# + id="VDbGdB0tNmFg" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1616673710693, "user_tz": -330, "elapsed": 1055, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="8497ba80-36fe-4c4a-daf2-83fe105f94fc"
# Your code here . . .
animal_classes = animals.Class.unique()
#animal_classes
for Class in animal_classes:
  print(Class, animals[animals.Class == Class].Species.nunique())

# + [markdown] id="jmHgNGzNs09E"
# ### Q4. What are the unique scientific names of Mammals with the genus name Macaca?
# *Recall: the scientific name starts with genus followed by a space and then the species name. Example: Homo sapiens.*

# + id="QsIWVd1xs09E" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1616673922320, "user_tz": -330, "elapsed": 977, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="b7359180-85cb-4104-a575-f04dc389128b"
# Your code here . . .
mammals_with_genus_macaca = animals[(animals.Class == "Mammals") & (animals.Species.str.split(' ').str[0] == "Macaca")]
mammals_with_genus_macaca.Species.unique().tolist()

# + [markdown] id="Kim6Ilvxs09F"
# ### Q5. Modify the Species names to only contain the scientific names  and create a new dataframe.
#
# Some of the names in the Species column have more than two parts, such as `Emiliania huxleyi CCMP1516`. Create a new column Species that contains only the first two parts of name, such as `Emiliania huxleyi`. Combine this new Species column with `Kingdom`, `Class`, `Size (Mb)`, `Number of genes`, and `Number of proteins`.
#
# Hint: Follow Q3 in Lecture 4 with a little twist. Instead of just getting the first element from the split results, you will get the first two elements using .str[0:2] which will give you a list. You can then use .str.join(" ") to change it back to a string.

# + id="hXeXewm_s09F" colab={"base_uri": "https://localhost:8080/", "height": 419} executionInfo={"status": "ok", "timestamp": 1616675791419, "user_tz": -330, "elapsed": 982, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="a1d1bbf6-9aa2-467e-b49a-e195fe1142d2"
# Your code here . . .
new_euk = euk.copy()
#new_species = new_euk.Species.str.split(' ').str[0:2]
new_euk ['Species'] = new_euk.Species.str.split(' ').str[0:2].str.join(" ")
new_euk.drop(columns= ['GC%', 'Publication year', 'Assembly status'])

# + [markdown] id="_JqgByIXs09F"
# ### Q6. Create a pie plot using pandas to show the number of unique Species in each Class of Animals using the new dataframe you created in Q5
# Hint: First, create a new dataframe that contains the number of unique Species and the index is the corresponding Animals Class. Then, use that dataframe to plot the pie plot.
# [Check out this documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.pie.html)

# + id="oU_4Cxr_s09G" colab={"base_uri": "https://localhost:8080/", "height": 359} executionInfo={"status": "ok", "timestamp": 1616673686947, "user_tz": -330, "elapsed": 1774, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="be1a029c-3516-4ba1-95b5-656eccd1d19b"
# Create a new dataframe that contains the number of unique Species and 
# the index is the corresponding Animals Class.

# Your code here . . .
new_animals = new_euk[new_euk["Kingdom"] == "Animals"]
new_animals_classes = new_animals.Class.unique()
CLASS = []
unique_species = []
for Class in new_animals_classes:
  CLASS.append(Class)
  unique_species.append(new_animals[new_animals.Class == Class].Species.nunique())

df = pd.DataFrame(CLASS ,columns = ['Class']) 
df['Unique species'] = unique_species
df.set_index('Class')

# + id="B5ZHZ_d49D2h"


# + id="fZJNiJ4ts09G" colab={"base_uri": "https://localhost:8080/", "height": 428} executionInfo={"status": "ok", "timestamp": 1616673687649, "user_tz": -330, "elapsed": 2464, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "15645487820315763273"}} outputId="991c66e3-88b9-46fc-cb0f-f7f80aa49e30"
# Create a panda pie plot using the dataframe above

# Your code here . . .
df.plot.pie(y = 'Unique species', figsize=(7,7), labels = df['Class'], legend = False)

StarcoderdataJupyterScriptsDedupFiltered