|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | +# use this file except in compliance with the License. You may obtain a copy of |
| 5 | +# the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations under |
| 13 | +# the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for Genome.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import os |
| 22 | +import numpy as np |
| 23 | + |
| 24 | +import tensorflow as tf |
| 25 | +import tensorflow_io as tfio # pylint: disable=wrong-import-position |
| 26 | + |
| 27 | +fastq_path = os.path.join( |
| 28 | + os.path.dirname(os.path.abspath(__file__)), |
| 29 | + "test_genome", "test.fastq") |
| 30 | + |
| 31 | +def test_genome_fastq_reader(): |
| 32 | + """test_genome_fastq_reader""" |
| 33 | + |
| 34 | + data = tfio.genome.read_fastq(filename=fastq_path) |
| 35 | + |
| 36 | + data_expected = [ |
| 37 | + b'GATTACA', |
| 38 | + b'CGTTAGCGCAGGGGGCATCTTCACACTGGTGACAGGTAACCGCCGTAGTAAAGGTTCCGCCTTTCACT', |
| 39 | + b'CGGCTGGTCAGGCTGACATCGCCGCCGGCCTGCAGCGAGCCGCTGC', |
| 40 | + b'CGG'] |
| 41 | + |
| 42 | + quality_expected = [ |
| 43 | + b'BB>B@FA', |
| 44 | + b'AAAAABF@BBBDGGGG?FFGFGHBFBFBFABBBHGGGFHHCEFGGGGG?FGFFHEDG3EFGGGHEGHG', |
| 45 | + b'FAFAF;F/9;.:/;999B/9A.DFFF;-->.AAB/FC;9-@-=;=.', |
| 46 | + b'FAD'] |
| 47 | + |
| 48 | + assert np.all(data.sequences == data_expected) |
| 49 | + assert np.all(data.raw_quality == quality_expected) |
| 50 | + |
| 51 | + |
| 52 | +def test_genome_sequences_to_onehot(): |
| 53 | + """test sequence one hot encoder""" |
| 54 | + expected = [ |
| 55 | + [[0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], |
| 56 | + [0, 1, 0, 0], [1, 0, 0, 0]], |
| 57 | + [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], |
| 58 | + [0, 0, 1, 0], [0, 1, 0, 0], |
| 59 | + [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], |
| 60 | + [0, 0, 1, 0], [0, 0, 1, 0], |
| 61 | + [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], |
| 62 | + [0, 0, 0, 1], [0, 0, 0, 1], |
| 63 | + [0, 1, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], |
| 64 | + [0, 0, 0, 1], [0, 0, 1, 0], |
| 65 | + [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], |
| 66 | + [1, 0, 0, 0], [0, 0, 1, 0], |
| 67 | + [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], |
| 68 | + [0, 1, 0, 0], [0, 0, 1, 0], |
| 69 | + [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], |
| 70 | + [0, 0, 1, 0], [0, 0, 0, 1], |
| 71 | + [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], |
| 72 | + [0, 0, 0, 1], [0, 0, 0, 1], |
| 73 | + [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 1, 0, 0], |
| 74 | + [0, 0, 0, 1], [0, 0, 0, 1], |
| 75 | + [0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]], |
| 76 | + [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], |
| 77 | + [0, 0, 1, 0], [0, 0, 1, 0], |
| 78 | + [0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], |
| 79 | + [0, 1, 0, 0], [0, 0, 0, 1], |
| 80 | + [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], |
| 81 | + [0, 1, 0, 0], [0, 0, 1, 0], |
| 82 | + [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 1, 0, 0], |
| 83 | + [0, 0, 1, 0], [0, 0, 1, 0], |
| 84 | + [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], |
| 85 | + [1, 0, 0, 0], [0, 0, 1, 0], |
| 86 | + [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], |
| 87 | + [0, 1, 0, 0], [0, 0, 1, 0], |
| 88 | + [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]], |
| 89 | + [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]] |
| 90 | + |
| 91 | + raw_data = tfio.genome.read_fastq(filename=fastq_path) |
| 92 | + data = tfio.genome.sequences_to_onehot( |
| 93 | + sequences=raw_data.sequences) |
| 94 | + |
| 95 | + assert np.all(data.to_list() == expected) |
| 96 | + |
| 97 | + |
| 98 | +def test_genome_phred_sequences_to_probability(): |
| 99 | + """Test conversion of phred qualities to probabilities""" |
| 100 | + example_quality_list = [b'BB<', b'ABFF'] |
| 101 | + expected_probabilities = [0.0005011872854083776, 0.0005011872854083776, |
| 102 | + 0.0019952619913965464, 0.0006309572490863502, |
| 103 | + 0.0005011872854083776, 0.00019952621369156986, |
| 104 | + 0.00019952621369156986] |
| 105 | + |
| 106 | + example_quality = tf.constant(example_quality_list) |
| 107 | + converted_phred = tfio.genome.phred_sequences_to_probability( |
| 108 | + example_quality) |
| 109 | + |
| 110 | + # Compare flat values |
| 111 | + assert np.allclose( |
| 112 | + converted_phred.flat_values.numpy().flatten(), expected_probabilities) |
| 113 | + # Ensure nested array lengths are correct |
| 114 | + assert np.all( |
| 115 | + [len(a) == len(b) |
| 116 | + for a, b in zip(converted_phred.to_list(), example_quality_list)]) |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + test.main() |
0 commit comments