Introduction


Batch alignment is step 2/4 of the general discovery workflow, and facilitates the alignment of data between batches. The dataset is initially processed using step 1 of the general discovery workflow, including arcsinh transformation and the addition of sample metadata, and saved as a single CSV file. This file is then read into R at the start of this workflow. To perform batch alignment, we use specific 'reference' samples, perform an initial 'coarse' alignment by adjusting the data by the 95th percentile, and then a further 'fine' alignment using CytoNorm with quantile adjustment. More detailed information can be found in our pre-print (Ashhurst TM, Marsh-Wakefield F, Putri GH et al. (2020). bioRxiv. 2020.10.22.349563).

Warning

Please note, the scripts and instructions for the general discovery workflow are being updated, and may change significantly in the next update.

Batch alignment

If your samples are derived from a single batch, then this step can be skipped, and you can proceed straight to step 3 (clustering & dimensionality reduction)If you have samples derived from multiple batches, but do not have reference controls processed with each batch, then other forms of batch alignment might be possible – see our batch alignment/data integration tutorial for more information.


Analysis session setup



##########################################################################################################
#### Analysis session setup
##########################################################################################################
    ### Load packages

        library(Spectre)
        Spectre::package.check()    # Check that all required packages are installed
        Spectre::package.load()     # Load required packages
    ### Set DT threads

        getDTthreads()
    ### Set primary directory

        dirname(rstudioapi::getActiveDocumentContext()$path)            # Finds the directory where this script is located
        setwd(dirname(rstudioapi::getActiveDocumentContext()$path))     # Sets the working directory to where the script is located
        getwd()
        PrimaryDirectory <- getwd()
        PrimaryDirectory
    ### Set input directory

        setwd(PrimaryDirectory)
        setwd("Output 1 - data prep/Output 2 - transformed data/")
        InputDirectory <- getwd()
        InputDirectory
        setwd(PrimaryDirectory)
    ### Set metadata directory
        
        setwd(PrimaryDirectory)
        setwd("metadata/")
        MetaDirectory <- getwd()
        MetaDirectory
        setwd(PrimaryDirectory)
    ### Set output directory

        setwd(PrimaryDirectory)
        dir.create("Output 2 - batch alignment", showWarnings = FALSE)
        setwd("Output 2 - batch alignment")
        OutputDirectory <- getwd()
        
        setwd(PrimaryDirectory)


Import data


##########################################################################################################
#### Import data
##########################################################################################################

Set the working directory to 'InputDirectory', and check to see that the dataset CSV file ('cell.dat.csv') is there.

    ### Read in cellular data
        
        setwd(InputDirectory)
        
        list.files(getwd(), ".csv")
[1] "cell.dat.csv"

Read the dataset into R, and assign it at 'cell.dat'.

        cell.dat <- fread("cell.dat.csv")

You can check the dataset.

        cell.dat
        PECy5-5 CD3e PECy7 CD16-32 DL800 Ly6G AF700 CD45 APCCy7 CD48 BUV395 CD11b BUV737 B220 BV605 Ly6C   FileName FileNo
     1:      51.6403     1239.3600   -681.257   1752.270    2600.430      627.259   17763.600    128.934  Mock_01_A      1
     2:      58.8824     -140.9210   -267.039   1188.640     392.962      304.615   11834.800    907.365  Mock_01_A      1
     3:      60.0077     2191.2600   -362.088   1676.960    1441.300     4740.710     185.947   3915.820  Mock_01_A      1
     4:     313.9580     2532.7000   -438.388    478.109    1809.220     9440.380    1056.290   6221.670  Mock_01_A      1
     5:     -83.0391     1603.3900  -1200.350    564.994    3946.200     1636.110    4579.870    274.527  Mock_01_A      1
    ---                                                                                                                   
