DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
61 stars 13 forks source link

Removing unused channels #55

Closed rwbaer closed 4 years ago

rwbaer commented 4 years ago

Outline the steps taken to attempt to reach this goal (paste code below): My goal is simply to remove unused channels from my gating set. I first tried setting restrict = TRUE in cyto_setup() but I must not have used it properly. I then tried cyto_channels_restrict() before reading help and learning it was for flowFrames and flowSets and not gating sets.. In my example the only gates are on fluoresence channels FL3-AREA and FL4-AREA.

I guess the other question I need to ask is how does one guarantee that a channel is an "unassigned channel" since cyto_setup appears to think it has taken care of me? What is the proper way to do this in a gating set workflow in cytoExploreR?

I have some channels with non-"BD standard" names like FSC-WIDTH, TIME_LSW, TIME_MSW, etc. Should they work as restricted channels without special effort? Am I having trouble because my channels have names like FL3-AREA instead of FL3-A? Not sure how it works in the background.

> #Load samples
  > std_gs <- cyto_setup("Standard-Samples", 
                         +                      gatingTemplate = "LP3-gatingTemplate.csv",
                         +                      markers = FALSE,
                         +                      details = FALSE, 
                         +                      restrict = TRUE)
  Loading FCS files into a GatingSet...
  Removing unassigned channels...
  Setting LP3-gatingTemplate.csv as the active gatingTemplate...
  Done!
    > 
    > cyto_channels(std_gs)
  [1] "TIME_MSW"   "TIME_LSW"   "FSC-HEIGHT" "FSC-AREA"   "FSC-WIDTH"  "SSC-HEIGHT" "SSC-AREA"   "SSC-WIDTH" 
  [9] "FL1-HEIGHT" "FL1-AREA"   "FL1-WIDTH"  "FL2-HEIGHT" "FL2-AREA"   "FL2-WIDTH"  "FL3-HEIGHT" "FL3-AREA"  
  [17] "FL3-WIDTH"  "FL4-HEIGHT" "FL4-AREA"   "FL4-WIDTH"  "SORT"      
  > cyto_channels_restrict(std_gs)
  Error during wrapup: argument "i" is missing, with no default
  Error: no more error handlers available (recursive errors?); invoking 'abort' restart
  > cyto_channels(std_gs)
  [1] "TIME_MSW"   "TIME_LSW"   "FSC-HEIGHT" "FSC-AREA"   "FSC-WIDTH"  "SSC-HEIGHT" "SSC-AREA"   "SSC-WIDTH" 
  [9] "FL1-HEIGHT" "FL1-AREA"   "FL1-WIDTH"  "FL2-HEIGHT" "FL2-AREA"   "FL2-WIDTH"  "FL3-HEIGHT" "FL3-AREA"  
  [17] "FL3-WIDTH"  "FL4-HEIGHT" "FL4-AREA"   "FL4-WIDTH"  "SORT"      
  > 

Include any associated screenshots or images here:

DillonHammill commented 4 years ago

cyto_channels_restrict() will keep all channels that show up in cyto_markers(). If you don't want a certain channel, remove its marker assignment using cyto_markers_edit(). I suspect that you may have a marker assigned to every channel. FSC/SSC/Time are priveleged channels and are always included.

rwbaer commented 4 years ago

Thanks again for your help. Understanding a little better each day ...

You are correct that I have all my channels as markers. I'm doing something wrong when I use cyto_markers_edit() though. I am right clicking and removing rows to get rid of unwanted channels, but that results in an error. Should I be manually editing "Experiment Markers.csv" instead? Is there a basic setup I need to do for channels in advance of cyto_setup()?

During the error below I removed all fluoresence channels (right click delete row) except FL3-AREA and FL4-AREA, but I did not touch either TIME_ channel or any rows for -HEIGHT -WIDTH or -AREA of SSC and FSC. I think previously I have tried just blanking the marker cell to no avail (by which I mean the spill editor still has lots of unwanted/unneeded cells)

> # pass name of folder containing compensation files to cyto_setup
> # Setup compensation controls
> gs <- cyto_setup("Compensation-Samples",
+                  gatingTemplate = "Compensation-gatingTemplate.csv",
+                  restrict = TRUE,
+                  clean = FALSE,
+                  details = TRUE)
Loading FCS files into a GatingSet...
Assigning markers to channels...
Experiment-Markers.csv found in working directory.
Updating experiment details...
Experiment-Details.csv found in working directory.
Removing unassigned channels...
Setting Compensation-gatingTemplate.csv as the active gatingTemplate...
Done!
> # Uncomment to draw gates the first time
> # # Gate Cells - side efect add gate to Compensation-gatingTemplate.csv
> cyto_channels(gs)
 [1] "TIME_MSW"   "TIME_LSW"   "FSC-HEIGHT" "FSC-AREA"   "FSC-WIDTH"  "SSC-HEIGHT" "SSC-AREA"   "SSC-WIDTH"  "FL1-HEIGHT"
