Part 1. Mendelian Randomization Analysis
#1.Exposure Data Cleaning
library(TwoSampleMR)
inputFile="exposure_data.csv"      # input file
setwd("")     # set working directory
# Read the input file
Data=read.csv(inputFile, header=T, sep=",", check.names=F)
# Calculate the F-statistic value
Data$R2<-(2*Data$beta.exposure*Data$beta.exposure*Data$eaf.exposure*(1-Data$eaf.exposure)/(2*Data$beta.exposure*Data$beta.exposure*Data$eaf.exposure*(1-Data$eaf.exposure)+2*Data$se.exposure*Data$se.exposure*Data$samplesize.exposure*Data$eaf.exposure*(1-Data$eaf.exposure)))     # Calculate R2
Data$F<-Data$R2*(Data$samplesize.exposure-2)/(1-Data$R2)     # Calculate the F-statistic value
# Filter data based on F-value > 10, remove weak instrumental variables
exposure=dat[as.numeric(dat$F)>10,]
write.csv(exposure, file="exposure.F.csv", row.names=F)
###Manhattan Plot
library(VariantAnnotation)
library(gwasglue)
library(dplyr)
library(tidyr)
library(CMplot)
# Linear Manhattan Plot
data <- read.csv("eqtl_data.csv", header = TRUE, sep = ",")
CMplot(data,  plot.type="m",
LOG10=TRUE, threshold=5e-08, threshold.lwd=3, threshold.lty=1, signal.cex=0.2,
chr.den.col=NULL, cex=0.2, bin.size=1e5, ylim=c(0,50),
file="pdf", file.output=TRUE, width=15, height=9, verbose=TRUE)
# Perform MR 
#Read the exposure and outome file
Eqtl<-read.csv("exposure.F.csv", header=T, sep=",", check.names=F)
AD<- read.csv("outcome.csv", header=T, sep=",", check.names=F)
exposure_dat=read_exposure_data(filename=Eqtl,
                                sep = ",",
                                snp_col = "SNP",
                                beta_col = "beta.exposure",
                                se_col = "se.exposure",
                                pval_col = "pval.exposure",
                                effect_allele_col="effect_allele.exposure",
                                other_allele_col = "other_allele.exposure",
                                eaf_col = "eaf.exposure",
                                phenotype_col = "exposure",
                                id_col = "id.exposure",
                                samplesize_col = "samplesize.exposure",
                                chr_col="chr.exposure", pos_col = "pos.exposure",
                                clump=FALSE)
outcome_dat=read_outcome_data(filename=AD,
                              sep = ",",
                              snp_col = "SNP",
                              beta_col = "beta.outcome",
                              se_col = "se.outcome",
                              pval_col = "pval.outcome",
                              effect_allele_col="effect_allele.outcome",
                              other_allele_col = "other_allele.outcome",
                              eaf_col = "eaf.outcome",
                              phenotype_col = "outcome",
                              id_col = "id.outcome")
# Merge exposure data and outcome data
outcome_dat$outcome=outcomeName
dat=harmonise_data(exposure_dat, outcome_dat)
# Output instrumental variables for Mendelian randomization
SNP=dat[dat$mr_keep=="TRUE",]
write.csv(SNP, file="table.SNP.csv", row.names=F)
# Mendelian randomization analysis
mrResult=mr(dat)
# Calculate the OR values for the results
OR=generate_odds_ratios(mrResult)
write.csv(OR, file="table.MRresult.csv", row.names=F)
# Heterogeneity analysis
heterogeneity=mr_heterogeneity(dat)
write.csv(heterogeneity, file="table.heterogeneity.csv", row.names=F)
# Pleiotropy test
Pleiotropy=mr_pleiotropy_test(dat)
write.csv(Pleiotropy, file="table.pleiotropy.csv", row.names=F)
# Plot scatter plot
pdf(file="pic.scatter_plot.pdf", width=7.5, height=7)
mr_scatter_plot(mrResult, dat)
dev.off()
# Forest plot
res_single=mr_singlesnp(dat)      # Get the effect of each instrumental variable on the outcome
pdf(file="pic.forest.pdf", width=7, height=5.5)
mr_forest_plot(res_single)
dev.off()
# Funnel plot
pdf(file="pic.funnel_plot.pdf", width=7, height=6.5)
mr_funnel_plot(singlesnp_results = res_single)
dev.off()
# Leave-one-out sensitivity analysis
pdf(file="pic.leaveoneout.pdf", width=7, height=5.5)
mr_leaveoneout_plot(leaveoneout_results = mr_leaveoneout(dat))
dev.off()
2. Acquisition of Plasma Secretome Data
library(jsonlite)
setwd("")
secretomegenes <- fromJSON("protein_class_SPOCTOPUS.json")
#Screening for Human Proteins
secretomegenes <- secretomegenes[secretomegenes$Evidence=="Evidence at protein level",]
ITGBL1 <- secretomegenes[secretomegenes$Gene=="ITGBL1"]
#Screening of Plasma Secretome Proteins
secreted <- secretomegenes[grepl(pattern = "Plasma proteins",secretomegenes[,8]),]
#Exporting a List of Genes
plasmaproteingenelist <- secreted[1:11]
plasmaproteingenelist$`Gene synonym` <- data.frame(sapply(plasmaproteingenelist$`Gene synonym`,function(x)unlist(x,",")[1]))[,1]
plasmaproteingenelist$Uniprot <- data.frame(sapply(plasmaproteingenelist$Uniprot,function(x)unlist(x,",")[1]))[,1]
genelist <- plasmaproteingenelist[1:7]
write.table(genelist,file = "plasma_protein_list.txt",sep = "\t")

