Instruction:
Can you create a Python 3 function that
gets a user s logs.

Response:
def get_logs(self, login=None, **kwargs):
        """Get a user's logs.

        :param str login: User's login (Default: self._login)
        :return: JSON
        """

        _login = kwargs.get(
            'login',
            login
        )
        log_events_url = GSA_LOGS_URL.format(login=_login)
        return self._request_api(url=log_events_url).json()


Instruction:
Make a summary of the following Python 3 code
def negotiate(cls, headers):
        # set lower keys
        headers = {k.lower(): v for k, v in headers.items()}

        accept = headers.get('accept', "*/*")

        parsed_accept = accept.split(";")
        parsed_accept = [i.strip() for i in parsed_accept]

        # Protobuffer (only one version)
        if all([i in parsed_accept for i in cls.PROTOBUF['default']]):
            return ProtobufFormat
        elif all([i in parsed_accept for i in cls.PROTOBUF['text']]):
            return ProtobufTextFormat
        # Text 0.0.4
        elif all([i in parsed_accept for i in cls.TEXT['0.0.4']]):
            return TextFormat
        # Text (Default)
        elif all([i in parsed_accept for i in cls.TEXT['default']]):
            return TextFormat
        # Default
        else:
            return cls.FALLBACK

Response: Process the headers dict to return the format class


Instruction:
How would you explain what the following Python 3 function does
def register(self, collector):
        if not isinstance(collector, Collector):
            raise TypeError(
                "Can't register instance, not a valid type of collector")

        if collector.name in self.collectors:
            raise ValueError("Collector already exists or name colision")

        with mutex:
            self.collectors[collector.name] = collector

Response: Registers a collector with the manager.


Instruction:
Write a Python 3 function that can
add a new entry to the registry.

Response:
def add(self, registry):
        """ Add works like replace, but only previously pushed metrics with the
            same name (and the same job and instance) will be replaced.
            (It uses HTTP method 'POST' to push to the Pushgateway.)
        """
        # POST
        payload = self.formatter.marshall(registry)
        r = requests.post(self.path, data=payload, headers=self.headers)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def replace(self, registry):
        # PUT
        payload = self.formatter.marshall(registry)
        r = requests.put(self.path, data=payload, headers=self.headers)

Response: Replaces all collected metrics with the given registry.


Instruction:
Make a summary of the following Python 3 code
def marshall_lines(self, collector):

        if isinstance(collector, collectors.Counter):
            exec_method = self._format_counter
        elif isinstance(collector, collectors.Gauge):
            exec_method = self._format_gauge
        elif isinstance(collector, collectors.Summary):
            exec_method = self._format_summary
        else:
            raise TypeError("Not a valid object format")

        # create headers
        help_header = TextFormat.HELP_FMT.format(name=collector.name,
                                                 help_text=collector.help_text)

        type_header = TextFormat.TYPE_FMT.format(name=collector.name,
                                                 value_type=collector.REPR_STR)

        # Prepare start headers
        lines = [help_header, type_header]

        for i in collector.get_all():
            r = exec_method(i, collector.name, collector.const_labels)

            # Check if it returns one or multiple lines
            if not isinstance(r, str) and isinstance(r, collections.Iterable):
                lines.extend(r)
            else:
                lines.append(r)

        return lines

Response:
Marshalls a collector and returns the storage / transfer format in
            a tuple


Instruction:
Implement a function in Python 3 to
return bytes representation of the message in the registry.

Response:
def marshall(self, registry):
        """Returns bytes"""
        result = b""

        for i in registry.get_all():
            # Each message needs to be prefixed with a varint with the size of
            # the message (MetrycType)
            # https://github.com/matttproud/golang_protobuf_extensions/blob/master/ext/encode.go
            # http://zombietetris.de/blog/building-your-own-writedelimitedto-for-python-protobuf/
            body = self.marshall_collector(i).SerializeToString()
            msg = encoder._VarintBytes(len(body)) + body
            result += msg

        return result


Instruction:
How would you explain what the following Python 3 function does
def gather_data(registry):

    # Get the host name of the machine
    host = socket.gethostname()

    # Create our collectors
    trig_metric = Gauge("trigonometry_example",
                        "Various trigonometry examples.",
                        {'host': host})

    # register the metric collectors
    registry.register(trig_metric)

    # Start gathering metrics every second
    counter = 0
    while True:
        time.sleep(1)

        sine = math.sin(math.radians(counter % 360))
        cosine = math.cos(math.radians(counter % 360))
        trig_metric.set({'type': "sine"}, sine)
        trig_metric.set({'type': "cosine"}, cosine)

        counter += 1

Response: Gathers the metrics for the next second


Instruction:
Create a Python 3 function to
set a value in the container.

Response:
def set_value(self, labels, value):
        """ Sets a value in the container"""

        if labels:
            self._label_names_correct(labels)

        with mutex:
            self.values[labels] = value


Instruction:
Create a Python 3 function for
raising exception if labels not correct

Response:
def _label_names_correct(self, labels):
        """Raise exception (ValueError) if labels not correct"""

        for k, v in labels.items():
            # Check reserved labels
            if k in RESTRICTED_LABELS_NAMES:
                raise ValueError("Labels not correct")

            # Check prefixes
            if any(k.startswith(i) for i in RESTRICTED_LABELS_PREFIXES):
                raise ValueError("Labels not correct")

        return True


Instruction:
Create a Python 3 function for
returning a list of tuples of 2 elements first one is a dict with all the labels and second elemnt is the value of the metric itself

Response:
def get_all(self):
        """ Returns a list populated by tuples of 2 elements, first one is
            a dict with all the labels and the second elemnt is the value
            of the metric itself
        """
        with mutex:
            items = self.values.items()

        result = []
        for k, v in items:
            # Check if is a single value dict (custom empty key)
            if not k or k == MetricDict.EMPTY_KEY:
                key = None
            else:
                key = decoder.decode(k)
            result.append((key, self.get(k)))

        return result


Instruction:
Given the following Python 3 function, write the documentation
def add(self, labels, value):

        try:
            current = self.get_value(labels)
        except KeyError:
            current = 0

        self.set_value(labels, current + value)

Response: Adds the given value to the Gauge.


Instruction:
Make a summary of the following Python 3 code
def add(self, labels, value):

        if type(value) not in (float, int):
            raise TypeError("Summary only works with digits (int, float)")

        # We have already a lock for data but not for the estimator
        with mutex:
            try:
                e = self.get_value(labels)
            except KeyError:
                # Initialize quantile estimator
                e = quantile.Estimator(*self.__class__.DEFAULT_INVARIANTS)
                self.set_value(labels, e)
            e.observe(float(value))

Response: Adds a single observation to the summary.


Instruction:
Implement a function in Python 3 to
get the data in the form of 0. 5 9 and 0. 99 percentiles. Also get sum and count.

Response:
def get(self, labels):
        """ Get gets the data in the form of 0.5, 0.9 and 0.99 percentiles. Also
            you get sum and count, all in a dict
        """

        return_data = {}

        # We have already a lock for data but not for the estimator
        with mutex:
            e = self.get_value(labels)

            # Set invariants data (default to 0.50, 0.90 and 0.99)
            for i in e._invariants:
                q = i._quantile
                return_data[q] = e.query(q)

            # Set sum and count
            return_data[self.__class__.SUM_KEY] = e._sum
            return_data[self.__class__.COUNT_KEY] = e._observations

        return return_data


Instruction:
Write a Python 3 script to
gather the metrics for the next available node

Response:
def gather_data(registry):
    """Gathers the metrics"""

    # Get the host name of the machine
    host = socket.gethostname()

    # Create our collectors
    ram_metric = Gauge("memory_usage_bytes", "Memory usage in bytes.",
                       {'host': host})
    cpu_metric = Gauge("cpu_usage_percent", "CPU usage percent.",
                       {'host': host})

    # register the metric collectors
    registry.register(ram_metric)
    registry.register(cpu_metric)

    # Start gathering metrics every second
    while True:
        time.sleep(1)

        # Add ram metrics
        ram = psutil.virtual_memory()
        swap = psutil.swap_memory()

        ram_metric.set({'type': "virtual", }, ram.used)
        ram_metric.set({'type': "virtual", 'status': "cached"}, ram.cached)
        ram_metric.set({'type': "swap"}, swap.used)

        # Add cpu metrics
        for c, p in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
            cpu_metric.set({'core': c}, p)


Instruction:
Given the following Python 3 function, write the documentation
def get_child(self, name, attribs=None):
        if name == '.':
            if attribs is None or len(attribs) == 0:
                return self
            if attribs == self.attribs:
                return self
        return self.child_index.get(nodehash(name, attribs))

Response: Returns the first child that matches the given name and attributes.


Instruction:
How would you code a function in Python 3 to
create a new node in the tree.

Response:
def create(self, path, data=None):
        """
        Creates the given node, regardless of whether or not it already
        exists.
        Returns the new node.
        """
        node = self.current[-1]
        path = self._splitpath(path)
        n_items = len(path)
        for n, item in enumerate(path):
            tag, attribs = self._splittag(item)

            # The leaf node is always newly created.
            if n == n_items-1:
                node = node.add(Node(tag, attribs))
                break

            # Parent nodes are only created if they do not exist yet.
            existing = node.get_child(tag, attribs)
            if existing is not None:
                node = existing
            else:
                node = node.add(Node(tag, attribs))
        if data:
            node.text = unquote(data)
        return node


Instruction:
Can you tell what is the following Python 3 function doing
def add(self, path, data=None, replace=False):
        node = self.current[-1]
        for item in self._splitpath(path):
            tag, attribs = self._splittag(item)
            next_node = node.get_child(tag, attribs)
            if next_node is not None:
                node = next_node
            else:
                node = node.add(Node(tag, attribs))
        if replace:
            node.text = ''
        if data:
            if node.text is None:
                node.text = unquote(data)
            else:
                node.text += unquote(data)
        return node

Response: Adds a new entry to the current node.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_attribute(self, path, name, value):
        node = self.add(path)
        node.attribs.append((name, value))
        return node

Response: Adds the given attribute to the given value.


Instruction:
Write a Python 3 script for
creating and enters the given node. Returns the node that already exists.

