{"id":409,"date":"2019-08-06T23:15:59","date_gmt":"2019-08-06T15:15:59","guid":{"rendered":"http:\/\/www.wuchangsong.com\/?p=409"},"modified":"2019-09-28T19:53:40","modified_gmt":"2019-09-28T11:53:40","slug":"seurat%e4%bd%bf%e7%94%a8%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.wuchangsong.com\/?p=409","title":{"rendered":"Seurat\u4f7f\u7528\u6d41\u7a0b"},"content":{"rendered":"<h1 class=\"title-article\">seurat\u8f6f\u4ef6\u5b89\u88c5<\/h1>\n<h3 class=\"title-article\">Depends R (&gt;= 3.4.0), methods<\/h3>\n<pre><code>if (!requireNamespace(\"BiocManager\", quietly = TRUE))\r\n    install.packages(\"BiocManager\")\r\nBiocManager::install(\"Seurat\")<\/code><\/pre>\n<p>CentOS\u7cfb\u7edf\u5b89\u88c5\u65f6\u8981\u6ce8\u610fgcc\u7684\u7248\u672c<\/p>\n<pre>setwd(\"D:\/Experiment_data\/zxj\/outs\")\r\nlibrary(Seurat)\r\npbl.data &lt;- Read10X(data.dir = \"D:\/Experiment_data\/zxj\/outs\/filtered_feature_bc_matrix\")\r\ndim(pbl.data) #\u67e5\u770b\u884c\u548c\u5217\r\n#\u521b\u5efa Seurat \u5bf9\u8c61\u4e0e\u6570\u636e\u8fc7\u6ee4\u3002\u4fdd\u7559\u5728&gt;=3 \u4e2a\u7ec6\u80de\u4e2d\u8868\u8fbe\u7684\u57fa\u56e0\uff1b\u4fdd\u7559\u80fd\u68c0\u6d4b\u5230&gt;=200 \u4e2a\u57fa\u56e0\u7684\u7ec6\u80de\u3002\r\npbl &lt;- CreateSeuratObject(counts = pbl.data, project = \"pbl1907\", min.cells = 3, min.features = 200) \r\n#mt-\u5f00\u5934\u7684\u4e3a\u7ebf\u7c92\u4f53\u57fa\u56e0\uff0c\u8fd9\u91cc\u5c06\u5176\u8fdb\u884c\u6807\u8bb0\u5e76\u7edf\u8ba1\u5176\u5206\u5e03\u9891\u7387\r\npbl[[\"percent.mt\"]] &lt;- PercentageFeatureSet(pbl, pattern = \"^mt-\")\r\n# \u5bf9 pbmc \u5bf9\u8c61\u505a\u5c0f\u63d0\u7434\u56fe\uff0c\u5206\u522b\u4e3a\u57fa\u56e0\u6570\uff0c\u7ec6\u80de\u6570\u548c\u7ebf\u7c92\u4f53\u5360\u6bd4\r\nVlnPlot(object = pbmc, features = c(\"nFeature_RNA\", \"nCount_RNA\", \"percent.mt\"), ncol = 3)\r\n#\u6839\u636e\u56fe\u7247\u4e2d\u57fa\u56e0\u6570\u548c\u7ebf\u7c92\u4f53\u6570\uff0c\u5206\u522b\u8bbe\u7f6e\u8fc7\u6ee4\u53c2\u6570\uff0c\u8fd9\u91cc\u57fa\u56e0\u6570 200-2500\uff0c\u7ebf\u7c92\u4f53\u767e\u5206\u6bd4\u4e3a\u5c0f\u4e8e 5%\r\npbl &lt;- subset(pbl, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 2500 &amp; percent.mt &lt; 5)\r\nplot1 &lt;- FeatureScatter(pbmc, feature1 = \"nCount_RNA\", feature2 = \"percent.mt\")+ NoLegend()\r\nplot2 &lt;- FeatureScatter(pbmc, feature1 = \"nCount_RNA\", feature2 = \"nFeature_RNA\")+ NoLegend()\r\nCombinePlots(plots = list(plot1, plot2))\r\n#\u6807\u51c6\u5316\r\npbmc &lt;- NormalizeData(pbmc, normalization.method = \"LogNormalize\", scale.factor = 10000)\r\n#\u9274\u5b9a\u9ad8\u53d8\u57fa\u56e0\r\npbmc &lt;- FindVariableFeatures(pbmc, selection.method = \"vst\", nfeatures = 2000)\r\nplot3 &lt;- VariableFeaturePlot(pbmc)+ NoLegend()\r\nplot4 &lt;- LabelPoints(plot = plot3, points = top10, repel = TRUE,xnudge=0,ynudge=0)\r\nCombinePlots(plots = list(plot1, plot2))\r\n#\u6570\u636e\u5f52\u4e00\u5316\uff0c\u5bf9\u6240\u6709\u57fa\u56e0\u8fdb\u884c\u6807\u51c6\u5316\uff0c\u9ed8\u8ba4\u53ea\u662f\u6807\u51c6\u5316\u9ad8\u53d8\u57fa\u56e0\uff08 2000 \u4e2a\uff09\uff0c\u901f\u5ea6\u66f4\u5feb\uff0c\u4e0d\u5f71\u54cd PCA \u548c\u5206\u7fa4\uff0c\u4f46\u5f71\u54cd\u70ed\u56fe\u7684\u7ed8\u5236\u3002\r\nall.genes &lt;- rownames(pbmc)\r\npbmc &lt;- ScaleData(pbmc,features = all.genes,vars.to.regress = \"percent.mt\")\r\n#\u7ebf\u6027\u964d\u7ef4\uff08PCA\uff09\uff0c\u9ed8\u8ba4\u7528\u9ad8\u53d8\u57fa\u56e0\u96c6\uff0c\u4f46\u4e5f\u53ef\u901a\u8fc7 features \u53c2\u6570\u81ea\u5df1\u6307\u5b9a\r\npbmc &lt;- RunPCA(pbmc, features = VariableFeatures(object = pbmc))\r\n#\u5b9a\u4e49\u53ef\u89c6\u5316\u7ec6\u80de\u548c\u529f\u80fd\u7684\u51e0\u79cd\u6709\u7528\u7684\u65b9\u5f0fPCA\uff0c\u5305\u62ecVizDimReduction\uff0cDimPlot\uff0c\u548cDimHeatmap\r\nVizDimLoadings(object = pbmc, dims = 1:2, reduction = \"pca\")\r\nDimPlot(pbmc, reduction = \"pca\")+ NoLegend()\r\nDimHeatmap(pbmc, dims = 1:2, cells = 500, balanced = TRUE)\r\n#\u9274\u5b9a\u6570\u636e\u96c6\u7684\u53ef\u7528\u7ef4\u5ea6\uff0c\u4e3b\u6210\u5206\u5206\u6790\u7ed3\u675f\u540e\u9700\u8981\u786e\u5b9a\u54ea\u4e9b\u4e3b\u6210\u5206\u6240\u4ee3\u8868\u7684\u57fa\u56e0\u53ef\u4ee5\u8fdb\u5165\u4e0b\u6e38\u5206\u6790\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528JackStraw\u505a\u91cd\u62bd\u6837\u5206\u6790\u3002\u53ef\u4ee5\u7528JackStrawPlot\u53ef\u89c6\u5316\u770b\u770b\u54ea\u4e9b\u4e3b\u6210\u5206\u53ef\u4ee5\u8fdb\u884c\u4e0b\u6e38\u5206\u6790\u3002\r\npbmc &lt;- JackStraw(pbmc, num.replicate = 100)\r\npbmc &lt;- ScoreJackStraw(pbmc, dims = 1:20)\r\nJackStrawPlot(pbmc, dims = 1:15)#\u865a\u7ebf\u4ee5\u4e0a\u7684\u4e3a\u53ef\u7528\u7ef4\u5ea6\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8c03\u6574 dims \u53c2\u6570\uff0c\u753b\u51fa\u6240\u6709 pca \u67e5\u770b\r\n#\u8098\u90e8\u56fe\uff08\u788e\u77f3\u56fe\uff09\uff0c\u57fa\u4e8e\u6bcf\u4e2a\u4e3b\u6210\u5206\u5bf9\u65b9\u5dee\u89e3\u91ca\u7387\u7684\u6392\u540d\u3002\u5efa\u8bae\u5c1d\u8bd5\u9009\u62e9\u591a\u4e2a\u4e3b\u6210\u5206\u4e2a\u6570\u505a\u4e0b\u6e38\u5206\u6790\uff0c\u5bf9\u6574\u4f53\u5f71\u54cd\u4e0d\u5927\uff1b\u5728\u9009\u62e9\u6b64\u53c2\u6570\u65f6\uff0c\u5efa\u8bae\u9009\u62e9\u504f\u9ad8\u7684\u6570\u5b57\uff08 \u201c\u5b81\u6ee5\u52ff\u7f3a\u201d\uff0c\u4e3a\u4e86\u83b7\u53d6\u66f4\u591a\u7684\u7a00\u6709\u5206\u7fa4\uff09\uff1b\u6709\u4e9b\u4e9a\u7fa4\u5f88\u7f55\u89c1\uff0c\u5982\u679c\u6ca1\u6709\u5148\u9a8c\u77e5\u8bc6\uff0c\u5f88\u96be\u5c06\u8fd9\u79cd\u5927\u5c0f\u7684\u6570\u636e\u96c6\u4e0e\u80cc\u666f\u566a\u58f0\u533a\u5206\u5f00\u6765\u3002\r\nElbowPlot(object = pbmc)\r\n#\u975e\u7ebf\u6027\u964d\u7ef4\uff08 UMAP\/tSNE)\r\n#\u57fa\u4e8e PCA \u7a7a\u95f4\u4e2d\u7684\u6b27\u6c0f\u8ddd\u79bb\u8ba1\u7b97 nearest neighbor graph\uff0c\u4f18\u5316\u4efb\u610f\u4e24\u4e2a\u7ec6\u80de\u95f4\u7684\u8ddd\u79bb\u6743\u91cd\uff08\u8f93\u5165\u4e0a\u4e00\u6b65\u5f97\u5230\u7684 PC \u7ef4\u6570\uff09\r\npbmc &lt;- FindNeighbors(pbmc, dims = 1:10)\r\n#resolution \u53c2\u6570\u51b3\u5b9a\u4e0b\u6e38\u805a\u7c7b\u5206\u6790\u5f97\u5230\u7684\u5206\u7fa4\u6570\uff0c\u5bf9\u4e8e 3K \u5de6\u53f3\u7684\u7ec6\u80de\uff0c\u8bbe\u4e3a 0.4-1.2 \u80fd\u5f97\u5230\u8f83\u597d\u7684\u7ed3\u679c(\u5b98\u65b9\u8bf4\u660e)\uff1b\u5982\u679c\u6570\u636e\u91cf\u589e\u5927\uff0c\u8be5\u53c2\u6570\u4e5f\u5e94\u8be5\u9002\u5f53\u589e\u5927\uff1b\u589e\u52a0\u7684\u503c\u4f1a\u5bfc\u81f4\u66f4\u591a\u7684\u7fa4\u96c6\u3002\r\npbmc &lt;- FindClusters(pbmc, resolution = 0.5)\r\n#\u4f7f\u7528 Idents\uff08\uff09\u51fd\u6570\u53ef\u67e5\u770b\u4e0d\u540c\u7ec6\u80de\u7684\u5206\u7fa4\uff1b\r\nhead(Idents(pbmc), 8)\r\ntable(pbmc@active.ident) # \u67e5\u770b\u6bcf\u4e00\u7c7b\u6709\u591a\u5c11\u4e2a\u7ec6\u80de\r\n#Seurat \u63d0\u4f9b\u4e86\u51e0\u79cd\u975e\u7ebf\u6027\u964d\u7ef4\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff08\u5728\u4f4e\u7ef4\u7a7a\u95f4\u628a\u76f8\u4f3c\u7684\u7ec6\u80de\u805a\u5728\u4e00\u8d77\uff09\uff0c\u6bd4\u5982 UMAP \u548c t-SNE\uff0c\u8fd0\u884c UMAP \u9700\u8981\u5148\u5b89\u88c5'umap-learn'\u5305\uff0c\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u4e0d\u8981\u6df7\u7528\uff0c\u8fd9\u6837\uff0c\u540e\u9762\u7684\u7ed3\u7b97\u7ed3\u679c\u4f1a\u5c06\u5148\u524d\u7684\u805a\u7c7b\u8986\u76d6\u6389\uff0c\u53ea\u80fd\u4fdd\u7559\u4e00\u4e2a\r\n# If you haven't installed UMAP, you can do so via reticulate::py_install(packages = 'umap-learn')\r\npbmc &lt;- RunTSNE(pbmc, dims = 1:10)\r\n#pbmc &lt;- RunUMAP(object = pbmc, dims = 1:10)\r\n#DimPlot(object = pbmc, reduction = \"umap\")\r\n#\u7528 DimPlot()\u51fd\u6570\u7ed8\u5236\u6563\u70b9\u56fe\uff0c reduction = \"tsne\"\uff0c\u6307\u5b9a\u7ed8\u5236\u7c7b\u578b\uff1b\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u5148\u4ece\u641c\u7d22 umap\uff0c \u7136\u540e tsne, \u518d\u7136\u540e pca\uff1b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9 3 \u4e2a\u51fd\u6570 PCAPlot()\u3001 TSNEPlot()\u3001UMAPPlot()\uff1b cols\uff0c pt.size \u5206\u522b\u8c03\u6574\u5206\u7ec4\u989c\u8272\u548c\u70b9\u7684\u5927\u5c0f\uff1b\r\ntsneplot&lt;-TSNEPlot(pbmc,label = TRUE, pt.size = 1.5)+ NoLegend()\r\n#\u4fdd\u5b58\u6570\u636e\r\nsaveRDS(pbmc, file = \"pbmc_tutorial.rds\")\r\nsave(pbmc,file=\"pbmc.RData\")\r\nload(file = \"pbmc.RData\")\r\n#\u5bfb\u627e\u5dee\u5f02\u8868\u8fbe\u7684\u7279\u5f81\uff08\u805a\u7c7b\u751f\u7269\u6807\u5fd7\u7269\uff09\r\n#\u5bfb\u627e\u67d0\u4e2a\u805a\u7c7b\u548c\u5176\u4ed6\u6240\u6709\u805a\u7c7b\u663e\u8457\u8868\u8fbe\u7684\u57fa\u56e0\r\ncluster1.markers &lt;- FindMarkers(object = pbmc, ident.1 = 1, min.pct = 0.25)\r\nhead(x = cluster1.markers, n = 5)\r\n#find all markers distinguishing cluster 5 from clusters 0 and 3\r\ncluster5.markers &lt;- FindMarkers(object = pbmc, ident.1 = 5, ident.2 = c(0,3), min.pct = 0.25)\r\nwrite.table(as.data.frame(cluster5.markers), file=\"cluster1vs2.xls\", sep=\"\\t\", quote = F)\r\n# find markers for every cluster compared to all remaining cells, report only the positive ones\r\n#pbmc.markers &lt;- FindAllMarkers(object = pbmc, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)\r\npbmc.markers <span class=\"\">&lt;- FindAllMarkers(object = pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25, return.thresh = 0.01)\r\nlibrary(dplyr)\r\ntop10 &lt;- pbmc.markers %&gt;% group_by(cluster) %&gt;% top_n(n = 10, wt = avg_logFC)\r\nDoHeatmap(object = pbmc, features = top10$gene) + NoLegend()\r\n<\/span>#\u753b\u56fe\r\nVlnPlot(object = pbmc, features = c(\"MS4A1\", \"CD79A\"))\r\nFeaturePlot(object = pbmc, features = c(\"MS4A1\", \"GNLY\", \"CD3E\", \"CD14\", \"FCER1A\", \"FCGR3A\", \"LYZ\", \"PPBP\", \"CD8A\"))\r\n# you can plot raw UMI counts as well\r\nVlnPlot(object = pbmc, features.plot = c(\"NKG7\", \"PF4\"), use.raw = TRUE, y.log = TRUE)\r\nFeaturePlot(object = pbmc, features = c(\"hbaa1\",\"hbaa2\",\"ighv1-4\",\"cd79a\",\"cd79b\",\"cd4-1\",\"cd8a\",\"cd8b\",\"itga2b\"), cols = c(\"grey\", \"blue\"), reduction = \"tsne\")\r\n#\u5c06\u5355\u5143\u7c7b\u578b\u6807\u8bc6\u5206\u914d\u7ed9\u96c6\u7fa4\r\nnew.cluster.ids &lt;- c(\"1\", \"3\", \"4\", \"2\", \"5\", \"6\", \"7\", \"8\")\r\nnames(x = new.cluster.ids) &lt;- levels(x = pbmc)\r\npbmc &lt;- RenameIdents(object = pbmc, new.cluster.ids)\r\nDimPlot(object = pbmc, label = TRUE, pt.size = 1.5) + NoLegend()\r\n#\u6c14\u6ce1\u56fe\r\nmarkers.to.plot &lt;- c(\"cd74a\", \"cd74b\")\r\nDotPlot(pbmc, features = markers.to.plot) + RotatedAxis()\r\n\r\n\r\n<\/pre>\n<p>\u672a\u5b8c\u5f85\u7eed\u3002\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>seurat\u8f6f\u4ef6\u5b89\u88c5 Depends R (&gt;= 3.4.0), methods if (!requireNamespace(&#8220;BiocManager&#8221;, quietly = TRUE)) install.packages(&#8220;BiocManager&#8221;) BiocManager::install(&#8220;Seurat&#8221;) CentOS\u7cfb\u7edf\u5b89\u88c5\u65f6\u8981\u6ce8\u610fgcc\u7684\u7248\u672c setwd(&#8220;D:\/Experiment_data\/zxj\/outs&#8221;) library(Seurat) pbl.data &lt;- Read10X(data.dir = &#8220;D:\/Experiment_data\/zxj\/outs\/filtered_feature_bc_matrix&#8221;) dim(pbl.data) #\u67e5\u770b\u884c\u548c\u5217 #\u521b\u5efa Seurat \u5bf9\u8c61\u4e0e\u6570\u636e\u8fc7\u6ee4\u3002\u4fdd\u7559\u5728&gt;=3 \u4e2a\u7ec6\u80de\u4e2d\u8868\u8fbe\u7684\u57fa\u56e0\uff1b\u4fdd\u7559\u80fd\u68c0\u6d4b\u5230&gt;=200 \u4e2a\u57fa\u56e0\u7684\u7ec6\u80de\u3002 pbl &lt;- CreateSeuratObject(counts = pbl.data, project = &#8220;pbl1907&#8221;, min.cells = 3, min.features = 200) #mt-\u5f00\u5934\u7684\u4e3a\u7ebf\u7c92\u4f53\u57fa\u56e0\uff0c\u8fd9\u91cc\u5c06\u5176\u8fdb\u884c\u6807\u8bb0\u5e76\u7edf\u8ba1\u5176\u5206\u5e03\u9891\u7387 pbl[[&#8220;percent.mt&#8221;]] &lt;- PercentageFeatureSet(pbl, pattern = &#8220;^mt-&#8220;) # \u5bf9 pbmc \u5bf9\u8c61\u505a\u5c0f\u63d0\u7434\u56fe\uff0c\u5206\u522b\u4e3a\u57fa\u56e0\u6570\uff0c\u7ec6\u80de\u6570\u548c\u7ebf\u7c92\u4f53\u5360\u6bd4 VlnPlot(object = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,10],"tags":[],"_links":{"self":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/409"}],"collection":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=409"}],"version-history":[{"count":17,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":449,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/409\/revisions\/449"}],"wp:attachment":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}