Part 2.  Single-Cell Data Analysis
library(devtools)
library(harmony)
library(Seurat)
library(ggplot2)
library(dplyr)
setwd("")
scRNA1=Read10X("GSM6593315_AD/")
scRNA2=Read10X("GSM6593316_AD/")
scRNA3=Read10X("GSM6593317_AD/")
scRNA4=Read10X("GSM6593318_AD/")
scRNA5=Read10X("GSM6593319_AD/")
scRNA6=Read10X("GSM6593320_AD/")
scRNA7=Read10X("GSM6593321_Normal/")
scRNA8=Read10X("GSM6593322_Normal/")
scRNA9=Read10X("GSM6593323_Normal/")
scRNA1=CreateSeuratObject(scRNA1 ,project="sample_1",min.cells = 3, min.features = 200)
scRNA2=CreateSeuratObject(scRNA2 ,project="sample_2",min.cells = 3, min.features = 200)
scRNA3=CreateSeuratObject(scRNA3 ,project="sample_3",min.cells = 3, min.features = 200)
scRNA4=CreateSeuratObject(scRNA4 ,project="sample_4",min.cells = 3, min.features = 200)
scRNA5=CreateSeuratObject(scRNA5 ,project="sample_5",min.cells = 3, min.features = 200)
scRNA6=CreateSeuratObject(scRNA6 ,project="sample_6",min.cells = 3, min.features = 200)
scRNA7=CreateSeuratObject(scRNA7 ,project="sample_7",min.cells = 3, min.features = 200)
scRNA8=CreateSeuratObject(scRNA8 ,project="sample_8",min.cells = 3, min.features = 200)
scRNA9=CreateSeuratObject(scRNA9 ,project="sample_9",min.cells = 3, min.features = 200)

scRNA_harmony <- merge(scRNA1, y=c(scRNA2,scRNA3,scRNA4,scRNA5,scRNA6,scRNA7,scRNA8,scRNA9))
##Quality Control
scRNA_harmony[["percent.mt"]] <- PercentageFeatureSet(scRNA_harmony, pattern = "^MT-")
##Calculate Red Blood Cell Genes
HB.genes <- c("HBA1","HBA2","HBB","HBD","HBE1","HBG1","HBG2","HBM","HBQ1","HBZ")
HB_m <- match(HB.genes, rownames(scRNA_harmony@assays$RNA)) 
HB.genes <- rownames(scRNA_harmony@assays$RNA)[HB_m] 
HB.genes <- HB.genes[!is.na(HB.genes)] 
col.num <- length(levels(scRNA_harmony@active.ident))
##Post-QC Seurat object
scRNA_harmonyPC <- subset(scRNA_harmony, subset = nFeature_RNA > 500&  percent.mt < 20 &  nCount_RNA > 1000)
##Normalization and Clustering
scRNA_harmonyPC <- NormalizeData(scRNA_harmonyPC) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA(verbose=FALSE)
scRNA_harmonyPC <- RunHarmony(scRNA_harmonyPC, group.by.vars = "orig.ident")
####Clustering
scRNA_harmonyPC <- FindNeighbors(scRNA_harmonyPC, reduction = "harmony", dims = 1:20) %>% FindClusters(resolution = 0.2)
###Check for the appropriate resolution
library(clustree)
library(patchwork)
scRNA_harmonyPC <- RunUMAP(scRNA_harmonyPC, reduction = "harmony", dims = 1:20)
seq <- seq(0.1, 1, by = 0.1)
for (res in seq) {
  scRNA_harmonyPC <- FindClusters(scRNA_harmonyPC, resolution = res)
}