Response:
def open(self, path):
        """
        Creates and enters the given node, regardless of whether it already
        exists.
        Returns the new node.
        """
        self.current.append(self.create(path))
        return self.current[-1]


Instruction:
Explain what the following Python 3 code does
def enter(self, path):
        self.current.append(self.add(path))
        return self.current[-1]

Response: Enters the given node. Creates it if it does not exist. Returns the node s id.


Instruction:
How would you code a function in Python 3 to
give a converter (as returned by compile()), this function reads the given input file and converts it to the requested output format. Supported output formats are 'xml', 'yaml', 'json', or 'none'. :type converter: compiler.Context :param converter: The compiled converter. :type input_file: str :param input_file: Name of a file to convert. :type format: str :param format: The output format. :type encoding: str :param encoding: Character encoding of the input file. :rtype: str :return: The resulting output.

Response:
def generate(converter, input_file, format='xml', encoding='utf8'):
    """
    Given a converter (as returned by compile()), this function reads
    the given input file and converts it to the requested output format.

    Supported output formats are 'xml', 'yaml', 'json', or 'none'.

    :type  converter: compiler.Context
    :param converter: The compiled converter.
    :type  input_file: str
    :param input_file: Name of a file to convert.
    :type  format: str
    :param format: The output format.
    :type  encoding: str
    :param encoding: Character encoding of the input file.
    :rtype:  str
    :return: The resulting output.
    """
    with codecs.open(input_file, encoding=encoding) as thefile:
        return generate_string(converter, thefile.read(), format=format)


Instruction:
Write a Python 3 script for
liking generate(), but writes the output to the given output file instead. :type converter: compiler.Context :param converter: The compiled converter. :type input_file: str :param input_file: Name of a file to convert. :type output_file: str :param output_file: The output filename. :type format: str :param format: The output format. :type in_encoding: str :param in_encoding: Character encoding of the input file. :type out_encoding: str :param out_encoding: Character encoding of the output file. :rtype: str :return: The resulting output.

Response:
def generate_to_file(converter,
                     input_file,
                     output_file,
                     format='xml',
                     in_encoding='utf8',
                     out_encoding='utf8'):
    """
    Like generate(), but writes the output to the given output file
    instead.

    :type  converter: compiler.Context
    :param converter: The compiled converter.
    :type  input_file: str
    :param input_file: Name of a file to convert.
    :type  output_file: str
    :param output_file: The output filename.
    :type  format: str
    :param format: The output format.
    :type  in_encoding: str
    :param in_encoding: Character encoding of the input file.
    :type  out_encoding: str
    :param out_encoding: Character encoding of the output file.
    :rtype:  str
    :return: The resulting output.
    """
    with codecs.open(output_file, 'w', encoding=out_encoding) as thefile:
        result = generate(converter, input_file, format=format, encoding=in_encoding)
        thefile.write(result)


Instruction:
Can you implement a function in Python 3 that
likes generate(), but reads the input from a string instead of from a file. :type converter: compiler.Context :param converter: The compiled converter. :type input: str :param input: The string to convert. :type format: str :param format: The output format. :rtype: str :return: The resulting output.

Response:
def generate_string(converter, input, format='xml'):
    """
    Like generate(), but reads the input from a string instead of
    from a file.

    :type  converter: compiler.Context
    :param converter: The compiled converter.
    :type  input: str
    :param input: The string to convert.
    :type  format: str
    :param format: The output format.
    :rtype:  str
    :return: The resulting output.
    """
    serializer = generator.new(format)
    if serializer is None:
        raise TypeError('invalid output format ' + repr(format))
    builder = Builder()
    converter.parse_string(input, builder)
    return builder.serialize(serializer)


Instruction:
Make a summary of the following Python 3 code
def generate_string_to_file(converter,
                            input,
                            output_file,
                            format='xml',
                            out_encoding='utf8'):
    """
    Like generate(), but reads the input from a string instead of
    from a file, and writes the output to the given output file.

    :type  converter: compiler.Context
    :param converter: The compiled converter.
    :type  input: str
    :param input: The string to convert.
    :type  output_file: str
    :param output_file: The output filename.
    :type  format: str
    :param format: The output format.
    :type  out_encoding: str
    :param out_encoding: Character encoding of the output file.
    :rtype:  str
    :return: The resulting output.
    """
    with codecs.open(output_file, 'w', encoding=out_encoding) as thefile:
        result = generate_string(converter, input, format=format)
        thefile.write(result)

Response: This function generates a string from a file instead of a string.


Instruction:
How would you explain what the following Python 3 function does
def best_trial_tid(self, rank=0):
        candidates = [t for t in self.trials
                      if t['result']['status'] == STATUS_OK]
        if len(candidates) == 0:
            return None
        losses = [float(t['result']['loss']) for t in candidates]
        assert not np.any(np.isnan(losses))
        lid = np.where(np.argsort(losses).argsort() == rank)[0][0]
        return candidates[lid]["tid"]

Response: Get tid of the best trial with the given rank


Instruction:
Here you have a function in Python 3, explain what it does
def count_by_state_unsynced(self, arg):
        if self.kill_timeout is not None:
            self.delete_running(self.kill_timeout)
        return super(KMongoTrials, self).count_by_state_unsynced(arg)

Response:
Extends the original object in order to inject checking
            for stalled jobs and killing them if they are still running for too long


Instruction:
Explain what the following Python 3 code does
def plot_history(self, tid, scores=["loss", "f1", "accuracy"],
                     figsize=(15, 3)):
        """Plot the loss curves"""
        history = self.train_history(tid)
        import matplotlib.pyplot as plt

        fig = plt.figure(figsize=figsize)
        for i, score in enumerate(scores):
            plt.subplot(1, len(scores), i + 1)
            plt.tight_layout()
            plt.plot(history[score], label="train")
            plt.plot(history['val_' + score], label="validation")
            plt.title(score)
            plt.ylabel(score)
            plt.xlabel('epoch')
            plt.legend(loc='best')
        return fig

Response: Plot the loss curves for a specific tid


Instruction:
How would you explain what the following Python 3 function does
def load_model(self, tid, custom_objects=None):
        if tid is None:
            tid = self.best_trial_tid()

        model_path = self.get_trial(tid)["result"]["path"]["model"]
        return load_model(model_path, custom_objects=custom_objects)

Response: Load saved keras model of the trial.


Instruction:
Create a Python 3 function to
number of ok trials in the user.

Response:
def n_ok(self):
        """Number of ok trials()
        """
        if len(self.trials) == 0:
            return 0
        else:
            return np.sum(np.array(self.statuses()) == "ok")


Instruction:
Given the following Python 3 function, write the documentation
def get_ok_results(self, verbose=True):
        if len(self.trials) == 0:
            return []

        not_ok = np.where(np.array(self.statuses()) != "ok")[0]

        if len(not_ok) > 0 and verbose:
            print("{0}/{1} trials were not ok.".format(len(not_ok), len(self.trials)))
            print("Trials: " + str(not_ok))
            print("Statuses: " + str(np.array(self.statuses())[not_ok]))

        r = [merge_dicts({"tid": t["tid"]}, t["result"].to_dict())
             for t in self.trials if t["result"]["status"] == "ok"]
        return r

Response: Return a list of results with ok status


Instruction:
Explain what the following Python 3 code does
def pyoidcMiddleware(func):

    def wrapper(environ, start_response):
        data = get_or_post(environ)
        cookies = environ.get("HTTP_COOKIE", "")
        resp = func(request=data, cookie=cookies)
        return resp(environ, start_response)

    return wrapper

Response: A wrapper for the underlying pyoidc library functions.


Instruction:
Make a summary of the following Python 3 code
def resp2flask(resp):
    if isinstance(resp, Redirect) or isinstance(resp, SeeOther):
        code = int(resp.status.split()[0])
        raise cherrypy.HTTPRedirect(resp.message, code)
    return resp.message, resp.status, resp.headers

Response: Convert an oic. utils. http_util instance to Flask.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setup_authentication_methods(authn_config, template_env):
    routing = {}
    ac = AuthnBroker()
    for authn_method in authn_config:
        cls = make_cls_from_name(authn_method["class"])
        instance = cls(template_env=template_env, **authn_method["kwargs"])
        ac.add(authn_method["acr"], instance)
        routing[instance.url_endpoint] = VerifierMiddleware(instance)

    return ac, routing

Response: Setup all authentication methods specified in the configuration.


Instruction:
Implement a Python 3 function for
returning a tuple of SQL select statements and the list of SQL arguments for the features query.

Response:
def featuresQuery(self, **kwargs):
        """
        Converts a dictionary of keyword arguments into a tuple
        of SQL select statements and the list of SQL arguments
        """
        # TODO: Optimize by refactoring out string concatenation
        sql = ""
        sql_rows = "SELECT * FROM FEATURE WHERE id > 1 "
        sql_args = ()
        if 'name' in kwargs and kwargs['name']:
            sql += "AND name = ? "
            sql_args += (kwargs.get('name'),)
        if 'geneSymbol' in kwargs and kwargs['geneSymbol']:
            sql += "AND gene_name = ? "
            sql_args += (kwargs.get('geneSymbol'),)
        if 'start' in kwargs and kwargs['start'] is not None:
            sql += "AND end > ? "
            sql_args += (kwargs.get('start'),)
        if 'end' in kwargs and kwargs['end'] is not None:
            sql += "AND start < ? "
            sql_args += (kwargs.get('end'),)
        if 'referenceName' in kwargs and kwargs['referenceName']:
            sql += "AND reference_name = ?"
            sql_args += (kwargs.get('referenceName'),)
        if 'parentId' in kwargs and kwargs['parentId']:
            sql += "AND parent_id = ? "
            sql_args += (kwargs['parentId'],)
        if kwargs.get('featureTypes') is not None \
                and len(kwargs['featureTypes']) > 0:
            sql += "AND type IN ("
            sql += ", ".join(["?", ] * len(kwargs.get('featureTypes')))
            sql += ") "
            sql_args += tuple(kwargs.get('featureTypes'))
        sql_rows += sql
        sql_rows += " ORDER BY reference_name, start, end ASC "
        return sql_rows, sql_args


