Running FICTURE in a local machine¶
Overview¶
This document provides detailed instructions on how to run FICTURE on a local machine with real data.
This instruction is intended for Ubuntu OS, but it should also work for Mac OS X and other Unix-like systems.
If you rather want to run all steps together
with run_togetehr
command, please refer to Quick start for details.
Setup¶
Input Data¶
A small sub-region of Vizgen MERSCOPE mouse liver data is provided as an example in the GitHub repository
See the following explanation for each file. If you have trouble, try Format input to see some examples of formatting raw output from different platforms.
Transcript file¶
One file contains the molecular or pixel level information, the required columns are X
, Y
, gene
, and Count
. (There could be other columns in the file which would be ignored.)
The coordinates (X, Y)
can be float or integer numbers in arbitrary units, but if it is not in the unit of \(\mu m\) we would need to specify the translation ratio later.
The file has to be sorted by one of the coordinates. (Usually it is the longer axis, but it does not matter if the tissue area is not super asymmetric.)
Count
(could be any other name) is the number of transcripts for the specified gene
observed at the coordinate. For imaging based technologies where each molecule has its unique coordinates, Count
could be always 1.
Gene list file¶
Another file contains the (unique) names of genes that should be used in analysis. The required columns is just gene
(including the header), the naming of genes should match the gene
column in the transcript file. If your data contain negative control probes or if you would like to remove certain genes this is where you can specify. (If you would like to use all genes present in your input transcript file the gene list is not necessary, but you would need to modify the command in examples/script/generic_III.sh
to remove the argument --feature
)
Bounding box of spatial coordinates¶
We also prefer to keep a file listing the min and max of the coordinates (this is primarily for visualizing very big tissue region where we do not read all data at once but would want to know the image dimension). The unit of the coordinates is micrometer.
Note that, when run_together
command is used, the gene list file and bounding box files will be automatically generated.
Prepare environment¶
Activate your virtual environment if needed:
Suppose you have installed FICTURE and dependencies following Install in this environment. Verify FICTURE is successfully installed with command ficture
.
Analysis with FICTURE¶
Key parameters¶
First, specify the base directory that contains the input data
The following data-specific setup may be required:
mu_scale
is the ratio between \(\mu m\) and the unit used in the transcript coordinates. For example, if the coordinates are stored innm
this number should be1000
.key
is the column name in the transcripts file corresponding to the gene counts (Count
in our example).major_axis
specify which axis the transcript file is sorted by. (eitherX
orY
)
mu_scale=1 # If your data's coordinates are already in micrometer
key=Count # If you data has 'Count' as the column name for gene counts
major_axis=Y # If your data is sorted by the Y-axis
Preprocessing¶
Anchor-level minibatch¶
Create pixel minibatches (${path}/batched.matrix.tsv.gz
) that will be used for anchor-level analysis using the following command:
Training hexagons¶
Prepare training hexagons. Even if you need to fit multiple models with different number of factors, you only need to run once for each training width. The training width is the flat-to-flat width of the hexagon in \(\mu m\).
LDA Model training¶
To run FICTURE in a fully unsupervised manner, you need to initialize the model with LDA based on the hexagons created in the previous step.
Parameters for initializing the model¶
Setting the input and output paths¶
Initialize the model with LDA¶
(Optional) Initializing LDA model from pseudo-bulk data¶
Instead of initializing the model using LDA as shown above, if you want to initialize the model using pseudo-bulk data, you can
prepare the pseudo-bulk data as a model matrix in the following TSV format in a tsv.gz
file:
This model matrix can be directly used for pixel-level decoding step described below. However, if the gene list do not match between the pseudo-bulk data and the raw data, you may need to use the following command to initialize the model from the pseudo-bulk data.
After running the following command, the model will be initialized using the pseudo-bulk data.
Visualizing the model¶
The results from the initial model fitting can be visualized using the following commands:
Pixel level decoding¶
Parameters for pixel level decoding¶
After fitting the model, FICTURE performs pixel level decoding to infer the factors for each pixel. The pixel-level decoding consists of two steps: * Perform anchor-level projection based on the fitted model * Perform pixel-level decoding based on anchor-level projection
The following parameters can be used for pixel level decoding steps.
Produce anchor-level projection¶
Anchor-level projection can be performed using the following command:
Perform pixel-level decoding¶
Pixel-level decoding can be performed using the following command:
Optional post-processing¶
Although not required, after performing pixel-level decoding, it is useful to generates summary statistics and visualize the results.
First step is to sort the pixel level output. This is primarily for visualizing large images with limited memory usage.
Next, we can identify differentially expressed genes for each factor. This is a naive pseudo-bulk chi-squared test, please view the results with caution.
Next, generalize pixel level images representing the factorization result
You may also want to generate heatmaps for individual factors. If the data is very large, making all individual factor maps may take some time.
Generate everything in one run
Alternatively, you can generate by batch
Output¶
In the above example the analysis outputs are stored in
There is an html file reporting the color code and top genes of the inferred factors
Pixel level visualization is stored in
Pixel level output is stored in
We store the top 3 factors and their corresponding posterior probabilities for each pixel in tab delimted text files.
As a temporary hack for accessing specific regions in large dataset faster, we divided the data along one axis (X or Y), sorted within each block by the other axis.
The first 3 lines of the file, starting with ##
, are metadata, the 4th line, starting with #
, contains columns names.
To use the file as plain text, you can ignore this complication and read the file from the 4th line.
The first few lines of the file are as follows:
The 4th line contains the column names. From the 5th line on, each line contains the information for one pixel with coordinates (X, Y)
, the top 3 factors indicated by K1, K2, K3
and their corresponding posterior probabilities P1, P2, P3
. Factors are 0-indexed.
The 1st line indicates that the data is from a model with 12 factors (K=12
) and we store the top 3 factors for each pixel (TOPK=3
).
The 2nd line indicates that the data is separated into blocks by the X axis (BLOCK_AXIS=X
) with block size 2000\(\mu m\) (BLOCK_SIZE=2000
), then within each block the data is sorted by the Y axis (INDEX_AXIS=Y
).
The block IDs (first column in the file) are integer multiples of the block size (in \(\mu m\)), i.e. the 1st block, with \(X \in [0, 2000)\) have block ID 0, the 2nd block, with \(X \in [2000, 4000)\) have block ID 2000, etc.
The 3rd line describes the translation between the stored cooredinates and the physical coordinates in \(\mu m\).
Take (X, Y)
as a pixel coordinates read from the file, the physical coordinates in \(\mu m\) is (X / SCALE + OFFSET_X, Y / SCALE + OFFSET_Y)
.
In this above example, the raw data from Vizgen MERSCOPE mouse liver data contains negative coordinates, but for convineince we shifted all coordinates to positive. SIZE_X
and SIZE_Y
record the size of the raw data in \(\mu m\).