[10] "FL1-AREA"   "FL1-WIDTH"  "FL2-HEIGHT" "FL2-AREA"   "FL2-WIDTH"  "FL3-HEIGHT" "FL3-AREA"   "FL3-WIDTH"  "FL4-HEIGHT"
[19] "FL4-AREA"   "FL4-WIDTH"  "SORT"      
> cyto_markers(gs)
          TIME_MSW           TIME_LSW         FSC-HEIGHT           FSC-AREA          FSC-WIDTH         SSC-HEIGHT 
        "TIME-MSW"         "TIME-LSW"       "FSC-HEIGHT"         "FSC-AREA"        "FSC-WIDTH"       "SSC-HEIGHT" 
          SSC-AREA          SSC-WIDTH         FL1-HEIGHT           FL1-AREA          FL1-WIDTH         FL2-HEIGHT 
        "SSC-AREA"        "SSC-WIDTH"       "FL1-HEIGHT"         "FL1-AREA"        "FL1-WIDTH"  "FL2-FITC-HEIGHT" 
          FL2-AREA          FL2-WIDTH         FL3-HEIGHT           FL3-AREA          FL3-WIDTH         FL4-HEIGHT 
   "FL2-FITC-AREA"   "FL2-FITC-WIDTH"    "FL3-PE-HEIGHT"      "FL3-PE-AREA"     "FL3-PE-WIDTH" "FL4-PerCP-HEIGHT" 
          FL4-AREA          FL4-WIDTH               SORT 
  "FL4-PerCP-AREA"  "FL4-PerCP-WIDTH"             "SORT" 
> cyto_markers_edit(gs)
Experiment-Markers.csv found in working directory.
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
rwbaer commented 4 years ago

The workings of the editor upon row removal is much like described for issue Experimental Details Editor doesn't like row elimination #49. In fact, the same "error message" typo shows up if you try setting multiple markers to NULL. [Just a stupid idea ...]

Experiment-Markers.csv found in working directory.
Error in `markernames<-`(`*tmp*`, value = value) : 
  Trying to assing the marker: NULL to multiple channels
DillonHammill commented 4 years ago

Sorry I should have been clearer. Don't delete the row just delete the text in the marker column next to the channel that you want to remove (i.e. leave the entry in the marker column empty). I have not written anything to appropriately handle row removals in the data editors as yet, but it is something that will likely be supported in the future. For now, do not remove any rows.

rwbaer commented 4 years ago

This has not been working for me. (which is why I tried the NULL bit) Could I be doing this at the wrong time in the overall sequence. I have tried leaving just the channels of interest at cyto_setup() time as well, but it doesn't seem to help.

If I delete BOTH the channel AND the marker its not happy either

Experiment-Markers.csv found in working directory.
Error in set_all_channels(x@pointer, value) : 
  The input of 'set_channels' has duplicates!

Is the message a hint to delete the .csv before getting to this step? I'm lost about who controls what.

DillonHammill commented 4 years ago

I will look into this and get back to you.

DillonHammill commented 4 years ago

@rwbaer, I have just pushed an update to cyto_markers_edit() which should fix this issue. Please pull down the latest CytoExploreR (no need to update RGLab packages):

devtools::install_github("DillonHammill/CytoExploreR")

I would delete any Experiment-Markers.csv file before trying again.

rwbaer commented 4 years ago

Something still seems amiss. The plot produced by the code below still seems to produce graphs for all channels, despite removing many channels. 'Experiment__Markers.csv' has NA for channels I deleted in editor. Out of interest, should the NA in cyto_markers(gs) be "NA" as they are or just NA?

image

Here is what cyto_markers(gs) returns: cyto_markers(gs) TIME_MSW TIME_LSW FSC-HEIGHT FSC-AREA FSC-WIDTH SSC-HEIGHT "NA" "TIME-" "FSC-HEIGHT" "FSC-AREA" "NA" "NA" SSC-AREA SSC-WIDTH FL1-HEIGHT FL1-AREA FL1-WIDTH FL2-HEIGHT "SSC-AREA" "NA" "NA" "NA" "NA" "NA" FL2-AREA FL2-WIDTH FL3-HEIGHT FL3-AREA FL3-WIDTH FL4-HEIGHT "NA" "NA" "NA" "FL3-PE-AREA" "NA" "NA" FL4-AREA FL4-WIDTH SORT "FL4-PerCP-AREA" "NA" "NA"

image

> # Make sure you are using the latest code
> devtools::install_github("DillonHammill/CytoExploreR")
Skipping install of 'CytoExploreR' from a github remote, the SHA1 (20de58f3) has not changed since last install.
  Use `force = TRUE` to force installation
