Blog of Andrés Aravena
CMB2:

# Some answers to Homework 6

03 May 2021

Some students have been generous and shared their answers to homework – after the deadline, of course. Here you can see their answers and some of my comments.

These are some of the answers to Homework 6. We will share more homework answers later. If you want to share your homework’s answers, send me an email.

Most of the students prefer that answers are published anonimously. At the end, it is easier and better that all answers are shown without disclosing the author.

 :::marginnote # Student 1 ::: # 1. The town is full of rats r cat_and_rat <- function(N, birth_rate, catch_rate, death_rate, rats_ini, cats_ini) { cats <- d_cats <- rep(NA, N) rats <- d_rats <- rep(NA, N) cats[1] <- cats_ini rats[1] <- rats_ini d_cats[1] <- d_rats[1] <- 0 for(i in 2:N) { birth <- birth_raterats[i-1] catch <- catch_raterats[i-1]cats[i-1] death <- death_ratecats[i-1] d_rats[i] <- 2birth -birth - catch d_cats[i] <- 2catch -catch - death cats[i] <- cats[i-1] + d_cats[i] rats[i] <- rats[i-1] + d_rats[i] } return(data.frame(cats,rats)) }  ## How does behavior change when conditions change? r initial_cats <- seq(from=10, to=500, by=10) behaviour <- function(cats_ini) { cats <- d_cats <- rep(NA, 3650) rats <- d_rats <- rep(NA, 3650) cats[1] <- cats_ini rats[1] <- 3000 d_cats[1] <- d_rats[1] <- 0 for(i in 2:3650) { birth <- 1.3/100rats[i-1] catch <- 1e-6rats[i-1]cats[i-1] death <- 0.7/100cats[i-1] d_rats[i] <- 2birth -birth - catch d_cats[i] <- 2catch -catch - death cats[i] <- cats[i-1] + d_cats[i] rats[i] <- rats[i-1] + d_rats[i] } return(data.frame(initial_cats,rats)) } changing <- lapply(initial_cats,behaviour) min_rats <- sapply(changing, min) rats <- data.frame(initial_cats,min_rats) plot(rats$min_rats, xlab=“Inıtıal Cats”, ylab=“Rats”, type=“l”)  ## Bonus :::marginnote This is interesting. The Bonus does not have really any question. I’m curious why people answered a no-question. In those cases you should ask. ::: r min(town$$rats) max(town$$rats) sum(town$rats) plot(rats ~ cats, data=town)  # 2. When the cat is away, the rats will play ::: marginnote Here the filename includes the full path. That is not recommended. It is better to create a project for the homework and move the sequence.txt file to the working directory. ::: r library(seqinr) genes <- read.fasta("C:/Users/Hp2020/Downloads/sequence.txt", seqtype= "DNA", set.attributes = FALSE) length(genes) ## Calculate GC content ## Find extreme values Use these indices to look inside names(genes)

# Student 2

Here the sections are not well marked. There must be a space after the # symbol.

#1. The town is full of rats

Here the function is defined with default values. This is not recommended, since it makes confusing to use the function.

cat_and_rat <- function(N, birth_rate=1.3/100, catch_rate=1e-6,
death_rate=0.7/100, rats_ini=3000, cats_ini=100){
rats <- d_rats <- rep(NA, N)
cats <- d_cats <- rep(NA, N)

rats[1] <- rats_ini
cats[1] <- cats_ini
d_rats[1] <- d_cats[1] <- 0

for(i in 2:N){
birth <- birth_rate*rats[i-1]
catch <- catch_rate*rats[i-1]*cats[i-1]
death <- death_rate*cats[i-1]

d_cats[i] <- catch - death
d_rats[i] <- birth - catch

rats[i] <- rats[i-1] + d_rats[i]
cats[i] <- cats[i-1] + d_cats[i]
}
return(data.frame(rats, cats))
}

## How does behavior change when conditions change?

initial_cats <- seq(from=10, to=10, by=10)

