-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnvAnalysis.R
1189 lines (1008 loc) · 72.6 KB
/
cnvAnalysis.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mysql_delete_tables <- function (con, sample_name, control_name){
drop_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_3_random_ref;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_reference;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_reference;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_cov;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_cov_oe;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_control;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_cov;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_cov;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_cov;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_tso_cnv;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_control;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_control;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_control;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_within_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_within_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_tso;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene_norm;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_pileup;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_bwa_pileup;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_bowtie_pileup;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_pileup;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bwa;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bowtie;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bwa_bowtie_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_pileup_bowtie_bwa;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS tso_",sample_name,"_window;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS tso_exon_60bp_segments_main_data_",sample_name,";",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_pileup_bowtie_bwa;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS tso_",control_name,"_window;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS tso_exon_60bp_segments_main_data_",control_name,";",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_heterozygous_mult;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_heterozygous_mult_oe;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_homozygous;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_ref1;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_ref2;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_ref3;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_gene_list;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_ordered_genes;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS data_",sample_name,";",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ref1_",sample_name,"_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ref2_",sample_name,"_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS ref3_",sample_name,"_ratio;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_tso_exon_60bp_segments_main_data;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_ordered_genes;",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_bowtie_pileup",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_bwa_pileup",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_bowtie",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_bwa",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_bwa_bowtie_ratio",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_exon_pileup",sep="");
drop <- dbGetQuery(con, drop_str);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_pileup",sep="");
drop <- dbGetQuery(con, drop_str);
}
mysql_get_raw_data<- function (con, sample_name, control_name){
drop_str1 <- paste("DROP TABLE IF EXISTS ",sample_name,"_gene_list;",sep="");
drop1 <- dbGetQuery(con, drop_str1);
create_table_sql1 <- paste("CREATE TABLE ",sample_name,"_gene_list AS SELECT DISTINCT gene_symbol FROM ",sample_name,"_tso_cnv UNION SELECT DISTINCT gene_symbol FROM cnv_",sample_name,"_ordered_genes;",sep="");
create_table1 <- dbGetQuery(con, create_table_sql1);
drop_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_tso_exon_60bp_segments_main_data;",sep="");
drop2 <- dbGetQuery(con, drop_str2);
create_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_tso_exon_60bp_segments_main_data AS SELECT A.* FROM tso_exon_60bp_segments_main_data A JOIN ",sample_name,"_gene_list B USING (gene_symbol);",sep="");
create_table2 <- dbGetQuery(con, create_table_sql2);
drop_str3 <- paste("DROP TABLE IF EXISTS ref1_",sample_name,"_ratio;",sep="");
drop3 <- dbGetQuery(con, drop_str3);
create_table_sql3 <- paste("CREATE TABLE ref1_",sample_name,"_ratio AS SELECT window_id, window_number, window_start, window_end, MIN(gene_symbol) AS gene_symbol, AVG(A_over_B_ratio) AS avg_ratio, MIN(bowtie_bwa_ratio) AS min_bowtie_bwa_ratio, MAX(bowtie_bwa_ratio) AS max_bowtie_bwa_ratio FROM (SELECT A.*, A_over_B_ratio, bowtie_bwa_ratio FROM cnv_",sample_name,"_tso_exon_60bp_segments_main_data A JOIN cnv_",sample_name,"_over_",control_name,"_ref1 B ON(A.chr = B.chr AND A.pos = B.pos)) C GROUP BY window_id;",sep="");
create_table3 <- dbGetQuery(con, create_table_sql3);
drop_str4 <- paste("DROP TABLE IF EXISTS ref2_",sample_name,"_ratio;",sep="");
drop4 <- dbGetQuery(con, drop_str4);
create_table_sql4 <- paste("CREATE TABLE ref2_",sample_name,"_ratio AS SELECT window_id, window_number, window_start, window_end, MIN(gene_symbol) AS gene_symbol, AVG(A_over_B_ratio) AS avg_ratio, MIN(bowtie_bwa_ratio) AS min_bowtie_bwa_ratio, MAX(bowtie_bwa_ratio) AS max_bowtie_bwa_ratio FROM (SELECT A.*, A_over_B_ratio, bowtie_bwa_ratio FROM cnv_",sample_name,"_tso_exon_60bp_segments_main_data A JOIN cnv_",sample_name,"_over_",control_name,"_ref2 B ON(A.chr = B.chr AND A.pos = B.pos)) C GROUP BY window_id;",sep="");
create_table4 <- dbGetQuery(con, create_table_sql4);
drop_str5 <- paste("DROP TABLE IF EXISTS ref3_",sample_name,"_ratio;",sep="");
drop5 <- dbGetQuery(con, drop_str5);
create_table_sql5 <- paste("CREATE TABLE ref3_",sample_name,"_ratio AS SELECT window_id, window_number, window_start, window_end, MIN(gene_symbol) AS gene_symbol, AVG(A_over_B_ratio) AS avg_ratio, MIN(bowtie_bwa_ratio) AS min_bowtie_bwa_ratio, MAX(bowtie_bwa_ratio) AS max_bowtie_bwa_ratio FROM (SELECT A.*, A_over_B_ratio, bowtie_bwa_ratio FROM cnv_",sample_name,"_tso_exon_60bp_segments_main_data A JOIN cnv_",sample_name,"_over_",control_name,"_ref3 B ON(A.chr = B.chr AND A.pos = B.pos)) C GROUP BY window_id;",sep="");
create_table5 <- dbGetQuery(con, create_table_sql5);
get_data_str <- paste("SELECT A1.*, B1.avg_ratio AS ref3_avg_ratio, B1.min_bowtie_bwa_ratio AS ref3_min_bowtie_bwa_ratio, B1.max_bowtie_bwa_ratio AS ref3_max_bowtie_bwa_ratio FROM (SELECT A.*, B.avg_ratio AS ref2_avg_ratio, B.min_bowtie_bwa_ratio AS ref2_min_bowtie_bwa_ratio, B.max_bowtie_bwa_ratio AS ref2_max_bowtie_bwa_ratio FROM ref1_",sample_name,"_ratio A JOIN ref2_",sample_name,"_ratio B ON(A.window_id = B.window_id)) A1 JOIN ref3_",sample_name,"_ratio B1 ON(A1.window_id = B1.window_id);",sep="");
data <- dbGetQuery(con, get_data_str);
# file_name <- paste(sample_name,"_raw_data.txt",sep="");
file_name <- "raw_data.txt";
write.table(data, file = file_name, sep = "\t", col.names = TRUE, quote=FALSE,row.names = F);
}
mysql_output_cnv <- function (con, sample_name){
get_data_str <- paste("SELECT capture, gene_symbol, type, avg_coverage, coverage_std_dev, bb_ratio_std_dev, median_abs_residual FROM ",sample_name,"_tso_cnv ORDER BY median_abs_residual;",sep="");
data <- dbGetQuery(con, get_data_str);
# file_name <- paste(sample_name,"_cnv.txt",sep="");
file_name <- "cnv.txt";
write.table(data, file = file_name, sep = "\t", col.names = TRUE, quote=FALSE,row.names = F);
}
plot_cnv_and_ordered_genes_bak <- function (con, sample_name, control_name){
get_data_str <- paste("SELECT gene_symbol FROM ",sample_name,"_tso_cnv UNION SELECT gene_symbol FROM cnv_",sample_name,"_ordered_genes;",sep="");
data <- dbGetQuery(con, get_data_str);
gene_list <- unique(data$gene_symbol);
n =length(gene_list);
data_table = paste("cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out",sep="");
for(i in 1:n){
gene = gene_list[i];
plot_label = paste(gene,"_",sample_name,"_noise_red_ratio",sep="");
image_name = paste(plot_label,".png",sep="");
png(image_name, width=23, height=6, units="in", res=600)
cnv_plot_all_bowtie_bwa(con, data_table, "pos","gene_symbol", gene, "A_over_B_ratio","bowtie_bwa_ratio",2.0,plot_label);
garbage <- dev.off ();
}
}
plot_cnv_and_ordered_genes <- function (con, sample_name, control_name, width_val,single_plot_height){
get_data_str <- paste("SELECT gene_symbol FROM ",sample_name,"_tso_cnv UNION SELECT gene_symbol FROM cnv_",sample_name,"_ordered_genes;",sep="");
data <- dbGetQuery(con, get_data_str);
gene_list <- unique(data$gene_symbol);
n =length(gene_list);
data_table = paste("cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out",sep="");
pdf('CoveragePlots.pdf', width=width_val, onefile=TRUE);
for(i in 1:n){
gene = gene_list[i];
plot_label = paste(gene,"_",sample_name,"_noise_red_ratio",sep="");
cnv_plot_all_bowtie_bwa(con, data_table, "pos","gene_symbol", gene, "A_over_B_ratio","bowtie_bwa_ratio",single_plot_height,plot_label);
}
garbage <- dev.off ();
}
mysql_load_ordered_genes <- function (con,sample_name, ordered_genes){
data = read.table(ordered_genes);
colnames(data)[1] <- 'gene_symbol'
ans <- data.frame(data);
drop_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_ordered_genes;",sep="");
drop <- dbGetQuery(con, drop_str);
out_table = paste("cnv_",sample_name,"_ordered_genes",sep="");
garbage <- dbWriteTable(con, out_table, ans, append=FALSE,row.names=FALSE);
}
mysql_combine_cnv <- function (con,sample_name, control_name, platform){
drop_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_tso_cnv;",sep="");
drop <- dbGetQuery(con, drop_str);
create_out_table_sql1 <- paste("CREATE TABLE ",sample_name,"_tso_cnv AS SELECT '",platform,"' AS capture, gene_symbol, type, avg_coverage, coverage_variance, coverage_std_dev, POWER(2,bb_ratio_variance) AS bb_ratio_variance, POWER(2,bb_ratio_std_dev) AS bb_ratio_std_dev, 100.0000 AS median_abs_residual FROM (SELECT gene_symbol, type, AVG(coverage) AS avg_coverage, VAR_SAMP(coverage) AS coverage_variance, STDDEV_SAMP(coverage) AS coverage_std_dev, VAR_SAMP(bowtie_bwa_ratio) AS bb_ratio_variance, STDDEV_SAMP(bowtie_bwa_ratio) AS bb_ratio_std_dev FROM (SELECT A1.*, coverage, LOG2(bowtie_bwa_ratio) AS bowtie_bwa_ratio FROM (SELECT DISTINCT A.gene_symbol, type, chr, pos FROM (SELECT * FROM cnv_",sample_name,"_heterozygous_mult UNION SELECT * FROM cnv_",sample_name,"_heterozygous_mult_oe) A JOIN tso_exon_60bp_segments_main_data B USING (gene_symbol)) A1 JOIN cnv_",sample_name,"_pileup_bowtie_bwa B1 ON(A1.chr = B1.chr AND A1.pos = B1.pos)) C GROUP BY gene_symbol) D UNION SELECT '",platform,"' AS capture, gene_symbol, type, avg_coverage, coverage_variance, coverage_std_dev, POWER(2,bb_ratio_variance) AS bb_ratio_variance, POWER(2,bb_ratio_std_dev) AS bb_ratio_std_dev, 100.0000 AS median_abs_residual FROM (SELECT gene_symbol, type, AVG(coverage) AS avg_coverage, VAR_SAMP(coverage) AS coverage_variance, STDDEV_SAMP(coverage) AS coverage_std_dev, VAR_SAMP(bowtie_bwa_ratio) AS bb_ratio_variance, STDDEV_SAMP(bowtie_bwa_ratio) AS bb_ratio_std_dev FROM (SELECT A1.*, coverage, LOG2(bowtie_bwa_ratio) AS bowtie_bwa_ratio FROM (SELECT DISTINCT A.gene_symbol, type, chr, pos FROM cnv_",sample_name,"_homozygous A JOIN tso_exon_60bp_segments_main_data B USING (gene_symbol)) A1 JOIN cnv_",control_name,"_pileup_bowtie_bwa B1 ON(A1.chr = B1.chr AND A1.pos = B1.pos)) C GROUP BY gene_symbol) D;",sep="");
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
}
mysql_get_cnv <- function (con,sample_name, control_name){
# Get heterozygous CNV from gene with more than one exon
cnv_gene_list <- c();
table = paste("cnv_",sample_name,"_over_",control_name,"_joint_cov",sep="");
out_table = paste("cnv_",sample_name,"_heterozygous_mult",sep="");
min_cnv_ratio = 0.3;
max_cnv_ratio = 0.7;
min_exons = 2;
min_windows = 3;
gene_symbol = mysql_table_cnv(con, table, min_cnv_ratio, max_cnv_ratio, min_exons, min_windows, cnv_gene_list);
type = rep("heterozygous",length(gene_symbol));
ans <- data.frame(gene_symbol,type);
drop_str <- paste("DROP TABLE IF EXISTS ",out_table,";",sep="");
drop <- dbGetQuery(con, drop_str);
dbWriteTable(con, out_table, ans, append=FALSE,row.names=FALSE);
# Get heterozygous CNV from gene with exon
cnv_gene_list <- c();
table = paste("cnv_",sample_name,"_over_",control_name,"_joint_cov_oe",sep="");
out_table = paste("cnv_",sample_name,"_heterozygous_mult_oe",sep="");
min_cnv_ratio = 0.3;
max_cnv_ratio = 0.7;
min_exons = 1;
min_windows = 3;
gene_symbol = mysql_table_cnv(con, table, min_cnv_ratio, max_cnv_ratio, min_exons, min_windows, cnv_gene_list);
type = rep("heterozygous",length(gene_symbol));
ans <- data.frame(gene_symbol,type);
drop_str <- paste("DROP TABLE IF EXISTS ",out_table,";",sep="");
drop <- dbGetQuery(con, drop_str);
garbage <- dbWriteTable(con, out_table, ans, append=FALSE,row.names=FALSE);
# Get homozygous CNV
cnv_gene_list <- c();
table = paste("cnv_",sample_name,"_over_",control_name,"_joint_control",sep="");
out_table = paste("cnv_",sample_name,"_homozygous",sep="");
min_cnv_ratio = 0;
max_cnv_ratio = 0.3;
min_exons = 0;
min_windows = 3;
gene_symbol = mysql_table_cnv(con, table, min_cnv_ratio, max_cnv_ratio, min_exons, min_windows, cnv_gene_list);
type = rep("homozygous",length(gene_symbol));
ans <- data.frame(gene_symbol,type);
drop_str <- paste("DROP TABLE IF EXISTS ",out_table,";",sep="");
drop <- dbGetQuery(con, drop_str);
garbage <- dbWriteTable(con, out_table, ans, append=FALSE,row.names=FALSE);
}
mysql_table_cnv <- function (con, table, min_cnv_ratio, max_cnv_ratio, min_exons, min_windows, cnv_gene_list){
get_data_str <- paste("SELECT DISTINCT gene_symbol,exon_number, window_id, window_number FROM ",table," WHERE cnv_ratio >= ",min_cnv_ratio," AND cnv_ratio < ",max_cnv_ratio," ORDER BY gene_symbol, window_number ASC;",sep="");
data <- dbGetQuery(con, get_data_str);
gene_list <- unique(data$gene_symbol);
n =length(gene_list);
for(i in 1:n){
gene_i_data <- data[data$gene_symbol == gene_list[i],]
exon_number_array = gene_i_data$exon_number
window_number_array = gene_i_data$window_number
if(mysql_check_cnv(exon_number_array, window_number_array, min_exons, min_windows)){
cnv_gene_list <- append(cnv_gene_list, gene_list[i])
}
}
return (cnv_gene_list);
}
mysql_check_cnv <- function (exon_number_array, window_number_array, min_exons, min_windows){
isCNV = FALSE;
n =length(window_number_array);
if((min_exons <= 1) & (min_windows <= 1) & (n == 1)){
isCNV = TRUE;
}
min_exons = min_exons - 1;
min_windows = min_windows - 1;
start_index = 1;
end_index = 1;
if(n > 1){
for(i in 2:n){
if((window_number_array[i] - window_number_array[i-1]) == 1){
end_index = i;
}else{
if((end_index - start_index) >= min_windows){
exons <- unique(exon_number_array[start_index:end_index]);
temp <- rle(diff(exons));
if(any(temp$lengths >= min_exons & temp$values==1)){
isCNV = TRUE;
break;
}
if(min_exons < 1){
isCNV = TRUE;
}
}
start_index = i;
end_index = i;
}
}
}
if((end_index - start_index) >= min_windows){
exons <- unique(exon_number_array[start_index:end_index]);
temp <- rle(diff(exons));
if(any(temp$lengths >= min_exons & temp$values==1)){
isCNV = TRUE;
}
if(min_exons < 1){
isCNV = TRUE;
}
}
return(isCNV);
}
mysql_cnv_filter <- function (con, sample_name, control_name, het_low_limit, het_high_limit, min_bb_ratio, max_bb_ratio, min_avg_cov){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_cov;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_joint_cov AS SELECT DISTINCT A1.* FROM (SELECT A.* FROM (SELECT DISTINCT * FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_cov WHERE min_bowtie_bwa_ratio > ",min_bb_ratio," AND max_bowtie_bwa_ratio < ",max_bb_ratio," AND cnv_ratio > ",het_low_limit," AND cnv_ratio < ",het_high_limit," AND avg_window_coverage > ",min_avg_cov," ) A JOIN (SELECT DISTINCT gene_symbol, gene_num_exons, exon_contig_id, window_id FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_cov WHERE min_bowtie_bwa_ratio > ",min_bb_ratio," AND max_bowtie_bwa_ratio < ",max_bb_ratio," AND cnv_ratio > ",het_low_limit," AND cnv_ratio < ",het_high_limit," AND avg_window_coverage > ",min_avg_cov,") B USING(window_id)) A1 JOIN (SELECT DISTINCT gene_symbol, gene_num_exons, exon_contig_id, window_id FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_cov WHERE min_bowtie_bwa_ratio > ",min_bb_ratio," AND max_bowtie_bwa_ratio < ",max_bb_ratio," AND cnv_ratio > ",het_low_limit," AND cnv_ratio < ",het_high_limit," AND avg_window_coverage > ",min_avg_cov,") B1 USING(window_id);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_cov_oe;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_joint_cov_oe AS SELECT * FROM cnv_",sample_name,"_over_",control_name,"_joint_cov WHERE gene_num_exons = 1;",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
del_str3 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_joint_control;",sep="");
create_out_table_sql3 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_joint_control AS SELECT DISTINCT A1.* FROM (SELECT A.* FROM (SELECT DISTINCT * FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_control WHERE cnv_ratio <= ",het_low_limit," AND avg_window_coverage > ",min_avg_cov," AND ref_min_bowtie_bwa_ratio > ",min_bb_ratio," AND ref_max_bowtie_bwa_ratio < ",max_bb_ratio,") A JOIN (SELECT DISTINCT gene_symbol, gene_num_exons, exon_contig_id, window_id FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_control WHERE cnv_ratio <= ",het_low_limit," AND avg_window_coverage > ",min_avg_cov," AND ref_min_bowtie_bwa_ratio > ",min_bb_ratio," AND ref_max_bowtie_bwa_ratio < ",max_bb_ratio,") B USING(window_id)) A1 JOIN (SELECT DISTINCT gene_symbol, gene_num_exons, exon_contig_id, window_id FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_control WHERE cnv_ratio <= ",het_low_limit," AND avg_window_coverage > ",min_avg_cov," AND ref_min_bowtie_bwa_ratio > ",min_bb_ratio," AND ref_max_bowtie_bwa_ratio < ",max_bb_ratio,") B1 USING(window_id);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
}
mysql_add_control_stats <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_control;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_control AS SELECT DISTINCT A.*, avg_window_coverage, ref_min_bowtie_bwa_ratio, ref_max_bowtie_bwa_ratio FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene A JOIN tso_exon_60bp_segments_main_data_",control_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table1_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_contr ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_control(window_id);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_control;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_control AS SELECT DISTINCT A.*, avg_window_coverage, ref_min_bowtie_bwa_ratio, ref_max_bowtie_bwa_ratio FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene A JOIN tso_exon_60bp_segments_main_data_",control_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table2_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_contr ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_control(window_id);",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_control;",sep="");
create_out_table_sql3 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_control AS SELECT DISTINCT A.*, avg_window_coverage, ref_min_bowtie_bwa_ratio, ref_max_bowtie_bwa_ratio FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene A JOIN tso_exon_60bp_segments_main_data_",control_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table3_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_contr ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_control(window_id);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
}
mysql_add_window_stats <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_cov;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_cov AS SELECT DISTINCT A.*, avg_window_coverage, window_coverage_std, window_bb_ratio_std FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene A JOIN tso_exon_60bp_segments_main_data_",sample_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table1_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_cov(window_id);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_cov;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_cov AS SELECT DISTINCT A.*, avg_window_coverage, window_coverage_std, window_bb_ratio_std FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene A JOIN tso_exon_60bp_segments_main_data_",sample_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table2_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_cov(window_id);",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_cov;",sep="");
create_out_table_sql3 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_cov AS SELECT DISTINCT A.*, avg_window_coverage, window_coverage_std, window_bb_ratio_std FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene A JOIN tso_exon_60bp_segments_main_data_",sample_name," B ON(A.window_id = B.window_id);",sep="");
create_out_table3_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_cov(window_id);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
}
mysql_compute_window_stats <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_pileup_bowtie_bwa;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_pileup_bowtie_bwa AS SELECT DISTINCT A.chr, A.pos, A.coverage, bowtie_bwa_ratio FROM cnv_",sample_name,"_exon_pileup A JOIN cnv_",sample_name,"_exon_bwa_bowtie_ratio B USING(chr,pos);",sep="");
create_out_table1_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_pileup_bowtie_bwa_i1 ON cnv_",sample_name,"_pileup_bowtie_bwa(chr,pos);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS tso_",sample_name,"_window;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE tso_",sample_name,"_window AS SELECT window_id, AVG(coverage) AS avg_window_coverage, VAR_SAMP(coverage) AS window_coverage_var, STDDEV_SAMP(coverage) AS window_coverage_std, MIN(bowtie_bwa_ratio) AS min_bowtie_bwa_ratio, MAX(bowtie_bwa_ratio) AS max_bowtie_bwa_ratio, VAR_SAMP(bowtie_bwa_ratio) AS window_bb_ratio_var, STDDEV_SAMP(bowtie_bwa_ratio) AS window_bb_ratio_std FROM (SELECT DISTINCT window_id, A.pos, coverage, bowtie_bwa_ratio FROM tso_exon_60bp_segments_main_data A JOIN cnv_",sample_name,"_pileup_bowtie_bwa B ON(A.chr = B.chr AND A.pos = B.pos)) A1 GROUP BY window_id;",sep="");
create_out_table2_index_sql <- paste("CREATE INDEX tso_",sample_name,"_window_i1 ON tso_",sample_name,"_window(window_id);",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS tso_exon_60bp_segments_main_data_",sample_name,";",sep="");
create_out_table_sql3 <- paste("CREATE TABLE tso_exon_60bp_segments_main_data_",sample_name," AS SELECT DISTINCT A.*, avg_window_coverage, window_coverage_var, window_coverage_std, min_bowtie_bwa_ratio, max_bowtie_bwa_ratio, window_bb_ratio_var, window_bb_ratio_std FROM tso_exon_60bp_segments_main_data A JOIN tso_",sample_name,"_window B USING(window_id);",sep="");
create_out_table3_index_sql <- paste("CREATE INDEX tso_exon_60bp_segments_main_data_",sample_name,"_i1 ON tso_exon_60bp_segments_main_data_",sample_name,"(window_id);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
del_str4 <- paste("DROP TABLE IF EXISTS cnv_",control_name,"_pileup_bowtie_bwa;",sep="");
create_out_table_sql4 <- paste("CREATE TABLE cnv_",control_name,"_pileup_bowtie_bwa AS SELECT DISTINCT A.chr, A.pos, A.coverage, bowtie_bwa_ratio FROM cnv_",control_name,"_exon_pileup A JOIN cnv_",control_name,"_exon_bwa_bowtie_ratio B USING(chr,pos);",sep="");
create_out_table4_index_sql <- paste("CREATE INDEX cnv_",control_name,"_pileup_bowtie_bwa_i1 ON cnv_",control_name,"_pileup_bowtie_bwa(chr,pos);",sep="");
drop4 <- dbGetQuery(con, del_str4);
create_out_table4 <- dbGetQuery(con, create_out_table_sql4);
create_out_table4_index <- dbGetQuery(con, create_out_table4_index_sql);
del_str5 <- paste("DROP TABLE IF EXISTS tso_",control_name,"_window;",sep="");
create_out_table_sql5 <- paste("CREATE TABLE tso_",control_name,"_window AS SELECT window_id, AVG(coverage) AS avg_window_coverage, VAR_SAMP(coverage) AS window_coverage_var, STDDEV_SAMP(coverage) AS window_coverage_std, MIN(bowtie_bwa_ratio) AS ref_min_bowtie_bwa_ratio, MAX(bowtie_bwa_ratio) AS ref_max_bowtie_bwa_ratio, VAR_SAMP(bowtie_bwa_ratio) AS window_bb_ratio_var, STDDEV_SAMP(bowtie_bwa_ratio) AS window_bb_ratio_std FROM (SELECT DISTINCT window_id, A.pos, coverage, bowtie_bwa_ratio FROM tso_exon_60bp_segments_main_data A JOIN cnv_",control_name,"_pileup_bowtie_bwa B ON(A.chr = B.chr AND A.pos = B.pos)) A1 GROUP BY window_id;",sep="");
create_out_table5_index_sql <- paste("CREATE INDEX tso_",control_name,"_window_i1 ON tso_",control_name,"_window(window_id);",sep="");
drop5 <- dbGetQuery(con, del_str5);
create_out_table5 <- dbGetQuery(con, create_out_table_sql5);
create_out_table5_index <- dbGetQuery(con, create_out_table5_index_sql);
del_str6 <- paste("DROP TABLE IF EXISTS tso_exon_60bp_segments_main_data_",control_name,";",sep="");
create_out_table_sql6 <- paste("CREATE TABLE tso_exon_60bp_segments_main_data_",control_name," AS SELECT DISTINCT A.*, avg_window_coverage, window_coverage_var, window_coverage_std, ref_min_bowtie_bwa_ratio, ref_max_bowtie_bwa_ratio, window_bb_ratio_var, window_bb_ratio_std FROM tso_exon_60bp_segments_main_data A JOIN tso_",control_name,"_window B USING(window_id);",sep="");
create_out_table6_index_sql <- paste("CREATE INDEX tso_exon_60bp_segments_main_data_",control_name,"_i1 ON tso_exon_60bp_segments_main_data_",control_name,"(window_id);",sep="");
drop1 <- dbGetQuery(con, del_str6);
create_out_table6 <- dbGetQuery(con, create_out_table_sql6);
create_out_table6_index <- dbGetQuery(con, create_out_table6_index_sql);
}
mysql_add_gene_data <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene AS SELECT DISTINCT A.*, exon_contig_id, exon_length, exon_number, gene_num_exons, gene_num_windows, window_number FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med A JOIN tso_exon_60bp_segments_window_data B ON(A.window_id = B.window_id);",sep="");
create_out_table1_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_med_gene(gene_symbol);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene AS SELECT DISTINCT A.*, exon_contig_id, exon_length, exon_number, gene_num_exons, gene_num_windows, window_number FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med A JOIN tso_exon_60bp_segments_window_data B ON(A.window_id = B.window_id);",sep="");
create_out_table2_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_med_gene(gene_symbol);",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene;",sep="");
create_out_table_sql3 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene AS SELECT DISTINCT A.*, exon_contig_id, exon_length, exon_number, gene_num_exons, gene_num_windows, window_number FROM cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med A JOIN tso_exon_60bp_segments_window_data B ON(A.window_id = B.window_id);",sep="");
create_out_table3_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_med_gene(gene_symbol);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
}
mysql_add_window_info <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1;",sep="");
create_out_table_sql1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1 AS SELECT DISTINCT A.*,ref_exon_contig_id, A_over_B_ratio, bowtie_bwa_ratio FROM tso_exon_60bp_segments_pileup A JOIN cnv_",sample_name,"_over_",control_name,"_ref1 B ON(A.chr = B.chr AND A.pos = B.pos);",sep="");
create_out_table1_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref1(window_id);",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2;",sep="");
create_out_table_sql2 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2 AS SELECT DISTINCT A.*,ref_exon_contig_id, A_over_B_ratio, bowtie_bwa_ratio FROM tso_exon_60bp_segments_pileup A JOIN cnv_",sample_name,"_over_",control_name,"_ref2 B ON(A.chr = B.chr AND A.pos = B.pos);",sep="");
create_out_table2_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref2(window_id);",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3;",sep="");
create_out_table_sql3 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3 AS SELECT DISTINCT A.*,ref_exon_contig_id, A_over_B_ratio, bowtie_bwa_ratio FROM tso_exon_60bp_segments_pileup A JOIN cnv_",sample_name,"_over_",control_name,"_ref3 B ON(A.chr = B.chr AND A.pos = B.pos);",sep="");
create_out_table3_index_sql <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3_1 ON cnv_",sample_name,"_over_",control_name,"_60bp_exon_ref3(window_id);",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
}
mysql_separate_ref <- function (con, sample_name, control_name){
input <- paste("cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out",sep="");
out_table1 <- paste("cnv_",sample_name,"_over_",control_name,"_ref1",sep="");
out_table2 <- paste("cnv_",sample_name,"_over_",control_name,"_ref2",sep="");
out_table3 <- paste("cnv_",sample_name,"_over_",control_name,"_ref3",sep="");
gene_ref_array_str <- paste("SELECT ref_exon_contig_id FROM (SELECT DISTINCT ref_exon_contig_id FROM ",input,")A ORDER BY RAND() LIMIT 3;",sep="");
gene_ref_array <- dbGetQuery(con, gene_ref_array_str);
gene_ref <- gene_ref_array[,1];
ref_exon1 <- gene_ref[1];
ref_exon2 <- gene_ref[2];
ref_exon3 <- gene_ref[3];
del_str1 <- paste("DROP TABLE IF EXISTS ",out_table1,";",sep="");
drop1 <- dbGetQuery(con, del_str1);
create_out_table_sql1 = paste("CREATE TABLE ",out_table1," AS SELECT * FROM ",input," WHERE ref_exon_contig_id = '",ref_exon1,"';",sep="");
create_out_table1 <- dbGetQuery(con, create_out_table_sql1);
create_out_table1_index_sql <- paste("CREATE INDEX ",out_table1,"_1 ON ",out_table1,"(chr,pos);",sep="");
create_out_table1_index <- dbGetQuery(con, create_out_table1_index_sql);
del_str2 <- paste("DROP TABLE IF EXISTS ",out_table2,";",sep="");
drop2 <- dbGetQuery(con, del_str2);
create_out_table_sql2 = paste("CREATE TABLE ",out_table2," AS SELECT * FROM ",input," WHERE ref_exon_contig_id = '",ref_exon2,"';",sep="");
create_out_table2 <- dbGetQuery(con, create_out_table_sql2);
create_out_table2_index_sql <- paste("CREATE INDEX ",out_table2,"_1 ON ",out_table2,"(chr,pos);",sep="");
create_out_table2_index <- dbGetQuery(con, create_out_table2_index_sql);
del_str3 <- paste("DROP TABLE IF EXISTS ",out_table3,";",sep="");
drop3 <- dbGetQuery(con, del_str3);
create_out_table_sql3 = paste("CREATE TABLE ",out_table3," AS SELECT * FROM ",input," WHERE ref_exon_contig_id = '",ref_exon3,"';",sep="");
create_out_table3 <- dbGetQuery(con, create_out_table_sql3);
create_out_table3_index_sql <- paste("CREATE INDEX ",out_table3,"_1 ON ",out_table3,"(chr,pos);",sep="");
create_out_table3_index <- dbGetQuery(con, create_out_table3_index_sql);
}
mysql_create_norm_out_table <- function (con, sample_name, control_name){
del_str1 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene_norm;",sep="");
del_str2 <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out;",sep="");
create_str1 <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene_norm AS SELECT * FROM cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene WHERE 1 > 2;",sep="");
create_str2 <- paste("CREATE TABLE cnv_",sample_name,"_tso_over_",control_name,"_n_bowtie_bwa_ratio_gene_out AS SELECT * FROM cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene WHERE 1 > 2;",sep="");
drop1 <- dbGetQuery(con, del_str1);
drop2 <- dbGetQuery(con, del_str2);
create1 <- dbGetQuery(con, create_str1);
create2 <- dbGetQuery(con, create_str2);
}
mysql_add_gene_symbol <- function (con, sample_name, control_name){
del_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene;",sep="");
create_str <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene AS SELECT DISTINCT A.*, gene_symbol FROM cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio A JOIN tso_exon_contig_pileup B ON(A.chr = B.chr AND A.pos = B.pos);",sep="");
index_str <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene_1 ON cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_gene(ref_exon_contig_id, gene_symbol);",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
index <- dbGetQuery(con, index_str);
}
mysql_add_bowtie_bwa <- function (con, sample_name, control_name){
del_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio;",sep="");
create_str <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio AS SELECT DISTINCT A.*, bowtie_bwa_ratio FROM cnv_",sample_name,"_over_",control_name,"_tso A JOIN cnv_",sample_name,"_exon_bwa_bowtie_ratio B ON(A.chr = B.chr AND A.pos = B.pos);",sep="");
index_str <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio_1 ON cnv_",sample_name,"_over_",control_name,"_n_bowtie_bwa_ratio(chr, pos);",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
index <- dbGetQuery(con, index_str);
}
mysql_compute_ratio <- function (con, sample_name, control_name){
del_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_over_",control_name,"_tso;",sep="");
create_str <- paste("CREATE TABLE cnv_",sample_name,"_over_",control_name,"_tso(chr VARCHAR(8),pos INT,ref_exon_contig_id VARCHAR(56),A_over_B_ratio DECIMAL(35,30));",sep="");
insert_data_str <- paste("INSERT INTO cnv_",sample_name,"_over_",control_name,"_tso(chr, pos, ref_exon_contig_id, A_over_B_ratio) SELECT DISTINCT A.chr, A.pos, A.ref_exon_contig_id, (A.within_ratio/B.within_ratio) AS A_over_B_ratio FROM cnv_",sample_name,"_exon_within_ratio A JOIN cnv_",control_name,"_exon_within_ratio B USING(chr, pos, ref_exon_contig_id);",sep="");
index_str <- paste("CREATE INDEX cnv_",sample_name,"_over_",control_name,"_1 ON cnv_",sample_name,"_over_",control_name,"_tso(chr, pos);",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
insert_data <- dbGetQuery(con, insert_data_str);
index <- dbGetQuery(con, index_str);
}
mysql_within_sample <- function (con, sample_name){
del_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_within_ratio;",sep="");
create_str <- paste("CREATE TABLE cnv_",sample_name,"_exon_within_ratio(exon_contig_id VARCHAR(56),chr VARCHAR(32),pos INT(11),coverage INT(11),ref_exon_contig_id VARCHAR(56),ref_chr VARCHAR(32),ref_pos INT(11),ref_coverage INT(11),within_ratio DECIMAL(20,16));",sep="");
insert_data_str <- paste("INSERT INTO cnv_",sample_name,"_exon_within_ratio(exon_contig_id,chr,pos,coverage,ref_exon_contig_id,ref_chr,ref_pos,ref_coverage,within_ratio) SELECT A.exon_contig_id, A.chr, A.pos, A.coverage, B.exon_contig_id AS ref_exon_contig_id, B.chr AS ref_chr, B.pos AS ref_pos, B.coverage AS ref_coverage, (A.coverage/B.coverage) AS within_ratio FROM cnv_",sample_name,"_exon_pileup A JOIN cnv_",sample_name,"_exon_reference B;",sep="");
index_str <- paste("CREATE INDEX cnv_",sample_name,"_exon_within_ratio_1 ON cnv_",sample_name,"_exon_within_ratio(chr,pos,ref_exon_contig_id );",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
insert_data <- dbGetQuery(con, insert_data_str);
index <- dbGetQuery(con, index_str);
}
mysql_create_reference <- function (con, sample_name){
del_str <- paste("DROP TABLE IF EXISTS ",sample_name,"_3_random_ref;",sep="");
create_str <- paste(" CREATE TABLE ",sample_name,"_3_random_ref AS SELECT DISTINCT A1.* FROM tso_reference A1 WHERE exon_contig_id = 'chr13:23904274-23915829' OR exon_contig_id = 'chr13:32910401-32915333' OR exon_contig_id = 'chr6:152655142-152655408';",sep="");
index1_str <- paste("CREATE INDEX ",sample_name,"_3_random_ref_i1 ON ",sample_name,"_3_random_ref(chr,pos);",sep="");
index2_str <- paste("CREATE INDEX ",sample_name,"_3_random_ref_i2 ON ",sample_name,"_3_random_ref(exon_contig_id);",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
index1 <- dbGetQuery(con, index1_str);
index2 <- dbGetQuery(con, index2_str);
}
mysql_load_pileup <- function (con, sample_name, data_path, load_type){
append_str <- "";
if(load_type == "pileup"){
append_str <- "";
}else if (load_type == "bwa"){
append_str <- "_bwa";
}else if (load_type == "bowtie"){
append_str <- "_bowtie";
}else{
stop("You need to specify a load type: pileup/bwa/bowtie")
}
del_str <- paste("DROP TABLE IF EXISTS cnv_",sample_name,append_str,"_pileup;",sep="");
create_str <- paste("CREATE TABLE cnv_",sample_name,append_str,"_pileup(chr VARCHAR(32), pos INT, coverage INT, mapped_reads INT);",sep="");
load_str <- paste("LOAD DATA LOCAL INFILE '",data_path,"' INTO TABLE cnv_",sample_name,append_str,"_pileup FIELDS TERMINATED BY '\t';",sep="");
index_str <- paste("CREATE INDEX cnv_",sample_name,append_str,"_pileup_i1 ON cnv_",sample_name,append_str,"_pileup(chr,pos);",sep="");
drop <- dbGetQuery(con, del_str);
create <- dbGetQuery(con, create_str);
load <- dbGetQuery(con, load_str);
index <- dbGetQuery(con, index_str);
}
mysql_exon_bwa_bowtie <- function (con, sample_name){
d1a <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_pileup;",sep="");
drop1a <- dbGetQuery(con, d1a);
q1a <- paste("CREATE TABLE cnv_",sample_name,"_exon_pileup AS ",sep="");
q2a <- "SELECT DISTINCT A.*, coverage FROM ";
q3a <- "tso_exon_contig_pileup A ";
q4a <- "JOIN ";
q5a <- paste("cnv_",sample_name,"_pileup B ",sep="");
q6a <- "USING(chr,pos); ";
load1a_str <- paste(q1a,q2a,q3a,q4a,q5a,q6a);
load1a <- dbGetQuery(con, load1a_str);
q7a <- paste("CREATE INDEX cnv_",sample_name,"_exon_pileup_1 ON cnv_",sample_name,"_exon_pileup(exon_contig_id);",sep="");
index1a <- dbGetQuery(con, q7a);
q8a <- paste("CREATE INDEX cnv_",sample_name,"_exon_pileup_2 ON cnv_",sample_name,"_exon_pileup(chr,pos);",sep="");
index2a <- dbGetQuery(con, q8a);
d1b <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bwa;",sep="");
drop1b <- dbGetQuery(con, d1b);
q1b <- paste("CREATE TABLE cnv_",sample_name,"_exon_bwa AS ",sep="");
q2b <- "SELECT DISTINCT A.*, coverage FROM ";
q3b <- "tso_exon_contig_pileup A ";
q4b <- "JOIN ";
q5b <- paste("cnv_",sample_name,"_bwa_pileup B ",sep="");
q6b <- "USING(chr,pos);";
load1b_str <- paste(q1b,q2b,q3b,q4b,q5b,q6b);
load1b <- dbGetQuery(con, load1b_str);
q7b <- paste("CREATE INDEX cnv_",sample_name,"_exon_bwa_1 ON cnv_",sample_name,"_exon_bwa(exon_contig_id);",sep="");
index1b <- dbGetQuery(con, q7b);
q8b <- paste("CREATE INDEX cnv_",sample_name,"_exon_bwa_2 ON cnv_",sample_name,"_exon_bwa(chr,pos);",sep="");
index2b <- dbGetQuery(con, q8b);
d1c <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bowtie;",sep="");
drop1c <- dbGetQuery(con, d1c);
q1c <- paste("CREATE TABLE cnv_",sample_name,"_exon_bowtie AS ",sep="");
q2c <- "SELECT DISTINCT A.*, coverage FROM ";
q3c <- "tso_exon_contig_pileup A ";
q4c <- "JOIN ";
q5c <- paste("cnv_",sample_name,"_bowtie_pileup B ",sep="");
q6c <- "USING(chr,pos); ";
load1c_str <- paste(q1c,q2c,q3c,q4c,q5c,q6c);
load1c <- dbGetQuery(con, load1c_str);
q7c <- paste("CREATE INDEX cnv_",sample_name,"_exon_bowtie_1 ON cnv_",sample_name,"_exon_bowtie(exon_contig_id);",sep="");
index1c <- dbGetQuery(con, q7c);
q8c <- paste("CREATE INDEX cnv_",sample_name,"_exon_bowtie_2 ON cnv_",sample_name,"_exon_bowtie(chr,pos);",sep="");
index2c <- dbGetQuery(con, q8c);
d1d <- paste("DROP TABLE IF EXISTS cnv_",sample_name,"_exon_bwa_bowtie_ratio;",sep="");
drop1d <- dbGetQuery(con, d1d);
q1d <- paste("CREATE TABLE cnv_",sample_name,"_exon_bwa_bowtie_ratio AS ",sep="");
q2d <- "SELECT DISTINCT A.chr, A.pos, (A.coverage/B.coverage) AS bowtie_bwa_ratio FROM ";
q3d <- paste("cnv_",sample_name,"_exon_bowtie A ",sep="");
q4d <- "JOIN ";
q5d <- paste("cnv_",sample_name,"_exon_bwa B ",sep="");
q6d <- "ON(A.chr = B.chr AND A.pos = B.pos);"
load1d_str <- paste(q1d,q2d,q3d,q4d,q5d,q6d);
load1d <- dbGetQuery(con, load1d_str);
q7d <- paste("CREATE INDEX cnv_",sample_name,"_exon_bwa_bowtie_ratio_1 ON cnv_",sample_name,"_exon_bwa_bowtie_ratio(chr,pos);",sep="");
index1d <- dbGetQuery(con, q7d);
}
piece.formula <- function(var.name, knots) {
# Code modified from http://rsnippets.blogspot.com/2013/04/estimating-continuous-piecewise-linear.html
formula.sign <- rep(" - ", length(knots))
formula.sign[knots < 0] <- " + "
paste(var.name, "+",
paste("I(pmax(", var.name, formula.sign, abs(knots), ", 0))",
collapse = " + ", sep=""))
}
cnv_lm_fit_gene_obsolete <- function (gene_symbol){
image_name = paste(gene_symbol,".png",sep="");
png(image_name, width=23, height=6, units="in", res=600)
get_ref <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",input_table," ORDER BY RAND() LIMIT 1;");
i_ref <- dbGetQuery(con, get_ref);
get_data <- paste("SELECT pos, A_over_B_ratio FROM ",input_table," WHERE ref_exon_contig_id = '",i_ref,"' AND gene_symbol = '",gene_symbol,"' ORDER BY pos ASC;",sep="");
i_data <- dbGetQuery(con, get_data);
n =length(i_data[,1]);
if(n < twice_seg_len){
K = 1;
}else{
K = ceiling(n/twice_seg_len);
}
x <- seq(1,n,1);
y = as.numeric(i_data[,2]);
plot(x, y, col="green", xlim=c(-1,n), ylim=c(-0.3,y_limit),xlab="chromosome position", ylab="ratio", title(main = gene_symbol));
abline(h=0.5,col = "blue");
abline(h=1,col = "blue", lty=2);
knots <- seq(min(x), max(x), len = K + 2)[-c(1, K + 2)];
model <- lm(formula(paste("y ~", piece.formula("x", knots))))
points(knots, predict(model, newdata = data.frame(x = knots)), col = "blue", pch = "o",cex=2)
y1 <- predict(model,newdata = data.frame(x));
points(x,y1,pch="*",col="black");
garbage <- dev.off ();
median_abs_residual = median(abs(y-y1));
update_residual_str <- paste(" UPDATE ",cnv_table," SET ",cnv_table,".median_abs_residual = ",median_abs_residual," WHERE gene_symbol = '",gene_symbol,"';",sep="");
update_res <- dbGetQuery(con, update_residual_str);
}
cnv_lm_fit_gene <- function (gene_symbol){
get_ref <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",input_table," ORDER BY RAND() LIMIT 1;");
i_ref <- dbGetQuery(con, get_ref);
get_data <- paste("SELECT pos, A_over_B_ratio FROM ",input_table," WHERE ref_exon_contig_id = '",i_ref,"' AND gene_symbol = '",gene_symbol,"' ORDER BY pos ASC;",sep="");
i_data <- dbGetQuery(con, get_data);
n =length(i_data[,1]);
if(n < twice_seg_len){
K = 1;
}else{
K = ceiling(n/twice_seg_len);
}
x <- seq(1,n,1);
y = as.numeric(i_data[,2]);
knots <- seq(min(x), max(x), len = K + 2)[-c(1, K + 2)];
model <- lm(formula(paste("y ~", piece.formula("x", knots))))
y1 <- predict(model,newdata = data.frame(x));
median_abs_residual = median(abs(y-y1));
update_residual_str <- paste(" UPDATE ",cnv_table," SET ",cnv_table,".median_abs_residual = ",median_abs_residual," WHERE gene_symbol = '",gene_symbol,"';",sep="");
update_res <- dbGetQuery(con, update_residual_str);
}
cnv_lm_fit <- function (con, input_table, cnv_table, twice_seg_len){
gene_symbol_str <- paste("SELECT DISTINCT gene_symbol FROM ",cnv_table,";",sep="");
gene_symbol_array <- dbGetQuery(con, gene_symbol_str);
gene_symbol_list <- gene_symbol_array[,1];
x <- lapply(X=gene_symbol_list, FUN=cnv_lm_fit_gene);
}
cnv_median_window_coverage <- function (con, sample_table_name, output_table_name){
drop_table_str <- paste("DROP TABLE IF EXISTS ",output_table_name,";",sep="");
drop_table <- dbGetQuery(con, drop_table_str);
create_table_str <- paste("CREATE TABLE ",output_table_name," AS SELECT gene_symbol, ref_exon_contig_id, window_id, -10000.000001 AS min_bowtie_bwa_ratio,
-0.000001 AS max_bowtie_bwa_ratio, -10000.01 AS cnv_ratio FROM ",sample_table_name," WHERE 1 > 2;",sep="");
create_table <- dbGetQuery(con, create_table_str);
group_by_array_str <- paste("SELECT CONCAT(ref_exon_contig_id,'.',window_id) AS vals FROM (SELECT DISTINCT ref_exon_contig_id, window_id FROM ",sample_table_name,") A;",sep="");
group_by_array <- dbGetQuery(con, group_by_array_str);
group_by_vals <- group_by_array[,1];
x <- lapply(X=group_by_vals, FUN=cnv_window_coverage);
index_str <- paste("CREATE INDEX ",output_table_name,"_1 ON ",output_table_name,"(window_id);",sep="");
output_index <- dbGetQuery(con, index_str);
}
cnv_window_coverage <- function (vals){
vals_vector <- unlist(strsplit(vals, "[.]"));
ref_exon_contig_id_val <- vals_vector[1];
window_id_val <- vals_vector[2];
window_coverage_str = paste("SELECT DISTINCT gene_symbol, A_over_B_ratio, bowtie_bwa_ratio FROM ",sample_table_name," WHERE ref_exon_contig_id = '",
ref_exon_contig_id_val,"' AND window_id ='",window_id_val,"' AND A_over_B_ratio IS NOT NULL AND bowtie_bwa_ratio IS NOT NULL;",sep="");
window_coverage_data <- dbGetQuery(con, window_coverage_str);
if(length(window_coverage_data) > 0){
gene_symbol_val = window_coverage_data[1,1];
window_coverage <- as.numeric(window_coverage_data[,2]);
window_bowtie_bwa <- as.numeric(window_coverage_data[,3]);
cnv_ratio_val <- median(window_coverage);
min_bowtie_bwa_ratio_val <- min(window_bowtie_bwa);
max_bowtie_bwa_ratio_val <- max(window_bowtie_bwa);
insert_query_str <- paste("INSERT INTO ",output_table_name,"(gene_symbol,ref_exon_contig_id,window_id,cnv_ratio,min_bowtie_bwa_ratio,max_bowtie_bwa_ratio) VALUES('",gene_symbol_val,"','",ref_exon_contig_id_val,"','",window_id_val,"',",cnv_ratio_val,",",min_bowtie_bwa_ratio_val,",",max_bowtie_bwa_ratio_val,");",sep="");
insert_query <- dbGetQuery(con, insert_query_str);
}
}
cnv_normalize <- function (con, norm_input,norm_output){
ref_array_str <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",norm_input,";",sep="");
ref_array <- dbGetQuery(con, ref_array_str);
ref <- ref_array[,1];
x <- lapply(X=ref, FUN=cnv_normalize_one_ref);
index_str <- paste("CREATE INDEX ",norm_output,"_i ON ",norm_output,"(ref_exon_contig_id, gene_symbol);",sep="");
norm_output_index <- dbGetQuery(con, index_str);
}
cnv_normalize_one_ref <- function(ref){
panel_cov_str = paste("SELECT A_over_B_ratio FROM ",norm_input," WHERE ref_exon_contig_id = '",ref,"' AND A_over_B_ratio > 0.5 AND A_over_B_ratio < 2 AND bowtie_bwa_ratio > 0.8 AND bowtie_bwa_ratio < (10/8);",sep="");
panel_cov = dbGetQuery(con, panel_cov_str);
ratio = as.numeric(panel_cov[,1]);
avg_log2 = mean(log2(ratio));
data_values_str = paste("SELECT chr,pos,ref_exon_contig_id,A_over_B_ratio,bowtie_bwa_ratio,gene_symbol FROM ",norm_input," WHERE ref_exon_contig_id = '",ref,"';",sep="");
data_values = dbGetQuery(con,data_values_str);
data_values[,4] <- 2^(log2(as.numeric(data_values[,4])) - avg_log2);
ans <- data.frame(data_values);
dbWriteTable(con, norm_output, ans, append=TRUE,row.names=FALSE);
}
cnv_smooth_gene <- function(gene_ref){
gene_ref_vector <- unlist(strsplit(gene_ref, "[.]"));
ref <- gene_ref_vector[1];
gene <- gene_ref_vector[2];
input_table_str <- paste("SELECT DISTINCT gene_symbol, ref_exon_contig_id , chr, pos, A_over_B_ratio, bowtie_bwa_ratio FROM ",
out_input," WHERE ref_exon_contig_id = '",ref,"' AND gene_symbol ='",gene,"' ORDER BY chr, pos ASC;",sep="");
input_table <- dbGetQuery(con, input_table_str);
gene_symbol <- input_table[,1];
ref_exon_contig_id <- input_table[,2];
chr <- input_table[,3];
pos <- input_table[,4];
gene_length <- length(pos);
if(window_length >= (gene_length/4)){
window_length = round(gene_length/4);
}
A_over_B_ratio <- rollmean(input_table[,5],window_length,na.pad=TRUE,na.fill="extend",align=c("left"));
A_over_B_ratio[is.na(A_over_B_ratio)] <- A_over_B_ratio[max(which(!is.na(A_over_B_ratio)))];
bowtie_bwa_ratio <- rollmean(input_table[,6],window_length,na.pad=TRUE,na.fill="extend",align=c("left"));
bowtie_bwa_ratio[is.na(bowtie_bwa_ratio)] <- bowtie_bwa_ratio[max(which(!is.na(bowtie_bwa_ratio)))];
output = cbind(gene_symbol,ref_exon_contig_id,chr,pos,A_over_B_ratio,bowtie_bwa_ratio);
ans <- data.frame(output);
dbWriteTable(con, out_output, ans, append=TRUE,row.names=FALSE);
}
cnv_smooth_coverages<- function (con, out_input, out_output, window_length){
gene_ref_array_str <- paste("SELECT CONCAT(ref_exon_contig_id,'.',gene_symbol) AS gene_ref FROM (SELECT DISTINCT ref_exon_contig_id,gene_symbol FROM ",out_input,") A;",sep="");
gene_ref_array <- dbGetQuery(con, gene_ref_array_str);
gene_ref <- gene_ref_array[,1];
x <- lapply(X=gene_ref, FUN=cnv_smooth_gene);
out_index_str <- paste("CREATE INDEX ",out_output,"_i ON ",out_output,"(ref_exon_contig_id, gene_symbol);",sep="");
out_output_index <- dbGetQuery(con, out_index_str);
}
cnv_plot_all_screen <- function (con, input_table, pos, filter_column1, filter_value1, data_column1, y_limit, title_label){
get_ref_exon_contig_id <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"';",sep="");
i_ref_exon <- dbGetQuery(con, get_ref_exon_contig_id);
n1 =length(i_ref_exon[,1]);
for(j in 1:n1){
filter_value2 = i_ref_exon[j,1]
get_data <- paste("SELECT DISTINCT ",pos,", ",data_column1," FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"' AND ref_exon_contig_id = '",
filter_value2,"' ORDER BY ",pos," ASC;",sep="");
i_data <- dbGetQuery(con, get_data);
n =length(i_data[,1]);
num_exon = 0;
if (length(i_data) < 1){
# empty array
}else{
n =length(i_data[,1]);
if(j == 1){
dev.new(width=11, height=6);
plot(0, 0.07,pch = "o", col="blue", xlim=c(-0.08,n), ylim=c(-0.08,y_limit),xlab="chromosome position", ylab="ratio", title(main = title_label));
abline(h=0.5,col = "blue");
abline(h=0.4,col = "blue");
abline(h=0.3,col = "blue");
abline(h=0.2,col = "blue");
abline(h=0.1,col = "blue");
abline(h=1,col = "blue", lty=2)
textxy(0, 0, num_exon);
}
for(i in 2:n){
data_value1 = as.numeric(i_data[i,2]);
data_value2 = as.numeric(i_data[i,3]);
points(i, data_value1, pch = "+", col=j);
if((as.numeric(i_data[i,1]) - as.numeric(i_data[(i-1),1])) > 1){
num_exon = num_exon + 1;
textxy(i, 0, num_exon);
points(i,0.07, pch = "o", col="blue");
}
}
}
}
}
cnv_plot_all <- function (con, input_table, pos, filter_column1, filter_value1, data_column1, y_limit, title_label, dir_path){
image_name = paste(title_label,".png",sep="");
setwd(dir_path);
png(image_name, width=23, height=6, units="in", res=600)
get_ref_exon_contig_id <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"';",sep="");
i_ref_exon <- dbGetQuery(con, get_ref_exon_contig_id);
n1 =length(i_ref_exon[,1]);
for(j in 1:n1){
filter_value2 = i_ref_exon[j,1]
get_data <- paste("SELECT DISTINCT ",pos,", ",data_column1," FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"' AND ref_exon_contig_id = '",
filter_value2,"' ORDER BY ",pos," ASC;",sep="");
i_data <- dbGetQuery(con, get_data);
n =length(i_data[,1]);
num_exon = 0;
if (length(i_data) < 1){
# empty array
}else{
n =length(i_data[,1]);
if(j == 1){
plot(0, 0.07, pch = "o", col="blue", xlim=c(-0.08,n), ylim=c(-0.08,y_limit),xlab="chromosome position", ylab="ratio", title(main = title_label));
abline(h=0.5,col = "blue");
abline(h=0.4,col = "blue");
abline(h=0.3,col = "blue");
abline(h=0.2,col = "blue");
abline(h=0.1,col = "blue");
abline(h=1,col = "blue", lty=2)
textxy(0, 0, num_exon);
}
for(i in 2:n){
data_value1 = as.numeric(i_data[i,2]);
data_value2 = as.numeric(i_data[i,3]);
points(i, data_value1, pch = "+", col=j);
if((as.numeric(i_data[i,1]) - as.numeric(i_data[(i-1),1])) > 1){
num_exon = num_exon + 1;
textxy(i, 0, num_exon);
points(i,0.07, pch = "o", col="blue");
}
}
}
}
garbage <- dev.off ();
}
cnv_plot_all_bowtie_bwa <- function (con, input_table, pos, filter_column1, filter_value1, data_column1, bowtie_bwa_ratio, y_limit, title_label){
get_ref_exon_contig_id <- paste("SELECT DISTINCT ref_exon_contig_id FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"';",sep="");
i_ref_exon <- dbGetQuery(con, get_ref_exon_contig_id);
n1 =length(i_ref_exon[,1]);
for(j in 1:n1){
filter_value2 = i_ref_exon[j,1]
get_data <- paste("SELECT DISTINCT ",pos,", ",data_column1,",",bowtie_bwa_ratio," FROM ",input_table," WHERE ",filter_column1," = '",filter_value1,"' AND ref_exon_contig_id = '",
filter_value2,"' ORDER BY ",pos," ASC;",sep="");
i_data <- dbGetQuery(con, get_data);
n =length(i_data[,1]);
num_exon = 0;
xmin = min(as.numeric(i_data[,1]));
xmax = max(as.numeric(i_data[,1]));
x_min = xmin-5;
x_max = xmax+5;
if (length(i_data) < 1){
# empty array
}else{
n =length(i_data[,1]);
if(j == 1){
# plot(0, 0.07, pch = "o", col="blue", xlim=c(-1,n), ylim=c(-0.3,y_limit),xlab="chromosome position", ylab="ratio", title(main = title_label));