Class 7: Making decisions

Computing for Molecular Biology 2

Andrés Aravena, PhD

26 March 2021

Making Decisions

A problem with GC skew

We know that the GC skew is \[\frac{G-C}{G+C}\]

What is the GC skew of “ATATATATATATAT”?

We have a division by zero

Since \(G+C\) is 0, we are in trouble

Fortunately \(G-C\) is also 0,
so we can do something

We will say that, in this case, GC skew is 0

New rules for GC skew

  • count C and G

    C <- sum(V=="C")
    G <- sum(V=="G")
  • if G+C is 0, then

    • result is 0
  • else

    • result is (G-C)/(G+C)
  • return the results

But, how do we recognize that G+C is 0?

Making a decision

The last tool in our programming kit is

if(condition) {
    code
}

Here condition is a logic value. For example, a comparison

The code is only executed if condition is TRUE

if-then-else

This version has two code blocks

if(cond) {
    # do this only if `cond` is true
} else {
    # do this only if `cond` is false
}

Only one block is executed

The first block is executed only when cond is true

The second block is executed only when cond is false

Updated version of calculate_GC_skew

This version works in all cases

calculate_GC_skew <- function(dna) {
    V <- toupper(dna)
    count_C <- sum(V=="C")
    count_G <- sum(V=="G")
    if(count_C+count_G==0) {
      GC_skew <- 0
    } else {
      GC_skew <- (count_G-count_C)/(count_C+count_G)
    }
    return(GC_skew)
}

Testing the updated code

calculate_GC_skew(c("A", "T", "A", "T"))
[1] 0
calculate_GC_skew(c("G", "T", "A", "T"))
[1] 1
calculate_GC_skew(c("C", "T", "A", "T"))
[1] -1

Application

Smallest of two values

How do we find the smallest of two numbers?

Can you give some examples?

Smallest of two values

The contract of the function is:

  • Our function will get two numbers, let’s say a and b
  • We must return one number: the smallest value between a and b

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What are the steps (in plain English)?

Answer now in Google Documents

For the “Smallest of two values”

  • if a is smaller than b
    • then the answer is a
  • else (a is greater than b)
    • then the answer is b

 

if(a<b) {
    answer <- a
} else {
    answer <- b
}

 

Please finish the code and write a complete R function

Smaller of three

Smallest of three

The contract is this:

  • Our function will get three numbers, let’s say a, b, and c
  • We must return one number: the smallest value between a, b, and c

Let’s write a function for this

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What is the code?

Answer now

Smallest of many

Smallest of many

The contract is this:

  • Our function will get a vector of numbers, let’s say x
  • We must return one number: the smallest value of x

Let’s write a function for this

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What is the code?

Answer now

Now is your turn to ask

The only stupid question is the question not asked