|
| 1 | + |
| 2 | +# How to: Compute a seqcol digest given a sequence collection |
| 3 | + |
| 4 | +## Use case |
| 5 | + |
| 6 | + |
| 7 | +One of the most common uses of the seqcol specification is to compute a standard, universal identifier for a particular sequence collection. You have a collection of sequences, like a reference genome or transcriptome, and you want to determine its seqcol identifier. There are two ways to approach this: 1. Using an existing implementation; 2. Implement the seqcol digest algorithm yourself (it's not that hard). |
| 8 | + |
| 9 | + |
| 10 | +## 1. Using existing implementations |
| 11 | + |
| 12 | +### Reference implementation in Python |
| 13 | + |
| 14 | +If working from within Python, you can use the reference implementation like this: |
| 15 | + |
| 16 | +1. Install the seqcol package with some variant of `pip install seqcol`. |
| 17 | +2. Build up your canonical seqcol object |
| 18 | +3. Compute its digest: |
| 19 | + |
| 20 | +``` |
| 21 | +import seqcol |
| 22 | +seqcol.digest(seqcol_obj) |
| 23 | +``` |
| 24 | + |
| 25 | +If you have a FASTA file, you could get a canonical seqcol object like this: |
| 26 | + |
| 27 | +``` |
| 28 | +seqcol_obj = seqcol.csc_from_fasta(fa_file) |
| 29 | +``` |
| 30 | + |
| 31 | +## 2. Implement the seqcol digest algorithm yourself |
| 32 | + |
| 33 | +Follow the procedure under the section for [Encoding](/specification/#1-encoding-computing-sequence-digests-from-sequence-collections). Briefly, the steps are: |
| 34 | + |
| 35 | +- **Step 1**. Organize the sequence collection data into *canonical seqcol object representation*. |
| 36 | +- **Step 2**. Apply [RFC-8785 JSON Canonicalization Scheme](https://www.rfc-editor.org/rfc/rfc8785) (JCS) to canonicalize the value associated with each attribute individually. |
| 37 | +- **Step 3**. Digest each canonicalized attribute value using the GA4GH digest algorithm. |
| 38 | +- **Step 4**. Apply [RFC-8785 JSON Canonicalization Scheme](https://www.rfc-editor.org/rfc/rfc8785) again to canonicalize the JSON of new seqcol object representation. |
| 39 | +- **Step 5**. Digest the final canonical representation again. |
| 40 | + |
| 41 | +Details on each step can be found in the specification. |
| 42 | + |
| 43 | + |
| 44 | +### Example Python code for computing a seqcol encoding |
| 45 | + |
| 46 | +```python |
| 47 | +# Demo for encoding a sequence collection |
| 48 | + |
| 49 | +import binascii |
| 50 | +import hashlib |
| 51 | +import json |
| 52 | + |
| 53 | +def canonical_str(item: dict) -> str: |
| 54 | + """Convert a dict into a canonical string representation""" |
| 55 | + return json.dumps( |
| 56 | + item, separators=(",", ":"), ensure_ascii=False, allow_nan=False, sort_keys=True |
| 57 | + ) |
| 58 | + |
| 59 | +def trunc512_digest(seq, offset=24): |
| 60 | + """ GA4GH digest function """ |
| 61 | + digest = hashlib.sha512(seq.encode()).digest() |
| 62 | + hex_digest = binascii.hexlify(digest[:offset]) |
| 63 | + return hex_digest.decode() |
| 64 | + |
| 65 | +# 1. Get data as canonical seqcol object representation |
| 66 | + |
| 67 | +seqcol_obj = { |
| 68 | + "lengths": [ |
| 69 | + 248956422, |
| 70 | + 133797422, |
| 71 | + 135086622 |
| 72 | + ], |
| 73 | + "names": [ |
| 74 | + "chr1", |
| 75 | + "chr2", |
| 76 | + "chr3" |
| 77 | + ], |
| 78 | + "sequences": [ |
| 79 | + "2648ae1bacce4ec4b6cf337dcae37816", |
| 80 | + "907112d17fcb73bcab1ed1c72b97ce68", |
| 81 | + "1511375dc2dd1b633af8cf439ae90cec" |
| 82 | + ] |
| 83 | +} |
| 84 | + |
| 85 | +# Step 1a: We would here need to remove any non-inherent attributes, |
| 86 | +# so that only the inherent attributes contribute to the digest. |
| 87 | +# In this example, all attributes are inherent. |
| 88 | + |
| 89 | +# Step 2: Apply RFC-8785 to canonicalize the value |
| 90 | +# associated with each attribute individually. |
| 91 | + |
| 92 | +seqcol_obj2 = {} |
| 93 | +for attribute in seqcol_obj: |
| 94 | + seqcol_obj2[attribute] = canonical_str(seqcol_obj[attribute]) |
| 95 | +seqcol_obj2 # visualize the result |
| 96 | + |
| 97 | +# Step 3: Digest each canonicalized attribute value |
| 98 | +# using the GA4GH digest algorithm. |
| 99 | + |
| 100 | +seqcol_obj3 = {} |
| 101 | +for attribute in seqcol_obj2: |
| 102 | + seqcol_obj3[attribute] = trunc512_digest(seqcol_obj2[attribute]) |
| 103 | +print(json.dumps(seqcol_obj3, indent=2)) # visualize the result |
| 104 | + |
| 105 | +# Step 4: Apply RFC-8785 again to canonicalize the JSON |
| 106 | +# of new seqcol object representation. |
| 107 | + |
| 108 | +seqcol_obj4 = canonical_str(seqcol_obj3) |
| 109 | +seqcol_obj4 # visualize the result |
| 110 | + |
| 111 | +# Step 5: Digest the final canonical representation again. |
| 112 | + |
| 113 | +seqcol_digest = trunc512_digest(seqcol_obj4) |
| 114 | +``` |
0 commit comments