1

This is what Im tring to achieve

I read list of files based on pattern, then I have to read files except a few which im trying to define a pattern which im not sure how to achieve. Taking a long way this is what Im doing

bam.files <-list.files("MACS2_peak_call/phsc_peaks/",pattern = ".bam$")
 bam.files

 bam.files
 [1] "Blast1.bam"  "Blast10.bam" "Blast11.bam" "Blast12.bam" "Blast13.bam" "Blast14.bam" "Blast15.bam"
 [8] "Blast16.bam" "Blast17.bam" "Blast18.bam" "Blast19.bam" "Blast2.bam"  "Blast20.bam" "Blast21.bam"
[15] "Blast22.bam" "Blast23.bam" "Blast3.bam"  "Blast4.bam"  "Blast5.bam"  "Blast6.bam"  "Blast7.bam" 
[22] "Blast8.bam"  "Blast9.bam"  "HSC1.bam"    "HSC2.bam"    "HSC3.bam"    "HSC4.bam"    "HSC5.bam"   
[29] "HSC6.bam"    "HSC7.bam"    "LSC1.bam"    "LSC2.bam"    "LSC3.bam"    "LSC4.bam"    "LSC5.bam"   
[36] "LSC6.bam"    "LSC7.bam"    "LSC8.bam"    "Mono1.bam"   "Mono2.bam"   "Mono3.bam"   "Mono4.bam"  
[43] "Mono5.bam"   "Mono6.bam"   "pHSC1.bam"   "pHSC10.bam"  "pHSC11.bam"  "pHSC12.bam"  "pHSC13.bam" 
[50] "pHSC14.bam"  "pHSC15.bam"  "pHSC16.bam"  "pHSC17.bam"  "pHSC18.bam"  "pHSC19.bam"  "pHSC2.bam"  
[57] "pHSC3.bam"   "pHSC4.bam"   "pHSC5.bam"   "pHSC6.bam"   "pHSC7.bam"   "pHSC8.bam"   "pHSC9.bam" 

My objective is to remove any files which contains the word "Mono"

To do that the below code. But here I am making another character vector called remove then doing it where Im have to copy+paste each file name I want to remove.

remove <- c ("Mono1.bam", "Mono2.bam", "Mono3.bam","Mono4.bam","Mono5.bam","Mono6.bam")
     bam.files %in% remove
     bam.files [! bam.files %in% remove]

How to do the above in better way?Any help or suggestion would be really appreciated.

PesKchan
  • 868
  • 6
  • 14

4 Answers4

1

grep("Mono", bam.files) will return the indexes of bam.files having "Mono" in it. But you may want to add complexity to your pattern, like using regular expressions.

yeahman269
  • 705
  • 7
  • 16
1

try this

bam.files <- bam.files[unlist(lapply(bam.files, \(x) !grepl('Mono', x)))]
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
1

Try:

new_list <- bam.files[-grep('Mono',bam.files)]
CodingBiology
  • 262
  • 4
  • 13
1
grep("Mono", bam.files, invert=TRUE, value = TRUE)
TarJae
  • 72,363
  • 6
  • 19
  • 66