Instruction:
Create a Python 3 function to
perform a full features query in database.

Response:
def searchFeaturesInDb(
            self, startIndex=0, maxResults=None,
            referenceName=None, start=None, end=None,
            parentId=None, featureTypes=None,
            name=None, geneSymbol=None):
        """
        Perform a full features query in database.

        :param startIndex: int representing first record to return
        :param maxResults: int representing number of records to return
        :param referenceName: string representing reference name, ex 'chr1'
        :param start: int position on reference to start search
        :param end: int position on reference to end search >= start
        :param parentId: string restrict search by id of parent node.
        :param name: match features by name
        :param geneSymbol: match features by gene symbol
        :return an array of dictionaries, representing the returned data.
        """
        # TODO: Refactor out common bits of this and the above count query.
        sql, sql_args = self.featuresQuery(
            startIndex=startIndex, maxResults=maxResults,
            referenceName=referenceName, start=start, end=end,
            parentId=parentId, featureTypes=featureTypes,
            name=name, geneSymbol=geneSymbol)
        sql += sqlite_backend.limitsSql(startIndex, maxResults)
        query = self._dbconn.execute(sql, sql_args)
        return sqlite_backend.sqliteRowsToDicts(query.fetchall())


Instruction:
Can you write a function in Python 3 where it
fetches a feature by featureID.

Response:
def getFeatureById(self, featureId):
        """
        Fetch feature by featureID.

        :param featureId: the FeatureID as found in GFF3 records
        :return: dictionary representing a feature object,
            or None if no match is found.
        """
        sql = "SELECT * FROM FEATURE WHERE id = ?"
        query = self._dbconn.execute(sql, (featureId,))
        ret = query.fetchone()
        if ret is None:
            return None
        return sqlite_backend.sqliteRowToDict(ret)


Instruction:
Can you generate the documentation for the following Python 3 function
def toProtocolElement(self):
        gaFeatureSet = protocol.FeatureSet()
        gaFeatureSet.id = self.getId()
        gaFeatureSet.dataset_id = self.getParentContainer().getId()
        gaFeatureSet.reference_set_id = pb.string(self._referenceSet.getId())
        gaFeatureSet.name = self._name
        gaFeatureSet.source_uri = self._sourceUri
        attributes = self.getAttributes()
        for key in attributes:
            gaFeatureSet.attributes.attr[key] \
                .values.extend(protocol.encodeValue(attributes[key]))
        return gaFeatureSet

Response:
Returns the representation of this FeatureSet as the corresponding
        ProtocolElement.


Instruction:
Can you tell what is the following Python 3 function doing
def getCompoundIdForFeatureId(self, featureId):
        if featureId is not None and featureId != "":
            compoundId = datamodel.FeatureCompoundId(
                self.getCompoundId(), str(featureId))
        else:
            compoundId = ""
        return str(compoundId)

Response: Returns server - style compound ID for the specified internal featureId.


Instruction:
Can you generate the documentation for the following Python 3 function
def getFeature(self, compoundId):
        if compoundId is None:
            raise exceptions.ObjectWithIdNotFoundException(compoundId)
        randomNumberGenerator = random.Random()
        randomNumberGenerator.seed(self._randomSeed)
        feature = self._generateSimulatedFeature(randomNumberGenerator)
        feature.id = str(compoundId)
        feature.parent_id = ""  # TODO: Test with nonempty parentIDs?
        return feature

Response: Fetches a simulated feature by ID.


Instruction:
How would you explain what the following Python 3 function does
def getFeatures(self, referenceName=None, start=None, end=None,
                    startIndex=None, maxResults=None,
                    featureTypes=None, parentId=None,
                    name=None, geneSymbol=None, numFeatures=10):
        """
        Returns a set number of simulated features.

        :param referenceName: name of reference to "search" on
        :param start: start coordinate of query
        :param end: end coordinate of query
        :param startIndex: None or int
        :param maxResults: None or int
        :param featureTypes: optional list of ontology terms to limit query
        :param parentId: optional parentId to limit query.
        :param name: the name of the feature
        :param geneSymbol: the symbol for the gene the features are on
        :param numFeatures: number of features to generate in the return.
            10 is a reasonable (if arbitrary) default.
        :return: Yields feature list
        """
        randomNumberGenerator = random.Random()
        randomNumberGenerator.seed(self._randomSeed)
        for featureId in range(numFeatures):
            gaFeature = self._generateSimulatedFeature(randomNumberGenerator)
            gaFeature.id = self.getCompoundIdForFeatureId(featureId)
            match = (
                gaFeature.start < end and
                gaFeature.end > start and
                gaFeature.reference_name == referenceName and (
                    featureTypes is None or len(featureTypes) == 0 or
                    gaFeature.feature_type in featureTypes))
            if match:
                gaFeature.parent_id = ""  # TODO: Test nonempty parentIDs?
                yield gaFeature

Response: Returns a list of simulated features.


Instruction:
Can you generate the documentation for the following Python 3 function
def populateFromFile(self, dataUrl):
        self._dbFilePath = dataUrl
        self._db = Gff3DbBackend(self._dbFilePath)

Response: Populates the instance variables of this FeatureSet from the specified data URL.


Instruction:
Can you tell what is the following Python 3 function doing
def populateFromRow(self, featureSetRecord):
        self._dbFilePath = featureSetRecord.dataurl
        self.setAttributesJson(featureSetRecord.attributes)
        self._db = Gff3DbBackend(self._dbFilePath)

Response: Populates the instance variables of this FeatureSet from the specified featureSetRecord.


Instruction:
Write a Python 3 function that can
return a protocol. Feature object corresponding to a datamodel. FeatureCompoundId object.

Response:
def getFeature(self, compoundId):
        """
        Returns a protocol.Feature object corresponding to a compoundId
        :param compoundId: a datamodel.FeatureCompoundId object
        :return: a Feature object.
        :raises: exceptions.ObjectWithIdNotFoundException if invalid
            compoundId is provided.
        """
        featureId = long(compoundId.featureId)
        with self._db as dataSource:
            featureReturned = dataSource.getFeatureById(featureId)

        if featureReturned is None:
            raise exceptions.ObjectWithIdNotFoundException(compoundId)
        else:
            gaFeature = self._gaFeatureForFeatureDbRecord(featureReturned)
            return gaFeature


Instruction:
Explain what the following Python 3 code does
def _gaFeatureForFeatureDbRecord(self, feature):
        gaFeature = protocol.Feature()
        gaFeature.id = self.getCompoundIdForFeatureId(feature['id'])
        if feature.get('parent_id'):
            gaFeature.parent_id = self.getCompoundIdForFeatureId(
                    feature['parent_id'])
        else:
            gaFeature.parent_id = ""
        gaFeature.feature_set_id = self.getId()
        gaFeature.reference_name = pb.string(feature.get('reference_name'))
        gaFeature.start = pb.int(feature.get('start'))
        gaFeature.end = pb.int(feature.get('end'))
        gaFeature.name = pb.string(feature.get('name'))
        if feature.get('strand', '') == '-':
            gaFeature.strand = protocol.NEG_STRAND
        else:
            # default to positive strand
            gaFeature.strand = protocol.POS_STRAND
        gaFeature.child_ids.extend(map(
                self.getCompoundIdForFeatureId,
                json.loads(feature['child_ids'])))
        gaFeature.feature_type.CopyFrom(
            self._ontology.getGaTermByName(feature['type']))
        attributes = json.loads(feature['attributes'])
        # TODO: Identify which values are ExternalIdentifiers and OntologyTerms
        for key in attributes:
            for v in attributes[key]:
                gaFeature.attributes.attr[key].values.add().string_value = v
        if 'gene_name' in attributes and len(attributes['gene_name']) > 0:
            gaFeature.gene_symbol = pb.string(attributes['gene_name'][0])
        return gaFeature

Response: Returns the GA4GH protocol. Feature object for the given feature in the database.


Instruction:
How would you implement a function in Python 3 that
returns a generator that yields a protocol. Feature at a time

Response:
def getFeatures(self, referenceName=None, start=None, end=None,
                    startIndex=None, maxResults=None,
                    featureTypes=None, parentId=None,
                    name=None, geneSymbol=None):
        """
        method passed to runSearchRequest to fulfill the request
        :param str referenceName: name of reference (ex: "chr1")
        :param start: castable to int, start position on reference
        :param end: castable to int, end position on reference
        :param startIndex: none or castable to int
        :param maxResults: none or castable to int
        :param featureTypes: array of str
        :param parentId: none or featureID of parent
        :param name: the name of the feature
        :param geneSymbol: the symbol for the gene the features are on
        :return: yields a protocol.Feature at a time
        """
        with self._db as dataSource:
            features = dataSource.searchFeaturesInDb(
                startIndex, maxResults,
                referenceName=referenceName,
                start=start, end=end,
                parentId=parentId, featureTypes=featureTypes,
                name=name, geneSymbol=geneSymbol)
            for feature in features:
                gaFeature = self._gaFeatureForFeatureDbRecord(feature)
                yield gaFeature


Instruction:
Here you have a function in Python 3, explain what it does
def addRnaQuantification(self, rnaQuantification):
        id_ = rnaQuantification.getId()
        self._rnaQuantificationIdMap[id_] = rnaQuantification
        self._rnaQuantificationIds.append(id_)

Response: Adds an rnaQuantification to this rnaQuantificationSet


Instruction:
Here you have a function in Python 3, explain what it does
def toProtocolElement(self):
        protocolElement = protocol.RnaQuantificationSet()
        protocolElement.id = self.getId()
        protocolElement.dataset_id = self._parentContainer.getId()
        protocolElement.name = self._name
        self.serializeAttributes(protocolElement)
        return protocolElement

Response: Converts this rnaQuant into its GA4GH protocol equivalent.


Instruction:
Can you generate the documentation for the following Python 3 function
def populateFromFile(self, dataUrl):
        self._dbFilePath = dataUrl
        self._db = SqliteRnaBackend(self._dbFilePath)
        self.addRnaQuants()

Response: Populates the instance variables of this RnaQuantificationSet from the specified data URL.


Instruction:
How would you code a function in Python 3 to
populate the instance variables of this RnaQuantificationSet from the specified DB row.

