|
| 1 | +/** |
| 2 | + * @module Taxon |
| 3 | + * @author Erik Pearson |
| 4 | + * @version 0.1.0 |
| 5 | + * @param {TaxonLibrary} taxon |
| 6 | + * @param {TriftLibrary} Thrift |
| 7 | + * @param {BluebirdPromise} Promise |
| 8 | + * @returns {Taxon_L12.factory} |
| 9 | + */ |
| 10 | +/*global define*/ |
| 11 | +/*jslint white: true, browser: true*/ |
| 12 | +define([ |
| 13 | + 'bluebird', |
| 14 | + 'taxon_service', |
| 15 | + 'thrift', |
| 16 | + |
| 17 | + // These don't have representations. Loading them causes the Thrift module |
| 18 | + // to be enhanced with additional properties (typically just a single |
| 19 | + // property, the new capability added.) |
| 20 | + 'thrift_transport_xhr', |
| 21 | + 'thrift_protocol_binary' |
| 22 | +], function (Promise, taxon, Thrift) { |
| 23 | + 'use strict'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Represents an interface to the Taxon data service. |
| 27 | + * @alias module:Taxon |
| 28 | + * @constructs Taxon |
| 29 | + * @param {object} config |
| 30 | + * @param {ObjectReference} config.ref The object reference for the object to be accessed. |
| 31 | + * @param {string} config.url The url for the Taxon Service endpoint. |
| 32 | + * @param {string} config.token The KBase authorization token to be used to access the service. |
| 33 | + * @returns {Taxon} A taxon api object |
| 34 | + */ |
| 35 | + var Taxon = function (config) { |
| 36 | + var objectReference, |
| 37 | + dataAPIUrl, |
| 38 | + authToken, |
| 39 | + timeout; |
| 40 | + |
| 41 | + // Construction argument contract enforcement, throw useful exceptions |
| 42 | + if (!config) { |
| 43 | + throw { |
| 44 | + type: 'ArgumentError', |
| 45 | + name: 'ConfigurationObjectMissing', |
| 46 | + message: 'Configuration object missing', |
| 47 | + suggestion: 'This is an API usage error; the taxon factory object is required to have a single configuration object as an argument.' |
| 48 | + }; |
| 49 | + } |
| 50 | + objectReference = config.ref; |
| 51 | + if (!objectReference) { |
| 52 | + throw { |
| 53 | + type: 'ArgumentError', |
| 54 | + name: 'ObjectReferenceMissing', |
| 55 | + message: 'Object reference "ref" missing', |
| 56 | + suggestion: 'The object reference is provided as in the "ref" argument to the config property' |
| 57 | + }; |
| 58 | + } |
| 59 | + dataAPIUrl = config.url; |
| 60 | + if (!dataAPIUrl) { |
| 61 | + throw { |
| 62 | + type: 'ArgumentError', |
| 63 | + name: 'UrlMissing', |
| 64 | + message: 'Cannot find a url for the data api', |
| 65 | + suggestion: 'The url is provided as in the "url" argument property' |
| 66 | + }; |
| 67 | + |
| 68 | + } |
| 69 | + authToken = config.token; |
| 70 | + if (!authToken) { |
| 71 | + throw { |
| 72 | + type: 'ArgumentError', |
| 73 | + name: 'AuthTokenMissing', |
| 74 | + message: 'No Authorization found; Authorization is required for the data api', |
| 75 | + suggestion: 'The authorization is provided in the "token" argument" property' |
| 76 | + }; |
| 77 | + } |
| 78 | + timeout = config.timeout; |
| 79 | + if (!timeout) { |
| 80 | + timeout = 30000; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates and returns an instance of the Taxon Thrift client. Note that |
| 85 | + * this is |
| 86 | + * |
| 87 | + * @returns {Taxon_L22.taxon.thrift_serviceClient} |
| 88 | + * @private |
| 89 | + * @ignore |
| 90 | + */ |
| 91 | + function client() { |
| 92 | + try { |
| 93 | + var transport = new Thrift.TXHRTransport(dataAPIUrl, {timeout: timeout}), |
| 94 | + protocol = new Thrift.TBinaryProtocol(transport), |
| 95 | + thriftClient = new taxon.thrift_serviceClient(protocol); |
| 96 | + return thriftClient; |
| 97 | + } catch (ex) { |
| 98 | + // Rethrow exceptions in our format: |
| 99 | + if (ex.type && ex.name) { |
| 100 | + throw ex; |
| 101 | + } else { |
| 102 | + throw { |
| 103 | + type: 'ThriftError', |
| 104 | + message: 'An error was encountered creating the thrift client objects', |
| 105 | + suggestion: 'This could be a configuration or runtime error. Please consult the console for the error object', |
| 106 | + errorObject: ex |
| 107 | + }; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * If the Taxon has a parent object, this is returned. Otherwise, |
| 115 | + * it is ... |
| 116 | + * |
| 117 | + * @returns {Promise<ObjectReference|Error>} An object reference string |
| 118 | + * @throws {ThriftClientError} For networking and client side issues. These errors |
| 119 | + * are detected by the front end (javascript) code. |
| 120 | + * @throws {ThriftServiceError} For errors generated by the back end service. |
| 121 | + * |
| 122 | + */ |
| 123 | + function getParent() { |
| 124 | + return Promise.resolve(client().get_parent(authToken, objectReference, true)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get a array of object reference strings which are children (sub objects) |
| 129 | + * of this Taxon. |
| 130 | + * |
| 131 | + * @returns {Array<ObjectReference>} An array of object references representing the children of this object. |
| 132 | + */ |
| 133 | + function getChildren() { |
| 134 | + return Promise.resolve(client().get_children(authToken, objectReference, true)); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * |
| 139 | + * @returns {Array<String>} An array of genome annotation strings |
| 140 | + */ |
| 141 | + function getGenomeAnnotations() { |
| 142 | + return Promise.resolve(client().get_genome_annotations(authToken, objectReference, true)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get a list of taxonomic groups the species belongs to, with least specific first, |
| 147 | + * and most specific, that is the species itself, last. |
| 148 | + * |
| 149 | + * @returns {Array<String>} The lineage for this taxon, as an ordered list of taxonomic |
| 150 | + * ranks from least specific to most specific. |
| 151 | + * |
| 152 | + * @example |
| 153 | + * |
| 154 | + * Life |
| 155 | + * Domain |
| 156 | + * Kingdom |
| 157 | + * Phylum |
| 158 | + * Clas |
| 159 | + * Order |
| 160 | + * Family |
| 161 | + * Genus |
| 162 | + * Species |
| 163 | + * |
| 164 | + */ |
| 165 | + function getScientificLineage() { |
| 166 | + return Promise.resolve(client().get_scientific_lineage(authToken, objectReference, true)) |
| 167 | + .then(function (data) { |
| 168 | + return data.split(';').map(function (x) { |
| 169 | + return x.trim(' '); |
| 170 | + }); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * |
| 176 | + * @returns {String} |
| 177 | + */ |
| 178 | + function getScientificName() { |
| 179 | + return Promise.resolve(client().get_scientific_name(authToken, objectReference, true)); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * |
| 184 | + * @returns {Number} |
| 185 | + */ |
| 186 | + function getTaxonomicId() { |
| 187 | + return Promise.resolve(client().get_taxonomic_id(authToken, objectReference, true)); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * |
| 192 | + * @returns {String} |
| 193 | + */ |
| 194 | + function getKingdom() { |
| 195 | + return Promise.resolve(client().get_kingdom(authToken, objectReference, true)); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * |
| 200 | + * @returns {String} |
| 201 | + */ |
| 202 | + function getDomain() { |
| 203 | + return Promise.resolve(client().get_domain(authToken, objectReference, true)); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * The NCBI genetic code for the species. |
| 208 | + * |
| 209 | + * @returns {Number} |
| 210 | + * @see {@link http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi} NCBI "The Genetic Codes" |
| 211 | + */ |
| 212 | + function getGeneticCode() { |
| 213 | + return Promise.resolve(client().get_genetic_code(authToken, objectReference, true)); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * |
| 218 | + * @returns {Array<String>} |
| 219 | + */ |
| 220 | + function getAliases() { |
| 221 | + return Promise.resolve(client().get_aliases(authToken, objectReference, true)); |
| 222 | + } |
| 223 | + |
| 224 | + // API |
| 225 | + return Object.freeze({ |
| 226 | + getParent: getParent, |
| 227 | + getChildren: getChildren, |
| 228 | + getGenomeAnnotations: getGenomeAnnotations, |
| 229 | + getScientificLineage: getScientificLineage, |
| 230 | + getScientificName: getScientificName, |
| 231 | + getTaxonomicId: getTaxonomicId, |
| 232 | + getKingdom: getKingdom, |
| 233 | + getDomain: getDomain, |
| 234 | + getGeneticCode: getGeneticCode, |
| 235 | + getAliases: getAliases |
| 236 | + }); |
| 237 | + |
| 238 | + }; |
| 239 | + |
| 240 | + return Taxon; |
| 241 | +}); |
0 commit comments