{"id":372,"date":"2022-03-29T22:29:46","date_gmt":"2022-03-29T14:29:46","guid":{"rendered":"https:\/\/linguopeng.top\/?p=372"},"modified":"2022-03-29T22:29:49","modified_gmt":"2022-03-29T14:29:49","slug":"predict_puls","status":"publish","type":"post","link":"https:\/\/linguopeng.top\/?p=372","title":{"rendered":"predict_puls"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env Rscript\n\n#################################\n#\n# Functions\n#\n#################################\n\nprocess_pairs &lt;- function(idx, td) {\n\n        mindbc &lt;- dbcan&#91;dbcan$protein_id %in% td$protein_id,]\n\n\tmindbc &lt;- unique(mindbc&#91;,c(\"protein_id\",\"hmm\")])\n\t\n\tgh &lt;- data.frame(protein_id=\"\",hmm=\"\", stringsAsFactors=FALSE)\n\tif (nrow(mindbc) > 0) {\n        \tgh &lt;- aggregate(mindbc$hmm, by=list(protein_id=mindbc$protein_id), paste, collapse=\";\")\n        }\n\tcolnames(gh) &lt;- c(\"protein_id\",\"hmm\")\n\n        cmin = idx -1\n        while(cmin > 0) {\n\n                contains = 0\n                for (j in cmin:(cmin-4)) {\n\n                        if (j>0) {\n\n\t\t\t\tdist &lt;- td$start&#91;j+1] - td$end&#91;j]\n\t\t\t\tif (dist > 500) {\n\t                                break\n        \t                }\n\n                                # check if it is susC\/D\n                                if (td$sus&#91;j] == \"susC\" || td$sus&#91;j] == \"susD\") {\n                                        contains = 1\n                                }\n\n                                # check if it is a GH\n                                prot_id = td$protein_id&#91;j]\n                                ghvec &lt;- gh$hmm&#91;gh$protein_id==prot_id]\n                                if (length(ghvec) > 0) {\n                                        contains = 1\n                                }\n\n                        }\n\n                }\n\n                if (contains > 0) {\n                        cmin &lt;- cmin -1\n                } else {\n                        break\n                }\n        }\n\n        cmin &lt;- cmin + 1\n\n        cmax = idx + 1\n        while(1) {\n\n                contains = 0\n                for (j in cmax:(cmax+4)) {\n\n                        if (j &lt;= nrow(td)) {\n\n\t\t\t\tdist &lt;- td$start&#91;j] - td$end&#91;j-1]\n\t\t\t\tif (dist > 500) {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\n                                # check if it is susC\/D\n                                if (td$sus&#91;j] == \"susC\" || td$sus&#91;j] == \"susD\") {\n                                        contains = 1\n                                }\n\n                                # check if it is a GH\n                                prot_id = td$protein_id&#91;j]\n                                ghvec &lt;- gh$hmm&#91;gh$protein_id==prot_id]\n                                if (length(ghvec) > 0) {\n                                        contains = 1\n                                }\n                        }\n\n                }\n\n                if (contains > 0) {\n                        cmax &lt;- cmax + 1\n                } else {\n                        break\n                }\n        }\n\n        cmax &lt;- cmax - 1\n\n\tif (cmax > cmin) {\n\n        \tpultdf &lt;- merge(td&#91;cmin:cmax,], gh, all.x=TRUE, sort=FALSE)\n        \tpultdf &lt;- pultdf&#91;order(pultdf$order),]\n        \tpultdf&#91;is.na(pultdf)] &lt;- \"\"\n        \tpultdf$pulid &lt;- rep(paste(\"PUL\",PULCOUNTER, sep=\"\"), nrow(pultdf))\n\n\t\tprint(paste(\"created PUL\", PULCOUNTER))\n\t\tprint(paste(cmin,\"-\",cmax))\n\n        \tALLPULS &lt;&lt;- rbind(ALLPULS, pultdf)\n\n        \tPULCOUNTER &lt;&lt;- PULCOUNTER + 1\n\t}\n        return(cmax)\n\n}\n\n#################################\n#\n# code\n#\n#################################\n\n# get command line arguments as an array\n# args &lt;- commandArgs(trailingOnly = TRUE)\nid&lt;-c(\"MAG609\")\nargs &lt;-c(paste0(\"..\/feature_table\/\",id&#91;1],\"_ft.txt\"),\n         paste0(\"..\/pfam\/\",id&#91;1],\".pfam\"),\n         paste0(\"..\/dbcan\/\",id&#91;1],\".out.dm.ps.filtered\"),\n         paste0(\"..\/puls\/\",id&#91;1],\".puls.tsv\"),\n         paste0(\"..\/puls\/\",id&#91;1],\".puls.sum.tsv\"),\n         id&#91;1])\n\n# arguments\nft.name &lt;- args&#91;1]\npf.name &lt;- args&#91;2]\ndb.name &lt;- args&#91;3]\n\n# file base\nfile.out &lt;- args&#91;4]\nfile.sum &lt;- args&#91;5]\n\n# genome\ngenome &lt;- args&#91;6]\n\n# ft.name&lt;-read.table(\"Prevotella_rodentium_ft.txt\",stringsAsFactors=FALSE)\n# colnames(ft.name)&lt;-c(\"contig\",\"start\",\"end\",\"strand\",\"protein_id\",\"protein_name\")\ncn &lt;- c(\"contig\",\"start\",\"end\",\"strand\",\"protein_id\",\"protein_name\")\nft &lt;- read.table(ft.name, \n\t\theader=FALSE, \n\t\tsep=\"\\t\", \n\t\tstringsAsFactors=FALSE, \n\t\tcol.names=cn)\n\nft$order = 1:nrow(ft)\n\ncn &lt;- c(\"protein_id\",\"as\",\"ae\",\"es\",\"ee\",\"ha\",\"hn\",\"type\",\"hs\",\"he\",\"hl\",\"bit\",\"e\",\"sig\",\"clan\",\"active\")\npfam &lt;- read.table(pf.name, \n\t\tskip=29, header=FALSE, \n\t\tstringsAsFactors=FALSE, \n\t\tfill=TRUE, \n\t\tcol.names=cn)\n\ncn &lt;- c(\"hmm\",\"hit_len\",\"protein_id\",\"query_len\",\"evalue\",\"hit_start\",\"hit_end\",\"query_start\",\"query_end\",\"cov\")\ndbcan &lt;- read.table(db.name, \n\t\tsep=\"\\t\", \n\t\theader=FALSE, \n\t\tstringsAsFactors=FALSE, \n\t\tcol.names=cn)\ndbcan$hmm &lt;- gsub(\".hmm\",\"\",dbcan$hmm)\n\n# GLOBAL VARIABLES\nPULCOUNTER &lt;- 1\nALLPULS &lt;- data.frame(protein_id=character(),\n\t\t      contig=character(),\n\t\t      start=numeric(),\n\t\t      end=numeric(),\n\t\t      strand=character(),\n\t\t      protein_name=character(),\n\t\t      order=numeric(), \n\t\t      active=character(), \n\t\t      sus=character(), \n\t\t      hmm=character(), \n\t\t      pulid=character(),\n\t\t      stringsAsFactors=FALSE)\n\n\n# filter pfam\nhalign_prop = (pfam$he - pfam$hs + 1) \/ pfam$hl\npfam &lt;- pfam&#91;halign_prop>=0.6,]\n\n# find and annotate susC\/susD\npfam$sus &lt;- rep(\"none\", nrow(pfam))\npfam$sus&#91;grep(\"PF00593\",pfam$ha)] &lt;- \"susC\"\npfam$sus&#91;grep(\"PF13715\",pfam$ha)] &lt;- \"susC\"\npfam$sus&#91;grep(\"PF07715\",pfam$ha)] &lt;- \"susC\"\npfam$sus&#91;grep(\"PF07980\",pfam$ha)] &lt;- \"susD\"\npfam$sus&#91;grep(\"PF12741\",pfam$ha)] &lt;- \"susD\"\npfam$sus&#91;grep(\"PF12771\",pfam$ha)] &lt;- \"susD\"\npfam$sus&#91;grep(\"PF14322\",pfam$ha)] &lt;- \"susD\"\n\n# limit pfam to relevant rows\npfam &lt;- pfam&#91;pfam$sus != \"none\",]\n\n# merge with feature table\nftp &lt;- merge(ft, pfam, by.x=\"protein_id\", by.y=\"protein_id\", all.x=TRUE, sort=FALSE)&#91;,c(1:7,22,23)]\nftp &lt;- unique(ftp)\nftp &lt;- ftp&#91;order(ftp$order),]\n\n# unique list of contigs\ncons &lt;- unique(ftp$contig)\n\n# go through each contig\nfor (c in cons&#91;order(cons)]) {\n\n\ttdf &lt;- ftp&#91;ftp$contig==c,]\n\ttdf &lt;- tdf\n\ttdf&#91;is.na(tdf)] &lt;- \"\"\n\n\ti &lt;- 1\n\twhile(i &lt;= nrow(tdf)) {\n\t#for (i in 1:nrow(tdf)) {\n\n\t\t#print(c)\n\t\t#print(nrow(tdf))\n\t\t#print(i)\n\t\tif (i>=nrow(tdf)) {\n\t\t\tbreak\n\t\t}\n\t\tif (tdf$sus&#91;i] == \"susC\" &amp;&amp; tdf$sus&#91;i+1] == \"susD\") {\n\t\t\t# we have a pair - do something\n\t\t\tprint(paste(\"pair at\",c,i))\n\t\t\ti &lt;- process_pairs(i,tdf)\n\t\t\tprint(i)\n\t\t}\n\n\t\tif (i>=nrow(tdf)) {\n                        break\n                } \n\n\t\tif (tdf$sus&#91;i] == \"susD\" &amp;&amp; tdf$sus&#91;i+1] == \"susC\") {\n\t\t\t# we have a pair - do something\n\t\t\tprint(paste(\"pair at\",c, i))\n\t\t\ti &lt;- process_pairs(i,tdf)\n\t\t\tprint(i)\n\t\t}\n\t\t\n\t\ti &lt;- i+1\n\t}\n\t\n}\n\nif (nrow(ALLPULS) >= 2) {\n\ttwo &lt;- ALLPULS$end&#91;1:(nrow(ALLPULS)-1)]\n\tone &lt;- ALLPULS$start&#91;2:nrow(ALLPULS)]\n\n\tALLPULS$dist &lt;- c(0,one - two)\n\n\tALLPULS$genome &lt;- rep(genome, nrow(ALLPULS))\n\n\tALLPULS &lt;- ALLPULS&#91;, c(\"genome\",\"pulid\",\"protein_id\",\"contig\",\"start\",\"end\",\"strand\",\"dist\",\"protein_name\",\"sus\",\"hmm\",\"active\")]\n\t\n\tALLPULS &lt;- ALLPULS&#91;order(ALLPULS$contig, ALLPULS$start),]\n\n\twrite.table(ALLPULS, file.out, col.names=TRUE, row.names=FALSE, quote=FALSE, sep=\"\\t\")\n\n} else {\n\tALLPULS$dist &lt;- numeric()\n\tfile.create(file.out)\n}\n\nif (nrow(ALLPULS) >= 2) {\n\n\tALLPULS$GENE &lt;- ALLPULS$hmm\n\tALLPULS$GENE&#91;ALLPULS$sus!=\"\"] &lt;- ALLPULS$sus&#91;ALLPULS$sus!=\"\"]\n\tALLPULS$GENE&#91;ALLPULS$GENE==\"\"] &lt;- \"unk\"\n\n\tagg &lt;- aggregate(ALLPULS$GENE, by=list(pulid=ALLPULS$pulid), paste, sep=\"-\", collapse=\"-\")\n\tuni &lt;- unique(ALLPULS&#91;,c(\"genome\",\"pulid\",\"contig\")])\n\tstart &lt;- aggregate(ALLPULS$start,  by=list(pulid=ALLPULS$pulid), function(x) return(x&#91;1]))\n\tend   &lt;- aggregate(ALLPULS$end,  by=list(pulid=ALLPULS$pulid), function(x) return(x&#91;length(x)]))\n\n\tpos &lt;- merge(start, end, by=\"pulid\")\n\tdeets &lt;- merge(uni, pos, by=\"pulid\")\n\n\tout &lt;- merge(deets,agg,by=\"pulid\")\n\n\tcolnames(out) &lt;- c(\"pulid\",\"genome\",\"contigid\",\"start\",\"end\",\"pattern\")\n\n\tout &lt;- out&#91;,c(\"genome\",\"pulid\",\"contigid\",\"start\",\"end\",\"pattern\")]\n\n\tout &lt;- out&#91;order(out$contigid, out$start),]\n\n\twrite.table(out, file.sum, col.names=TRUE, row.names=FALSE, quote=FALSE, sep=\"\\t\")\n} else {\n\tfile.create(file.sum)\n}\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-372","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=372"}],"version-history":[{"count":1,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":373,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions\/373"}],"wp:attachment":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}