-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_sql.R
132 lines (105 loc) · 4.5 KB
/
write_sql.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This script writes variant tables to MySQL database
# After generating initial tables, you need to uncomment to update existing tables.
library(data.table)
library(RMySQL)
library(dplyr)
mutagen<-fread("mutagen_cron.txt")
mouse<-fread("mouse_cron.txt")
celegans<-fread("celegans_cron.txt")
clinvar<-fread("clinvar_cron.txt")
topmed<-fread("topmed_cron.txt")
mouseall<-rbind(mutagen, mouse) %>%
distinct
mouseall$id<-row.names(mouseall)
mouseall$id<-as.numeric(mouseall$id)
con <- dbConnect(RMySQL::MySQL(), host = 'localhost', user = 'user', password = 'password', dbname = 'convart', port = 3306)
# dbSendQuery(con, 'DROP TABLE `mouse_variants_old`')
# dbSendQuery(con, 'RENAME TABLE `convart`.`mouse_variants_new`
# TO `convart`.`mouse_variants_old`;')
dbSendQuery(con, 'CREATE TABLE `convart`.`mouse_variants_new` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`variation_id` bigint(20) DEFAULT NULL,
`gene_name` varchar(255) DEFAULT NULL,
`phenotype` varchar(255) DEFAULT NULL,
`aa_change` varchar(255) DEFAULT NULL,
`pos` bigint(20) DEFAULT NULL,
`refseq_id` varchar(255) DEFAULT NULL,
`variant_type` varchar(255) DEFAULT NULL,
`variation` varchar(255) DEFAULT NULL,
`polyphen_score` varchar(255) DEFAULT NULL,
`predicted_effect` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;')
dbWriteTable(con, "mouse_variants_new", mouseall, row.names = FALSE, overwrite = FALSE, append = TRUE)
# C. elegans ----
celegans<-fread("celegans_cron.txt")
# dbSendQuery(con, 'DROP TABLE `celegans_variants_old`')
# dbSendQuery(con, 'RENAME TABLE `convart`.`celegans_variants`
# TO `convart`.`celegans_variants_old`;')
dbSendQuery(con, 'CREATE TABLE `convart`.`celegans_variants` (
`primary_id` int(11) NOT NULL,
`ids` varchar(255) DEFAULT NULL,
`pos` bigint(20) DEFAULT NULL,
`refseq_id` varchar(255) DEFAULT NULL,
`aa_change` varchar(255) DEFAULT NULL,
`gene_name` varchar(255) DEFAULT NULL,
`variant_type` varchar(255) DEFAULT NULL,
`phenotype` varchar(255) DEFAULT NULL,
`SIFT` varchar(255) DEFAULT NULL,
`PolyPhen` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
PRIMARY KEY (primary_id),
KEY `celegans_refseq_id_index` (`refseq_id`(191))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;')
dbWriteTable(con, "celegans_variants", celegans, row.names = FALSE, overwrite = FALSE, append = TRUE)
# Clinvar ----
clinvar<-fread("clinvar_cron.txt")
# dbSendQuery(con, 'DROP TABLE `clinvar_old`')
# dbSendQuery(con, 'RENAME TABLE `convart`.`clinvar`
# TO `convart`.`clinvar_old`;')
dbSendQuery(con, 'CREATE TABLE `convart`.`clinvar` (
`clinvar_id` bigint(20) NOT NULL,
`gene_id` bigint(20) DEFAULT NULL,
`allele_id` bigint(20) DEFAULT NULL,
`symbol` varchar(255) DEFAULT NULL,
`rs_number` varchar(255) DEFAULT NULL,
`rcv_accession` varchar(255) DEFAULT NULL,
`variation_id` bigint(20) DEFAULT NULL,
`variant_type` varchar(255) DEFAULT NULL,
`phenotype_ids` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`clinical_significance` varchar(255) DEFAULT NULL,
`review_status` varchar(255) DEFAULT NULL,
`phenotypes` varchar(255) DEFAULT NULL,
`nm_id` text,
`variation` varchar(255) DEFAULT NULL,
`position` bigint(20) DEFAULT NULL,
`np_id` varchar(255) DEFAULT NULL,
PRIMARY KEY (clinvar_id),
UNIQUE KEY `clinvar_id` (`clinvar_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;')
dbWriteTable(con, "clinvar", clinvar, row.names = FALSE, overwrite = FALSE, append = TRUE)
# Topmed ----
topmed<-fread("topmed_cron.txt")
# dbSendQuery(con, 'DROP TABLE `dbsnp`')
# dbSendQuery(con, 'RENAME TABLE `convart`.`dbsnp_new`
# TO `convart`.`dbsnp`;')
dbSendQuery(con, 'CREATE TABLE `convart`.`dbsnp_new` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`ensm_protein_id` varchar(255) DEFAULT NULL,
`rs_id` varchar(50) DEFAULT NULL,
`ensm_gene_id` varchar(255) DEFAULT NULL,
`ensm_transcript_id` varchar(80) DEFAULT NULL,
`variant_type` varchar(255) DEFAULT NULL,
`position` int(20) DEFAULT NULL,
`variation` varchar(50) DEFAULT NULL,
`sift_score` varchar(255) DEFAULT NULL,
`polyphen_score` varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY `canonical_transcript_2` (`rs_id`,`ensm_transcript_id`,`variation`) USING BTREE,
KEY `ensm_transcript_id` (`ensm_transcript_id`),
KEY `rs_id` (`rs_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;')
dbWriteTable(con, "dbsnp_new", topmed, row.names = FALSE, overwrite = FALSE, append = TRUE)
dbDisconnect(con)