Question 1

Consider the following simple function that produces a DNA sequence:

n = 5
sequence = function() {
  bases = c("C", "G", "A", "T")
  n = 10
  paste(sample(bases, n, TRUE), collapse = "")
}
sequence()
  1. Why does the final line return a sequence of length 10 and not 5.
## sequence uses the local variable n
  1. Delete line 4 in the above piece of code. Now change sequence() to allow v to be passed as an argument, i.e. we can write sequence(5). Call this function to make sure it works.
sequence = function(n) {
  bases = c("C", "G", "A", "T")
  paste(sample(bases, n, TRUE), collapse = "")
}
sequence(5)

Question 2

one = function(x = 10) {
  return(x)
}

two = function(x) {
  return(x)
}
  1. Why does
one()

work, but this raises an error

two()
## two() expects an argument x, but
## we haven't given one and there is
## no default.
  1. Change the sequence function so that it has a default value for n.
sequence = function(n = 10) {
  bases = c("C", "G", "A", "T")
  paste(sample(bases, n, TRUE), collapse = "")
}
sequence()

Question 3

So far we have been creating random DNA sequences. Lets do another routine job, split them into codons. You could approach this in a number of ways, but lets start with something like this (you will need the stringr package).

library(stringr)
# create the sequence
s = sequence()

# establish indeces of the first and last base in the codon
offset = 0
start = seq(1 + offset, str_length(s), by = 3)
end = start + 2

# retrieve the codons
str_sub(s, start, end)
  1. In the code above, delete the first two lines and put the code into a function called codons.
codons = function(sequence) {
  offset = 0
  start = seq(1 + offset, nchar(sequence), by = 3)
  end = start + 2
  str_sub(sequence, start, end)
}
  1. Alter the function such that offset is an argument with the default value of 0.
codons = function(sequence, offset = 0) {
  start = seq(1 + offset, nchar(sequence), by = 3)
  end = start + 2
  str_sub(sequence, start, end)
}
  1. Lets imagine that codons could be any length, alter the function such that the length of the codons is an argument of the function with a default value.
codons = function(sequence, offset = 0, length = 3) {
  start = seq(1 + offset, nchar(sequence), by = length)
  end = start + length - 1
  str_sub(sequence, start, end)
}

Solutions

Solutions are contained within this package:

vignette("solutions2", package = "jrProgBio")


jr-packages/jrProgBio documentation built on Jan. 18, 2020, 1:32 a.m.