p1 <- clustree(scRNA_harmonyPC, prefix = 'RNA_snn_res.') + coord_flip()
p2 <- DimPlot(scRNA_harmonyPC, group.by = 'RNA_snn_res.0.2', label = T)
M=p1 + p2 + plot_layout(widths = c(4, 2))
ggsave("resolution.plot.tiff", plot = M, width = 12, height = 10)
##UMAP
scRNA_harmonyPC <- RunUMAP(scRNA_harmonyPC, reduction = "harmony", dims = 1:20)
plot1 =DimPlot(scRNA_harmonyPC, reduction = "umap",label = T) 
plot2 = DimPlot(scRNA_harmonyPC, reduction = "umap", group.by='orig.ident') 
plotb <- plot1+plot2
ggsave("UMAP-harmony1.pdf", plot=plot1,width = 6, height = 5)
ggsave("UMAP-harmony2.pdf", plot=plot2,width = 6, height = 5)
ggsave("UMAP-harmony-Merge.pdf", plot=plotb,width = 8, height = 5)

save(scRNA_harmonyPC,file = "scRNA_harmonyPCData.Rdata")   
scRNA_harmonyPC <- FindNeighbors(scRNA_harmonyPC, reduction = "harmony", dims = 1:20) %>% FindClusters(resolution = 0.2)
scRNA_harmonyPC <- RunUMAP(scRNA_harmonyPC, reduction = "harmony", dims = 1:20)
plot1 =DimPlot(scRNA_harmonyPC, reduction = "umap",label = T) 
plot2 = DimPlot(scRNA_harmonyPC, reduction = "umap", group.by='orig.ident')
ggsave("AnnUMAP-Samples.pdf", plot=plot2,width = 8, height = 6)
ggsave("AnnUMAP-Samples.tiff", plot=plot2,width = 8, height = 6)
ggsave("AnnUMAP-Samples.png", plot=plot2,width = 8, height = 6)
ggsave("AnnUMAP-Samples.tiff", plot=plot2,width = 8, height = 6)
ggsave("AnnUMAP.pdf", plot=plot1,width = 8, height = 6)
ggsave("AnnUMAP.tiff", plot=plot1,width = 8, height = 6)
ggsave("AnnUMAP.png", plot=plot1,width = 8, height = 6)
##Annotation
genes <- c("LYZ","S100A8","S100A9",
           "TAGLN","ACTA2","MYH11",
           "DCN","LOX","LUM",
           "PECAM1","ADGRL4","AQP1",
           "CD3D","CCL5","CD2",
           "CD14","C1QA","C1QB",
           "NRXN1","SOX10","SOX2",
           "CD79A","CD79B","MS4A1")

genes <- as.factor(genes)
genes <- unique(genes)

#Bubble Chart
dot.plot <- DotPlot(scRNA_harmonyPC, features = genes) +
  theme_minimal() + 
  theme(
    axis.text.x = element_text(angle = 45, vjust = 1, hjust=1, size = 8),     legend.position = "right" 
  ) +
  scale_color_gradientn(colors = c("blue", "yellow", "red")) 


ggsave("dot.plot.pdf", plot = dot.plot, width = 8, height = 6) 

B_Lymphocytes = c("CD79A","CD79B","MS4A1")
Endothelial_cell = c("PECAM1","ADGRL4","AQP1")
Fibrobiasts = c("DCN","LOX","LUM")
Macrophages = c("LYZ","S100A8","S100A9")
Smooth_Muscle_cell = c("TAGLN","ACTA2","MYH11")
Mesenchymal = c("NRXN1","SOX10","SOX2")
T_Lymphocytes = c("CD3D","CCL5","CD2")
Monocytes = c("CD14","C1QA","C1QB")
##Rename Annotation
scRNA_harmonyPC <- RenameIdents(scRNA_harmonyPC, "0" = "Macrophages",
                                "1"="Fibrobiasts",
                                "2"="Smooth_Muscle_cell",
                                "3"="Endothelial_cell",
                                "4"="Smooth_Muscle_cell",
                                "5"="T_Lymphocytes",
                                "6"="Smooth_Muscle_cell",
                                "7"="Macrophages",
                                "8"="Monocytes",
                                "9"="Fibrobiasts",
                                "10"="Endothelial_cell",
                                "11"="Mesenchymal",
                                "12"="B_Lymphocytes"
                                ,"13"="Endothelial_cell",
                                "14"="Macrophages")
