|
| 1 | +# EntityViews |
| 2 | +EntityViews in Synapse allow you to create a queryable view that provides a unified selection |
| 3 | +of entities stored in different locations within your Synapse project. This can be |
| 4 | +particularly useful for managing and querying metadata across multiple files, folders, |
| 5 | +or projects that you manage. |
| 6 | + |
| 7 | +Views display rows and columns of information, and they can be shared and queried with |
| 8 | +SQL. Views are queries of other data already in Synapse. They allow you to see groups |
| 9 | +of entities including files, tables, folders, or datasets and any associated |
| 10 | +annotations about those items. |
| 11 | + |
| 12 | +Annotations are an essential component to building a view. Annotations are labels that |
| 13 | +you apply to your data, stored as key-value pairs in Synapse. They help users search |
| 14 | +for and find data, and they are a powerful tool used to systematically group and |
| 15 | +describe things in Synapse. |
| 16 | + |
| 17 | +This tutorial will follow a [Flattened Data Layout](../../explanations/structuring_your_project.md#flattened-data-layout-example). With a project that has this example layout: |
| 18 | +``` |
| 19 | +. |
| 20 | +
|
| 21 | +└── single_cell_RNAseq_batch_1 |
| 22 | + ├── SRR12345678_R1.fastq.gz |
| 23 | + └── SRR12345678_R2.fastq.gz |
| 24 | +``` |
| 25 | + |
| 26 | +## Tutorial Purpose |
| 27 | +In this tutorial you will: |
| 28 | + |
| 29 | +1. Create a EntityView with a number of columns |
| 30 | +2. Query the EntityView |
| 31 | +3. Update rows in the EntityView |
| 32 | +4. Update the scope of your EntityView |
| 33 | +5. Update the types of entities in your EntityView |
| 34 | + |
| 35 | +## Prerequisites |
| 36 | +* This tutorial assumes that you have a project in Synapse with one or more |
| 37 | +files/folders. It does not need to match the given structure in this tutorial, but, if |
| 38 | +you do not have this already set up you may reference the [Folder](./folder.md) |
| 39 | +and [File](./file.md) tutorials. |
| 40 | +* Pandas must be installed as shown in the [installation documentation](../installation.md) |
| 41 | + |
| 42 | + |
| 43 | +## 1. Find the synapse ID of your project |
| 44 | + |
| 45 | +First let's set up some constants we'll use in this script, and find the ID of our project |
| 46 | +```python |
| 47 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=5-22} |
| 48 | +``` |
| 49 | + |
| 50 | +## 2. Create a EntityView with Columns |
| 51 | + |
| 52 | +Now, we will create 4 columns to add to our EntityView. Recall that any data added to |
| 53 | +these columns will be stored as an annotation on the underlying File. |
| 54 | + |
| 55 | +```python |
| 56 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=24-31} |
| 57 | +``` |
| 58 | + |
| 59 | +Next we're going to store what we have to Synapse and print out the results |
| 60 | + |
| 61 | +```python |
| 62 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=33-47} |
| 63 | +``` |
| 64 | + |
| 65 | +## 3. Query the EntityView |
| 66 | + |
| 67 | +```python |
| 68 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=49-54} |
| 69 | +``` |
| 70 | + |
| 71 | +<details class="example"> |
| 72 | + <summary>The result of querying your File View should look like:</summary> |
| 73 | +``` |
| 74 | + id name species dataType... |
| 75 | +0 syn1 SRR12345678_R1.fastq.gz Homo sapiens geneExpression |
| 76 | +1 syn2 SRR12345678_R1.fastq.gz Homo sapiens geneExpression |
| 77 | +``` |
| 78 | +</details> |
| 79 | + |
| 80 | +## 4. Update rows in the EntityView |
| 81 | + |
| 82 | +Now that we know the data is present in the EntityView, let's go ahead and update the |
| 83 | +annotations on these Files. The following code sets all returned rows to a single |
| 84 | +value. Since the results were returned as a Pandas DataFrame you have many |
| 85 | +options to search through and set values on your data. |
| 86 | + |
| 87 | +```python |
| 88 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=56-66} |
| 89 | +``` |
| 90 | + |
| 91 | +A note on `wait_for_eventually_consistent_view`: EntityViews in Synapse are eventually |
| 92 | +consistent, meaning that updates to data may take some time to be reflected in the |
| 93 | +view. The `wait_for_eventually_consistent_view` flag allows the code to pause until |
| 94 | +the changes are fully propagated to your EntityView. When this flag is set to `True` a |
| 95 | +query is automatically executed on the view to determine if the view contains the |
| 96 | +updated changes. It will allow your next query on your view to reflect any changes that |
| 97 | +you made. Conversely, if this is set to `False`, there is no guarantee that your next |
| 98 | +query will reflect your most recent changes. |
| 99 | + |
| 100 | +## 5. Update the scope of your EntityView |
| 101 | + |
| 102 | +As your project expands or contracts you will need to adjust the containers you'd like |
| 103 | +to include in your view. In order to accomplish this you may modify the `scope_ids` |
| 104 | +attribute on your view. |
| 105 | + |
| 106 | +```python |
| 107 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=69-73} |
| 108 | +``` |
| 109 | + |
| 110 | +## 6. Update the types of Entities included in your EntityView |
| 111 | + |
| 112 | +You may also want to change what types of Entities may be included in your view. To |
| 113 | +accomplish this you'll be modifying the `view_type_mask` attribute on your view. |
| 114 | + |
| 115 | +```python |
| 116 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!lines=75-79} |
| 117 | +``` |
| 118 | + |
| 119 | +## Results |
| 120 | +Now that you have created and updated your File View, you can inspect it in the |
| 121 | +Synapse web UI. It should look similar to: |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +## Source code for this tutorial |
| 126 | + |
| 127 | +<details class="quote"> |
| 128 | + <summary>Click to show me</summary> |
| 129 | + |
| 130 | +```python |
| 131 | +{!docs/tutorials/python/tutorial_scripts/entityview.py!} |
| 132 | +``` |
| 133 | +</details> |
| 134 | + |
| 135 | +## References used in this tutorial |
| 136 | + |
| 137 | +- [EntityView](../../reference/experimental/sync/entityview.md) |
| 138 | +- [Column][synapseclient.models.Column] |
| 139 | +- [syn.login][synapseclient.Synapse.login] |
| 140 | +- [Project](../../reference/experimental/sync/project.md) |
0 commit comments