Response:
def populateFromRow(self, quantificationSetRecord):
        """
        Populates the instance variables of this RnaQuantificationSet from the
        specified DB row.
        """
        self._dbFilePath = quantificationSetRecord.dataurl
        self.setAttributesJson(quantificationSetRecord.attributes)
        self._db = SqliteRnaBackend(self._dbFilePath)
        self.addRnaQuants()


Instruction:
How would you code a function in Python 3 to
convert this rnaQuantification into its GA4GH protocol equivalent.

Response:
def toProtocolElement(self):
        """
        Converts this rnaQuant into its GA4GH protocol equivalent.
        """
        protocolElement = protocol.RnaQuantification()
        protocolElement.id = self.getId()
        protocolElement.name = self._name
        protocolElement.description = self._description
        protocolElement.read_group_ids.extend(self._readGroupIds)
        protocolElement.programs.extend(self._programs)
        protocolElement.biosample_id = self._biosampleId
        protocolElement.feature_set_ids.extend(self._featureSetIds)
        protocolElement.rna_quantification_set_id = \
            self._parentContainer.getId()
        self.serializeAttributes(protocolElement)
        return protocolElement


Instruction:
Write a Python 3 function that can
add the RNA Quantization Metadata to the internal data structure.

Response:
def addRnaQuantMetadata(self, fields):
        """
        data elements are:
        Id, annotations, description, name, readGroupId
        where annotations is a comma separated list
        """
        self._featureSetIds = fields["feature_set_ids"].split(',')
        self._description = fields["description"]
        self._name = fields["name"]
        self._biosampleId = fields.get("biosample_id", "")
        if fields["read_group_ids"] == "":
            self._readGroupIds = []
        else:
            self._readGroupIds = fields["read_group_ids"].split(',')
        if fields["programs"] == "":
            self._programs = []
        else:
            # Need to use program Id's here to generate a list of Programs
            # for now set to empty
            self._programs = []


Instruction:
Here you have a function in Python 3, explain what it does
def getRnaQuantMetadata(self):
        rnaQuantId = self.getLocalId()
        with self._db as dataSource:
            rnaQuantReturned = dataSource.getRnaQuantificationById(
                rnaQuantId)
        self.addRnaQuantMetadata(rnaQuantReturned)

Response: Add the metadata for the rnaQuantification for this entry.


Instruction:
Can you generate the documentation for the following Python 3 function
def populateFromFile(self, dataUrl):
        self._dbFilePath = dataUrl
        self._db = SqliteRnaBackend(self._dbFilePath)
        self.getRnaQuantMetadata()

Response: Populates the instance variables of this FeatureSet from the specified data URL.


Instruction:
Can you generate the documentation for the following Python 3 function
def populateFromRow(self, row):
        self._dbFilePath = row[b'dataUrl']
        self._db = SqliteRnaBackend(self._dbFilePath)
        self.getRnaQuantMetadata()

Response: Populates the instance variables of this FeatureSet from the specified DB row.


Instruction:
How would you code a function in Python 3 to
return the list of ExpressionLevels in this RNA Quantification.

Response:
def getExpressionLevels(
            self, threshold=0.0, names=[], startIndex=0, maxResults=0):
        """
        Returns the list of ExpressionLevels in this RNA Quantification.
        """
        rnaQuantificationId = self.getLocalId()
        with self._db as dataSource:
            expressionsReturned = dataSource.searchExpressionLevelsInDb(
                rnaQuantificationId,
                names=names,
                threshold=threshold,
                startIndex=startIndex,
                maxResults=maxResults)
            expressionLevels = [
                SqliteExpressionLevel(self, expressionEntry) for
                expressionEntry in expressionsReturned]
            return expressionLevels


Instruction:
Make a summary of the following Python 3 code
def searchRnaQuantificationsInDb(
            self, rnaQuantificationId=""):
        """
        :param rnaQuantificationId: string restrict search by id
        :return an array of dictionaries, representing the returned data.
        """
        sql = ("SELECT * FROM RnaQuantification")
        sql_args = ()
        if len(rnaQuantificationId) > 0:
            sql += " WHERE id = ? "
            sql_args += (rnaQuantificationId,)
        query = self._dbconn.execute(sql, sql_args)
        try:
            return sqlite_backend.iterativeFetch(query)
        except AttributeError:
            raise exceptions.RnaQuantificationNotFoundException(
                rnaQuantificationId)

Response: searchRnaQuantificationsInDb - Searches the database for the rnaQuantification with the given id.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getRnaQuantificationById(self, rnaQuantificationId):
        sql = ("SELECT * FROM RnaQuantification WHERE id = ?")
        query = self._dbconn.execute(sql, (rnaQuantificationId,))
        try:
            return sqlite_backend.fetchOne(query)
        except AttributeError:
            raise exceptions.RnaQuantificationNotFoundException(
                rnaQuantificationId)

Response: Returns a dictionary representing an RnaQuantification object and the corresponding object.


Instruction:
Write a Python 3 script for
returning the dictionary representation of an ExpressionLevel object identified by the given ID.

Response:
def getExpressionLevelById(self, expressionId):
        """
        :param expressionId: the ExpressionLevel ID
        :return: dictionary representing an ExpressionLevel object,
            or None if no match is found.
        """
        sql = ("SELECT * FROM Expression WHERE id = ?")
        query = self._dbconn.execute(sql, (expressionId,))
        try:
            return sqlite_backend.fetchOne(query)
        except AttributeError:
            raise exceptions.ExpressionLevelNotFoundException(
                expressionId)


Instruction:
How would you code a function in Python 3 to
populate this CallSet from the specified DB row.

Response:
def populateFromRow(self, callSetRecord):
        """
        Populates this CallSet from the specified DB row.
        """
        self._biosampleId = callSetRecord.biosampleid
        self.setAttributesJson(callSetRecord.attributes)


Instruction:
Given the following Python 3 function, write the documentation
def toProtocolElement(self):
        variantSet = self.getParentContainer()
        gaCallSet = protocol.CallSet(
            biosample_id=self.getBiosampleId())
        if variantSet.getCreationTime():
            gaCallSet.created = variantSet.getCreationTime()
        if variantSet.getUpdatedTime():
            gaCallSet.updated = variantSet.getUpdatedTime()
        gaCallSet.id = self.getId()
        gaCallSet.name = self.getLocalId()
        gaCallSet.variant_set_ids.append(variantSet.getId())
        self.serializeAttributes(gaCallSet)
        return gaCallSet

Response:
Returns the representation of this CallSet as the corresponding
        ProtocolElement.


Instruction:
Create a Python 3 function for
adding the specified variantAnnotationSet to this dataset.

Response:
def addVariantAnnotationSet(self, variantAnnotationSet):
        """
        Adds the specified variantAnnotationSet to this dataset.
        """
        id_ = variantAnnotationSet.getId()
        self._variantAnnotationSetIdMap[id_] = variantAnnotationSet
        self._variantAnnotationSetIds.append(id_)


Instruction:
Can you tell what is the following Python 3 function doing
def getVariantAnnotationSet(self, id_):
        if id_ not in self._variantAnnotationSetIdMap:
            raise exceptions.AnnotationSetNotFoundException(id_)
        return self._variantAnnotationSetIdMap[id_]

Response: Returns the AnnotationSet with the specified id or raises an exception if it does not exist.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addCallSet(self, callSet):
        callSetId = callSet.getId()
        self._callSetIdMap[callSetId] = callSet
        self._callSetNameMap[callSet.getLocalId()] = callSet
        self._callSetIds.append(callSetId)
        self._callSetIdToIndex[callSet.getId()] = len(self._callSetIds) - 1

Response: Adds the specified CallSet to this VariantSet.


Instruction:
Can you implement a function in Python 3 that
adds a CallSet for the specified sample name.

Response:
def addCallSetFromName(self, sampleName):
        """
        Adds a CallSet for the specified sample name.
        """
        callSet = CallSet(self, sampleName)
        self.addCallSet(callSet)


Instruction:
Implement a Python 3 function for
returning a CallSet with the specified name or raises a NameException if it does not exist.

Response:
def getCallSetByName(self, name):
        """
        Returns a CallSet with the specified name, or raises a
        CallSetNameNotFoundException if it does not exist.
        """
        if name not in self._callSetNameMap:
            raise exceptions.CallSetNameNotFoundException(name)
        return self._callSetNameMap[name]


Instruction:
Make a summary of the following Python 3 code
def getCallSet(self, id_):
        if id_ not in self._callSetIdMap:
            raise exceptions.CallSetNotFoundException(id_)
        return self._callSetIdMap[id_]

Response:
Returns a CallSet with the specified id or raises a
NameException if it does not exist.


Instruction:
Can you implement a function in Python 3 that
converts this VariantSet into its GA4GH protocol equivalent.

Response:
def toProtocolElement(self):
        """
        Converts this VariantSet into its GA4GH protocol equivalent.
        """
        protocolElement = protocol.VariantSet()
        protocolElement.id = self.getId()
        protocolElement.dataset_id = self.getParentContainer().getId()
        protocolElement.reference_set_id = self._referenceSet.getId()
        protocolElement.metadata.extend(self.getMetadata())
        protocolElement.dataset_id = self.getParentContainer().getId()
        protocolElement.reference_set_id = self._referenceSet.getId()
        protocolElement.name = self.getLocalId()
        self.serializeAttributes(protocolElement)
        return protocolElement


Instruction:
Can you generate the documentation for the following Python 3 function
def _createGaVariant(self):
        ret = protocol.Variant()
        if self._creationTime:
            ret.created = self._creationTime
        if self._updatedTime:
            ret.updated = self._updatedTime
        ret.variant_set_id = self.getId()
        return ret

Response: Creates a new GA Variant object from this variant set.


Instruction:
Create a Python 3 function for
returning an ID string suitable for the specified GA Variant object in this variant set.

Response:
def getVariantId(self, gaVariant):
        """
        Returns an ID string suitable for the specified GA Variant
        object in this variant set.
        """
        md5 = self.hashVariant(gaVariant)
        compoundId = datamodel.VariantCompoundId(
            self.getCompoundId(), gaVariant.reference_name,
            str(gaVariant.start), md5)
        return str(compoundId)


