Views

A view is a view of all entities (File, Folder, Project, Table, Docker Repository, View) within one or more Projects or Folders. Views can:

  • Provide a way of isolating or linking data based on similarities
  • Provide the ability to link entities together by their annotations
  • Allow view/editing entities attributes in bulk
  • Allow entities to be easily searched and queried

Preliminaries:

library(synapser)
## 
## TERMS OF USE NOTICE:
##   When using Synapse, remember that the terms and conditions of use require that you:
##   1) Attribute data contributors when discussing these data or results from these data.
##   2) Not discriminate, identify, or recontact individuals or groups represented by the data.
##   3) Use and contribute only data de-identified to HIPAA standards.
##   4) Redistribute data only under these same terms of use.
synLogin()
## Welcome, test!
## NULL
# Create a new project
# use hex_digits to generate random string
hex_digits <- c(as.character(0:9), letters[1:6])
projectName <- sprintf("My unique project %s", paste0(sample(hex_digits, 32, replace = TRUE), collapse = ""))
project <- Project(projectName)
project <- synStore(project)

# Create some files
filePath <- tempfile()
connection <- file(filePath)
writeChar("this is the content of the first file", connection, eos = NULL)
close(connection)
file <- File(path = filePath, parent = project)
file <- synStore(file)
## ################################################## Uploading file to Synapse storage ##################################################
Uploading [--------------------]0.00%   0.0bytes/37.0bytes  file86104a1ec12b     
Uploading [####################]100.00%   37.0bytes/37.0bytes (31.0bytes/s) file86104a1ec12b Done...

filePath2 <- tempfile()
connection2 <- file(filePath2)
writeChar("this is the content of the second file", connection, eos = NULL)
close(connection2)
file2 <- File(path = filePath2, parent = project)
file2 <- synStore(file2)
## ################################################## Uploading file to Synapse storage ##################################################
Uploading [--------------------]0.00%   0.0bytes/38.0bytes  file8610291c1411     
Uploading [####################]100.00%   38.0bytes/38.0bytes (67.4bytes/s) file8610291c1411 Done...

# add some annotations
synSetAnnotations(file, list(contributor = "Sage", class = "V"))
## $class
## $class[[1]]
## [1] "V"
## 
## 
## $contributor
## $contributor[[1]]
## [1] "Sage"
synSetAnnotations(file2, list(contributor = "UW", rank = "X"))
## $rank
## $rank[[1]]
## [1] "X"
## 
## 
## $contributor
## $contributor[[1]]
## [1] "UW"

Creating a View:

view <- EntityViewSchema(name = "my first file view",
                         columns = c(
                           Column(name = "contributor", columnType = "STRING"),
                           Column(name = "class", columnType = "STRING"),
                           Column(name = "rank", columnType = "STRING")),
                         parent = project$properties$id,
                         scopes = project$properties$id,
                         includeEntityTypes = c(EntityViewType$FILE, EntityViewType$FOLDER),
                         add_default_columns = TRUE)

view <- synStore(view)
## 
 [####################]100.00%   1/1   Done...

We support the following entity type in a View:

EntityViewType
## $DOCKER
## EntityViewType.DOCKER
## 
## $FILE
## EntityViewType.FILE
## 
## $FOLDER
## EntityViewType.FOLDER
## 
## $PROJECT
## EntityViewType.PROJECT
## 
## $TABLE
## EntityViewType.TABLE
## 
## $VIEW
## EntityViewType.VIEW

To see the content of your newly created View, use synTableQuery():

queryResults <- synTableQuery(sprintf("select * from %s", view$properties$id))
## 
 [####################]100.00%   1/1   Done...    
Downloading  [####################]100.00%   834.0bytes/834.0bytes (502.1kB/s) SYNAPSE_TABLE_QUERY_2926613.csv Done...
data <- as.data.frame(queryResults)
data
##     ROW_ID ROW_VERSION                             ROW_ETAG          id
## 1 11769481           1 4836e928-7466-44a7-8764-0b80b0de8dc3 syn11769481
## 2 11769482           1 c12a41f9-bd78-434b-98a3-c9739e4d459f syn11769482
##               name           createdOn createdBy
## 1 file86104a1ec12b 2021-04-28 22:02:16   3336368
## 2 file8610291c1411 2021-04-28 22:02:18   3336368
##                                   etag type currentVersion    parentId
## 1 4836e928-7466-44a7-8764-0b80b0de8dc3 file              1 syn11769480
## 2 c12a41f9-bd78-434b-98a3-c9739e4d459f file              1 syn11769480
##   benefactorId   projectId          modifiedOn modifiedBy dataFileHandleId
## 1  syn11769480 syn11769480 2021-04-28 22:02:19    3336368          2926611
## 2  syn11769480 syn11769480 2021-04-28 22:02:19    3336368          2926612
##   dataFileSizeBytes                   dataFileMD5Hex contributor class rank
## 1                37 47dfe7f5eaa49a5413c7b79b67ab9c43        Sage     V <NA>
## 2                38 ba01e01b9e3ffea3ebef95efa62998b0          UW  <NA>    X

Updating Annotations using View

To update ‘class’ annotation for ‘file2’, simply update the view:

data["class"] <- c("V", "VI")
synStore(Table(view$properties$id, data))

The change in annotations is reflected in synGetAnnotations():

synGetAnnotations(file2$properties$id)

A unique etag is associated with every file that updates when changes are made to a file, including the contents, annotations, or metadata. Any updates pushed to Synapse will change an object’s etag.

data$etag

There may be cases where you want to update the annotations on a subset of files in a view. In order to preserve the etag, and thus the file history, you will need to store only the rows that have been modified.

data$contributor[1] <- c("Sage Bionetworks")
synStore(Table(view$properties$id, data[1,]))
## 
Uploading [--------------------]0.00%   0.0bytes/555.0bytes  file861036131520     
Uploading [####################]100.00%   555.0bytes/555.0bytes (503.7bytes/s) file861036131520 Done...    
 [####################]100.00%   1/1   Done...
## <synapseclient.table.CsvFileTable object at 0x143ff3080>

Update View’s Content

A view can contain different types of entity. To change the types of entity that will show up in a view:

view <- synGet(view$properties$id)
view$set_entity_types(list(EntityViewType$FILE))
## NULL

A View is a Table. Please visit Tables vignettes to see how to change schema, update content, and other operations that can be done on View.

synDelete(project)
## NULL