Last updated: 2023-08-30

Checks: 6 1

Knit directory: lab-notes/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(1) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Recording the operating system, R version, and package versions is critical for reproducibility. To record the session information, add sessioninfo: “sessionInfo()” to _workflowr.yml. Alternatively, you could use devtools::session_info() or sessioninfo::session_info(). Lastly, you can manually add a code chunk to this file to run any one of these commands and then disable to automatic insertion by changing the workflowr setting to sessioninfo: ““.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version ba8cc1d. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    analysis/doc_prefix.html

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/geonotes.Rmd) and HTML (docs/geonotes.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html d9c3393 1onic 2023-08-28 Build site.
html d4afa3b 1onic 2023-07-12 Build site.
html 289324b 1onic 2023-07-12 Build site.
html be52451 1onic 2023-07-12 Build site.
html a73ac21 1onic 2023-06-13 Build site.
html 41874f0 1onic 2023-06-13 Build site.
html 75989aa 1onic 2023-06-13 Build site.
html 14b2f73 1onic 2023-06-13 Build site.
html 6f3f6a4 1onic 2023-05-17 Build site.
html 5192404 1onic 2023-05-17 Build site.
html 93a8936 1onic 2023-05-10 Build site.
html 454b232 1onic 2023-05-10 Build site.
html 6a1bef0 1onic 2023-05-10 Build site.
html b8fbbd1 1onic 2023-05-10 Build site.
html e5648db 1onic 2023-05-10 Build site.
html c0c4bf0 1onic 2023-04-26 Build site.
html 9fbd5e6 1onic 2023-04-26 Build site.
html 1478db1 1onic 2023-04-19 Build site.
html e1b57ff 1onic 2023-03-29 Build site.
html 7288d9c 1onic 2023-03-29 Build site.
html 3aef7a8 1onic 2023-03-29 Build site.
html 3f796b6 1onic 2023-03-22 Build site.
html cdae3ea 1onic 2023-03-10 Build site.
html 99420b0 1onic 2023-03-10 Build site.
html 0f70ffe 1onic 2023-03-10 Build site.
html e4922c1 1onic 2023-03-02 Build site.
html 2d991e9 1onic 2023-03-02 Build site.
html c350c44 1onic 2023-02-06 Build site.
html f12e92e 1onic 2023-02-06 Build site.
Rmd 92d5b64 1onic 2023-02-06 update
html 0b2cc20 1onic 2023-02-06 Build site.
html fdd0022 1onic 2023-02-06 Build site.
Rmd e24db96 1onic 2023-02-06 update

2/6/2023 Downloading Sample Tables from GEO

The goal for this section is to describe how to download ‘sample tables’ from GEO. Basically the core problem is that sometimes authors will upload raw data to supplement files in GEO and place the processed results as a table per sample. For example this sort of reporting is done for both the MESA and GENOA datasets.

The GENOA and MESA datasets are found here: https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE138914 and https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE56045 (respectively). You can navigate to a particular samples’ processed results by clicking on the sample in the Series and then selecting ‘view full table’. Our goal is to download these such tables. You can see an example here: https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSM1352002

Approach

We will actaully make urls from sample IDs and then just loop over this accross all samples. Generally the flow and how this works is like so: sample id > make url > connect to internet > read html > import to BS4 lib > extract table w/ BS4 > pandas dataframe > save

Annotated Code

This code is meant to run inside of a jupyter notebook on Midway3. In order to run this locally change the tqdm module (shows progress bar, time, iterations) to: “import tqdm”.

import sys, os
import io
import requests
import pandas
import urllib
from bs4 import BeautifulSoup
from tqdm import tqdm_notebook as tqdm
from pathlib import Path
import time

# GEO SITE :: https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE56045
# series ID : 56045
# GEO BROWSER SERIES LINK :: https://www.ncbi.nlm.nih.gov/geo/browse/?view=samples&series=56045
# series browser gives geo series sample IDs in the samples tab -> csv/tsv table
# upload or copy/paste the data to server/machine
geo_browser_samples_export = '.../MESA_signal_tables/mesa_GEO_samples.tsv'

# now read (table from above) in
sampledf = pandas.read_csv(geo_browser_samples_export, sep = '\t')

# now we need to extract just the GSM id (Accession) codes for each sample in the series
gsm_ids = sampledf['Accession'].tolist()
print('Accession codes loaded for {} samples'.format(len(gsm_ids))) # this will correspond EXACTLY to the # of samples in GEO series

# main loop outs
out_dir = '../MESA_signal_tables/sample_tables'
errors = []

# main loop
for GSM in tqdm(gsm_ids):
    #print('Fetching table for {}'.format(GSM))
    save_path = out_dir+'/{}_gc_rma_signal_table.tsv'.format(GSM)
    if not Path(save_path).exists(): # skip if its already downloaded
        try:
            time.sleep(1)
            target_url = 'https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?view=data&acc={}'.format(GSM)
            response = urllib.request.urlopen(target_url)
            code = response.getcode()
        except:
            print('Error w/ ' + GSM ) # this catches 502 errors (likely connection issue from urlopen() call; re-try later)
            errors.append(GSM)      

        if code == 200:        
            html = response.read() # read the response into html
            soup = BeautifulSoup(html)
            response.close()
            table_search = soup.find("pre")
            if not table_search:
                print('Table-Search Error w/ ' + GSM )
                errors.append(GSM)

            for e in table_search: # if we find genes in one of the elements [out of 6]
                #print(type(e)) # print the element type: either a BS4 tag or BS4 navigatable str
                if 'ENSG' in e:
                    textdata = e.get_text() # or str(child.encode('utf-8')) # extract text / encode the bytes w/ utf8
                    #print(textdata)
                    df = pandas.read_csv(io.StringIO(textdata), header=None, sep="\t") # feed str to io then to pandas 
                    df.columns = ['ID_REF','log2_GC-RMA_signal'] # set the correct column names (also present in table_search[0?] + table_search[1?] in theory)
                    #print(save_path)
                    df.to_csv(save_path, sep = '\t')
        else:
            print('Error w/ ' + GSM + ' eCode: '+code) # mainly a catch for 404 errors (likely bad sample URL / or sample itself)
            errors.append(GSM)


edf = pandas.DataFrame(errors) # save the errors to a seperate file for review
edf.columns = ['GSM_dl_error']
edf.to_csv(out_dir+'/_dl_error_table.tsv', sep = '\t')