Learn to use tools from the Bioconductor project to perform analysis of genomic data. This is the fifth course in the Genomic Big Data Specialization from Johns Hopkins University.

Loading...

From the course by Johns Hopkins University

Bioconductor for Genomic Data Science

123 ratings

Learn to use tools from the Bioconductor project to perform analysis of genomic data. This is the fifth course in the Genomic Big Data Specialization from Johns Hopkins University.

From the lesson

Week One

The class will cover how to install and use Bioconductor software. We will discuss common data structures, including ExpressionSets, SummarizedExperiment and GRanges used across several types of analyses.

- Kasper Daniel Hansen, PhDAssistant Professor, Biostatistics and Genetic Medicine

Bloomberg School of Public Health

In this class, we assume some basic familiarity with R.

Â In this session, however, we'll discuss some basic types of objects in R such

Â as vectors and matrices that we'll see again and again in Bioconductor and

Â where it's important to have some basic understanding of how to subset,

Â how to operate on these objects.

Â So there's a big class of objects in R called atomic vectors, or vectors.

Â Atomic vectors are vectors where every single element is of the same type.

Â For example, a sequence of numbers from one to ten.

Â Vectors, all vectors can have names.

Â And we can see what the class is, in this case an integer.

Â And we can subset vectors with a one dimensional subsetting.

Â That means we have a single set of intake vectors.

Â Or we can give it a set, now that it has names, we can index it based on its names.

Â Names do not have to be unique on vectors.

Â That's a source of frequent problems.

Â Let's take an example here where we set the names(x) to be c("a", "a","b").

Â So we have nonunique names here and we will say x,

Â give us the element of x with the name a.

Â We only get a single element back, not both of the elements.

Â We get the first match.

Â That can be very confusing.

Â And you can check whether there's duplicated names in vectors using unique

Â or duplicated which are base R functions you should learn about.

Â But there's also a little convenient function called anyDuplicated.

Â It returns either 0 or the index of the first duplication.

Â In this case it's the second element in the names vector

Â where we see the first duplication.

Â So getting a good hit back is getting anyDuplicated equal to zero.

Â Let's try to say that.

Â And now we get 0 back.

Â Now, in R,.you can in R, there's a slight difference between integers and numerics.

Â Numerics allow you to hold numbers of arbitrary precision and

Â integers are standard integers.

Â That's a little confusing and

Â it's something I would prefer not to spend too much time on.

Â But we are going to, in Biconductor sometimes, run into limits associated

Â with integers, so I feel we have to have a brief discussion about it.

Â Let's say that we set x equal to 1.

Â This looks like an integer, But

Â when I do it like this, I actually get a numeric back.

Â Unlike if I set x equal to this function here where I get a set of integers.

Â So how do I represent a single

Â integer on the command line?

Â I say x = 1 and then I put a capital L afterwards.

Â That's something you just have to know that this is how you do it.

Â So here we get a class x, and we get an integer back.

Â Now there's a limit to how big integers you can represent in R.

Â It's given by a machine constant.

Â You get various machine limits out by writing .Machine.

Â And this case, yes, integer.max.

Â It's a big number, it's equal to 2 to the 31st minus 1.

Â You can check whether that's equal to the Machine.$integer.

Â It's true.

Â And what is it roughly equal to?

Â Let's round it.

Â Let's divide it by ten, by a million and let's round it.

Â So it's roughly equal to 2.1 billions.

Â This is a number that's slightly smaller, or

Â smaller than the size of the human genome.

Â And this is why we're going to run into limits associated with vectors.

Â There's something in R called long vectors,

Â which are, let me back up a little bit.

Â This is a limit for how big numbers you can have in an integer.

Â It's also a limit for how long a vector can be in R.

Â So you can't have roughly more than 2 billion,

Â 2.1 billion elements in a vector unless it's something new called

Â a long vector that R has introduced recently where we still are waiting

Â to get kind of pervasive support for long vectors through all functions in R.

