<jupyter_start><jupyter_text>Loading an HCA matrix into seuratThis vignette illustrates requesting an expression matrix from the HCA matrix service and loading it into seurat.First, install and import some dependencies:<jupyter_code>library(httr)
install.packages('Seurat')
library(Seurat)<jupyter_output>Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘bitops’, ‘lsei’, ‘bibtex’, ‘gbRd’, ‘gtools’, ‘gdata’, ‘caTools’, ‘colorspace’, ‘R.oo’, ‘R.methodsS3’, ‘gtable’, ‘npsurv’, ‘globals’, ‘listenv’, ‘reshape2’, ‘viridisLite’, ‘plyr’, ‘zoo’, ‘Rdpack’, ‘tidyr’, ‘hexbin’, ‘crosstalk’, ‘gplots’, ‘labeling’, ‘munsell’, ‘gridExtra’, ‘R.utils’, ‘ape’, ‘cowplot’, ‘fitdistrplus’, ‘future’, ‘future.apply’, ‘ggplot2’, ‘ggrepel’, ‘ggridges’, ‘ica’, ‘igraph’, ‘irlba’, ‘lmtest’, ‘metap’, ‘pbapply’, ‘plotly’, ‘RANN’, ‘RColorBrewer’, ‘reticulate’, ‘ROCR’, ‘rsvd’, ‘Rtsne’, ‘scales’, ‘sctransform’, ‘SDMTools’, ‘tsne’, ‘RcppEigen’, ‘RcppProgress’<jupyter_text>Now, we're going to make some requests to describe what fields and values we can filter on when we're selecting our matrix.<jupyter_code>r <- GET("https://matrix.data.humancellatlas.org/v1/filters")
content(r)<jupyter_output><empty_output><jupyter_text>That's the list of metadata fields we can filter on when requesting the matrix. We can describe any of them with further API calls:<jupyter_code>r <- GET("https://matrix.data.humancellatlas.org/v1/filters/project.project_core.project_short_name")
content(r)
r <- GET("https://matrix.data.humancellatlas.org/v1/filters/genes_detected")
content(r)<jupyter_output><empty_output><jupyter_text>For categorical data, we see the number of cells associated with each category. For numeric, we see the range of value. If we want to request a matrix based on these metadata values, we can add them to the filter in the body of a POST request to the matrix service:<jupyter_code>payload = list(
    filter =  list(
          op = "and", 
          value = list(
              list(op = "=", value = "Single cell transcriptome analysis of human pancreas",
                   field = "project.project_core.project_short_name"),
              list(op = ">=", value = 300,
                   field = "genes_detected")
    )),
    format = "csv"
)
r <- POST("https://matrix.data.humancellatlas.org/v1/matrix", body = payload, encode = "json")
response <- content(r)
print(response)<jupyter_output>$eta
[1] ""

$matrix_url
[1] ""

$message
[1] "Job started."

$request_id
[1] "d8719e49-c00f-468c-81b4-84e39d290d79"

$status
[1] "In Progress"<jupyter_text>That call responds right away and tells us that the matrix is being prepared. We can use the request_id to wait until the matrix is done.<jupyter_code>request_id <- response["request_id"]
status <- response["status"]
message(status)
while (status != "Complete") 
{
    url = paste("https://matrix.data.humancellatlas.org/v1/matrix/", request_id, sep = "")
    r <- GET(url)
    response <- content(r)
    status = response["status"]
    message(status)
    Sys.sleep(15)
}
print(response)<jupyter_output>In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
In Progress
Complete<jupyter_text>Now, that the matrix is ready, we can download it. The file we download is a zip archive that contains a csv-formatted matrix and associated gene and cell metadata. Other formats (loom, mtx) can be specified in the matrix request.<jupyter_code>matrix_file_url = unlist(response["matrix_url"])

download.file(url=matrix_file_url,
              destfile='matrix.zip', method='curl')
unzip("matrix.zip", exdir = "./")<jupyter_output><empty_output><jupyter_text>Finally, we load the expression matrix into a seurat object.<jupyter_code>data_dir = paste("./", request_id, ".csv/", sep = "")
list.files(data_dir)
raw_counts<-t(read.table(file=paste0(data_dir,"expression.csv"),sep=",", header=T,row.names=1))
cell_metadata<-read.table(file=paste0(data_dir,"cells.csv"),sep=",", header=T,row.names=1)
head(raw_counts)
head(cell_metadata)
pancreas <- CreateSeuratObject(
    counts = raw_counts, 
    project = "Single cell transcriptome analysis of human pancreas",
    meta.data = cell_metadata,
    assay = "Smart-seq-2")
pancreas
pancreas <- FindVariableFeatures(pancreas, selection.method = "vst", nfeatures = 2000)

# Identify the 10 most highly variable genes
top10 <- head(VariableFeatures(pancreas), 10)
top10<jupyter_output>Warning message:
“All object keys must be alphanumeric characters, followed by an underscore ('_'), setting key to 'smartseq2_'”
{'fraction_non_alphanumeric': 0.1316339323025491, 'fraction_numerical': 0.012327622231508567, 'mean_word_length': 5.200777202072539, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 26, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '433213', 'license': 'MIT', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1536, 'n_words': 454, 'path': 'docs/HCA Matrix Service to Seurat.ipynb', 'repo_name': 'ambrosejcarr/matrix-service', 'size': '4786'}