/R/SearchBar-GUI.R

http://github.com/tengfei/visnab · R · 169 lines · 133 code · 24 blank · 12 comment · 22 complexity · 9139828b718d5c28698bebbd92773d2b MD5 · raw file

  1. qsetClass("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. # initialize the GRanges object containing the search range
  25. searchRange <- NULL
  26. # parse the text and update GRanges object when return pressed
  27. qconnect(this, "returnPressed", function() {
  28. parseSearchString(this$text, gr, ref)
  29. })
  30. })
  31. qsetSignal("rangeChanged", SearchBar)
  32. qsetMethod("getSearchRange", SearchBar, function() {
  33. this$searchRange
  34. })
  35. qsetMethod("setSearchRange", SearchBar, function(newRange) {
  36. this$searchRange <- newRange
  37. })
  38. qsetMethod("parseSearchString", SearchBar, function(text, gr = NULL, ref = NULL) {
  39. colon <- grepl(":",text,fixed=TRUE)
  40. minus <- grepl("-",text,fixed=TRUE)
  41. plus <- grepl("+",text,fixed=TRUE)
  42. if((!colon) & (minus) ){
  43. start <- as.numeric(unlist(strsplit(text, "-")))[1]
  44. end <- as.numeric(unlist(strsplit(text, "-")))[2]
  45. this$searchRange <- c(start, end)
  46. this$rangeChanged()
  47. }
  48. if(colon & minus) { # seqname and interval specified
  49. # obtain seqname, start, end
  50. colonIdx <- regexpr(":",text,fixed=TRUE)
  51. minusIdx <- regexpr("-",text,fixed=TRUE)
  52. name <- substr(text,1,colonIdx-1)
  53. start <- as.integer(substr(text,colonIdx+1,minusIdx-1))
  54. end <- as.integer(substr(text,minusIdx+1,nchar(text)))
  55. # return GRanges object with relevant sequence
  56. this$searchRange <- GRanges(seqnames =
  57. Rle(name, 1),
  58. ranges =
  59. IRanges(start = start, end = end),
  60. )
  61. this$rangeChanged()
  62. } else if(colon & plus) { # seqname and center/radius specified
  63. # obtain seqname, start, end
  64. colonIdx <- regexpr(":",text,fixed=TRUE)
  65. plusIdx <- regexpr("+",text,fixed=TRUE)
  66. name <- substr(text,1,colonIdx-1)
  67. center <- as.integer(substr(text,colonIdx+1,plusIdx-1))
  68. radius <- as.integer(substr(text,plusIdx+1,nchar(text)))
  69. start <- center - radius
  70. end <- center + radius
  71. # return GRanges object with relevant sequence
  72. this$searchRange <- GRanges(seqnames =
  73. Rle(name, 1),
  74. ranges =
  75. IRanges(start = start, end = end),
  76. )
  77. this$rangeChanged()
  78. } else {
  79. in.names <- NULL
  80. in.metadata <- NULL
  81. if(!is.null(gr)) {
  82. grNames <- levels(seqnames(gr))
  83. in.names <- grep(text,grNames,ignore.case=TRUE)
  84. metaData <- unlist(sapply(values(gr),as.character))
  85. in.metadata <- grep(text,metaData,ignore.case=TRUE)
  86. }
  87. in.namesRef <- NULL
  88. in.metadataRef <- NULL
  89. if(!is.null(ref)) {
  90. namesReference <- levels(seqnames(ref))
  91. in.namesRef <- grep(text,namesReference,ignore.case=TRUE)
  92. metaDataReference <- unlist(sapply(values(ref),as.character))
  93. in.metadataRef <- grep(text,metaDataReference,ignore.case=TRUE)
  94. }
  95. if(length(in.names) > 0) {
  96. grSubset <- gr[seqnames(gr) == text]
  97. minLoc <- min(IRanges::start(grSubset))
  98. maxLoc <- max(IRanges::end(grSubset))
  99. # return GRanges object with relevant sequence
  100. this$searchRange <- GRanges(seqnames =
  101. Rle(text, 1),
  102. ranges =
  103. IRanges(start = minLoc, end = maxLoc),
  104. )
  105. this$rangeChanged()
  106. } else if (length(in.metadataRef) > 0) {
  107. matchRows <- in.metadataRef %% dim(values(ref))[1]
  108. matchRows[matchRows == 0] <- dim(values(ref))[1]
  109. # return GRanges object with matching rows in reference set
  110. refSubset <- ref[matchRows]
  111. this$searchRange <- refSubset
  112. this$rangeChanged()
  113. # change text in search bar to match intervals being displayed
  114. setText(base::paste(GenomicRanges::seqnames(refSubset),":",
  115. IRanges::start(refSubset),
  116. "-",IRanges::end(refSubset),collapse="; ",sep=""))
  117. } else if (length(in.namesRef) > 0) {
  118. refSubset <- ref[seqnames(ref) == text]
  119. minLoc <- min(start(refSubset))
  120. maxLoc <- max(end(refSubset))
  121. # return GRanges object with relevant sequence
  122. this$searchRange <- GRanges(seqnames =
  123. Rle(text, 1),
  124. ranges =
  125. IRanges(start = minLoc, end = maxLoc),
  126. )
  127. this$rangeChanged()
  128. } else if (length(in.metadata) > 0) {
  129. matchRows <- in.metadata %% dim(values(gr))[1]
  130. matchRows[matchRows == 0] <- dim(values(gr))[1]
  131. # return GRanges object with matching rows in reference set
  132. grSubset <- gr[matchRows]
  133. this$searchRange <- grSubset
  134. this$rangeChanged()
  135. # change text in search bar to match intervals being displayed
  136. setText(base::paste(GenomicRanges::seqnames(grSubset),":",
  137. IRanges::start(grSubset),"-",
  138. IRanges::end(grSubset),collapse="; ",sep=""))
  139. } else {
  140. setText("")
  141. setPlaceholderText("Error: search string not recognized")
  142. this$searchRange <- NULL
  143. }
  144. }
  145. })