159996:     106.3880     1661.1200   -114.892   2138.700    8311.230      127.068     258.936   6161.070 Virus_08_B     16
159997:    1750.2300       65.4275   -843.984  19655.900    7874.900      122.179     550.596    132.445 Virus_08_B     16
159998:    -159.2870     5363.3900  17389.300   2578.650    -362.093    12091.600     121.659   4257.980 Virus_08_B     16
159999:     -62.6836     3132.7100   1941.660   1548.130     577.625    12904.000     338.244   1030.080 Virus_08_B     16
160000:     650.9440     1991.4900    254.429   1677.730    2209.210    22242.300     789.639  12467.000 Virus_08_B     16
        PECy5-5 CD3e_asinh PECy7 CD16-32_asinh DL800 Ly6G_asinh AF700 CD45_asinh APCCy7 CD48_asinh BUV395 CD11b_asinh BUV737 B220_asinh
     1:          0.1030979           1.6392988       -1.1159989        1.9669670         2.3510881          1.0504123         4.2636438
     2:          0.1174943          -0.2782380       -0.5114826        1.6006640         0.7216390          0.5767234         3.8577846
     3:          0.1197291           2.1835405       -0.6723598        1.9247953         1.7806527          2.9452507         0.3638149
     4:          0.5926148           2.3251843       -0.7915047        0.8500738         1.9977594          3.6319912         1.4928782
     5:         -0.1653241           1.8818839       -1.6097071        0.9703694         2.7630370          1.9011864         2.9109315
    ---                                                                                                                                
159996:          0.2112023           1.9157040       -0.2278085        2.1598848         3.5048055          0.2514770         0.4971396
159997:          1.9658468           0.1304844       -1.2947039        4.3648336         3.4509812          0.2419893         0.9511485
159998:         -0.3134175           3.0680563        4.2423561        2.3428298        -0.6723679          3.8792326         0.2409789
159999:         -0.1250411           2.5345014        2.0660180        1.8484549         0.9870066          3.9442068         0.6332984
160000:          1.0796017           2.0905763        0.4891206        1.9252352         2.1914959          4.4884166         1.2379493
        BV605 Ly6C_asinh    Sample Group Batch
     1:        0.2550924 Mock_01_A  Mock     A
     2:        1.3575720 Mock_01_A  Mock     A
     3:        2.7553704 Mock_01_A  Mock     A
     4:        3.2159434 Mock_01_A  Mock     A
     5:        0.5246514 Mock_01_A  Mock     A
    ---                                       
159996:        3.2061873  WNV_08_B Virus     B
159997:        0.2618862  WNV_08_B Virus     B
159998:        2.8385188  WNV_08_B Virus     B
159999:        1.4702206  WNV_08_B Virus     B
160000:        3.9097814  WNV_08_B Virus     B

We can also read in the metadata CSV file from MetaDirectory. This data is already embedded in the main dataset (cell.dat), but it is useful to have the table present as well.

    ### Read in metadata
        
        setwd(MetaDirectory)
        
        list.files(getwd(), ".csv")
        
        meta.dat <- fread("sample.details.csv")
        meta.dat


Define columns


##########################################################################################################
#### Define columns
##########################################################################################################

To get started, we need to define some columns.

    setwd(InputDirectory)
        
    ### Define other cols
        
        as.matrix(names(cell.dat))
      [,1]                 
 [1,] "PECy5-5 CD3e"       
 [2,] "PECy7 CD16-32"      
 [3,] "DL800 Ly6G"         
 [4,] "AF700 CD45"         
 [5,] "APCCy7 CD48"        
 [6,] "BUV395 CD11b"       
 [7,] "BUV737 B220"        
 [8,] "BV605 Ly6C"         
 [9,] "FileName"           
[10,] "FileNo"             
[11,] "PECy5-5 CD3e_asinh" 
[12,] "PECy7 CD16-32_asinh"
[13,] "DL800 Ly6G_asinh"   
[14,] "AF700 CD45_asinh"   
[15,] "APCCy7 CD48_asinh"  
[16,] "BUV395 CD11b_asinh" 
[17,] "BUV737 B220_asinh"  
[18,] "BV605 Ly6C_asinh"   
[19,] "Sample"             
[20,] "Group"              
[21,] "Batch"   

The first columns to specify are the ones that denote the sample name, group name, and batch name/number

        sample.col <- 'Sample'
        group.col <- 'Group'
        batch.col <- 'Batch'   

Next, we need to define which columns reflect cellular markers.

    ### Define cellular cols
        
        as.matrix(names(cell.dat))
        
        cellular.cols <- names(cell.dat)[c(12:20)]
        as.matrix(cellular.cols)

We also need to specify which columns we want to use for clustering.

    ### Define clustering cols
        
        as.matrix(names(cell.dat))
        
        cluster.cols <- names(cell.dat)[c(12:20)]
        as.matrix(cluster.cols)

Finally, we need to specify which columns reflect cellular markers are to be aligned.

    ### Define columns to align
        
        as.matrix(names(cell.dat))
        
        to.align <- names(cell.dat)[c(12:20)]
        as.matrix(to.align)  

