/R/SearchBar-GUI.R
R | 169 lines | 133 code | 24 blank | 12 comment | 22 complexity | 9139828b718d5c28698bebbd92773d2b MD5 | raw file
1qsetClass("SearchBar", Qt$QLineEdit, function(gr = NULL, ref = NULL, parent = NULL) 2{ 3 super(parent) 4 this$gr <- gr; this$ref <- ref 5 names <- NULL 6 metadata <- NULL 7 if(!is.null(gr)) { 8 names <- levels(seqnames(gr)) 9 metadata <- unique(unlist(sapply(values(gr),as.character))) 10 } 11 namesRef <- NULL 12 metadataRef <- NULL 13 if(!is.null(ref)) { 14 namesRef <- levels(seqnames(ref)) 15 metadataRef <- unique(unlist(sapply(values(ref),as.character))) 16 } 17 compVec <- c(names,metadata,namesRef,metadataRef) 18 if(!is.null(compVec)) { 19 compVec <- sort(unique(compVec)) 20 comp <- Qt$QCompleter(compVec) 21 comp$setCaseSensitivity(Qt$Qt$CaseInsensitive) 22 this$setCompleter(comp) 23 } 24 25 # initialize the GRanges object containing the search range 26 searchRange <- NULL 27 28 # parse the text and update GRanges object when return pressed 29 qconnect(this, "returnPressed", function() { 30 parseSearchString(this$text, gr, ref) 31 }) 32 33}) 34 35qsetSignal("rangeChanged", SearchBar) 36 37qsetMethod("getSearchRange", SearchBar, function() { 38 this$searchRange 39}) 40 41qsetMethod("setSearchRange", SearchBar, function(newRange) { 42 this$searchRange <- newRange 43}) 44 45qsetMethod("parseSearchString", SearchBar, function(text, gr = NULL, ref = NULL) { 46 colon <- grepl(":",text,fixed=TRUE) 47 minus <- grepl("-",text,fixed=TRUE) 48 plus <- grepl("+",text,fixed=TRUE) 49 if((!colon) & (minus) ){ 50 start <- as.numeric(unlist(strsplit(text, "-")))[1] 51 end <- as.numeric(unlist(strsplit(text, "-")))[2] 52 this$searchRange <- c(start, end) 53 this$rangeChanged() 54 } 55 if(colon & minus) { # seqname and interval specified 56 # obtain seqname, start, end 57 colonIdx <- regexpr(":",text,fixed=TRUE) 58 minusIdx <- regexpr("-",text,fixed=TRUE) 59 name <- substr(text,1,colonIdx-1) 60 start <- as.integer(substr(text,colonIdx+1,minusIdx-1)) 61 end <- as.integer(substr(text,minusIdx+1,nchar(text))) 62 63 # return GRanges object with relevant sequence 64 this$searchRange <- GRanges(seqnames = 65 Rle(name, 1), 66 ranges = 67 IRanges(start = start, end = end), 68 ) 69 70 this$rangeChanged() 71 72 } else if(colon & plus) { # seqname and center/radius specified 73 # obtain seqname, start, end 74 colonIdx <- regexpr(":",text,fixed=TRUE) 75 plusIdx <- regexpr("+",text,fixed=TRUE) 76 name <- substr(text,1,colonIdx-1) 77 center <- as.integer(substr(text,colonIdx+1,plusIdx-1)) 78 radius <- as.integer(substr(text,plusIdx+1,nchar(text))) 79 start <- center - radius 80 end <- center + radius 81 82 # return GRanges object with relevant sequence 83 this$searchRange <- GRanges(seqnames = 84 Rle(name, 1), 85 ranges = 86 IRanges(start = start, end = end), 87 ) 88 89 this$rangeChanged() 90 91 } else { 92 in.names <- NULL 93 in.metadata <- NULL 94 if(!is.null(gr)) { 95 grNames <- levels(seqnames(gr)) 96 in.names <- grep(text,grNames,ignore.case=TRUE) 97 metaData <- unlist(sapply(values(gr),as.character)) 98 in.metadata <- grep(text,metaData,ignore.case=TRUE) 99 } 100 in.namesRef <- NULL 101 in.metadataRef <- NULL 102 if(!is.null(ref)) { 103 namesReference <- levels(seqnames(ref)) 104 in.namesRef <- grep(text,namesReference,ignore.case=TRUE) 105 metaDataReference <- unlist(sapply(values(ref),as.character)) 106 in.metadataRef <- grep(text,metaDataReference,ignore.case=TRUE) 107 } 108 if(length(in.names) > 0) { 109 grSubset <- gr[seqnames(gr) == text] 110 minLoc <- min(IRanges::start(grSubset)) 111 maxLoc <- max(IRanges::end(grSubset)) 112 113 # return GRanges object with relevant sequence 114 this$searchRange <- GRanges(seqnames = 115 Rle(text, 1), 116 ranges = 117 IRanges(start = minLoc, end = maxLoc), 118 ) 119 120 this$rangeChanged() 121 } else if (length(in.metadataRef) > 0) { 122 matchRows <- in.metadataRef %% dim(values(ref))[1] 123 matchRows[matchRows == 0] <- dim(values(ref))[1] 124 125 # return GRanges object with matching rows in reference set 126 refSubset <- ref[matchRows] 127 this$searchRange <- refSubset 128 129 this$rangeChanged() 130 131 # change text in search bar to match intervals being displayed 132 setText(base::paste(GenomicRanges::seqnames(refSubset),":", 133 IRanges::start(refSubset), 134 "-",IRanges::end(refSubset),collapse="; ",sep="")) 135 } else if (length(in.namesRef) > 0) { 136 refSubset <- ref[seqnames(ref) == text] 137 minLoc <- min(start(refSubset)) 138 maxLoc <- max(end(refSubset)) 139 140 # return GRanges object with relevant sequence 141 this$searchRange <- GRanges(seqnames = 142 Rle(text, 1), 143 ranges = 144 IRanges(start = minLoc, end = maxLoc), 145 ) 146 147 this$rangeChanged() 148 } else if (length(in.metadata) > 0) { 149 matchRows <- in.metadata %% dim(values(gr))[1] 150 matchRows[matchRows == 0] <- dim(values(gr))[1] 151 152 # return GRanges object with matching rows in reference set 153 grSubset <- gr[matchRows] 154 this$searchRange <- grSubset 155 156 this$rangeChanged() 157 158 # change text in search bar to match intervals being displayed 159 setText(base::paste(GenomicRanges::seqnames(grSubset),":", 160 IRanges::start(grSubset),"-", 161 IRanges::end(grSubset),collapse="; ",sep="")) 162 } else { 163 setText("") 164 setPlaceholderText("Error: search string not recognized") 165 this$searchRange <- NULL 166 } 167 } 168 169})