def runListReferenceBases(self, id_, requestArgs): """ Runs a listReferenceBases request for the specified ID and request arguments. """ # parse arguments reference = _safeMapQuery(self._referenceIdMap, id_, exceptions.ObjectWithIdNotFoundException) start = 0 end = datamodel.PysamDatamodelMixin.fastaMax if 'start' in requestArgs: startString = requestArgs['start'] try: start = int(startString) except ValueError: raise exceptions.BadRequestIntegerException( 'start', startString) if 'end' in requestArgs: endString = requestArgs['end'] try: end = int(endString) except ValueError: raise exceptions.BadRequestIntegerException('end', endString) if 'pageToken' in requestArgs: pageTokenStr = requestArgs['pageToken'] start = _parsePageToken(pageTokenStr, 1)[0] chunkSize = self._maxResponseLength # get reference bases gbEnd = min(start + chunkSize, end) sequence = reference.getBases(start, gbEnd) # determine nextPageToken if len(sequence) == chunkSize: nextPageToken = start + chunkSize elif len(sequence) > chunkSize: raise exceptions.ServerError() # should never happen else: nextPageToken = None # build response response = protocol.ListReferenceBasesResponse() response.offset = start response.sequence = sequence response.nextPageToken = nextPageToken return response.toJsonString()
def _parseIntegerArgument(args, key, defaultValue): """ Attempts to parse the specified key in the specified argument dictionary into an integer. If the argument cannot be parsed, raises a BadRequestIntegerException. If the key is not present, return the specified default value. """ ret = defaultValue if key in args: try: ret = int(args[key]) except ValueError: raise exceptions.BadRequestIntegerException(key, args[key]) return ret