Reading multiple txt files and perform Two Sample Mendelian Randomization in R
1
0
Entering edit mode
6 weeks ago

I am trying to perform two sample mendelian randomization on over 300 text files (exposure data). All the files have the same column's name ( Bacteria, chr, bp,rsID, ref.allele, eff.allele, beta, SE,Pvalue ). I have kept all the txt files in a folder.

How can I load the txt files in the folder and perform two sample mendelian randomization for each of the txt file using one Rstudio code. I tried to use for-loop. However, don't know how to replace the filename in for-loop. My ultimate goal is to perform two sample mendelian randomization analysis on each of the txt files and get the result of each analysis.

setwd("D:/Project")
data_files <- list.files("p less than 5e-8")            # Identify file names in the folder "p less than 5e-8'
data_files                                                            # print all the files

library(TwoSampleMR)
ao <- available_outcomes()
for(i in 1:length(data_files)) {                              # Head of for-loop
exposure_dat <- read_exposure_data(
filename = data_files[i],
sep = '\t',
snp_col = 'rsID',
beta_col = 'beta',
se_col = 'SE',
effect_allele_col = 'eff.allele',
phenotype_col = '',
units_col = '',
other_allele_col = 'ref.allele',
eaf_col = '',
samplesize_col = '',
ncase_col = '',
ncontrol_col = '',
gene_col = '',
pval_col = 'P.value'
)
exposure_dat <- clump_data(exposure_dat)
exposure_dat
outcome_dat <- extract_outcome_data(exposure_datSNP, c('ieu-a-1058'), proxies = 1, rsq = 0.8, align_alleles = 1, palindromes = 1, maf_threshold = 0.3) dat <- harmonise_data(exposure_dat, outcome_dat, action = 2) mr_results <- mr(dat) mr_results  The above is my for loop r script. However, this does not work. If I input the file one by one using the following script. filename = '2_family.Bifidobacteriaceae.txt', is the code that input the file for analysis. It works perfect. library(TwoSampleMR) ao <- available_outcomes() exposure_dat <- read_exposure_data( filename = '2_family.Bifidobacteriaceae.txt', sep = '\t', snp_col = 'rsID', beta_col = 'beta', se_col = 'SE', effect_allele_col = 'eff.allele', phenotype_col = '', units_col = '', other_allele_col = 'ref.allele', eaf_col = '', samplesize_col = '', ncase_col = '', ncontrol_col = '', gene_col = '', pval_col = 'Pvalue' ) exposure_dat <- clump_data(exposure_dat) outcome_dat <- extract_outcome_data(exposure_datSNP, c('ieu-a-1058'), proxies = 1, rsq = 0.8, align_alleles = 1,
palindromes = 1, maf_threshold = 0.3)
dat <- harmonise_data(exposure_dat, outcome_dat, action = 2)
mr_results <- mr(dat)

2 sample MR Multiple txt files • 147 views
ADD COMMENT
0
Entering edit mode
6 weeks ago
Sam ★ 3.4k

One way to do it will be use  files <- list.files(pattern="csv\$")  which will stores all file name into the files object

You can then do a for loop with

for(i in files){
exposure_dat <- read_exposure_data( filename = i, .....)
.......
}

ADD COMMENT
1
Entering edit mode

Vectorization is better than loops.

Use lapply(files, read_exposure_data, sep="\t", snp_col='rsID',...)

ADD REPLY
0
Entering edit mode

Thank you, Sam. I tried your method. I think it should work. However, under the package of TwoSampleMR, a message of 'Please look at vignettes for options on running this locally if you need to run many instances of this command.' is popped out.

ADD REPLY
0
Entering edit mode

Thank you Ram. Agree lapply will be better and faster. Usage of read_exposure_data( filename, sep = "\t ", ...... ) Trying to work it out using lapply

ADD REPLY
0
Entering edit mode

It's not intuitive, but you can pass all arguments that you'd pass to read_exposure_data directly to lapply instead. lapply will pass them to each call of read_exposure_data. Example:

lapply(file, read.table, header=TRUE, sep="\t", stringsAsFactors=FALSE)

ADD REPLY

Login before adding your answer.

Traffic: 2671 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6327201,"math_prob":0.7567771,"size":3099,"snap":"2021-04-2021-17","text_gpt3_token_len":868,"char_repetition_ratio":0.16801292,"word_repetition_ratio":0.2879121,"special_character_ratio":0.2942885,"punctuation_ratio":0.20535715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97462064,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-15T10:01:31Z\",\"WARC-Record-ID\":\"<urn:uuid:c01bcb6a-787c-4a63-a715-0f56b9e6c50f>\",\"Content-Length\":\"28632\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:07a933bd-7afd-4b57-a235-ba57c1d4df48>\",\"WARC-Concurrent-To\":\"<urn:uuid:b100ef84-2634-4054-8cb0-7fed5e6571d2>\",\"WARC-IP-Address\":\"45.79.169.51\",\"WARC-Target-URI\":\"https://www.biostars.org/p/494414/\",\"WARC-Payload-Digest\":\"sha1:EXZ6SQMMNU7RO7TIZH43AHNHQMVRMV5I\",\"WARC-Block-Digest\":\"sha1:EQNWGKDCD7X42QDMIWJ5ONBWZ2LOFG4V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038084765.46_warc_CC-MAIN-20210415095505-20210415125505-00175.warc.gz\"}"}