Instruction:
How would you explain what the following Python 3 function does
def getCallSetId(self, sampleName):
        compoundId = datamodel.CallSetCompoundId(
            self.getCompoundId(), sampleName)
        return str(compoundId)

Response: Returns the callSetId for the specified sampleName in this VariantSet.


Instruction:
Here you have a function in Python 3, explain what it does
def hashVariant(cls, gaVariant):
        hash_str = gaVariant.reference_bases + \
            str(tuple(gaVariant.alternate_bases))
        return hashlib.md5(hash_str).hexdigest()

Response:
Generates an MD5 hash of the ga variant object to distinguish
        it from other variants at the same genomic coordinate.


Instruction:
Can you write a function in Python 3 where it
generates a random variant for the specified position using the specified random number generator.

Response:
def generateVariant(self, referenceName, position, randomNumberGenerator):
        """
        Generate a random variant for the specified position using the
        specified random number generator. This generator should be seeded
        with a value that is unique to this position so that the same variant
        will always be produced regardless of the order it is generated in.
        """
        variant = self._createGaVariant()
        variant.reference_name = referenceName
        variant.start = position
        variant.end = position + 1  # SNPs only for now
        bases = ["A", "C", "G", "T"]
        ref = randomNumberGenerator.choice(bases)
        variant.reference_bases = ref
        alt = randomNumberGenerator.choice(
            [base for base in bases if base != ref])
        variant.alternate_bases.append(alt)
        randChoice = randomNumberGenerator.randint(0, 2)
        if randChoice == 0:
            variant.filters_applied = False
        elif randChoice == 1:
            variant.filters_applied = True
            variant.filters_passed = True
        else:
            variant.filters_applied = True
            variant.filters_passed = False
            variant.filters_failed.append('q10')
        for callSet in self.getCallSets():
            call = variant.calls.add()
            call.call_set_id = callSet.getId()
            # for now, the genotype is either [0,1], [1,1] or [1,0] with equal
            # probability; probably will want to do something more
            # sophisticated later.
            randomChoice = randomNumberGenerator.choice(
                [[0, 1], [1, 0], [1, 1]])
            call.genotype.extend(randomChoice)
            # TODO What is a reasonable model for generating these likelihoods?
            # Are these log-scaled? Spec does not say.
            call.genotype_likelihood.extend([-100, -100, -100])
        variant.id = self.getVariantId(variant)
        return variant


Instruction:
Here you have a function in Python 3, explain what it does
def populateFromRow(self, variantSetRecord):
        self._created = variantSetRecord.created
        self._updated = variantSetRecord.updated
        self.setAttributesJson(variantSetRecord.attributes)
        self._chromFileMap = {}
        # We can't load directly as we want tuples to be stored
        # rather than lists.
        for key, value in json.loads(variantSetRecord.dataurlindexmap).items():
            self._chromFileMap[key] = tuple(value)
        self._metadata = []
        for jsonDict in json.loads(variantSetRecord.metadata):
            metadata = protocol.fromJson(json.dumps(jsonDict),
                                         protocol.VariantSetMetadata)
            self._metadata.append(metadata)

Response: Populates this VariantSet from the specified DB row.


Instruction:
Can you generate the documentation for the following Python 3 function
def populateFromFile(self, dataUrls, indexFiles):
        assert len(dataUrls) == len(indexFiles)
        for dataUrl, indexFile in zip(dataUrls, indexFiles):
            varFile = pysam.VariantFile(dataUrl, index_filename=indexFile)
            try:
                self._populateFromVariantFile(varFile, dataUrl, indexFile)
            finally:
                varFile.close()

Response: Populates this variant set using the specified data files and indexes.


Instruction:
Write a Python 3 script to
populate this VariantSet by examing all the VCF files in the specified directory.

Response:
def populateFromDirectory(self, vcfDirectory):
        """
        Populates this VariantSet by examing all the VCF files in the
        specified directory. This is mainly used for as a convenience
        for testing purposes.
        """
        pattern = os.path.join(vcfDirectory, "*.vcf.gz")
        dataFiles = []
        indexFiles = []
        for vcfFile in glob.glob(pattern):
            dataFiles.append(vcfFile)
            indexFiles.append(vcfFile + ".tbi")
        self.populateFromFile(dataFiles, indexFiles)


Instruction:
Write a Python 3 script to
perform consistency check on the variant set

Response:
def checkConsistency(self):
        """
        Perform consistency check on the variant set
        """
        for referenceName, (dataUrl, indexFile) in self._chromFileMap.items():
            varFile = pysam.VariantFile(dataUrl, index_filename=indexFile)
            try:
                for chrom in varFile.index:
                    chrom, _, _ = self.sanitizeVariantFileFetch(chrom)
                    if not isEmptyIter(varFile.fetch(chrom)):
                        self._checkMetadata(varFile)
                        self._checkCallSetIds(varFile)
            finally:
                varFile.close()


Instruction:
Here you have a function in Python 3, explain what it does
def _populateFromVariantFile(self, varFile, dataUrl, indexFile):
        if varFile.index is None:
            raise exceptions.NotIndexedException(dataUrl)
        for chrom in varFile.index:
            # Unlike Tabix indices, CSI indices include all contigs defined
            # in the BCF header.  Thus we must test each one to see if
            # records exist or else they are likely to trigger spurious
            # overlapping errors.
            chrom, _, _ = self.sanitizeVariantFileFetch(chrom)
            if not isEmptyIter(varFile.fetch(chrom)):
                if chrom in self._chromFileMap:
                    raise exceptions.OverlappingVcfException(dataUrl, chrom)
            self._chromFileMap[chrom] = dataUrl, indexFile
        self._updateMetadata(varFile)
        self._updateCallSetIds(varFile)
        self._updateVariantAnnotationSets(varFile, dataUrl)

Response: Populates the instance variables of this VariantSet from the specified pysam VariantFile object.


Instruction:
Write a Python 3 function that can
update the variant annotation set associated with this variant using the information in the specified pysam variantFile.

Response:
def _updateVariantAnnotationSets(self, variantFile, dataUrl):
        """
        Updates the variant annotation set associated with this variant using
        information in the specified pysam variantFile.
        """
        # TODO check the consistency of this between VCF files.
        if not self.isAnnotated():
            annotationType = None
            for record in variantFile.header.records:
                if record.type == "GENERIC":
                    if record.key == "SnpEffVersion":
                        annotationType = ANNOTATIONS_SNPEFF
                    elif record.key == "VEP":
                        version = record.value.split()[0]
                        # TODO we need _much_ more sophisticated processing
                        # of VEP versions here. When do they become
                        # incompatible?
                        if version == "v82":
                            annotationType = ANNOTATIONS_VEP_V82
                        elif version == "v77":
                            annotationType = ANNOTATIONS_VEP_V77
                        else:
                            # TODO raise a proper typed exception there with
                            # the file name as an argument.
                            raise ValueError(
                                "Unsupported VEP version {} in '{}'".format(
                                    version, dataUrl))
            if annotationType is None:
                infoKeys = variantFile.header.info.keys()
                if 'CSQ' in infoKeys or 'ANN' in infoKeys:
                    # TODO likewise, we want a properly typed exception that
                    # we can throw back to the repo manager UI and display
                    # as an import error.
                    raise ValueError(
                        "Unsupported annotations in '{}'".format(dataUrl))
            if annotationType is not None:
                vas = HtslibVariantAnnotationSet(self, self.getLocalId())
                vas.populateFromFile(variantFile, annotationType)
                self.addVariantAnnotationSet(vas)


Instruction:
Given the following Python 3 function, write the documentation
def _updateMetadata(self, variantFile):
        metadata = self._getMetadataFromVcf(variantFile)
        if self._metadata is None:
            self._metadata = metadata

Response: Updates the metadata for his variant set based on the specified variant file.


Instruction:
How would you explain what the following Python 3 function does
def _checkMetadata(self, variantFile):
        metadata = self._getMetadataFromVcf(variantFile)
        if self._metadata is not None and self._metadata != metadata:
            raise exceptions.InconsistentMetaDataException(
                variantFile.filename)

Response: Checks that the metadata of the specified variantFile is consistent with the current metadata.


Instruction:
Create a Python 3 function for
checking if the callSetIds of the current variantFile are consistent with the current callSetIds.

Response:
def _checkCallSetIds(self, variantFile):
        """
        Checks callSetIds for consistency
        """
        if len(self._callSetIdMap) > 0:
            callSetIds = set([
                self.getCallSetId(sample)
                for sample in variantFile.header.samples])
            if callSetIds != set(self._callSetIdMap.keys()):
                raise exceptions.InconsistentCallSetIdException(
                    variantFile.filename)


Instruction:
Write a Python 3 script for
updating the call set IDs based on the specified variant file.

Response:
def _updateCallSetIds(self, variantFile):
        """
        Updates the call set IDs based on the specified variant file.
        """
        if len(self._callSetIdMap) == 0:
            for sample in variantFile.header.samples:
                self.addCallSetFromName(sample)


Instruction:
Write a Python 3 script for
converting the pysam variant record into a GA4GH Variant object.

Response:
def convertVariant(self, record, callSetIds):
        """
        Converts the specified pysam variant record into a GA4GH Variant
        object. Only calls for the specified list of callSetIds will
        be included.
        """
        variant = self._createGaVariant()
        variant.reference_name = record.contig
        if record.id is not None:
            variant.names.extend(record.id.split(';'))
        variant.start = record.start          # 0-based inclusive
        variant.end = record.stop             # 0-based exclusive
        variant.reference_bases = record.ref
        if record.alts is not None:
            variant.alternate_bases.extend(list(record.alts))
        filterKeys = record.filter.keys()
        if len(filterKeys) == 0:
            variant.filters_applied = False
        else:
            variant.filters_applied = True
            if len(filterKeys) == 1 and filterKeys[0] == 'PASS':
                variant.filters_passed = True
            else:
                variant.filters_passed = False
                variant.filters_failed.extend(filterKeys)
        # record.qual is also available, when supported by GAVariant.
        for key, value in record.info.iteritems():
            if value is None:
                continue
            if key == 'SVTYPE':
                variant.variant_type = value
            elif key == 'SVLEN':
                variant.svlen = int(value[0])
            elif key == 'CIPOS':
                variant.cipos.extend(value)
            elif key == 'CIEND':
                variant.ciend.extend(value)
            elif isinstance(value, str):
                value = value.split(',')
            protocol.setAttribute(
                variant.attributes.attr[key].values, value)
        for callSetId in callSetIds:
            callSet = self.getCallSet(callSetId)
            pysamCall = record.samples[str(callSet.getSampleName())]
            variant.calls.add().CopyFrom(
                self._convertGaCall(callSet, pysamCall))
        variant.id = self.getVariantId(variant)
        return variant


