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.
-
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 its reassembled skeleton. So, use the values of
Expected outputs for Strings and Math: 1a
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
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 %).
Expected outputs for Strings from Data: 1String Data
This is a follow up to Strings from Data.
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.
Expected outputs for String Data: 1read.csv()
imports the data as adata.frame()
, unlike the numeric vector in the previous exercise.Improve Your Code
This is a follow up to String Data.
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
Expected outputs for Improve Your Code: 1for
loop and your function from String Data 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
Expected outputs for Split Strings: 1"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.