Here we can double check what we specified above.

    ### Double check selections
        
        sample.col
        group.col
        batch.col
        
        as.matrix(cellular.cols)
        as.matrix(cluster.cols)
        as.matrix(to.align)


Initial (pre-alignment) plots


##########################################################################################################
#### Initial (pre-alignment) plots
##########################################################################################################

Create an output directory for some initial plots, then perform subsampling and run UMAP on the dataset.

    setwd(OutputDirectory)
    dir.create("Output 2.1 - initial plots")
    setwd("Output 2.1 - initial plots")
        
    ### Pre-alignment UMAP
        
        rm(sub)
        sub <- do.subsample(cell.dat, 10000)
        sub <- run.umap(sub, cluster.cols)

Create some initial plots to assess the data, before proceeding to batch alignment. The first two are UMAP plots coloured by the batch or the experimental group. Evident in the plots from the demo dataset are batch effects that effectively split the neutrophils (top) and B cells (bottom right).

### Create plots           
       
		make.colour.plot(sub, "UMAP_X", "UMAP_Y", batch.col, col.type = 'factor', filename = paste0('Batches.png'))
        make.colour.plot(sub, "UMAP_X", "UMAP_Y", group.col, col.type = 'factor', filename = paste0('Groups.png'))

We can also create three multiplots – one for the cellular markrs, one for the clustering markers, and one for the markers to be aligned. There will be overlap in the plots between these files, but it is convenient to have them grouped in this way.

        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cellular.cols, figure.title = 'Celluar markers')
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cluster.cols, figure.title = 'Clustering markers')
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", to.align, figure.title = 'Markers to align')


    ### Cleanup
        
        rm(sub)


Define and assess reference samples


##########################################################################################################
#### Define and assess reference samples
##########################################################################################################

Create an output directory to save the plots for the reference samples.

    setwd(OutputDirectory)
    dir.create("Output 2 - reference samples")
    setwd("Output 2 - reference samples")

First, review the samples present in the dataset, and take note of which are to be used as reference samples. In this case, we are going to use 'Mock_01_A' (from batch A) and 'Mock_05_B' (from batch B).

    ### Define 'reference' samples
        
        meta.dat
          FileName    Sample Group Batch
 1:  Mock_01_A.csv Mock_01_A  Mock     A
 2:  Mock_02_A.csv Mock_02_A  Mock     A
 3:  Mock_03_A.csv Mock_03_A  Mock     A
 4:  Mock_04_A.csv Mock_04_A  Mock     A
 5: Virus_01_A.csv  WNV_01_A Virus     A
 6: Virus_02_A.csv  WNV_02_A Virus     A
 7: Virus_03_A.csv  WNV_03_A Virus     A
 8: Virus_04_A.csv  WNV_04_A Virus     A
 9:  Mock_05_B.csv Mock_05_B  Mock     B
10:  Mock_06_B.csv Mock_06_B  Mock     B
11:  Mock_07_B.csv Mock_07_B  Mock     B
12:  Mock_08_B.csv Mock_08_B  Mock     B
13: Virus_05_B.csv  WNV_05_B Virus     B
14: Virus_06_B.csv  WNV_06_B Virus     B
15: Virus_07_B.csv  WNV_07_B Virus     B
16: Virus_08_B.csv  WNV_08_B Virus     B  

Review the list of unique sample names within the dataset...

        as.matrix(unique(cell.dat[[sample.col]]))
      [,1]       
 [1,] "Mock_01_A"
 [2,] "Mock_02_A"
 [3,] "Mock_03_A"
 [4,] "Mock_04_A"
 [5,] "Mock_05_B"
 [6,] "Mock_06_B"
 [7,] "Mock_07_B"
 [8,] "Mock_08_B"
 [9,] "WNV_01_A" 
[10,] "WNV_02_A" 
[11,] "WNV_03_A" 
[12,] "WNV_04_A" 
[13,] "WNV_05_B" 
[14,] "WNV_06_B" 
[15,] "WNV_07_B" 
[16,] "WNV_08_B" 

... and then specify which of these represent the reference samples (in this case, samples 1 and 5).

        ref.ctrls <- unique(cell.dat[[sample.col]])[c(1,5)]
        ref.ctrls
[1] "Mock_01_A" "Mock_05_B"