Instruction:
How would you explain what the following Python 3 function does
def getPysamVariants(self, referenceName, startPosition, endPosition):
        if referenceName in self._chromFileMap:
            varFileName = self._chromFileMap[referenceName]
            referenceName, startPosition, endPosition = \
                self.sanitizeVariantFileFetch(
                    referenceName, startPosition, endPosition)
            cursor = self.getFileHandle(varFileName).fetch(
                referenceName, startPosition, endPosition)
            for record in cursor:
                yield record

Response: Returns an iterator over pysam VCF records corresponding to the specified query.


Instruction:
Can you write a function in Python 3 where it
returns an iterator over the specified variants.

Response:
def getVariants(self, referenceName, startPosition, endPosition,
                    callSetIds=[]):
        """
        Returns an iterator over the specified variants. The parameters
        correspond to the attributes of a GASearchVariantsRequest object.
        """
        if callSetIds is None:
            callSetIds = self._callSetIds
        else:
            for callSetId in callSetIds:
                if callSetId not in self._callSetIds:
                    raise exceptions.CallSetNotInVariantSetException(
                        callSetId, self.getId())
        for record in self.getPysamVariants(
                referenceName, startPosition, endPosition):
            yield self.convertVariant(record, callSetIds)


Instruction:
Create a Python 3 function to
return the id of a metadata object

Response:
def getMetadataId(self, metadata):
        """
        Returns the id of a metadata
        """
        return str(datamodel.VariantSetMetadataCompoundId(
            self.getCompoundId(), 'metadata:' + metadata.key))


Instruction:
Implement a function in Python 3 to
create a GA VariantAnnotation object from this variant set.

Response:
def _createGaVariantAnnotation(self):
        """
        Convenience method to set the common fields in a GA VariantAnnotation
        object from this variant set.
        """
        ret = protocol.VariantAnnotation()
        ret.created = self._creationTime
        ret.variant_annotation_set_id = self.getId()
        return ret


Instruction:
How would you code a function in Python 3 to
convert this VariantAnnotationSet into its GA4GH protocol equivalent.

Response:
def toProtocolElement(self):
        """
        Converts this VariantAnnotationSet into its GA4GH protocol equivalent.
        """
        protocolElement = protocol.VariantAnnotationSet()
        protocolElement.id = self.getId()
        protocolElement.variant_set_id = self._variantSet.getId()
        protocolElement.name = self.getLocalId()
        protocolElement.analysis.CopyFrom(self.getAnalysis())
        self.serializeAttributes(protocolElement)
        return protocolElement


Instruction:
Can you implement a function in Python 3 that
generates an MD5 hash of the gaVariant and gaVariantAnnotation objects

Response:
def hashVariantAnnotation(cls, gaVariant, gaVariantAnnotation):
        """
        Produces an MD5 hash of the gaVariant and gaVariantAnnotation objects
        """
        treffs = [treff.id for treff in gaVariantAnnotation.transcript_effects]
        return hashlib.md5(
            "{}\t{}\t{}\t".format(
                gaVariant.reference_bases, tuple(gaVariant.alternate_bases),
                treffs)
            ).hexdigest()


Instruction:
Write a Python 3 script for
generating a stringified compoundId representing a variantAnnotation.

Response:
def getVariantAnnotationId(self, gaVariant, gaAnnotation):
        """
        Produces a stringified compoundId representing a variant
        annotation.
        :param gaVariant:   protocol.Variant
        :param gaAnnotation: protocol.VariantAnnotation
        :return:  compoundId String
        """
        md5 = self.hashVariantAnnotation(gaVariant, gaAnnotation)
        compoundId = datamodel.VariantAnnotationCompoundId(
            self.getCompoundId(), gaVariant.reference_name,
            str(gaVariant.start), md5)
        return str(compoundId)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generateVariantAnnotation(self, variant):
        # To make this reproducible, make a seed based on this
        # specific variant.
        seed = self._randomSeed + variant.start + variant.end
        randomNumberGenerator = random.Random()
        randomNumberGenerator.seed(seed)
        ann = protocol.VariantAnnotation()
        ann.variant_annotation_set_id = str(self.getCompoundId())
        ann.variant_id = variant.id
        ann.created = datetime.datetime.now().isoformat() + "Z"
        # make a transcript effect for each alternate base element
        # multiplied by a random integer (1,5)
        for base in variant.alternate_bases:
            ann.transcript_effects.add().CopyFrom(
                self.generateTranscriptEffect(
                    variant, ann, base, randomNumberGenerator))
        ann.id = self.getVariantAnnotationId(variant, ann)
        return ann

Response: Generates a random variant annotation based on a given variant.


Instruction:
Explain what the following Python 3 code does
def populateFromRow(self, annotationSetRecord):
        self._annotationType = annotationSetRecord.annotationtype
        self._analysis = protocol.fromJson(
            annotationSetRecord.analysis, protocol.Analysis)
        self._creationTime = annotationSetRecord.created
        self._updatedTime = annotationSetRecord.updated
        self.setAttributesJson(annotationSetRecord.attributes)

Response: Populates this VariantAnnotationSet from the specified DB row.


Instruction:
Write a Python 3 function that can
return a GA4GH Analysis object for the specified VCF header.

Response:
def _getAnnotationAnalysis(self, varFile):
        """
        Assembles metadata within the VCF header into a GA4GH Analysis object.

        :return: protocol.Analysis
        """
        header = varFile.header
        analysis = protocol.Analysis()
        formats = header.formats.items()
        infos = header.info.items()
        filters = header.filters.items()
        for prefix, content in [("FORMAT", formats), ("INFO", infos),
                                ("FILTER", filters)]:
            for contentKey, value in content:
                key = "{0}.{1}".format(prefix, value.name)
                if key not in analysis.attributes.attr:
                    analysis.attributes.attr[key].Clear()
                if value.description is not None:
                    analysis.attributes.attr[
                        key].values.add().string_value = value.description
        analysis.created = self._creationTime
        analysis.updated = self._updatedTime
        for r in header.records:
            # Don't add a key to info if there's nothing in the value
            if r.value is not None:
                if r.key not in analysis.attributes.attr:
                    analysis.attributes.attr[r.key].Clear()
                analysis.attributes.attr[r.key] \
                    .values.add().string_value = str(r.value)
            if r.key == "created" or r.key == "fileDate":
                # TODO handle more date formats
                try:
                    if '-' in r.value:
                        fmtStr = "%Y-%m-%d"
                    else:
                        fmtStr = "%Y%m%d"
                    analysis.created = datetime.datetime.strptime(
                        r.value, fmtStr).isoformat() + "Z"
                except ValueError:
                    # is there a logger we should tell?
                    # print("INFO: Could not parse variant annotation time")
                    pass  # analysis.create_date_time remains datetime.now()
            if r.key == "software":
                analysis.software.append(r.value)
            if r.key == "name":
                analysis.name = r.value
            if r.key == "description":
                analysis.description = r.value
        analysis.id = str(datamodel.VariantAnnotationSetAnalysisCompoundId(
            self._compoundId, "analysis"))
        return analysis


Instruction:
Here you have a function in Python 3, explain what it does
def getVariantAnnotations(self, referenceName, startPosition, endPosition):
        variantIter = self._variantSet.getPysamVariants(
            referenceName, startPosition, endPosition)
        for record in variantIter:
            yield self.convertVariantAnnotation(record)

Response: Generator for iterating through the variant annotations in this variant annotation set.


Instruction:
Write a Python 3 script to
convert a position string to a GA4GH AlleleLocation object.

Response:
def convertLocation(self, pos):
        """
        Accepts a position string (start/length) and returns
        a GA4GH AlleleLocation with populated fields.
        :param pos:
        :return: protocol.AlleleLocation
        """
        if isUnspecified(pos):
            return None
        coordLen = pos.split('/')
        if len(coordLen) > 1:
            allLoc = self._createGaAlleleLocation()
            allLoc.start = int(coordLen[0]) - 1
            return allLoc
        return None


Instruction:
Implement a function in Python 3 to
convert an annotation in HGVS notation and returns an AlleleLocation with populated fields.

Response:
def convertLocationHgvsC(self, hgvsc):
        """
        Accepts an annotation in HGVS notation and returns
        an AlleleLocation with populated fields.
        :param hgvsc:
        :return:
        """
        if isUnspecified(hgvsc):
            return None
        match = re.match(".*c.(\d+)(\D+)>(\D+)", hgvsc)
        if match:
            pos = int(match.group(1))
            if pos > 0:
                allLoc = self._createGaAlleleLocation()
                allLoc.start = pos - 1
                allLoc.reference_sequence = match.group(2)
                allLoc.alternate_sequence = match.group(3)
                return allLoc
        return None


Instruction:
Given the following Python 3 function, write the documentation
def convertLocationHgvsP(self, hgvsp):
        if isUnspecified(hgvsp):
            return None
        match = re.match(".*p.(\D+)(\d+)(\D+)", hgvsp, flags=re.UNICODE)
        if match is not None:
            allLoc = self._createGaAlleleLocation()
            allLoc.reference_sequence = match.group(1)
            allLoc.start = int(match.group(2)) - 1
            allLoc.alternate_sequence = match.group(3)
            return allLoc
        return None

Response: Converts an annotation in HGVS notation and returns an AlleleLocation with populated fields.


Instruction:
Write a Python 3 script to
add locations to a GA4GH transcript effect object by parsing HGVS annotation fields in concert with and supplied position values.

