Files

Overview

Synapse files can be created by uploading content from your local computer or linking to digital files on the web.

Files in Synapse always have a “parent”, which could be a project or a folder. You can organize collections of files into folders and sub-folders, just as you would on your local computer.

See Domain Models of Synapse for more background on Files.

Note: You may optionally follow the Uploading data in bulk tutorial instead. The bulk tutorial may fit your needs better as it limits the amount of code that you are required to write and maintain.

This tutorial follows a Flattened Data Layout. With this example layout:

.
├── biospecimen_experiment_1
│   ├── fileA.txt
│   └── fileB.txt
├── biospecimen_experiment_2
│   ├── fileC.txt
│   └── fileD.txt
├── single_cell_RNAseq_batch_1
│   ├── SRR12345678_R1.fastq.gz
│   └── SRR12345678_R2.fastq.gz
└── single_cell_RNAseq_batch_2
    ├── SRR12345678_R1.fastq.gz
    └── SRR12345678_R2.fastq.gz

In this tutorial you will:

  1. Upload several files to Synapse
  2. Print stored attributes about your files
  3. List all Folders and Files within your project

Prerequisites

1. Upload several files to Synapse

First, retrieve the project and folders created in the Folders vignette

library(synapser)
synLogin()

my_project <- Project(name = "My uniquely named project about Alzheimer's Disease") |> synGet()

# Retrieve the folders I want to upload to
batch_1_folder <- Folder(parent_id = my_project$id, name = "single_cell_RNAseq_batch_1") |> synGet()
batch_2_folder <- Folder(parent_id = my_project$id, name = "single_cell_RNAseq_batch_2") |> synGet()
biospecimen_experiment_1_folder <- Folder(parent_id = my_project$id, name = "biospecimen_experiment_1") |> synGet()
biospecimen_experiment_2_folder <- Folder(parent_id = my_project$id, name = "biospecimen_experiment_2") |> synGet()

Next let’s create all of the File objects to upload content

biospecimen_experiment_1_a_2022 <- File(
  path = "~/my_ad_project/biospecimen_experiment_1/fileA.txt",
  parent_id = biospecimen_experiment_1_folder$id
) |> synStore()

biospecimen_experiment_1_b_2022 <- File(
  path = "~/my_ad_project/biospecimen_experiment_1/fileB.txt",
  parent_id = biospecimen_experiment_1_folder$id
) |> synStore()

biospecimen_experiment_2_c_2023 <- File(
  path = "~/my_ad_project/biospecimen_experiment_2/fileC.txt",
  parent_id = biospecimen_experiment_2_folder$id
) |> synStore()

biospecimen_experiment_2_d_2023 <- File(
  path = "~/my_ad_project/biospecimen_experiment_2/fileD.txt",
  parent_id = biospecimen_experiment_2_folder$id
) |> synStore()

batch_1_scrnaseq_file_1 <- File(
  path = "~/my_ad_project/single_cell_RNAseq_batch_1/SRR12345678_R1.fastq.gz",
  parent_id = batch_1_folder$id
) |> synStore()

batch_1_scrnaseq_file_2 <- File(
  path = "~/my_ad_project/single_cell_RNAseq_batch_1/SRR12345678_R2.fastq.gz",
  parent_id = batch_1_folder$id
) |> synStore()

batch_2_scrnaseq_file_1 <- File(
  path = "~/my_ad_project/single_cell_RNAseq_batch_2/SRR12345678_R1.fastq.gz",
  parent_id = batch_2_folder$id
) |> synStore()

batch_2_scrnaseq_file_2 <- File(
  path = "~/my_ad_project/single_cell_RNAseq_batch_2/SRR12345678_R2.fastq.gz",
  parent_id = batch_2_folder$id
) |> synStore()

3. List all Folders and Files within my project

Now that your project has a number of Folders and Files, let’s explore how we can traverse the content stored within the Project:

my_project <- my_project |> synSyncFromSynapse(download_file = FALSE)
dir_mapping <- my_project |> synMapDirectoryToAllContainedFiles(root_path = "./")

for (directory_name in names(dir_mapping)) {
  file_entities <- dir_mapping[[directory_name]]
  cat("Directory:", directory_name, "\n")
  for (file_entity in file_entities) {
    cat("\tFile:", file_entity$name, ", ID:", file_entity$id, "\n")
  }
}

The result of walking your project structure should look something like:

Directory: ./biospecimen_experiment_1
    File: fileA.txt, ID: syn60115444
    File: fileB.txt, ID: syn60115457
Directory: ./biospecimen_experiment_2
    File: fileC.txt, ID: syn60115472
    File: fileD.txt, ID: syn60115485
Directory: ./single_cell_RNAseq_batch_1
    File: SRR12345678_R1.fastq.gz, ID: syn60115498
    File: SRR12345678_R2.fastq.gz, ID: syn60115513
Directory: ./single_cell_RNAseq_batch_2
    File: SRR12345678_R1.fastq.gz, ID: syn60115526
    File: SRR12345678_R2.fastq.gz, ID: syn60115539

Results

Now that you have created your files you’ll be able to inspect them on the Files tab of your project in the Synapse web UI. It should look similar to:

File hierarchy shown on the Files tab of a Synapse project
File hierarchy shown on the Files tab of a Synapse project

Clean Up

Delete the project and everything in it when done:

synDelete(my_project)

References used in this tutorial

  • File
  • synLogin
  • synFindEntityId
  • synStore
  • Folder_Walk