We can then use the do.filter() function to extract only the reference samples from the dataset, and create a new dataset containing only the reference samples (ref.dat).

        ref.dat <- do.filter(cell.dat, use.col = sample.col, values = ref.ctrls)
        ref.dat

We can then perform a few checks to make sure we have set things up correctly.

Check the batches contained within ref.dat.

    ### Check reference in ref.dat and cell.dat
        
        unique(ref.dat[[batch.col]])		
[1] "A" "B"

Check the batches contained within cell.dat.

unique(cell.dat[[batch.col]])
[1] "A" "B"

Check the sample names contained within ref.dat.

unique(ref.dat[[sample.col]])
[1] "Mock_01_A" "Mock_05_B"

Check the sample names contained within cell.dat.

unique(cell.dat[[sample.col]])
 [1] "Mock_01_A" "Mock_02_A" "Mock_03_A" "Mock_04_A" "Mock_05_B" "Mock_06_B" "Mock_07_B" "Mock_08_B" "WNV_01_A"  "WNV_02_A"  "WNV_03_A" 
[12] "WNV_04_A"  "WNV_05_B"  "WNV_06_B"  "WNV_07_B"  "WNV_08_B" 

Now, we'll make a UMAP of just the reference data, to visualise any potential batch effects.

    ### UMAP of reference samples
        
        rm(sub)
        sub <- do.subsample(ref.dat, 10000)
        sub <- run.umap(sub, cluster.cols)
    ### Create plots                  

		make.colour.plot(sub, "UMAP_X", "UMAP_Y", batch.col, col.type = 'factor', filename = paste0('Batches.png'))
        make.colour.plot(sub, "UMAP_X", "UMAP_Y", group.col, col.type = 'factor', filename = paste0('Groups.png'))

        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cellular.cols, figure.title = 'Celluar markers')
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cluster.cols, figure.title = 'Clustering markers')
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", to.align, figure.title = 'Markers to align')

    ### Cleanup
        
        rm(sub)


Perform and validate COARSE alignment (to improve FlowSOM clustering)


##########################################################################################################
#### Perform and validate COARSE alignment (to improve FlowSOM clustering during fine alignment)
##########################################################################################################

Set an output directory for the results of the 'coarse alignment'.

    setwd(OutputDirectory)
    dir.create("Output 2.3 - coarse alignment")
    setwd("Output 2.3 - coarse alignment")
    
    crs.dir <- getwd()

Set the cell.dat as a new object called crs.dat. Check cellular.cols, cluster.cols, and to.align.

    ### Setup
    
        crs.dat <- cell.dat
    
        cellular.cols
        cluster.cols
        to.align

Here we can define the preferred method for coarse alignment. In this case we are going to align via the reference samples, using the 95th percentile of each channel ('95p'). For other options, please see our batch alignment tutorial. Also specify the text to be appended to aligned columns, in this case, '_coarseAlign'.

        method <- '95p'
        crs.append <- '_coarseAlign'

Run the batch alignment on crs.dat.

    ### Coarse alignment
        
        setwd(crs.dir)
        
        crs.dat <- run.align(ref.dat = ref.dat, 
                                target.dat = crs.dat, 
                                batch.col = batch.col, 
                                align.cols = to.align, 
                                method = method, 
                                append.name = crs.append, 
                                dir = crs.dir)

        setwd(crs.dir)
        gc()

Once the alignment is complete, check the results – and ensure that new aligned columns have been added to the dataset.

    ### Check results
        
        as.matrix(names(crs.dat))
        crs.dat

To check the results, we can plot each marker (y-axis) against the aligned version of that marker (a-axis). 

    ### Plot alignment results
        
        setwd(crs.dir)
        dir.create("A - Alignment plots")
        setwd("A - Alignment plots")
        
        rm(sub)
        sub  <- do.subsample(crs.dat, 100000)
        
        for(i in to.align){
            make.colour.plot(sub, paste0(i, crs.append), i, batch.col)
        }
        
        rm(sub)

Next, we can run UMAP to see check what the data distribution looks like, and see to what extent the coarse alignment has removed batch effects.

    ### Examine clustering
        
        setwd(crs.dir)
        dir.create("B - Clustering check")
        setwd("B - Clustering check")
        
        rm(sub)
        sub <- do.subsample(crs.dat, 10000)
        sub <- run.umap(sub, paste0(cluster.cols, crs.append))
        
        make.colour.plot(sub, "UMAP_X", "UMAP_Y", batch.col, col.type = 'factor', filename = paste0('Batches ', method, ".png"))
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cellular.cols, figure.title = paste0("Markers - raw - ", method))
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", paste0(cellular.cols, crs.append), figure.title = paste0("Markers - aligned - ", method))
 
        rm(sub)

