-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcytoscript_orig.R
5406 lines (4406 loc) · 206 KB
/
cytoscript_orig.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
##setwd("G:/My Drive/BRB work/cyto_project/Cytogenetic software/R/")
if(require("stringr")){
print("stringr is loaded correctly")
} else {
print("trying to install stringr")
install.packages("stringr")
if(require("stringr")){
print("stringr installed and loaded")
} else {
stop("could not install stringr")
}
}
if(require("DescTools")){
print("DescTools is loaded correctly")
} else {
print("trying to install DescTools")
install.packages("DescTools")
if(require("DescTools")){
print("DescTools installed and loaded")
} else {
stop("could not install DescTools")
}
}
if(require("stringi")){
print("stringi is loaded correctly")
} else {
print("trying to install stringr")
install.packages("stringi")
if(require("stringi")){
print("stringi installed and loaded")
} else {
stop("could not install stringi")
}
}
if(require("dplyr")){
print("dplyr is loaded correctly")
} else {
print("trying to install dplyr")
install.packages("dplyr")
if(require("dplyr")){
print("dplyr installed and loaded")
} else {
stop("could not install dplyr")
}
}
if(require("hash")){
print("hash is loaded correctly")
} else {
print("trying to install hash")
install.packages("hash", repos='https://mirrors.nics.utk.edu/cran')
if(require("hash")){
print("hash installed and loaded")
} else {
stop("could not install hash")
}
}
##takes first option for or
## assumes order of abberations is from top to bottom
##assumes all output goes top to bottom (eg no q20p23)
#' CytoConverter Function
#'
#' This function accepts string or matrix input. Strings must be karyotypes and tables must have sample name in the first column and karyotype in the second column. The function outputs a table
#' @param in_data
#' @param constitutional
#' @param build
#' @param guess
#' @param guess_q
#' @param guess_by_first_val
#' @param forMtn
#' @param orOption
#' @keyword
#' @export
#' @examples
#' CytoConverter()
CytoConverter<-function(in_data,build="GRCh38",constitutional=T,guess=F,guess_q=F,guess_by_first_val=F,forMtn=T,orOption=T,sexstimate=F,complexSexEstimate=F,allow_Shorthand=F){
##if for dr. mitelman, overide parameters
#if(forMtn==T)
# {
# constitutional=F
# quess_q=F
# guess=T
##dont make assumptions about sex or constitutional sex chromosomes if there is a XX? or X? or ? in sex chromosome field
# sexstimate=F
##more complex handelintg pf sex choromsomes in clones and triploidy up
# complexSexEstimate=T
# allow_Shorthand=F
# }
##reference file to load
##specific loc
if(build =="GRCh38")
{
Cyto_ref_table <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_GRCh38.txt", header = FALSE)
), as.character)
}else if(build =="hg19"){
Cyto_ref_table <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg19.txt", header = FALSE)
), as.character)
}else if(build =="hg18"){
Cyto_ref_table <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg18.txt", header = FALSE)
), as.character)
}else if(build=="hg17"){
Cyto_ref_table <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg17.txt", header = FALSE)
), as.character)
}else if(is.null(build))
{
##default is grch38
Cyto_ref_table <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_GRCh38.txt", header = FALSE)
), as.character)
}else{
return("Error : build incorrectly specified")
}
##reference object for end of the string
ref_table <-as.data.frame(Cyto_ref_table[sapply(unique(Cyto_ref_table[,1]),function(x){grep(x,Cyto_ref_table[,1])[length(grep(paste(x,"$",sep=""),Cyto_ref_table[,1]))]}),][,c(1,3)])
ref_table<-apply(ref_table,2,as.character)
##table with desired output
Final_table <- matrix(ncol = 5, nrow = 0)
colnames(Final_table) <- c("Sample ID", "Chr", "Start", "End", "Type")
##convert any single string into a table
if(is.vector(in_data))
{
in_data <- t(matrix(c("sample", in_data)))
}
##dump table of stuff containing unprocessed reads
##Write this later
##get all fish recorded
Dump_table <- matrix(ncol = 3, nrow = 0)
##double check that this does not delete later data potentially
fish_table <- in_data[grep("ish.*$", in_data[, 2]), ]
if (is.vector(fish_table) & length(fish_table)>0)
{
Dump_table <- rbind(Dump_table, c(fish_table, "Warning in fish reading"))
##now take out ish readings
in_data[, 2] <- gsub("ish.*$", "", as.character(in_data[, 2]))
}else {
Dump_table <- rbind(Dump_table, cbind(fish_table, "Warning in fish reading"))
##now take out ish readings
in_data[, 2] <- gsub("ish.*$", "", as.character(in_data[, 2]))
}
##set everything to lowercase, then set x and y to uppercase
in_data[, 2] <- tolower(in_data[, 2])
in_data[, 2] <- chartr("x", "X", in_data[, 2])
in_data[, 2] <- chartr("y", "Y", in_data[, 2])
Con_data = matrix(nrow = 0, ncol = 2)
##spliting cell lines
for (i in 1:nrow(in_data))
{
##if activated, allow for shorthand outside of clones and outside of translocations eg. del(1), or if its a clone
if(!is.na(in_data[i, 2]) && nchar(as.character(in_data[i, 2])) > 0 && (allow_Shorthand == T || grepl("/",in_data[i,2]))){
##string split this up
data_split<-unlist(strsplit(in_data[i, 2], ","))
data_split<-unlist(strsplit(gsub("/","/-;-;opj",data_split),"/"))
mut_index<-grep("\\(.*\\)(?!\\()",data_split,perl=T)
if(length(mut_index)>0)
{
mut_list<-sapply(data_split[mut_index],function(x){gsub("\\[|\\]","",gsub("\\)","\\\\)",gsub("\\(","\\\\(",gsub("\\?","\\\\?",gsub("\\+","",substr(x,2,nchar(x)) )))))})
index_match<-sapply(mut_list,function(x){grep(x,data_split)[1]})
index_match<-index_match[intersect(which(!is.na(index_match)),which(index_match>0))]
if(is.numeric(mut_index) && is.numeric(index_match)&& length(index_match)>0)
{
additional_to_add <- substring(names(index_match),0,regexpr("\\+",names(index_match)) )
data_split[mut_index]<-paste(additional_to_add ,data_split[index_match],sep="")
}
}
in_data[i,2]<-gsub(",-;-;opj","/",paste(data_split,collapse=","))
}
if (!is.na(in_data[i, 2]) & nchar(as.character(in_data[i, 2])) > 0)
{
c2 <- strsplit(in_data[i, 2], split = "/")[[1]]
c1 <- paste(in_data[i, 1], 1:length(c2), sep = "_")
Con_data <- rbind(Con_data, cbind(c1, c2))
} else{
Con_data <-
rbind(Con_data, cbind(paste(in_data[i, 1], "_1"), in_data[i, 2]))
}
##make idems here
if (any(grepl("idem|sl|sdl", Con_data[, 2])))
{
idem_index <- grep("idem|sl|sdl", Con_data[, 2])
temp_data <-
Con_data[(idem_index[1] - 1):idem_index[length(idem_index)], ]
if(!is.vector(temp_data) && nrow(temp_data)>1)
{
for (j in 2:nrow(temp_data))
{
##make sure temp data has more than 1 row
if (grepl("idem|sl", temp_data[j, 2]))
{
prev <- unlist(strsplit(temp_data[1, 2], "\\["))[1]
} else{
##implement this so it can handel two sl1 in sucession and sdl1 sdl2
prev <- unlist(strsplit(temp_data[j - 1, 2], "\\["))[1]
}
prev <- unlist(strsplit(prev, ","))
prev <- prev[2:length(prev)]
sexchromprev <- prev[grep("^[XY]+", prev)[1]]
autochromprev <- prev[grep("^[XY]+", prev, invert = T)]
clonecount = 1
if (grepl("idemx|idemX|slx|slX|sdl*x|sdl*X", temp_data[j, 2]))
{
##check if there is a X3 etc value , if there is pick that up, store, add to addtot, make it process through twice later
clonecount = unlist(strsplit(temp_data[j, 2], "idemx|idemX|slx|slX"))[2]
if (grepl("-|~", clonecount))
{
clonecount <- unlist(strsplit(clonecount, "~|-"))[1]
}
clonecount = as.numeric(unlist(strsplit(
as.character(clonecount), "\\[|,"
))[1])
}
cur <- unlist(strsplit(temp_data[j, 2], ","))
if (!is.na(sexchromprev))
{
if (grepl("^[XY]+", cur[2]))
{
cur[2] <-
paste(paste(rep(sexchromprev, clonecount), collapse = ''), cur[2], sep =
'')
} else if (grepl("idem|sl|sdl", cur[2]))
{
cur[2] <- paste(rep(sexchromprev, clonecount), collapse = '')
} else{
cur <-
c(cur[1], paste(rep(sexchromprev, clonecount), collapse = ''), cur[2:length(cur)])
}
}
if (!is.null(autochromprev))
{
if (length(cur) > 2)
{
cur <- c(cur[1:2], rep(autochromprev, clonecount), cur[3:length(cur)])
} else
{
cur <- c(cur[1:2], rep(autochromprev, clonecount))
}
}
cur <- cur[grep("idem|sl|sdl", cur, invert = T)]
temp_data[j, 2] <- paste(c(cur, "ids"), sep = '', collapse = ',')
}
Con_data[(idem_index[1] - 1):idem_index[length(idem_index)], ] <-
temp_data
}else{
##put it in the error table
Dump_table<-rbind(Dump_table, c(temp_data, "no proceeding cell line to refer to"))
}
}
}
rownames(Con_data)<-1:nrow(Con_data)
Con_data[, 2] <- gsub(" ", "", Con_data[, 2])
##any rejoins will be ;; now
##actually lets depricate this
##Con_data[, 2] <- gsub(":", ";", Con_data[, 2])
Con_data[, 2] <- gsub("crYp", "", Con_data[, 2])
## taking all reads with ? and ~ as well into dump table
##adding marker and add material as well
unsure_table <- Con_data[grep("\\?|\\~|inc|mar|add", Con_data[, 2]), ]
Dump_table <- if(is.vector(unsure_table)){
rbind(Dump_table, c(unsure_table, "Warning in ?,~,marker, unknown additional material or incomplete karyotype detected"))
}else{
rbind(Dump_table, cbind(unsure_table, "Warning in ?,~, marker, unknown additional material or incomplete karyotype detected"))
}
##test con data group
##Con_data<-rbind(Con_data[521,],Con_data[525,],Con_data[632,],Con_data[701,],Con_data[781,],Con_data[788,],Con_data[76,],Con_data[138,],Con_data[483,],Con_data[5,],Con_data[6,],Con_data[10,])
##Con_data[621,],
##position match function, given chromosome and position, matches to UCSC table
##for a row
##take compliment of genomic coordinates
##convert to genomic coordinates (used in parser several times)
##function for getting cytobands for translocations and insertions (taking compliment of stuff not included in discription)
getCytoBands <- function(lengthcount, o, temp,coln,derMods) {
##must take into account acen and qter pter and only one listing (will have to relte to two), reuse later code for this
chr_table <-
Cyto_ref_table[grep(paste(paste("chr", temp[[(lengthcount * 2-1)]][o], sep =
""), "$", sep = ""), Cyto_ref_table), ]
##for isoderivative chromosomes, end point is potentially different, make boolean now
isiso=FALSE
##quit if karyotype returns false
earlyReturn=F
if(any(grepl("ider",Cyto_sample[coln])&grepl("t\\(",Cyto_sample[coln])))
{
isiso=TRUE
arm<-gsub("[[:digit:]]","",temp[[(grep("ider",derMods))+1]])
if(arm=="q")
{
unused="p"
}
if(arm=="p")
{
unused="q"
}
}
currentvec <- vector()
# JP: if (any(grepl("::", temp[[lengthcount * 2]][i]) |
# JP: grepl("~>", temp[[lengthcount * 2]][i]) | grepl("->", temp[[lengthcount * 2]][i]) )) {
if (any(grepl("::", temp[[lengthcount * 2]][o]) |
grepl("~>", temp[[lengthcount * 2]][o]) | grepl("->", temp[[lengthcount * 2]][o]) )) {
#parse data according to ::, in front of p and q are chromosomes, if qter or pter, do stuff, afterward is position, make table of things included, then make list of stuff excluded
##ask tom about this one
##find p or q, take stuff before take stuff after, before is chromosomes after is positions, this will return 2 objects, must take into account
##parse data according to ::, in front of p and q are chromosomes, if qter or pter, do stuff, afterward is position, make table of things included, then make list of stuff excluded
##ask tom about this one
##only splits first one
longform_table <-
strsplit(strsplit(temp[[lengthcount * 2]][o], "::")[[1]], "(~>)|(->)")
##take away any front loaded :
longform_table <-lapply(longform_table,function(x){gsub(':','',x)})
in_table = data.frame()
##mark for dic, trc
if(grepl("dic|trc",derMods[lengthcount]))
{
addBool<-paste("long",addBool,sep='')
}
##get data for each read of something->something
for (j in 1:length(longform_table))
{
stringdx <- str_locate_all(pattern = "p|q", longform_table[[j]])
chr_name_long <-
substr(longform_table[[j]][1], 0, stringdx[[1]][1] - 1)
positions <-
as.vector(cbind(
substr(
longform_table[[j]][1],
stringdx[[1]][1],
nchar(longform_table[[j]][1])
),
substr(
longform_table[[j]][2],
stringdx[[2]][1],
nchar(longform_table[[j]][2])
)
))
if (nchar(chr_name_long) != 0)
{
chr_table_2 <-
Cyto_ref_table[grep(paste(paste(
"chr", chr_name_long, sep = ""
), "$", sep = ""), Cyto_ref_table), ]
} else {
chr_table_2 <- chr_table
}
##account for terminal ends
if (any(grepl("pter", positions)))
{
positions[grep("pter", positions)] <- chr_table_2[1, 4]
}
if (any(grepl("qter", positions)))
{
positions[grep("qter", positions)] <- chr_table_2[nrow(chr_table_2), 4]
}
##be careful on centromeneter
if (any(grepl("cen", positions)))
{
##likey will have to be careful about this one
positions[grep("cen", positions)] <-
chr_table_2[grep("acen", chr_table_2[, 5]), ][1, 4]
}
##account for p10/q10
##double check naming convention for this one
##have to change the one for q
positions[grep("p10", positions)] <-
chr_table_2[grep("acen", chr_table_2[, 5]), ][1, 4]
positions[grep("q10", positions)] <-
chr_table_2[grep("acen", chr_table_2[, 5]), ][2, 4]
##make sure positions is in order to be processed correctly
positions<-positionsorter(positions)
##put stuff in table
positions_table <-
matrix(chr_table_2[grep(paste(positions, collapse = "|", sep = "|"),
chr_table_2[, 4]), ], ncol = 5)
if (is.vector(positions_table))
{
positions_table <- t(positions_table)
}
currentvec<-c(currentvec, as.vector(positions_table[,4])[1],as.vector(positions_table[,4])[length(as.vector(positions_table[,4]))])
}
} else {
positions <-
strsplit(gsub("q", ",q", gsub("p", ",p", temp[[lengthcount * 2]][o])), ",")[[1]][2:length(strsplit(gsub("q", ",q", gsub(
"p", ",p", temp[[lengthcount * 2]][o]
)), ",")[[1]])]
##have to change the one to q
positions[grep("p10", positions)] <-
chr_table[grep("acen", chr_table[, 5]), ][1, 4]
positions[grep("q10", positions)] <-
chr_table[grep("acen", chr_table[, 5]), ][2, 4]
positions<-positionsorter(positions)
###########################################################################################################
#####################for mitelman data only###############################################################
#####check for more than one band per chromosome for translocations######################################
##############################################################################################################
if(forMtn==T & grepl("t\\(",derMods[lengthcount] ) & length(positions) > 1 )
{
earlyReturn=T
}else{
################################################
##############################################
#########################################
##if only one q or p , add on end point
##something is wrong here
if (length(positions) == 1)
{
if (any(grepl("p", positions)))
{
##currentvec <-
##c(currentvec, paste(chr_table[grep(positions, chr_table[, 4])[length(grep(positions, chr_table[, 4]))]
## , 4], chr_table[nrow(chr_table), 4], sep = ''))
currentvec <-
c(currentvec, paste(chr_table[grep(positions, chr_table[, 4])[length(grep(positions, chr_table[, 4]))] +
1, 4], chr_table[nrow(chr_table), 4], sep = ''))
}
if (any(grepl("q", positions)))
{
currentvec <-
##c(currentvec, paste(chr_table[grep(paste(positions,sep="",collapse="|"), chr_table[, 4])[length(grep(paste(positions,sep="",collapse="|"), chr_table[, 4]))] -
## 1, 4], chr_table[1, 4], sep = ''))
c(currentvec, paste(chr_table[grep(positions, chr_table[, 4])[1]
-1, 4], chr_table[1, 4], sep = ''))
##c(currentvec, paste(chr_table[grep(positions, chr_table[, 4])-1
## , 4][1], chr_table[1, 4], sep = ''))
##c(currentvec, paste(chr_table[grep(positions, chr_table[, 4])
## , 4][1], chr_table[1, 4], sep = ''))
}
}else{
## restrict if positions are at the ends of the chromosome
pos<-grep(paste(positions,sep="",collapse="|"), chr_table[, 4])
if(pos[length(pos)] +1 >nrow(chr_table) && pos[1]==1)
{
currentvec<-c(
currentvec,
paste(chr_table[nrow(chr_table), 4], sep = ''),
paste(chr_table[1, 4], sep = '')
)
}else if(pos[length(pos)] +1 >nrow(chr_table)){
##if cyto is at the top
currentvec<- c(
currentvec,
paste(chr_table[nrow(chr_table), 4], sep = ''),
paste(chr_table[pos[1] -1, 4], chr_table[1, 4], sep = '')
)
}else if(pos[1]==1){
##if cyto is on the bottom
currentvec<- c(
currentvec,
paste(chr_table[pos[length(pos)] +
1, 4], chr_table[nrow(chr_table), 4], sep = ''),
paste( chr_table[1, 4], sep = '')
)
}else{
##if not
currentvec <-
c(
currentvec,
paste(chr_table[pos[length(pos)] +
1, 4], chr_table[nrow(chr_table), 4], sep = ''),
paste(chr_table[pos[1] -
1, 4], chr_table[1, 4], sep = '')
)
##c(
##currentvec,
##paste(chr_table[grep(paste(positions,collapse=""), chr_table[, 4])[length(grep(paste(positions,collapse=""), chr_table[, 4]))]
## , 4], chr_table[nrow(chr_table), 4], sep = ''),
##paste(chr_table[grep(positions, chr_table[, 4])[length(grep(positions, chr_table[, 4]))]
## , 4], chr_table[1, 4], sep = '')
##)
}
}
}
}
##handling isoderivatives
if(isiso)
{
currentvec<-sapply(currentvec,function(x){gsub(paste(unused,"[[:digit:]]+(\\.[[:digit:]])*",sep=''),temp[[(grep("ider",derMods))+1]],x)})
}
return(list(currentvec,earlyReturn))
}
##make sure the order of the bands is in the order we like (p to q highers ps to low, lower qs to high )
positionsorter<-function(positions){
##if(length(positions)==2)
##{
##if(grepl("q",positions[1])&grepl("p",positions[2]))
##{
## tempstorage<-positions[2]
## positions[2]<-positions[1]
## positions[1]<-tempstorage
##}
##if(grepl("q",positions[1])&grepl("q",positions[2]))
##{
## bands<-sapply(positions,function(x){as.numeric(gsub("q","",x))})
## if(bands[1]>bands[2])
## {
## tempstorage<-positions[2]
## positions[2]<-positions[1]
## positions[1]<-tempstorage
## }
##}
##if(grepl("p",positions[1])&grepl("p",positions[2]))
##{
##bands<-sapply(positions,function(x){as.numeric(gsub("p","",x))})
##if(bands[1]<bands[2])
##{
## tempstorage<-positions[2]
## positions[2]<-positions[1]
## positions[1]<-tempstorage
##}
##}
##}
###########################333
#######new
##########################
################################
if(length(unlist(strsplit(positions,"-|~")))>1)
{
positions<-unlist(lapply(strsplit(positions,"-|~"),function(x){x[1]}))
}
return(positions)
}
##function for separating normal data
##take into acc same chrom insestion
##add cen into here (pter qter analouge)
parser <- function(coln, xmod, ymod, transloctable,addtot,Cyto)
{
Cyto_sample<-Cyto
##for or statements, take first statement
Cyto_sample[coln]<-gsub("or.*$","",Cyto_sample[coln])
##if we are guessing ? marks
if(guess_q == T & any(grepl("\\?",Cyto_sample[coln])))
{
Cyto_sample[coln] <- gsub("\\?","",Cyto_sample[coln])
}
##if we are counting constitutional, substitute c
if(constitutional==T)
{
Cyto_sample[coln]<-gsub("c$","",Cyto_sample[coln])
}
##derivative chromosomes with translocations are a loss (on native chromosome) gain (on new chromosome)
##figure out what add bool is and consaolidate that
##string splits by ;, takes into account multiple chromosomes odd values are chromosomes even are the positions, for derivaties, first one needs to be treated differently
test <-
strsplit(gsub("[\\(\\)]", "", regmatches(
Cyto_sample[coln], gregexpr("\\(.*?\\)", Cyto_sample[coln])
)[[1]]), ";")
temp <-
strsplit(gsub("[\\(\\)]", "", regmatches(
Cyto_sample[coln], gregexpr("\\(.*?\\)", Cyto_sample[coln])
)[[1]]), ";")
##loop to go through everything and extract table if its in short form
coord <- data.frame()
excoord <- data.frame()
derMods <- Cyto_sample[coln]
addBool <- ""
##right now this is doing both der stuff and adition stuff, want it just to do + stuff make dermods do other stuff
Mainchr <- vector()
Allchr <- vector()
##MiscChr<-matrix()
##remeber what this is supposed to do
derMods <- strsplit(derMods, "\\)")[[1]]
derModsBackup<-strsplit(derMods, "\\)")[[1]]
##skip second +t if first one is triggered
plusT<-F
##regins
regtranschrom=NULL
if (length(temp) > 0)
{
##chooses main chr for exclusion
Mainchr <- temp[[1]]
Mainchr <- paste(Mainchr, "$", sep = "")
Mainchr<-gsub("p|q","",Mainchr)
}
##this things not working properly
if (length(derMods) > 0)
{
##do this later on, + = +1 addtot, others = -
## addtot<-addtot+multi
##if(grepl("\\+.*\\(.*",derMods[1]))
##{
## addtot<-addtot+1
##}
##consider gsub
}
##addBool<- paste("+",addBool,sep="")
##}
##make sure this matches up with the next thing
##if (grepl("der\\(|rec\\(", Cyto_sample[coln])&&!grepl("ider\\(.*", Cyto_sample[coln]))
##{
##remeber what this is supposed to do
##derMods<-paste(")(",derMods,sep="")
##}
## if it straight up describes derivative makeup afterwads
if (grepl("der\\([0-9;]+\\)\\(|rec\\([0-9;]+\\)\\(", Cyto_sample[coln]) &
!grepl("ider", Cyto_sample[coln]))
{
addBool <- paste(addBool, "LongDer", sep = "")
} else if (grepl("der|rec|dic", Cyto_sample[coln]) &
!grepl("ider|idic", Cyto_sample[coln]))
{
if(grepl("der|rec",Cyto_sample[coln]))
{
addBool <- paste(addBool, derMods[1], sep = "")
}
##something is going wrong where when i changed this
##only want more than one
if (grepl(
"der\\([[:alnum:]]+(;[[:alnum:]])*\\)[[:alpha:]]+|rec\\([[:alnum:]]+(;[[:alnum:]])*\\)[[:alpha:]]+",
Cyto_sample[coln]
))
{
derMods <-
strsplit(Cyto_sample[coln], "(der|rec)\\([[:digit:]XY]+(;[[:digit:]XY])*\\)")[[1]][2]
derMods <- strsplit(derMods, "\\)")[[1]]
temp <- tail(temp, length(temp) - 1)
}else if((grepl("der|rec",Cyto_sample[coln]) && !is.na(derMods) && length(derMods)>1 && grepl("^r\\(",derMods[2]) )){
derMods <-tail(derMods, length(derMods) - 1)
temp <- tail(temp, length(temp) - 1)
}else if(grepl("der\\([[:digit:]]+;[[:digit:]]+\\)t\\(|dic\\([[:digit:]]+;[[:digit:]]+\\)t\\(",Cyto_sample)[coln])
{
##handle der(11;13)t( and dic(11;13)t( esq cases here
##remember to search t(11;13 if its referring to previous call of translocation
##figure out how to do this
##this will only work with two translocations and the translocation must immediantly follow
##the der or dic
if(grepl("der\\([[:digit:]]+;[[:digit:]]+\\)t\\([[:digit:]]+;[[:digit:]]+\\)\\(|dic\\([[:digit:]]+;[[:digit:]]+\\)t\\([[:digit:]]+;[[:digit:]]+\\)\\(",Cyto_sample[coln])){
##translocation is fully described
if(grepl("der\\(",Cyto_sample[coln])){
##replace der with dic
derMods[1]<-gsub("der","dic",derMods[1])
##delete der so its treated like a pure dic
addBool[1]<-gsub("der","dic",addBool[1])
}
##delete translocation from both temp and derMods
derMods<-derMods[-2]
temp<-temp[-2]
}else if(grepl("der\\([[:digit:]]+;[[:digit:]]+\\)t\\([[:digit:]]+;[[:digit:]]+\\)|dic\\([[:digit:]]+;[[:digit:]]+\\)t\\([[:digit:]]+;[[:digit:]]+\\)",Cyto_sample[coln])){
##if the translocation needs lookup
##see if translocation is found
##else this thing crashes
if(length(transloctable) > 0 && grepl(derMods[2],names(transloctable),perl=F,fixed=T))
{
##last chunk of translocation indicating position
addOnTrans<-names(transloctable)[grep(derMods[2],names(transloctable),perl=F,fixed=T)]
addOnTrans<- strsplit(addOnTrans,"\\)")[[1]][2]
addOnTransTemp<-strsplit(gsub("\\(|\\)","",addOnTrans),";")
##if the string is longer than just the translocation
if(length(derMods)>2){
derMods<-c(derMods[1:2],addOnTrans,derMods[3:length(derMods)])
temp<-c(temp[1:2],addOnTransTemp,temp[3:length(temp)])
}else{
##just the der and the translocation
derMods<-c(derMods[1:2],addOnTrans)
temp<-c(temp[1:2],addOnTransTemp)
}
if(grepl("der\\(",Cyto_sample[coln])){
##replace der with dic
derMods[1]<-gsub("der","dic",derMods[1])
##delete der so its treated like a pure dic
addBool[1]<-gsub("der","dic",addBool[1])
}
##delete translocation from both temp and derMods
derMods<-derMods[-2]
temp<-temp[-2]
}else{
print("translocation undefined")
return(NULL)
}
}
}
}
##if temp length is greater than 2 (derivative chromosome, then, dermods 1 is master indicator, not including ders above)
if(length(temp)>2&(!(grepl("der|rec", Cyto_sample[coln])))||grepl("ider",Cyto_sample[coln]))
{
addBool<-paste(addBool,derMods[1],sep='')
}
##if(grepl("\\+.*\\(.*",derMods[1]))
##{
##adds +etc on if it starts with something with +something
##this part is being messed up
##handle differently if we are not counting constitutional
multi = 1 ##check if there is a X3 etc value , if there is pick that up, store, add to addtot, make it process through twice later
if(constitutional==F)
{
temp_cyto<-gsub("(c$)|(c\\?$)","",Cyto_sample[coln])
if (grepl("\\)X|\\)x", temp_cyto))
{
multi = unlist(strsplit(temp_cyto, ")X|)x"))[2]
if (grepl("-|~", multi))
{
multi <- unlist(strsplit(multi, "~|-"))[1]
}
##multi=gsub("[^0-9]","",multi)
multi = as.numeric(multi)
addBool <- paste(addBool, "multi", multi, sep = "")
}
}else{
if (grepl("\\)X|\\)x", Cyto_sample[coln]))
{
multi = unlist(strsplit(Cyto_sample[coln], ")X|)x"))[2]
if (grepl("-|~", multi))
{
multi <- unlist(strsplit(multi, "~|-"))[1]
}
##multi=gsub("[^0-9]","",multi)
multi = as.numeric(multi)
addBool <- paste(addBool, "multi", multi, sep = "")
}
}
##########################################################################################################
######################################X chromosomes Y chromosomes #######################################
#############################################################################################################
##increment X in presence of x modifications here
if (any(grepl("X", Mainchr)))
{
xmod <- xmod + length(grep("X", Mainchr))
}
if (any(grepl("Y", Mainchr)))
{
ymod <- ymod + length(grep("Y", Mainchr))
}
##make sure you count + properly for ? marks or constitutional
if((any(grepl("\\?|\\~",Cyto_sample[coln])) & any(grepl("\\+",Cyto_sample[coln])) ) |(constitutional==F & multi > 1 & grepl("(c$)|(c\\?$)",Cyto_sample[coln]) ))
{
if(constitutional==F & multi > 2 & !grepl("\\+",Cyto_sample[coln]) & grepl("(c$)|(c\\?$)",Cyto_sample[coln] )){
addtot<-addtot+(1*(multi-2))
}else{
addtot<-addtot+(1*multi)
}
}
#########
##if guess is true, try to process ? marks
##think about how this can affect counting + and \\?
######new######
#############################
##not processing things like t(9;22)(p?;q10)
if(length(temp)==1){
if (grepl("(t|ins)\\(", derMods[1] ))
{
transchrom <-
str_extract(Cyto_sample[coln], paste(gsub("\\?","\\\\?",gsub("\\+","\\\\+",gsub("\\(","\\\\(",derMods[1]))),"\\)\\(.+?\\)",sep=''))
##if this is not labled
if(is.na(transchrom))
{
transchrom <-
str_extract(Cyto_sample[coln], paste(gsub("\\?","\\\\?",gsub("\\+","\\\\+",gsub("\\(","\\\\(",derMods[1]))),"\\)",sep=''))
}
regtranschrom<-gsub("\\+","",gsub("\\)","\\\\)",gsub("\\(","\\\\(",transchrom)))
}
}
if ((length(test) > 1 | any(grepl("p|q",temp))| grepl("(9;22)|(22;9)",paste(unlist(temp),collapse=';',sep=";")) |(!is.null(regtranschrom) && any(grepl(regtranschrom, names(transloctable))) ) ) & (!any(grepl("\\?|\\~", temp)) ) & !(guess_q==F & grepl("\\?",Cyto_sample[coln])) )
{
##goes by steps of 2, odd indexes indicate chromosomes, even indicate positions
##length_temp<-(if((length(temp) / 2)==0.5){1}else{length(temp)/2})
lengthcount=1
repeat
{
if(lengthcount > (if(!is.integer((length(temp) / 2))){ceiling(length(temp)/2)}else{length(temp)/2})) break
if(lengthcount> 60) {print("while loop not terminating");break}
Allchr <-
c(Allchr, as.vector(paste(temp[[(lengthcount * 2-1)]], "$", sep = "")))
##handle those t(__;__) and ins (__;__) here with no follow up
if ((((lengthcount * 2)-1)) == length(temp) || if(length(temp) > (lengthcount*2-1)){
all(grepl("^[[:digit:]]+$", temp[[lengthcount * 2]]))}else{FALSE})
{
#######
########
########
##think about inproving it instead of else then, if if
######
#####
#####
#########################
if (any(grepl("[pq]", temp[[(lengthcount * 2-1)]])))
{
if(any(grepl("p",temp[[(lengthcount*2-1)]])))
{
arm="p10"
}
if(any(grepl("q",temp[[(lengthcount*2-1)]])))
{
arm="q10"
}
temp<-c(if((lengthcount*2-1)!=1){temp[1:(lengthcount*2-1)]}else{temp[(lengthcount*2-1)]},arm,if(length(temp)>=2){temp[[(lengthcount*2):length(temp)]]})
temp[[(lengthcount*2-1)]]<-gsub("p|q","",temp[[(lengthcount*2-1)]])
}
if(any(grepl("^r\\(",derMods[lengthcount*2-1])) & forMtn==F){
rindex<-lengthcount*2-1
rChr<-paste("chr",temp[[rindex]],"$",sep="")
tempRingPosition<-NULL
tempRing<-NULL
for(c in 1:length(rChr))
{
tempRingTable<-matrix(nrow=0,ncol=5)
chr<-Cyto_ref_table[grep(rChr[c],Cyto_ref_table[,1]),]
tempRingTable<-rbind(tempRingTable,rbind(chr[1,],chr[nrow(chr),]))
tempRingTable[,4]
tempRingPosition<- c(tempRingPosition,paste(tempRingTable[,4],collapse=""))
}
tempRing<-list(unlist(temp[[rindex]]), tempRingPosition)
derModsRing<-c(paste("r(",paste(unlist(temp),sep="",collapse=";"),sep=""),paste("(",paste(tempRingPosition,collapse = ";"),sep=""))
tempstorage<-NULL
tempdermods<-NULL
if((length(temp))>(lengthcount*2))
{
tempstorage<-temp[((lengthcount*2)):length(temp)]
tempdermods<-derMods[((lengthcount*2)):length(temp)]
}
temp[[(lengthcount*2-1)]]<-tempRing[[1]]