Â So you are sometimes going to run into this limit.

Â And it's basically if you add up a lot of bases from the human genome and

Â you get up to more than 2 billion.

Â And the solution is usually you convert the integers into numeric using

Â a function called as.numeric.

Â And basically this will fix it.

Â Let's now discuss matrices.

Â Matrices in R are another are very important fundamental class.

Â And matrices are constructed using the matrix function.

Â They are two dimension, and we give it a number of rows and

Â a number of columns, number of rows, and now we get a 3 by 3 matrix back.

Â It can have row names and column names, and

Â like vectors,

Â the row names and the column names does not have to be unique.

Â And like with vectors, having non-unique row names or

Â column names is a frequent source of errors.

Â A matrix is two-dimensional.

Â You get the dimension using dim.

Â And you can also get separately the number of rows and the number of columns.

Â Oh, the number of columns.

Â You subset matrices using two-dimensional subsetting, because it has two dimensions.

Â We can say, we can get a set of the first dimension is rows.

Â The second dimension in columns.

Â This set gives us the first two rows This here

Â will give us the first two columns, and we can do both of them at the same time.

Â We can also subset using characters.

Â So here we get the first row back.

Â And note, we don't get the first row back as a matrix.

Â We get it back as a vector.

Â Sometimes you really want to have a one-dimensional matrix or

Â a matrix with a single row and three columns back in this case here.

Â And you do that by using this hidden argument, I would call it a hidden

Â argument, to the subsetting operation where I can say drop=FALSE.

Â And now we get 1 by 3 matrix back.

Â You can also subset matrices uses matrices.

Â And a typical case is, I have a matrix and

Â I want to subset it using some logical operator.

Â So for example I can say give me all the elements in the matrix

Â where the matrix is greater than 5.

Â And I get a vector back.

Â So matrix subsetting like this, it loses the dimensions of a matrix to get a vector

Â back, which kind of makes sense if you reflect a little bit about what happens.

Â Matrices are, behind the scenes, just really long vectors.

Â So, unless you do use long vectors,

Â there's also a limit to how big matrices can be.

Â Matrices are column first ,so that means when you created the matrix,

Â let's go back up here, we got 1 to 10, 1 to 9, we fill up the first column,

Â then we fill up the second column, then we fill up the third column.

Â We can do the same thing and then we can say byrow=TRUE.

Â So now we're going to fill it up by row and now we fill up by row.

Â The next basic object in R is something called a list.

Â And a list is just a list of different objects.

Â There's no requirement that the objects are even of the same kind, so

Â we construct it using the list operator, let's let it be a named list.

Â The first thing we are going to check some random numbers.

Â The next thing is going to be some letters, pick out five of those.

Â And then the last thing could be a function.

Â Let's say, mean.

Â So now we get a list out of three objects, we have three numbers,

Â we have five letters, or five characters, and we have a function.

Â So there is no requirement that they make sense in any way.

Â Since they can have names, we can refer to these with the names.

Â Let's back up for a moment.

Â Subsetting with matrices works in many ways like a vector.

Â You can think of a list, in a way, like a vector.

Â We can get the first two elements.

Â We can even get the first element.

Â And notice here that when we ask for

Â the first element we get back a list with one element.

Â In order to get the element itself we have to use double brackets.

Â So that gives us directly the vector,

Â whereas the x with one bracket, it gives us a list with one element in it.

Â There's a crucial difference between these two things and

Â it pays to get your head wrapped around this.

Â You can also, when it has names, you can refer to the components in the matrix by,

Â in the list by the names.

Â Of course we'll do it in the classic way like we do with vectors, but

Â you can also the dollar operator.

Â One thing that's a little bit surprising is that you can,

Â you have partial matching when you do these things here.

Â So let's say here names(x), let's call them c("a").

Â We'll call that "letters", "c." So

Â I can write x$letters but I can also write x$let.

Â Nothing is called x$let but it does partial matching.

Â And that can be a little bit dangerous, especially if