Once the coarse alignment has been performed successfully, then assign it back to cell.dat, and then delete crs.dat.

    ### Finalise data and assign as cell.dat
        
        cell.dat <- crs.dat
        rm(crs.dat)

Write the dataset (and corresponding FCS files for each sample) to disk.

    ### Write coarse aligned data
        
        setwd(crs.dir)
        dir.create("C - Coarse aligned data")
        setwd("C - Coarse aligned data")
        
        fwrite(cell.dat, 'cell.dat.csv')
        
        write.files(cell.dat, 
                    file.prefix = "Coarse_aligned", 
                    divide.by = sample.col, 
                    write.csv = FALSE, 
                    write.fcs = TRUE)


Fine alignment with CytoNorm


##########################################################################################################
#### Fine alignment with CytoNorm
##########################################################################################################

Set an output directory for the fine alignment results.

    setwd(OutputDirectory)
    dir.create("Output 2.4 - fine alignment")
    setwd("Output 2.4 - fine alignment")

Next, assign cell.dat as fine.dat and specify some settings.

  • The CytoNorm goal can be the name of a specific batch, or 'mean'
  • The CytoNorm nQ is the number of quantiles for alignment, which is 101 by default.
  • Also specify the text that will be appended to the end of the column names containing aligned data, in this case 'fineAlign'.
    ### Settings
    
        fine.dat <- cell.dat
    
        cytonorm.goal <- 'mean'
        cytonorm.nQ <- 101
        
        fine.append <- '_fineAlign'

Next, re-extract the reference data from cell.dat

    ### Re-extract reference data (from newly coarse aligned cell.dat)
    
        rm(ref.dat)
        
        ref.dat <- do.filter(fine.dat, use.col = sample.col, values = ref.ctrls)
        ref.dat
        
        unique(ref.dat[[sample.col]])
        unique(ref.dat[[batch.col]])