###Differential Analysis
genes <- c("CCN3", "DBNL", "NPC2", "PRKCSH", "SUMF2", "TEX264", "TFPI", "TGFBR3")
vln_plots <- list()
vln_CCN3 <- VlnPlot(object = scRNA1,
                      features = "CCN3",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_DBNL <- VlnPlot(object = scRNA1,
                      features = "DBNL",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_NPC2 <- VlnPlot(object = scRNA1,
                      features = "NPC2",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_PRKCSH <- VlnPlot(object = scRNA1,
                      features = "PRKCSH",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_TEX264 <- VlnPlot(object = scRNA1,
                      features = "TEX264",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_TFPI <- VlnPlot(object = scRNA1,
                      features = "TFPI",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_TGFBR3 <- VlnPlot(object = scRNA1,
                      features = "TGFBR3",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  vln_SUMF2 <- VlnPlot(object = scRNA1,
                      features = "SUMF2",
                      slot = "data",
                      group.by = "celltype",
                      split.by = "type",
                      pt.size = 0) +
    stat_compare_means(method = "t.test",
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
                                          symbols = c("****", "***", "**", "*", "ns")),
                       label = "p.signif")
  combined_plot1 <- wrap_plots(vln_CCN3,vln_DBNL,vln_SUMF2, vln_TEX264,ncol = 2)
  combined_plot2 <- wrap_plots(vln_NPC2,vln_PRKCSH,vln_TFPI, vln_TGFBR3,ncol = 2)
  
  "CCN3", "DBNL", "NPC2", "PRKCSH", "SUMF2", "TEX264", "TFPI", "TGFBR3")
  ggsave("violin_NPC2_PRKCSH-scRNA-.tiff",plot = combined_plot1,width=8,height=9)
  ggsave("violin_CCN3-DBNL-MR-scRNA-.tiff",plot = combined_plot2,width=8,height=9)

###cell ratio
library(Seurat)
library(tidyverse)
library(dplyr)
library(patchwork)
library(RColorBrewer)
load("scRNA.rdata")
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals))) 
pB4 <- ggplot(data = pB2_df, aes(x =Number, y = Sample, fill =  Cluster)) +
  geom_bar(stat = "identity", width=0.8,position="fill")+
  scale_fill_manual(values=col_vector[1:20]) +
  theme_bw()+
  theme(panel.grid =element_blank()) +
  labs(x="",y="Ratio")+
  theme(axis.text.y = element_text(size=12, colour = "black"))+
  theme(axis.text.x = element_text(size=12, colour = "black"))+
  theme(
    axis.text.x.bottom = element_text(hjust = 1, vjust = 1, angle = 45)
  ) 

degdf <- FindMarkers(scRNA,ident.1 = "",ident.2 = "", 
                     logfc.threshold = 0.3,group.by = "orig.ident",ident=1)
library(org.Hs.eg.db)
library(clusterProfiler)
degs.list=rownames(degdf)
erich.go.BP = enrichGO(gene =degs.list,
                       OrgDb = org.Hs.eg.db,
                       keyType = "SYMBOL",
                       ont = "BP",
                       pvalueCutoff = 0.05,
                       qvalueCutoff = 0.05)
dotplot(erich.go.BP,showCategory = 15)
barplot(erich.go.BP,showCategory = 8)
erich.go.BP=erich.go.BP@result
erich.go.CC = enrichGO(gene =degs.list,
                       OrgDb = org.Mm.eg.db,
                       keyType = "SYMBOL",
                       ont = "CC",
                       pvalueCutoff = 0.5,
                       qvalueCutoff = 0.5)
dotplot(erich.go.CC,showCategory = 8)
erich.go.MF = enrichGO(gene =degs.list,
                       OrgDb = org.Hs.eg.db,
                       keyType = "SYMBOL",
                       ont = "MF",
                       pvalueCutoff = 0.5,
                       qvalueCutoff = 0.5)
dotplot(erich.go.MF,showCategory = 8)
#enrichKEGG
erich.kegg.res <- enrichKEGG(gene = DEG.entrez_id,
                             organism = "Hsa",
                             keyType = "kegg")
dotplot(erich.kegg.res)






