<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/"></div></div><div><a data-url="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/" data-counturl="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/" data-count="horizontal" data-text="Creating Annotated Data Frames from GEO with the GEOquery package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rjbioinformatics.com/2016/08/05/creating-annotated-data-frames-from-geo-with-the-geoquery-package/"> Let's talk about science with R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Mining gene expression data from publicly available databases is a great way to find evidence to support you working hypothesis that gene X is relevant in condition Y. You may also want to mine publicly available data to build on an existing hypothesis or simply to find additional support for your favorite gene in a different animal model or experimental condition. In this post, we will go over how to use the <strong>GEOquery</strong> <strong>package</strong> to download a data matrix (or eset object) directly into R and append specific probe annotation information to this matrix for it to be exported as a csv file for easy manipulation in Excel or spreadsheet tools. This is especially useful for sharing data with collaborators who are not familiar with R and would rather look up there favorite genes in a spreadsheet format.</p>
<p>First, let’s start by opening an R session and creating a function to return the eset (ExpressionSet) object or the original list object downloaded by the<em><strong> getGEO()</strong></em> function in R.</p>
<pre>

getGEOdataObjects &lt;- function(x, getGSEobject=FALSE){
# Make sure the GEOquery package is installed
require(&quot;GEOquery&quot;)
# Use the getGEO() function to download the GEO data for the id stored in x
GSEDATA &lt;- getGEO(x, GSEMatrix=T, AnnotGPL=FALSE)
# Inspect the object by printing a summary of the expression values for the first 2 columns
print(summary(exprs(GSEDATA[[1]])[, 1:2]))

# Get the eset object
eset &lt;- GSEDATA[[1]]
# Save the objects generated for future use in the current working directory
save(GSEDATA, eset, file=paste(x, &quot;.RData&quot;, sep=&quot;&quot;))

# check whether we want to return the list object we downloaded on GEO or
# just the eset object with the getGSEobject argument
if(getGSEobject) return(GSEDATA) else return(eset)
}

</pre>
<p>We can test this function on a GEO dataset such as <a target="_blank" href="http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE73835">GSE73835</a> as follows:</p>
<pre>

# Store the dataset ids in a vector GEO_DATASETS just in case you want to loop through several GEO ids
GEO_DATASETS &lt;- c(&quot;GSE73835&quot;)

# Use the function we created to return the eset object
eset &lt;- getGEOdataObjects(GEO_DATASETS[1])
# Inspect the eset object to get the annotation GPL id
eset 

</pre>
<p>You will see the following output:</p>
<p><em>ExpressionSet (storageMode: lockedEnvironment)</em><br>
<em> assayData: 45281 features, 6 samples</em><br>
<em> element names: exprs</em><br>
<em> protocolData: none</em><br>
<em> phenoData</em><br>
<em> sampleNames: GSM1904293 GSM1904294 … GSM1904298 (6 total)</em><br>
<em> varLabels: title geo_accession … data_row_count (35 total)</em><br>
<em> varMetadata: labelDescription</em><br>
<em> featureData</em><br>
<em> featureNames: ILMN_1212602 ILMN_1212603 … ILMN_3163582 (45281 total)</em><br>
<em> fvarLabels: ID Species … ORF (30 total)</em><br>
<em> fvarMetadata: Column Description labelDescription</em><br>
<em> experimentData: use ‘experimentData(object)’</em><br>
<em> Annotation: GPL6887</em></p>
<p>We will first need to download the annotation file for GPL6887. Then we can create a data frame with the probe annotation categories we are most interested in as follows:</p>
<pre>

# Get the annotation GPL id (see Annotation: GPL10558)
gpl &lt;- getGEO('GPL6887', destdir=&quot;.&quot;)
Meta(gpl)$title

# Inspect the table of the gpl annotation object
colnames(Table(gpl))

# Get the gene symbol and entrez ids to be used for annotations
Table(gpl)[1:10, c(1, 6, 9, 12)]
dim(Table(gpl))

# Get the gene expression data for all the probes with a gene symbol
geneProbes &lt;- which(!is.na(Table(gpl)$Symbol))
probeids &lt;- as.character(Table(gpl)$ID[geneProbes])

probes &lt;- intersect(probeids, rownames(exprs(eset)))
length(probes)

geneMatrix &lt;- exprs(eset)[probes, ]

inds &lt;- which(Table(gpl)$ID %in% probes)
# Check you get the same probes
head(probes)
head(as.character(Table(gpl)$ID[inds]))

# Create the expression matrix with gene ids
geneMatTable &lt;- cbind(geneMatrix, Table(gpl)[inds, c(1, 6, 9, 12)])
head(geneMatTable)

# Save a copy of the expression matrix as a csv file
write.csv(geneMatTable, paste(GEO_DATASETS[1], &quot;_DataMatrix.csv&quot;, sep=&quot;&quot;), row.names=T)

</pre>
<p>Let’s take a look at the first 6 lines of the data frame we just created with the <em><strong>head()</strong> </em>function.</p>
<p><img alt="example1" class="wp-image-465" src="https://i2.wp.com/rjbioinformatics.files.wordpress.com/2016/08/example1.png?resize=456%2C257&ssl=1" data-recalc-dims="1">As you can see once we export this data frame as a csv file, it is much easier for others to open this file as a spreadsheet and get useful information such as the gene symbol or entrez id with the expression values across the samples.</p>
<p>Hope this helps and happy collaborations!</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rjbioinformatics.wordpress.com/418/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rjbioinformatics.wordpress.com/418/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/"></div></div><div><a data-url="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/" data-counturl="https://www.r-bloggers.com/creating-annotated-data-frames-from-geo-with-the-geoquery-package/" data-count="vertical" data-text="Creating Annotated Data Frames from GEO with the GEOquery package" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rjbioinformatics.com/2016/08/05/creating-annotated-data-frames-from-geo-with-the-geoquery-package/"> Let's talk about science with R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6xJpObPa9fA"></a></div>