#LDSC
#1.format GWAS summary 
python2.7 munge_sumstats.py \
--sumstats {GWAS} \
--N {samplesize} \
--out {outfilename}  \
--merge-alleles w_hm3.snplist

#2. compute heritability
python2.7 ldsc.py \
--h2 {outfilename}.sumstats.gz \
--ref-ld-chr eur_w_ld_chr/ \
--w-ld-chr eur_w_ld_chr/ \
--out {outfilename_h2}

#3. compute genetic correlation
python2.7 ldsc.py \
--rg {outfilename1}.sumstats.gz,{outfilename2}.sumstats.gz \
--ref-ld-chr eur_w_ld_chr/ \
--w-ld-chr eur_w_ld_chr/ \
--out {outfilename1_outfilename2}

#mr
args<-commandArgs(TRUE)
library(tidyverse)
library(data.table)
library(TwoSampleMR)
library(ieugwasr)
exposure <-fread(args[1])
ename <- gsub("txt.gz","",basename(args[1]))
colnames(exposure)<-c("SNP","chr","position","effect_allele","other_allele","beta","se","pval","eaf")
exposure$Phenotype<-ename
exposure<-exposure[exposure$pval<=5e-8,]
exposure<-TwoSampleMR::format_data(as.data.frame(exposure), type="exposure")
exposure <- clump_data(exposure, clump_kb = 500, clump_r2=0.1, pop = "EUR")
outcome<-fread(args[2])
colnames(outcome)<-c("SNP","chr","position","effect_allele","other_allele","beta","se","pval","eaf")
oname <- gsub("txt.gz","",basename(args[2]))
outcome$Phenotype<-oname
outcome<-TwoSampleMR::format_data(as.data.frame(y), type="outcome")
dat <-TwoSampleMR::harmonise_data(exposure_dat = exposure,outcome_dat =outcome)
res<-mr(tsmr_data,method_list =c("mr_egger_regression", "mr_ivw","mr_weighted_median","mr_ivw_mre","mr_simple_mode","mr_weighted_mode"))
het <- mr_heterogeneity(dat)
plt <- mr_pleiotropy_test(dat)

##RNA-seq
##Data acquisition and cleaning 
args<-commandArgs(TRUE)
library(DESeq2)
library(dplyr)
library(ggplot2)
library(survival)
library(survminer)
library(GDCRNATools)
library(DT)
library(data.table)
setwd("./TCGA")
project <- 'TCGA-CESC'
rnadir <- paste(project, 'RNAseq', sep='/')
gdcRNADownload(project.id     = 'TCGA-CESC', 
               data.type      = 'RNAseq', 
               write.manifest = FALSE,
               method         = 'gdc-client',
               directory      = rnadir)
clinicaldir <- paste(project, 'Clinical', sep='/')
gdcClinicalDownload(project.id     = 'TCGA-CESC', 
                    write.manifest = FALSE,
                    method         = 'gdc-client',
                    directory      = clinicaldir)
#### Parse RNAseq metadata
metaMatrix.RNA <- gdcParseMetadata(project.id = 'TCGA-CESC',
                                   data.type  = 'RNAseq', 
                                   write.meta = FALSE)

#### remove duplicates
metaMatrix.RNA <- gdcFilterDuplicate(metaMatrix.RNA)
####filter sample
metaMatrix.RNA <- gdcFilterSampleType(metaMatrix.RNA)
####### merge RNAseq data
rnaCounts <- gdcRNAMerge(metadata  = metaMatrix.RNA, 
                         path      = rnadir, # the folder in which the data stored
                         organized = FALSE, # if the data are in separate folders
                         data.type = 'RNAseq')


####### merge clinical data
clinicalDa <- gdcClinicalMerge(path = clinicaldir, key.info = TRUE)
clinicalDa[1:6,5:10]
### RNAseq data normalize
rnaExpr <- gdcVoomNormalization(counts = rnaCounts, filter = FALSE)

##DEG analysis
DEGAll <- gdcDEAnalysis(counts     = rnaCounts, 
                        group      = metaMatrix.RNA$sample_type, 
                        comparison = 'PrimaryTumor-SolidTissueNormal', 
                        method     = 'DESeq2')
data(DEGAll)
### All DEGs
deALL <- gdcDEReport(deg = DEGAll, gene.type = 'all')
### DE lncRNA
deLNC <- gdcDEReport(deg = DEGAll, gene.type = 'long_non_coding')
### DE protein-coding RNAs
dePC <- gdcDEReport(deg = DEGAll, gene.type = 'protein_coding')
### merge DEGs with depression related genes in cervical cancer
gene<-fread("./depression-related_genes_in_cervical_cancer.txt")
list <- gene$ID
selected_rows <- DEGAll$symbol %in% list
result <- DEGAll[selected_rows, ]
listname = rownames(result)
rna <- rownames(rnaExpr) %in% listname
result_rna <- rnaExpr[rna, ]
#enrichment analysis
enrichOutput <- gdcEnrichAnalysis(gene = listname, simplify = TRUE)
data(enrichOutput)
gdcEnrichPlot(enrichOutput, type = 'bar', category = 'GO', num.terms = 10)
gdcEnrichPlot(enrichOutput, type='bar', category='KEGG', num.terms = 10)

#survival analysis
#10 hub genes
genes <-c("TNF", "AKT1","INS","JUN","PTGS2","IGF1","IL6","ALB","TP53","VEGFA")
gene <- as.character(args[1])
surv.out <- gdcSurvivalAnalysis(gene = genes, rna.expr = rnaExpr,
                                metadata = metaMatrix.RNA)
gdcKMPlot(gene = gene, rna.expr = rna.voom, metadata = metadata)