Response:
def addLocations(self, effect, protPos, cdnaPos):
        """
        Adds locations to a GA4GH transcript effect object
        by parsing HGVS annotation fields in concert with
        and supplied position values.
        :param effect: protocol.TranscriptEffect
        :param protPos: String representing protein position from VCF
        :param cdnaPos: String representing coding DNA location
        :return: effect protocol.TranscriptEffect
        """
        self.addCDSLocation(effect, cdnaPos)
        self.addCDNALocation(effect, cdnaPos)
        self.addProteinLocation(effect, protPos)
        return effect


Instruction:
How would you explain what the following Python 3 function does
def convertTranscriptEffect(self, annStr, hgvsG):
        effect = self._createGaTranscriptEffect()
        effect.hgvs_annotation.CopyFrom(protocol.HGVSAnnotation())
        annDict = dict()
        if self._annotationType == ANNOTATIONS_SNPEFF:
            annDict = dict(zip(self. SNPEFF_FIELDS, annStr.split("|")))
        elif self._annotationType == ANNOTATIONS_VEP_V82:
            annDict = dict(zip(self.VEP_FIELDS, annStr.split("|")))
        else:
            annDict = dict(zip(self.CSQ_FIELDS, annStr.split("|")))
        annDict["hgvs_annotation.genomic"] = hgvsG if hgvsG else u''
        for key, val in annDict.items():
            try:
                protocol.deepSetAttr(effect, key, val)
            except AttributeError:
                if val and key not in self.EXCLUDED_FIELDS:
                    protocol.setAttribute(
                        effect.attributes.attr[key].values, val)
        effect.effects.extend(self.convertSeqOntology(annDict.get('effects')))
        self.addLocations(
            effect, annDict.get('protPos'), annDict.get('cdnaPos'))
        effect.id = self.getTranscriptEffectId(effect)
        return effect

Response: Converts an ANN string of a SnpEff generated VCF into a GA4GH transcript effect object.


Instruction:
Write a Python 3 function for
converting a string of sequence ontology effects and creates an array of ontology terms for each.

Response:
def convertSeqOntology(self, seqOntStr):
        """
        Splits a string of sequence ontology effects and creates
        an ontology term record for each, which are built into
        an array of return soTerms.
        :param seqOntStr:
        :return: [protocol.OntologyTerm]
        """
        return [
            self._ontology.getGaTermByName(soName)
            for soName in seqOntStr.split('&')]


Instruction:
How would you implement a function in Python 3 that
converts the specified pysam variant record into a GA4GH variant annotation object using the specified function to convert the variant and transcripts.

Response:
def convertVariantAnnotation(self, record):
        """
        Converts the specfied pysam variant record into a GA4GH variant
        annotation object using the specified function to convert the
        transcripts.
        """
        variant = self._variantSet.convertVariant(record, [])
        annotation = self._createGaVariantAnnotation()
        annotation.variant_id = variant.id
        gDots = record.info.get(b'HGVS.g')
        # Convert annotations from INFO field into TranscriptEffect
        transcriptEffects = []
        annotations = record.info.get(b'ANN') or record.info.get(b'CSQ')
        for i, ann in enumerate(annotations):
            hgvsG = gDots[i % len(variant.alternate_bases)] if gDots else None
            transcriptEffects.append(self.convertTranscriptEffect(ann, hgvsG))
        annotation.transcript_effects.extend(transcriptEffects)
        annotation.id = self.getVariantAnnotationId(variant, annotation)
        return variant, annotation


Instruction:
Create a Python 3 function for
returning name = value for a single attribute

Response:
def _attributeStr(self, name):
        """
        Return name=value for a single attribute
        """
        return "{}={}".format(
            _encodeAttr(name),
            ",".join([_encodeAttr(v) for v in self.attributes[name]]))


Instruction:
Given the following Python 3 function, write the documentation
def _attributeStrs(self):
        return ";".join([self._attributeStr(name)
                         for name in self.attributes.iterkeys()])

Response: Return name = value semi - colon - separated string for attributes


Instruction:
How would you implement a function in Python 3 that
returns the feature name of the record.

Response:
def featureName(self):
        """
        ID attribute from GFF3 or None if record doesn't have it.
        Called "Name" rather than "Id" within GA4GH, as there is
        no guarantee of either uniqueness or existence.
        """
        featId = self.attributes.get("ID")
        if featId is not None:
            featId = featId[0]
        return featId


Instruction:
Make a summary of the following Python 3 code
def _linkFeature(self, feature):
        parentNames = feature.attributes.get("Parent")
        if parentNames is None:
            self.roots.add(feature)
        else:
            for parentName in parentNames:
                self._linkToParent(feature, parentName)

Response: Link a feature with its parents.


Instruction:
Here you have a function in Python 3, explain what it does
def _linkToParent(self, feature, parentName):
        parentParts = self.byFeatureName.get(parentName)
        if parentParts is None:
            raise GFF3Exception(
                "Parent feature does not exist: {}".format(parentName),
                self.fileName)
        # parent maybe disjoint
        for parentPart in parentParts:
            feature.parents.add(parentPart)
            parentPart.children.add(feature)

Response: Link a feature with its children.


Instruction:
Create a Python 3 function for
linking child features to parents

Response:
def linkChildFeaturesToParents(self):
        """
        finish loading the set, constructing the tree
        """
        # features maybe disjoint
        for featureParts in self.byFeatureName.itervalues():
            for feature in featureParts:
                self._linkFeature(feature)


Instruction:
Write a Python 3 script to
return the key for a record in the order of genomic coordinates.

Response:
def _recSortKey(r):
        """
        Sort order for Features, by genomic coordinate,
        disambiguated by feature type (alphabetically).
        """
        return r.seqname, r.start, -r.end, r.type


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _writeRec(self, fh, rec):
        fh.write(str(rec) + "\n")
        for child in sorted(rec.children, key=self._recSortKey):
            self._writeRec(fh, child)

Response: Writes a single record to a filehandle fh.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write(self, fh):
        fh.write(GFF3_HEADER+"\n")
        for root in sorted(self.roots, key=self._recSortKey):
            self._writeRec(fh, root)

Response: Write set to a GFF3 format file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _open(self):
        if self.fileName.endswith(".gz"):
            return gzip.open(self.fileName)
        elif self.fileName.endswith(".bz2"):
            return bz2.BZ2File(self.fileName)
        else:
            return open(self.fileName)

Response: Open the input file optionally with decompression


Instruction:
Write a Python 3 function that can
parse the attribute value from the attribute string.

Response:
def _parseAttrVal(self, attrStr):
        """
        Returns tuple of tuple of (attr, value), multiple are returned to
        handle multi-value attributes.
        """
        m = self.SPLIT_ATTR_RE.match(attrStr)
        if m is None:
            raise GFF3Exception(
                "can't parse attribute/value: '" + attrStr +
                "'", self.fileName, self.lineNumber)
        name = urllib.unquote(m.group(1))
        val = m.group(2)
        # Split by comma to separate then unquote.
        # Commas in values must be url encoded.
        return name, [urllib.unquote(v) for v in val.split(',')]


Instruction:
Write a Python 3 function that can
parse the attributes and values from a string.

Response:
def _parseAttrs(self, attrsStr):
        """
        Parse the attributes and values
        """
        attributes = dict()
        for attrStr in self.SPLIT_ATTR_COL_RE.split(attrsStr):
            name, vals = self._parseAttrVal(attrStr)
            if name in attributes:
                raise GFF3Exception(
                    "duplicated attribute name: {}".format(name),
                    self.fileName, self.lineNumber)
            attributes[name] = vals
        return attributes


Instruction:
Make a summary of the following Python 3 code
def parse(self):
        fh = self._open()
        try:
            gff3Set = Gff3Set(self.fileName)
            for line in fh:
                self.lineNumber += 1
                self._parseLine(gff3Set, line[0:-1])
        finally:
            fh.close()
        gff3Set.linkChildFeaturesToParents()
        return gff3Set

Response: Run the parse and return the resulting Gff3Set object.


Instruction:
How would you implement a function in Python 3 that
adds the specified dataset to this data repository.

Response:
def addDataset(self, dataset):
        """
        Adds the specified dataset to this data repository.
        """
        id_ = dataset.getId()
        self._datasetIdMap[id_] = dataset
        self._datasetNameMap[dataset.getLocalId()] = dataset
        self._datasetIds.append(id_)


Instruction:
Implement a Python 3 function for
adding the specified reference set to this data repository.

Response:
def addReferenceSet(self, referenceSet):
        """
        Adds the specified reference set to this data repository.
        """
        id_ = referenceSet.getId()
        self._referenceSetIdMap[id_] = referenceSet
        self._referenceSetNameMap[referenceSet.getLocalId()] = referenceSet
        self._referenceSetIds.append(id_)


Instruction:
Can you tell what is the following Python 3 function doing
def addOntology(self, ontology):
        self._ontologyNameMap[ontology.getName()] = ontology
        self._ontologyIdMap[ontology.getId()] = ontology
        self._ontologyIds.append(ontology.getId())

Response: Add an ontology to this data repository.


Instruction:
Given the following Python 3 function, write the documentation
def getPeer(self, url):
        peers = filter(lambda x: x.getUrl() == url, self.getPeers())
        if len(peers) == 0:
            raise exceptions.PeerNotFoundException(url)
        return peers[0]

Response: Returns the first peer in the datarepo with the given url.


Instruction:
Implement a function in Python 3 to
return a dataset with the specified ID or raises a NameException if the dataset does not exist.

Response:
def getDataset(self, id_):
        """
        Returns a dataset with the specified ID, or raises a
        DatasetNotFoundException if it does not exist.
        """
        if id_ not in self._datasetIdMap:
            raise exceptions.DatasetNotFoundException(id_)
        return self._datasetIdMap[id_]


Instruction:
Write a Python 3 script for
returning the dataset with the specified name.

Response:
def getDatasetByName(self, name):
        """
        Returns the dataset with the specified name.
        """
        if name not in self._datasetNameMap:
            raise exceptions.DatasetNameNotFoundException(name)
        return self._datasetNameMap[name]


Instruction:
Implement a function in Python 3 to
return the ontology with the specified ID.