Â we have two names that are almost the same,

Â I write x$let, and I get null back.

Â This is very, very, confusing at first,

Â and it's also another possible source of errors.

Â One way to get around this is you use the single bracket notation.

Â If you do the single bracket notation you don't have partial matching.

Â Sometimes you want a list where each element of the list is a single number.

Â And you do that using the as.list function.

Â You give it a vector of three numbers and

Â back we get a list with one element in each component of the list.

Â List can be, for list we have, very often have list that,

Â where each element in the list is of the same type.

Â Let's make an example.

Â rnorm(3), so here we have a list were

Â each element in the list is a vector of numbers.

Â And we want to use the same function on each of the element.

Â This is something we do all of the time in Bioconductor.

Â The basic function for that is called lapply.

Â We take the list and we apply some function to it, and we get a list back,

Â where each element of the list is the function applied on each element.

Â Quite often in this particular case, where the function returns a single number for

Â each case, we often want it really as a vector instead of a list.

Â One way of doing that is by unlisting the list.

Â Or, more frequent, you use sapply, which stands for

Â simplify apply, and it really just simplifies the output.

Â It requires that the output, checking on each element of the list is

Â kind of either a number or vector of the same length.

Â The final topic we are going to discuss are data frames.

Â Data frames are fundamental for data analysis and

Â it allows us to hold observations of different types.

Â So let's say we have a variable called sex, let's say.

Â Let's say I'll just execute it from over here.

Â We have a data frame that has two variables, sex and age.

Â Let's look it.

Â We have sex and we have age.

Â So this is a type where we can have different lists.

Â We can have elements or components of this object that has different types.

Â This would not be possible with a matrix.

Â Data frames are naturally kind of column oriented.

Â We can get their columns by using x$sex.

Â But we can't say, let's say there were rows names on the matrix,

Â you can't use the dollar notation to get the rows.

Â This is because frequently what we want in data frame is get a single column.

Â The way we think of data frames is that the columns are variables and

Â the rows are observations or different samples.

Â We can use single brackets and double brackets here to get

Â a specific column, but we can also subset the matrix or

Â the data frame in exactly the same way we have used for matrices.

Â One specific thing with data frames is that

Â the row names are required to be unique.

Â And that's actually kind of nice.

Â It also is something that slows down data frames when you have very big data frames.

Â But it's pretty nice.

Â They have to have row names and they have to be unique.

Â But the row names can be 1, 2, 3, as in the example we have here,

Â where you can kind of think of them as not really having row names.

Â Underneath it all, a data frame is really a list where each component in the list is

Â of the dame length.

Â And that means we can do a couple of tricks, like we can

Â use sapply and lapply on data frames and we apply them to different columns.

Â So something I use a lot is sapply class over the data frame in

Â order to figure out what class is each column.

Â And I get back that sex is a vector and age is a numeric.

Â The final subject of this assessment is going to be conversion.

Â We are frequently in the case where we have an object of a specific type and

Â we want to convert it into another type.

Â So let's take the data frame here.

Â Let's say we wanted the matrix out of the data frame.

Â We can use s.matrix, which casts the data frame as a matrix.

Â Notice it's a matrix now, but

Â it's a character matrix, because it cannot convert it into a numeric matrix

Â without losing the first column, or the first column would be N/A.

Â In the same way, I could convert the matrix into a list.

Â I get a list that looks like this.

Â And in this way, there's a lot of as.something functions,

Â which are part of base R.

Â These are useful and used again and again.

Â Now, in Bioconductor, we often have very complicated objects and

Â we kind of want to do the same thing but for very complicated objects.

Â And for that, we don't have as.something function.

Â But we have a general function that's inside the methods package.

Â And the function is just called as.

Â And the way you use it is as, object and whatever you want to cast it as.

Â So this here is very similar to the as.matrix, but it works for

Â very general types of objects.

Â So matrix could be some of the many different new types of objects we'll

Â learn about in Bioconductor, and we can cast between them using the as function.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.