Learning Objectives
Following this assignment students should be able to:
- understand basic built-in and
stringr
functions- manipulate strings for data analysis
Exercises
-- Print Strings --
-
Print the following: Post hoc ergo propter hoc
-
Print the following with no quotes: What’s up with scientists using all of this snooty latin?
-
Print the following with no quotes and an extra blank line (?cat): Darwin’s “On the origin of species” is a seminal work in biology.
-
Assign x <- 3, then paste in the appropriate location of the statement: Then shalt thou count to x, no more, no less.
-
-- Built-in Functions --
Use the built-in functions
abs()
,round()
,sqrt()
. A built-in function is one that you don’t need to install and load a package to use. Use another function,help()
, to learn how to use any of the functions that you don’t know how to use appropriately.help()
takes one parameter, the name of the function you want information about. E.g.,help(round)
.- The absolute value of -48.
- 5.26485 rounded to one decimal place. The function
round()
takes two arguments, the number to be rounded and the number of decimal places. - 6.2 rounded to the nearest integer. You don’t have to specify the number of
decimal places in this case if you don’t want to, because
round()
will default to using 0 if the second argument is not provided. Look athelp(round)
or?round
to see how this is indicated. - Assign the value of the square root of 486 to a variable. Then round the variable you’ve created to 2 decimal places and assign it to another variable. Print out the rounded value.
- Do the same thing as in problem 4, but instead of creating the intermediate
variable, perform both the square root and the round on a single line by
putting the
sqrt()
call inside theround()
call.
-- Built-in Functions --
Use the built-in character functions
tolower
andtoupper
to manipulate and print the following string."species"
in all capital letters"SPECIES"
in all lower case letters
There are other built-in character functions in R, but they are surprisingly clunky and difficult to use. Lucky for us, someone else thought so too and built a package called
[click here for output]stringr
, that we introduce next.-- stringr Functions --
Use the character functions from the package
stringr
to print the following strings."atgcatgcatgcatgcatgcatgcatgcatgcatgcatgcatgcatgcatgcatgcatgc"
. Do this by duplicating “atgc” 15 times." Thank goodness it's Friday"
without the leading white space (i.e., without the spaces before"Thank"
)."gcagtctgaggattccaccttctacctgggagagaggacatactatatcgcagcagtggaggtggaatgg"
with all of the occurences of"a"
replaced with"A"
.- Print the length of this dna sequence
"gccgatgtacatggaatatacttttcaggaaacacatatctgtggagagg"
. - The number of
"a"
s in"gccgatgtacatggaatatacttttcaggaaacacatatctgtggagagg"
. - Print the first 20 positions of this dna sequence
"gccgatgtacatggaatatacttttcaggaaacacatatctgtggagagg"
. - Print the last 10 positions of this dna sequence
"gccgatgtacatggaatatacttttcaggaaacacatatctgtggagagg"
.
-- Strings and Math --
The length of an organism is typically strongly correlated with its body mass. This is useful because it allows us to estimate the mass of an organism even if we only know its length. This relationship generally takes the form
Mass (kg) = a * Length(m)b
where the parameters
a
andb
vary among groups. Write a script that prompts the user for the following pieces of information:- Genus name
- species name
- the length of the species
and then estimates the mass of the organism using the equation above. The script should paste the result as:
Genus species is length meters long and weighs approximately mass kg.
where the words in italics are replaced with the appropriate values. As is standard practice the first letter (and only the first letter) of the Genus name should be capitalized, and the species name should appear in all lower case letters when input.
An allometric approach is regularly used to estimate the mass of dinosaurs since we cannot typically weigh something that is only preserved as bones. I’ll be testing your script using the length of a Spinosaurus (Spinosaurus aegyptiacus), which is 16 m long based on it’s reassembled skeleton. So, use the values of
[click here for output]a
andb
for Theropoda (the appropriate dinosaur clade):a
has been estimated as 0.73 andb
has been estimated as 3.63 (Seebacher 2001). Spinosaurus is a predator that is bigger, and therefore, by definition, cooler, than that stupid Tyrannosaurus that everyone likes so much.-- Long Strings --
For the DNA sequence below determine the following properties and print them to the screen (you can cut and paste the following into your code, it’s a lot longer than you can see on the screen, but just select the whole thing and when you paste it into R you’ll see what it looks like):
dna="ttcacctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgtgtgtctagctaagatgtattattctgctgtggatcccactaaagatatattcactgggcttattgggccaatgaaaatatgcaagaaaggaagtttacatgcaaatgggagacagaaagatgtagacaaggaattctatttgtttcctacagtatttgatgagaatgagagtttactcctggaagataatattagaatgtttacaactgcacctgatcaggtggataaggaagatgaagactttcaggaatctaataaaatgcactccatgaatggattcatgtatgggaatcagccgggtctcactatgtgcaaaggagattcggtcgtgtggtacttattcagcgccggaaatgaggccgatgtacatggaatatacttttcaggaaacacatatctgtggagaggagaacggagagacacagcaaacctcttccctcaaacaagtcttacgctccacatgtggcctgacacagaggggacttttaatgttgaatgccttacaactgatcattacacaggcggcatgaagcaaaaatatactgtgaaccaatgcaggcggcagtctgaggattccaccttctacctgggagagaggacatactatatcgcagcagtggaggtggaatgggattattccccacaaagggagtgggattaggagctgcatcatttacaagagcagaatgtttcaaatgcatttttagataagggagagttttacataggctcaaagtacaagaaagttgtgtatcggcagtatactgatagcacattccgtgttccagtggagagaaaagctgaagaagaacatctgggaattctaggtccacaacttcatgcagatgttggagacaaagtcaaaattatctttaaaaacatggccacaaggccctactcaatacatgcccatggggtacaaacagagagttctacagttactccaacattaccaggtaaactctcacttacgtatggaaaatcccagaaagatctggagctggaacagaggattctgcttgtattccatgggcttattattcaactgtggatcaagttaaggacctctacagtggattaattggccccctgattgtttgtcgaagaccttacttgaaagtattcaatcccagaaggaagctggaatttgcccttctgtttctagtttttgatgagaatgaatcttggtacttagatgacaacatcaaaacatactctgatcaccccgagaaagtaaacaaagatgatgaggaattcatagaaagcaataaaatgcatgctattaatggaagaatgtttggaaacct"
- How long is the sequence?
- How many occurences of
"gagg"
occur in the sequence? - What is the starting position of the first occurrence of
"atta"
? - What is the GC content of the sequence? The GC content is the percentage of bases that are either G or C (as a percentage of total base pairs). Paste the result as “The GC content of this sequence is XX.XX%” where XX.XX is the actual GC content.
-- Strings from Data 1 --
A colleague has produced a file with one DNA sequence on each line. Download the file and load it into R using
read.csv()
. The file has no header and is separated by white space (""
).Calculate the GC content of each sequence. The GC content is the percentage of bases that are either G or C (as a percentage of total base pairs). Print each GC content in order to the screen (in %).
[click here for output]-- Strings from Data 2 --
This is a follow up to Strings from Data 1.
A colleague has produced a file with one DNA sequence on each line. Download the file and load it into R using
read.csv()
. The file has no header.Write a function to calculate GC content. GC content is the percentage of bases that are either G or C as a percentage of total base pairs. Your function should take a dna sequence as input and return the GC-content of that sequence. Print the result for each sequence.
Before we knew about functions we had to take each dna sequence one at a time and then rewrite or copy-paste the same code to analyze each one. Isn’t this better?
You may have noticed that for Loop prints the results differently.
[click here for output]read.csv()
imports the data as adata.frame()
, unlike the numeric vector in the previous exercise.-- Improve Your Code --
This is a follow up to Strings from Data 2.
A colleague has produced a file with one DNA sequence on each line. So far you’ve been manually extracting each DNA sequence and calculating it’s GC content, which as worked OK with five sequences, but isn’t going to work very well when the sequencer really gets going and you have to handle 100s-1000s of sequences.
Use a
[click here for output]for
loop and your function from Strings from Data 2 to calculate the GC content of each sequence and print them out. The function should work on a single sequence at a time and thefor
loop should repeatedly call the function and print out the result.-- Split Strings --
You have a data file with a single
[click here for output]"taxonomy"
column in it. This column contains the family, genus, and species for a single taxonomic group. You need to figure out how to split that information into separate values for family, genus, and species. To solve the basic problem take a single example string,"Ornithorhynchidae Ornithorhynchus anatinus"
, split it into three separate strings using astringr
command, and then print the family, genus, and species, each on a separate line.