Response:
def getOntology(self, id_):
        """
        Returns the ontology with the specified ID.
        """
        if id_ not in self._ontologyIdMap:
            raise exceptions.OntologyNotFoundException(id_)
        return self._ontologyIdMap[id_]


Instruction:
Make a summary of the following Python 3 code
def getOntologyByName(self, name):
        if name not in self._ontologyNameMap:
            raise exceptions.OntologyNameNotFoundException(name)
        return self._ontologyNameMap[name]

Response: Returns an ontology by name


Instruction:
How would you implement a function in Python 3 that
returns the ReferenceSet with the specified ID or raises a Exception if it does not exist.

Response:
def getReferenceSet(self, id_):
        """
        Retuns the ReferenceSet with the specified ID, or raises a
        ReferenceSetNotFoundException if it does not exist.
        """
        if id_ not in self._referenceSetIdMap:
            raise exceptions.ReferenceSetNotFoundException(id_)
        return self._referenceSetIdMap[id_]


Instruction:
How would you explain what the following Python 3 function does
def getReferenceSetByName(self, name):
        if name not in self._referenceSetNameMap:
            raise exceptions.ReferenceSetNameNotFoundException(name)
        return self._referenceSetNameMap[name]

Response: Returns the reference set with the specified name.


Instruction:
Can you generate the documentation for the following Python 3 function
def getReadGroupSet(self, id_):
        compoundId = datamodel.ReadGroupSetCompoundId.parse(id_)
        dataset = self.getDataset(compoundId.dataset_id)
        return dataset.getReadGroupSet(id_)

Response: Returns the readgroup set with the specified ID.


Instruction:
Write a Python 3 function for
returning the readgroup set with the specified ID.

Response:
def getVariantSet(self, id_):
        """
        Returns the readgroup set with the specified ID.
        """
        compoundId = datamodel.VariantSetCompoundId.parse(id_)
        dataset = self.getDataset(compoundId.dataset_id)
        return dataset.getVariantSet(id_)


Instruction:
Write a Python 3 function that can
print a summary of this data repository to stdout.

Response:
def printSummary(self):
        """
        Prints a summary of this data repository to stdout.
        """
        print("Ontologies:")
        for ontology in self.getOntologys():
            print(
                "",
                ontology.getOntologyPrefix(),
                ontology.getName(),
                ontology.getDataUrl(),
                sep="\t")
        print("ReferenceSets:")
        for referenceSet in self.getReferenceSets():
            print(
                "", referenceSet.getLocalId(), referenceSet.getId(),
                referenceSet.getDescription(), referenceSet.getDataUrl(),
                sep="\t")
            for reference in referenceSet.getReferences():
                print(
                    "\t", reference.getLocalId(), reference.getId(),
                    sep="\t")
        print("Datasets:")
        for dataset in self.getDatasets():
            print(
                "", dataset.getLocalId(), dataset.getId(),
                dataset.getDescription(), sep="\t")
            print("\tReadGroupSets:")
            for readGroupSet in dataset.getReadGroupSets():
                print(
                    "\t", readGroupSet.getLocalId(),
                    readGroupSet.getReferenceSet().getLocalId(),
                    readGroupSet.getId(),
                    readGroupSet.getDataUrl(), sep="\t")
                for readGroup in readGroupSet.getReadGroups():
                    print(
                        "\t\t", readGroup.getId(), readGroup.getLocalId(),
                        sep="\t")
            print("\tVariantSets:")
            for variantSet in dataset.getVariantSets():
                print(
                    "\t", variantSet.getLocalId(),
                    variantSet.getReferenceSet().getLocalId(),
                    variantSet.getId(),
                    sep="\t")
                if variantSet.getNumVariantAnnotationSets() > 0:
                    print("\t\tVariantAnnotationSets:")
                    for vas in variantSet.getVariantAnnotationSets():
                        print(
                            "\t\t", vas.getLocalId(),
                            vas.getAnnotationType(),
                            vas.getOntology().getName(), sep="\t")
            print("\tFeatureSets:")
            for featureSet in dataset.getFeatureSets():
                print(
                    "\t", featureSet.getLocalId(),
                    featureSet.getReferenceSet().getLocalId(),
                    featureSet.getOntology().getName(),
                    featureSet.getId(),
                    sep="\t")
            print("\tContinuousSets:")
            for continuousSet in dataset.getContinuousSets():
                print(
                    "\t", continuousSet.getLocalId(),
                    continuousSet.getReferenceSet().getLocalId(),
                    continuousSet.getId(),
                    sep="\t")
            print("\tPhenotypeAssociationSets:")
            for phenotypeAssociationSet in \
                    dataset.getPhenotypeAssociationSets():
                print(
                    "\t", phenotypeAssociationSet.getLocalId(),
                    phenotypeAssociationSet.getParentContainer().getId(),
                    sep="\t")
                # TODO -  please improve this listing
            print("\tRnaQuantificationSets:")
            for rna_quantification_set in dataset.getRnaQuantificationSets():
                print(
                    "\t", rna_quantification_set.getLocalId(),
                    rna_quantification_set.getId(), sep="\t")
                for quant in rna_quantification_set.getRnaQuantifications():
                        print(
                            "\t\t", quant.getLocalId(),
                            quant._description,
                            ",".join(quant._readGroupIds),
                            ",".join(quant._featureSetIds), sep="\t")


Instruction:
Write a Python 3 script for
returning an iterator over all read groups in the data repo

Response:
def allReadGroups(self):
        """
        Return an iterator over all read groups in the data repo
        """
        for dataset in self.getDatasets():
            for readGroupSet in dataset.getReadGroupSets():
                for readGroup in readGroupSet.getReadGroups():
                    yield readGroup


Instruction:
Here you have a function in Python 3, explain what it does
def allFeatures(self):
        for dataset in self.getDatasets():
            for featureSet in dataset.getFeatureSets():
                for feature in featureSet.getFeatures():
                    yield feature

Response: Return an iterator over all features in the data repo


Instruction:
Can you generate a brief explanation for the following Python 3 code
def allCallSets(self):
        for dataset in self.getDatasets():
            for variantSet in dataset.getVariantSets():
                for callSet in variantSet.getCallSets():
                    yield callSet

Response: Returns an iterator over all call sets in the data repo


Instruction:
Explain what the following Python 3 code does
def allVariantAnnotationSets(self):
        for dataset in self.getDatasets():
            for variantSet in dataset.getVariantSets():
                for vaSet in variantSet.getVariantAnnotationSets():
                    yield vaSet

Response: Returns an iterator over all variant annotation sets in the data repo


Instruction:
Can you generate a brief explanation for the following Python 3 code
def allRnaQuantifications(self):
        for dataset in self.getDatasets():
            for rnaQuantificationSet in dataset.getRnaQuantificationSets():
                for rnaQuantification in \
                        rnaQuantificationSet.getRnaQuantifications():
                    yield rnaQuantification

Response: Returns an iterator over all rna quantifications in this instance


Instruction:
Write a Python 3 script to
return an iterator over all expression levels in this instance

Response:
def allExpressionLevels(self):
        """
        Return an iterator over all expression levels
        """
        for dataset in self.getDatasets():
            for rnaQuantificationSet in dataset.getRnaQuantificationSets():
                for rnaQuantification in \
                        rnaQuantificationSet.getRnaQuantifications():
                    for expressionLevel in \
                            rnaQuantification.getExpressionLevels():
                        yield expressionLevel


Instruction:
Create a Python 3 function to
find a peer by URL and return the first peer record with that URL.

Response:
def getPeer(self, url):
        """
        Finds a peer by URL and return the first peer record with that URL.
        """
        peers = list(models.Peer.select().where(models.Peer.url == url))
        if len(peers) == 0:
            raise exceptions.PeerNotFoundException(url)
        return peers[0]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getPeers(self, offset=0, limit=1000):
        select = models.Peer.select().order_by(
            models.Peer.url).limit(limit).offset(offset)
        return [peers.Peer(p.url, record=p) for p in select]

Response: Get the list of peers using an SQL offset and limit. Returns a list of peers in a list.


Instruction:
Can you write a function in Python 3 where it
takes a model class and attempts to create a table in TSV format

Response:
def tableToTsv(self, model):
        """
        Takes a model class and attempts to create a table in TSV format
        that can be imported into a spreadsheet program.
        """
        first = True
        for item in model.select():
            if first:
                header = "".join(
                    ["{}\t".format(x) for x in model._meta.fields.keys()])
                print(header)
                first = False
            row = "".join(
                ["{}\t".format(
                    getattr(item, key)) for key in model._meta.fields.keys()])
            print(row)


Instruction:
Can you tell what is the following Python 3 function doing
def clearAnnouncements(self):
        try:
            q = models.Announcement.delete().where(
                models.Announcement.id > 0)
            q.execute()
        except Exception as e:
            raise exceptions.RepoManagerException(e)

Response: Clears the announcement table.


Instruction:
Given the following Python 3 function, write the documentation
def insertAnnouncement(self, announcement):
        url = announcement.get('url', None)
        try:
            peers.Peer(url)
        except:
            raise exceptions.BadUrlException(url)
        try:
            # TODO get more details about the user agent
            models.Announcement.create(
                url=announcement.get('url'),
                attributes=json.dumps(announcement.get('attributes', {})),
                remote_addr=announcement.get('remote_addr', None),
                user_agent=announcement.get('user_agent', None))
        except Exception as e:
            raise exceptions.RepoManagerException(e)

Response: Inserts an announcement into the registry.


Instruction:
Can you create a Python 3 function that
opens this repo in the specified mode.

Response:
def open(self, mode=MODE_READ):
        """
        Opens this repo in the specified mode.

        TODO: figure out the correct semantics of this and document
        the intended future behaviour as well as the current
        transitional behaviour.
        """
        if mode not in [MODE_READ, MODE_WRITE]:
            error = "Open mode must be '{}' or '{}'".format(
                MODE_READ, MODE_WRITE)
            raise ValueError(error)
        self._openMode = mode
        if mode == MODE_READ:
            self.assertExists()
        if mode == MODE_READ:
            # This is part of the transitional behaviour where
            # we load the whole DB into memory to get access to
            # the data model.
            self.load()