> library(CytoExploreR)
> library(CytoExploreR)
> # pass name of folder containing compensation files to cyto_setup
> # Setup compensation controls
> unlink("Compensation-gatingTemplate.csv") # delete compensation gating file
> gs <- cyto_setup("Compensation-Samples",
+                  gatingTemplate = "Compensation-gatingTemplate.csv",
+                  restrict = TRUE,
+                  clean = FALSE,
+                  details = TRUE)
Loading FCS files into a GatingSet...
Assigning markers to channels...
Experiment-Markers.csv found in working directory.
Updating experiment details...
Experiment-Details.csv found in working directory.
Removing unassigned channels...
Setting Compensation-gatingTemplate.csv as the active gatingTemplate...
Creating Compensation-gatingTemplate.csv ...
Done!
> # Transform fluorescent channels - default logicle transformations
> gs <- cyto_transform(gs) # Transforms and plots transformed plot
Error in fetch(key) : 
  lazy-load database 'C:/Users/rbaer/Documents/R/win-library/4.0/CytoExploreR/help/CytoExploreR.rdb' is corrupt
Error in fetch(key) : 
  lazy-load database 'C:/Users/rbaer/Documents/R/win-library/4.0/CytoExploreR/help/CytoExploreR.rdb' is corrupt
Error in fetch(key) : 
  lazy-load database 'C:/Users/rbaer/Documents/R/win-library/4.0/CytoExploreR/help/CytoExploreR.rdb' is corrupt
> cyto_markers()
Error in UseMethod("cyto_markers") : 
  no applicable method for 'cyto_markers' applied to an object of class "NULL"

Along the way I got a couple of dialogs telling me I had R execution errors.

DillonHammill commented 4 years ago

Looks like your CytoExploreR install is corrupt. Try a force re-install:


devtools::install_github("DillonHammill/CytoExploreR", force = TRUE)

Also you need to give cyto_markers() your samples in order to check marker assignment.

cyto_markers(gs)
rwbaer commented 4 years ago

I thought I had posted earlier, but apparently I forgot to click comment. Sorry.

Bottom line is that after carefully reinstalling everything there is still an issue. The graph returns all channels, and cyto_markers(gs) lists the full set, and not the marker-reduced set I'm trying to get (refer to previous comment).

A clue might be that cyto_markers(gs) returns the deleted markers as "NA" rather than just NA. [Of, course there are no quotes around NA in the .csv file.] Could this be the remaining issue?

> cyto_markers(gs)
        TIME_MSW         TIME_LSW       FSC-HEIGHT         FSC-AREA 
            "NA"          "TIME-"     "FSC-HEIGHT"       "FSC-AREA" 
       FSC-WIDTH       SSC-HEIGHT         SSC-AREA        SSC-WIDTH 
            "NA"             "NA"       "SSC-AREA"             "NA" 
      FL1-HEIGHT         FL1-AREA        FL1-WIDTH       FL2-HEIGHT 
            "NA"             "NA"             "NA"             "NA" 
        FL2-AREA        FL2-WIDTH       FL3-HEIGHT         FL3-AREA 
            "NA"             "NA"             "NA"    "FL3-PE-AREA" 
       FL3-WIDTH       FL4-HEIGHT         FL4-AREA        FL4-WIDTH 
            "NA"             "NA" "FL4-PerCP-AREA"             "NA" 
            SORT 
            "NA" 
DillonHammill commented 4 years ago

@rwbaer, can you outline the steps that you took to get to this point? Presumably you used cyto_setup() with restrict = TRUE and when the editor opened you simply deleted the markers that you don't want (i.e. left those cells empty)? I will update this to convert these to proper NAs.

DillonHammill commented 4 years ago

I see the issue now, working on a fix.

DillonHammill commented 4 years ago

@rwbaer, this should now be fixed. It required some changes to flowWorkspace so you will need to update both flowWorkspace and CytoExploreR before trying again:

devtools::install_github("RGLab/flowWorkspace")
devtools::install_github("DillonHammill/CytoExploreR")
rwbaer commented 4 years ago

Wonderful. Think this is fixed now. Closing. There are after-effects that I will open separately.

DillonHammill commented 4 years ago

@rwbaer, I still was not happy with the reliability of cyto_channels_restrict() so I re-wrote it to be more explicit. The new version removes all channels without marker assignments (except for FSC/SSC/Time parameters) as before, but now has an exclude argument to forcibly remove a FSC/SSC/Time parameter should you need to. This means that in your case both TIME channels will be retained by default and you can remove the extra one by supplying its name to the exclude argument. As a bonus I also added support for this directly through the restrict argument in cyto_setup() as well, see examples below:

# Manually restrict GatingSet & remove FSC-H parameter
gs <- cyto_channels_restrict(gs,
                             exclude = "FSC-H")

# Restrict channels during setup
gs <- cyto_setup(gs,
                 restrict = "FSC-H")

You can replace FSC-H with the name of your second TIME parameter above. So now you can manually perform channel restriction post cyto_setup() if you need to, before this was limited to flowFrame and flowSets only.