for(i in initial_cats){
min_rats <- rep(NA, length(initial_cats))
min_rats <- cat_and_rat(cats_ini = i, N=365)
min(town$rats) return(min_rats) } plot(min_rats) ## Bonus plot(rats ~ cats, data=town) # 2. When the cat is away, the rats will play library(seqinr) genes <- read.fasta("sequence.txt") length(genes) ## Calculate GC content gc_genes <- sapply(genes, GC) ## Find extreme values which.max(gc_genes) which.min(gc_genes) This answer is wrong since it used fixed numbers. It should not. The code should work even if you change the FASTA file. Use these indices to look inside names(genes) names(gc_genes[970]) names(gc_genes[238]) ## Bonus acc_gc_genes <- cumsum(gc_genes) :::marginnote # Student 3 ::: :::marginnote # Student 4 ::: # 1. The town is full of rats r cats_and_rats <- function(N, birth_rate, catch_rate, death_rate, cats_ini, rats_ini) { rats <- d_rats <- rep(NA, N) cats <- d_cats <- rep(NA, N) d_rats[1] <- d_cats[1] <- 0 cats[1] <- cats_ini rats[1] <- rats_ini for(i in 2:N){ d_rats[i] <- birth_raterats[i-1] - cats[i-1]rats[i-1]catch_rate d_cats[i] <- -death_ratecats[i-1] + cats[i-1]rats[i-1]catch_rate rats[i] <- rats[i-1] + d_rats[i] cats[i] <- cats[i-1] + d_cats[i] } return(data.frame(rats, cats)) }  ## How does behavior change when conditions change? r initial_cats <- seq(from=10,to=500,by=10) min_rats <- rep(NA,length(initial_cats)) for(a in 1:length(initial_cats)){ town <- cats_and_rats(N=3650, rats_ini=3000, cats_ini=initial_cats[a], birth_rate=1.3/100, catch_rate=1e-6, death_rate=0.7/100) min_rats[a] <- min(town$rats) } plot(initial_cats,min_rats, type = “l”, ylab = “Rats”) 
## Bonus r locate_half <- function(i){ mid <- (min(i) + max(i))/2 which(i >= mid) -> indices_over_half_value ans <- indices_over_half_value[1] return(ans) }
min_rats <- rep(NA,length(initial_cats)) max_rats <- rep(NA,length(initial_cats)) sum_rats <- rep(NA,length(initial_cats))
min_rats[a] <- min(town$$rats) max_rats[a] <- max(town$$rats) sum_rats[a] <- sum(town\$rats)
plot(rats ~ cats, data=town) 
# 2. When the cat is away, the rats will play
## Calculate GC content r gc_genes <- function(i) { V <- toupper(genes[[i]]) count_C <- sum(V==“C”) count_G <- sum(V==“G”) GC_content <- (count_C +count_G)/length(V) return(GC_content) }
gc_genes <- sapply(1:length(genes), gc_genes) 
## Find extreme values
## Use these indices to look inside names(genes)

# 1. The town is full of rats

cat_and_rat <- function(N, rats_ini, cats_ini, birth_rate,
catch_rate, death_rate) {
cats <- rep(NA, N)
rats <- rep(NA, N)
cats[1] <- cats_ini
rats[1] <- rats_ini
for(i in 2:N) {
cats[i] <-  cats[i-1] + cats[i-1]*catch_rate*rats[i-1] - cats[i-1]*death_rate
rats[i] <-  rats[i-1] + rats[i-1]*birth_rate - rats[i-1]*catch_rate*cats[i-1]
}
return(data.frame(cats, rats))
}

## How does behavior change when conditions change?

What are the inputs to seq()?
It is better to use the names of each parameter.

The paramters N, rats_ini, birth_rate, catch_rate, and death_rate must be function inputs, not fixed values.

initial_cats <- seq(10, 500, 10)
cat_and_rat2 <- function(cats_ini) {
N=3650
rats_ini=3000
birth_rate=1.3/100
catch_rate=1e-6
death_rate=0.7/100
cats <- rep(NA, N)
rats <- rep(NA, N)
cats[1] <- cats_ini
rats[1] <- rats_ini
for(i in 2:N) {
cats[i] <-  cats[i-1] + cats[i-1]*catch_rate*rats[i-1] - cats[i-1]*death_rate
rats[i] <-  rats[i-1] + rats[i-1]*birth_rate - rats[i-1]*catch_rate*cats[i-1]
}
return(data.frame(rats))
}
list_of_rat <- lapply(initial_cats, cat_and_rat2)
min_rats <- data.frame(initial_cats, sapply(list_of_rat, min))
plot(min_rats, ylab="Rats")

## Bonus

max_rats <- data.frame(initial_cats, sapply(list_of_rat, max))
plot(max_rats, main="Max of Rats", ylab="Rats")

sum_rats <- data.frame(initial_cats, sapply(list_of_rat, sum))
plot(sum_rats, main="Sum of Rats", ylab="Rats")

# 2. When the cat is away, the rats will play

library(seqinr)
length(genes)

## Calculate GC content

gc_genes <- rep(NA, length(genes))
for(i in 1:length(genes)) {
gc_genes[i] <- GC(genes[[i]])
}

## Find extreme values

which.max(gc_genes)
which.min(gc_genes)

Use these indices to look inside names(genes)

names(genes[which.max(gc_genes)])
names(genes[which.min(gc_genes)])

## Bonus

gc_genes2 <- rep(NA, length(gc_genes))
for (i in 1:length(gc_genes)) {
gc_genes2[i] <- gc_genes[i] - sum(gc_genes)/length(gc_genes)
}
plot(gc_genes)
abline(h=mean(gc_genes))
abline(v=mean(which.max(cumsum(gc_genes2))))
abline(v=mean(which.min(cumsum(gc_genes2))))