Prepare the CytoNorm alignment model, which includes a clustering step using FlowSOM. In this case, we are going to generate 8 metaclusters (using meta.k = 8). The columns used to create the clusters are the coarse aligned cluster cols (we specify this using paste0(cluster.cols, crs.append).

    ### Prep FlowSOM and perform clustering using ref.dat

        setwd(fine.dir)
        align.model <- prep.cytonorm(dat = ref.dat,
                                     cellular.cols = c(cellular.cols, paste0(cellular.cols, crs.append)),
                                     cluster.cols = paste0(cluster.cols, crs.append),
                                     batch.col = batch.col,
                                     dir = fine.dir,
                                     xdim = 14,
                                     ydim = 14,
                                     meta.k = 8)

You can check that the alignment model object has been created successfully.

str(align.model, 1)

Next, examine the clustering results and determine if they are suitable.

    ### Examine clustering and generate plots

        setwd(fine.dir)
        dir.create("A - Examine referece data clustering")
        setwd("A - Examine referece data clustering")

        sub <- do.subsample(align.model$dt, 10000)
        sub <- run.umap(sub, paste0(cluster.cols, crs.append))

        make.colour.plot(sub, "UMAP_X", "UMAP_Y", "File", col.type = 'factor', filename = "Reference data - batches.png")
        make.colour.plot(sub, "UMAP_X", "UMAP_Y", "prep.fsom.metacluster", col.type = 'factor', add.label = TRUE, filename = "Reference data - metaclusters.png")
       
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", paste0(cellular.cols, crs.append), figure.title = "Reference data - markers - coarse aligned")
        make.multi.plot(sub, "UMAP_X", "UMAP_Y", cellular.cols, figure.title = "Reference data - markers - raw")

        rm(sub)

 In this case, we want to see if the metaclusters capture consistent populations across batches. For example, cluster one should contain only neutrophils (Ly6G+) in both batches.

If the clustering is suitable, then we can train the alignment model.

    ### Train the alignment conversions in the 'align.model' object

        setwd(fine.dir)
        align.model <- train.cytonorm(model = align.model,
                                      align.cols = to.align,
                                      cytonorm.goal = cytonorm.goal,
                                      cytonorm.nQ = cytonorm.nQ,
                                      dir = fine.dir)

        str(align.model, 1)
        gc()

Once the alignment model has been trained, we can apply it to the dataset.

    ### Run cytonorm

        setwd(fine.dir)
        fine.dat <- run.cytonorm(dat = fine.dat,
                                 model = align.model,
                                 batch.col = batch.col,
                                 append.name = fine.append,
                                 dir = fine.dir)

        as.matrix(names(fine.dat))
        fine.dat

Once the fine alignment has been completed, we can check the alignment results to determine whether batch effects have been removed.

    ### Examine results
        
        setwd(fine.dir)
        dir.create("B - Examine cytonorm results")
        setwd("B - Examine cytonorm results")
        
        res <- do.subsample(fine.dat, 10000)
        res <- run.umap(res, paste0(cluster.cols, "_fineAlign"))
        
        make.colour.plot(res, "UMAP_X", "UMAP_Y", batch.col, col.type = 'factor', filename = "Target data - batches.png")
        make.colour.plot(res, "UMAP_X", "UMAP_Y", group.col, col.type = 'factor', filename = "Target data - groups.png")
        
        make.colour.plot(res, "UMAP_X", "UMAP_Y", paste0("Alignment_MC", fine.append), col.type = 'factor', add.label = TRUE, filename = "Target data - metaclusters.png")
        
        make.multi.plot(res, "UMAP_X", "UMAP_Y", paste0(cellular.cols, crs.append), figure.title = "Target data - markers - coarse aligned")
        make.multi.plot(res, "UMAP_X", "UMAP_Y", paste0(cellular.cols, fine.append), figure.title = "Target data - markers - fine aligned")
        make.multi.plot(res, "UMAP_X", "UMAP_Y", cellular.cols, figure.title = "Target data - markers - raw")
        
        rm(res)

As we can see, the batches are now sufficiently mixed together (left), allowing us to interpret relevant biological changes between experimental groups (right).

We can then also create a series of comparison plots, including raw vs coarse aligned, raw vs fine aligned, and coarse aligned vs fine aligned.

    ### Plot comparisons

        sub <- do.subsample(fine.dat, 10000)
        
        crs.append
        fine.append
        
        ## Raw vs coarse
        
            setwd(fine.dir)
            dir.create("C - Comparison plots - raw vs coarse")
            setwd("C - Comparison plots - raw vs coarse")
            
            for(i in cellular.cols){
                a <- paste0(i, crs.append)
                make.colour.plot(sub, a, i, batch.col)
            }
        
        ## Raw vs fine
            
            setwd(fine.dir)
            dir.create("D - Comparison plots - raw vs fine")
            setwd("D - Comparison plots - raw vs fine")

            for(i in cellular.cols){
              a <- paste0(i, fine.append)
              make.colour.plot(sub, a, i, batch.col)
            }

        ## Coarse vs fine    
            
            setwd(fine.dir)
            dir.create("E - Comparison plots - coarse vs fine")
            setwd("E - Comparison plots - coarse vs fine")
    
            for(i in cellular.cols){
              a <- paste0(i, fine.append)
              o <- paste0(i, crs.append)
              make.colour.plot(sub, a, o, batch.col)
            }

 The key plots to review are the raw vs fine aligned data. In this case we can see one string of cells per metacluster, coloured by batch. 

If the alignment looks suitable, then we can assign fine.dat to cell.dat, and then delete fine.dat.

    ### Finalsie data
        
        cell.dat <- fine.dat
        rm(fine.dat)

We can also save the dataset as a CSV file, and corresponding FCS files for each sample.

    ### Save initial data

        setwd(fine.dir)
        dir.create("F - Fine aligned data")
        setwd("F - Fine aligned data")

        fwrite(cell.dat, "cell.dat.csv")
        
        write.files(cell.dat, 
                    file.prefix = "Fine_aligned", 
                    divide.by = sample.col, 
                    write.csv = FALSE, 
                    write.fcs = TRUE)


Save session info


##########################################################################################################
#### Save session info
##########################################################################################################

Finally, we can save the session info to disk.

    setwd(OutputDirectory)
    dir.create("Output - info", showWarnings = FALSE)
    setwd("Output - info")

    ### Save session info to disk
        
        setwd(OutputDirectory)
        dir.create("Output - info", showWarnings = FALSE)
        setwd("Output - info")
        
        sink(file = "session_info.txt", append=TRUE, split=FALSE, type = c("output", "message"))
        session_info()
        sink()


  • No labels