Instruction:
Can you generate the documentation for the following Python 3 function
def toggle_axes(self, parameters = None):
        '''Toggle axes [x,y,z] on and off for the current representation
        Parameters: dictionary of parameters to control axes:
            position/p:     origin of axes
            length/l:       length of axis
            offset/o:       offset to place axis labels
            axis_colors/ac: axis colors
            text_colors/tc: label colors
            radii/r:        axis radii
            text/t:         label text
            sizes/s:        label sizes
            fonts/f:        label fonts'''

        if len(self._axes_reps)>0:
            for rep_id in self._axes_reps:
                self.remove_representation(rep_id)
            self._axes_reps = []
        else:
            if not isinstance(parameters,dict):
                parameters={}

            def defaults(pdict,keys,default,length=3,instance=(int,float)):
                '''Helper function to generate default values and handle errors'''
                for k in keys:
                    val=pdict.get(k)
                    if val!=None:
                        break
                if val==None:
                    val=default
                elif isinstance(val,instance) and length>1:
                    val = [val]*length
                elif isinstance(val,(list,np.generic,np.ndarray)) and length>1:
                    if not all([isinstance(v,instance) for v in val]):
                        raise RuntimeError("Invalid type {t} for parameter {p}. Use {i}.".format(t=type(val),p=val,i=instance))
                elif not isinstance(val,instance):
                    raise RuntimeError("Invalid type {t} for parameter {p}. Use {i}.".format(t=type(val),p=val,i=instance))
                return val

            p =  defaults(parameters,['positions','position','p'],np.average(self.coordinates,0))
            l =  defaults(parameters,['lengths','length','l'],max([np.linalg.norm(x-p) for x in self.coordinates]),1)
            o =  defaults(parameters,['offsets','offset','o'],l*1.05,1)
            ac = defaults(parameters,[a+c for a in ['axis_','a',''] for c in ['colors','colours','color','colour','c']],[0xff0000,0x00ff00,0x0000ff],3,(int,hex))
            tc = defaults(parameters,[a+c for a in ['text_','t',''] for c in ['colors','colours','color','colour','c']],[0xff0000,0x00ff00,0x0000ff],3,(int,hex))
            r =  defaults(parameters,['radii','radius','r'],[0.005]*3,3)
            t =  defaults(parameters,['text','labels','t'],['X','Y','Z'],3,str)
            s =  defaults(parameters,['sizes','size','s'],[32]*3,3)
            f =  defaults(parameters,['fonts','font','f'],['Arial']*3,3,str)

            starts=np.array([p,p,p],float)
            ends=np.array([p+[l,0,0],p+[0,l,0],p+[0,0,l]],float)
            axis_labels_coords=np.array([p+[o,0,0],p+[0,o,0],p+[0,0,o]],float)

            a_rep=self.add_representation('cylinders',{"startCoords":starts,
                                                     "endCoords":ends,
                                                     "colors":ac,
                                                     "radii":r})

            t_rep=self.add_representation('text',{"coordinates":axis_labels_coords,
                                                  "text":t,
                                                  "colors":tc,
                                                  "sizes":s,
                                                  "fonts":f})
            self._axes_reps = [a_rep, t_rep]

Response: Toggle the axes on and off for the current representation of the object.


Instruction:
Given the following Python 3 function, write the documentation
def lines(self):
        '''Display the system bonds as lines.

        '''
        if "bonds" not in self.topology:
            return

        bond_start, bond_end = zip(*self.topology['bonds'])
        bond_start = np.array(bond_start)
        bond_end = np.array(bond_end)

        color_array = np.array([get_atom_color(t) for t in self.topology['atom_types']])
        lines = self.add_representation('lines', {'startCoords': self.coordinates[bond_start],
                                                  'endCoords': self.coordinates[bond_end],
                                                  'startColors': color_array[bond_start].tolist(),
                                                  'endColors': color_array[bond_end].tolist()})

        def update(self=self, lines=lines):
            bond_start, bond_end = zip(*self.topology['bonds'])
            bond_start = np.array(bond_start)
            bond_end = np.array(bond_end)

            self.update_representation(lines, {'startCoords': self.coordinates[bond_start],
                                                 'endCoords': self.coordinates[bond_end]})
        self.update_callbacks.append(update)
        self.autozoom(self.coordinates)

Response: Display the system bonds as lines.


Instruction:
Create a Python 3 function to
display atoms as points and bonds as lines.

Response:
def wireframe(self, pointsize=0.2, opacity=1.0):
        '''Display atoms as points of size *pointsize* and bonds as lines.'''
        self.points(pointsize, opacity=opacity)
        self.lines()


Instruction:
Can you tell what is the following Python 3 function doing
def ball_and_sticks(self, ball_radius=0.05, stick_radius=0.02, colorlist=None, opacity=1.0):

        # Add the spheres

        if colorlist is None:
            colorlist = [get_atom_color(t) for t in self.topology['atom_types']]
        sizes = [ball_radius] * len(self.topology['atom_types'])

        spheres = self.add_representation('spheres', {'coordinates': self.coordinates.astype('float32'),
                                                      'colors': colorlist,
                                                      'radii': sizes,
                                                      'opacity': opacity})

        def update(self=self, spheres=spheres):
            self.update_representation(spheres, {'coordinates': self.coordinates.astype('float32')})

        self.update_callbacks.append(update)

        # Add the cylinders

        if 'bonds' in self.topology and self.topology['bonds'] is not None:
            start_idx, end_idx = zip(*self.topology['bonds'])
            # Added this so bonds don't go through atoms when opacity<1.0
            new_start_coords = []
            new_end_coords = []
            for bond_ind, bond in enumerate(self.topology['bonds']):
                trim_amt = (ball_radius**2 - stick_radius**2)**0.5 if ball_radius>stick_radius else 0
                start_coord = self.coordinates[bond[0]]
                end_coord = self.coordinates[bond[1]]
                vec = (end_coord-start_coord)/np.linalg.norm(end_coord-start_coord)
                new_start_coords.append(start_coord+vec*trim_amt)
                new_end_coords.append(end_coord-vec*trim_amt)

            cylinders = self.add_representation('cylinders', {'startCoords': np.array(new_start_coords,dtype='float32'),
                                                  'endCoords': np.array(new_end_coords,dtype='float32'),
                                                  'colors': [0xcccccc] * len(new_start_coords),
                                                  'radii': [stick_radius] * len(new_start_coords),
                                                  'opacity': opacity})
            # Update closure
            def update(self=self, rep=cylinders, start_idx=start_idx, end_idx=end_idx):
                self.update_representation(rep, {'startCoords': self.coordinates[list(start_idx)],
                                                 'endCoords': self.coordinates[list(end_idx)]})

            self.update_callbacks.append(update)
        self.autozoom(self.coordinates)

Response: Display the system using a ball and stick representation.


Instruction:
Given the following Python 3 function, write the documentation
def line_ribbon(self):
        '''Display the protein secondary structure as a white lines that passes through the
           backbone chain.

        '''
        # Control points are the CA (C alphas)
        backbone = np.array(self.topology['atom_names']) == 'CA'
        smoothline = self.add_representation('smoothline', {'coordinates': self.coordinates[backbone],
                                                            'color': 0xffffff})

        def update(self=self, smoothline=smoothline):
            self.update_representation(smoothline, {'coordinates': self.coordinates[backbone]})
        self.update_callbacks.append(update)

        self.autozoom(self.coordinates)

Response:
Display the protein secondary structure as a white lines that passes through the
           backbone chain.


Instruction:
Given the following Python 3 function, write the documentation
def cylinder_and_strand(self):
        '''Display the protein secondary structure as a white,
           solid tube and the alpha-helices as yellow cylinders.

        '''
        top = self.topology
        # We build a  mini-state machine to find the
        # start end of helices and such
        in_helix = False
        helices_starts = []
        helices_ends = []
        coils = []

        coil = []
        for i, typ in enumerate(top['secondary_structure']):
            if typ == 'H':
                if in_helix == False:
                    # We become helices
                    helices_starts.append(top['residue_indices'][i][0])
                    in_helix = True

                    # We end the previous coil
                    coil.append(top['residue_indices'][i][0])
            else:
                if in_helix == True:
                    # We stop being helices
                    helices_ends.append(top['residue_indices'][i][0])

                    # We start a new coil
                    coil = []
                    coils.append(coil)
                    in_helix = False

                # We add control points
                coil.append(top['residue_indices'][i][0])
                [coil.append(j) for j in top['residue_indices'][i] if top['atom_names'][j] == 'CA']

        # We add the coils
        coil_representations = []
        for control_points in coils:
            rid = self.add_representation('smoothtube', {'coordinates': self.coordinates[control_points],
                                                   'radius': 0.05,
                                                   'resolution': 4,
                                                   'color': 0xffffff})
            coil_representations.append(rid)

        start_idx, end_idx = helices_starts, helices_ends
        cylinders = self.add_representation('cylinders', {'startCoords': self.coordinates[list(start_idx)],
                                              'endCoords': self.coordinates[list(end_idx)],
                                              'colors': [0xffff00] * len(self.coordinates),
                                              'radii': [0.15] * len(self.coordinates)})
        def update(self=self, cylinders=cylinders, coils=coils,
                   coil_representations=coil_representations,
                   start_idx=start_idx, end_idx=end_idx, control_points=control_points):
            for i, control_points in enumerate(coils):
                rid = self.update_representation(coil_representations[i],
                                                 {'coordinates': self.coordinates[control_points]})

            self.update_representation(cylinders, {'startCoords': self.coordinates[list(start_idx)],
                                                  'endCoords': self.coordinates[list(end_idx)]})

        self.update_callbacks.append(update)
        self.autozoom(self.coordinates)

Response: Display the protein secondary structure as a white solid tube and the alpha - helices as yellow cylinders.


Instruction:
Implement a function in Python 3 to
display a protein secondary structure as a pymol - like cartoon representation.

Response:
def cartoon(self, cmap=None):
        '''Display a protein secondary structure as a pymol-like cartoon representation.

        :param cmap: is a dictionary that maps the secondary type
                    (H=helix, E=sheet, C=coil) to a hexadecimal color (0xffffff for white)
        '''
        # Parse secondary structure
        top = self.topology

        geom = gg.GeomProteinCartoon(gg.Aes(xyz=self.coordinates,
                                            types=top['atom_names'],
                                            secondary_type=top['secondary_structure']),
                                            cmap=cmap)

        primitives = geom.produce(gg.Aes())
        ids = [self.add_representation(r['rep_type'], r['options']) for r in primitives]

        def update(self=self, geom=geom, ids=ids):
            primitives = geom.produce(gg.Aes(xyz=self.coordinates))
            [self.update_representation(id_, rep_options)
                for id_, rep_options in zip(ids, primitives)]

        self.update_callbacks.append(update)
        self.autozoom(self.coordinates)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_isosurface(self, function, isolevel=0.3, resolution=32, style="wireframe", color=0xffffff):
        '''Add an isosurface to the current scene.

        :param callable function: A function that takes x, y, z coordinates as input and is broadcastable using numpy. Typically simple
                                  functions that involve standard arithmetic operations and functions
                                  such as ``x**2 + y**2 + z**2`` or ``np.exp(x**2 + y**2 + z**2)`` will work. If not sure, you can first
                                  pass the function through ``numpy.vectorize``.\

                                  Example: ``mv.add_isosurface(np.vectorize(f))``
        :param float isolevel: The value for which the function should be constant.
        :param int resolution: The number of grid point to use for the surface. An high value will give better quality but lower performance.
        :param str style: The surface style, choose between ``solid``, ``wireframe`` and ``transparent``.
        :param int color: The color given as an hexadecimal integer. Example: ``0xffffff`` is white.

        '''

        avail_styles = ['wireframe', 'solid', 'transparent']
        if style not in avail_styles:
            raise ValueError('style must be in ' + str(avail_styles))

        # We want to make a container that contains the whole molecule
        # and surface
        area_min = self.coordinates.min(axis=0) - 0.2
        area_max = self.coordinates.max(axis=0) + 0.2

        x = np.linspace(area_min[0], area_max[0], resolution)
        y = np.linspace(area_min[1], area_max[1], resolution)
        z = np.linspace(area_min[2], area_max[2], resolution)

        xv, yv, zv = np.meshgrid(x, y, z)
        spacing = np.array((area_max - area_min)/resolution)

        if isolevel >= 0:
            triangles = marching_cubes(function(xv, yv, zv), isolevel)
        else: # Wrong traingle unwinding roder -- god only knows why
            triangles = marching_cubes(-function(xv, yv, zv), -isolevel)

        if len(triangles) == 0:
            ## NO surface
            return

        faces = []
        verts = []
        for i, t in enumerate(triangles):
           faces.append([i * 3, i * 3 +1, i * 3 + 2])
           verts.extend(t)

        faces = np.array(faces)
        verts = area_min + spacing/2 + np.array(verts)*spacing
        rep_id = self.add_representation('surface', {'verts': verts.astype('float32'),
                                                     'faces': faces.astype('int32'),
                                                     'style': style,
                                                     'color': color})
        self.autozoom(verts)

Response: Add an isosurface to the current scene.


Instruction:
Create a Python 3 function for
adding an isosurface to the current scence using pre - computed data on a grid

Response:
def add_isosurface_grid_data(self, data, origin, extent, resolution,
                                 isolevel=0.3, scale=10,
                                 style="wireframe", color=0xffffff):
        """
        Add an isosurface to current scence using pre-computed data on a grid
        """
        spacing = np.array(extent/resolution)/scale
        if isolevel >= 0:
            triangles = marching_cubes(data, isolevel)
        else:
            triangles = marching_cubes(-data, -isolevel)
        faces = []
        verts = []
        for i, t in enumerate(triangles):
            faces.append([i * 3, i * 3 +1, i * 3 + 2])
            verts.extend(t)
        faces = np.array(faces)
        verts = origin + spacing/2 + np.array(verts)*spacing
        rep_id = self.add_representation('surface', {'verts': verts.astype('float32'),
                                                     'faces': faces.astype('int32'),
                                                     'style': style,
                                                     'color': color})

        self.autozoom(verts)


Instruction:
Make a summary of the following Python 3 code
def _refresh_token(self):

        # use basic auth with API key and secret
        client_auth = requests.auth.HTTPBasicAuth(self.api_key, self.api_secret)

        # make request
        post_data = {"grant_type": "client_credentials"}
        response = requests.post(self.auth, auth=client_auth, data=post_data, proxies=self.proxies)
        self.last_response = response

        # raise exception if status code indicates an error
        if 400 <= response.status_code < 600:
            message = "{} {} Error (Trace-Id: {}): {}".format(response.status_code,
                                                              "Client" if response.status_code < 500 else "Server",
                                                              self._get_trace_id(response),
                                                              "unable to get token")
            raise HTTPError(message, response=response)

        # set token property to the received token
        self.token = response.json()["access_token"]

Response: Refreshes the token for the current user.


Instruction:
How would you code a function in Python 3 to
create headers dictionary for a request.

Response:
def _get_headers(self, is_json=False):
        """
        Create headers dictionary for a request.

        :param boolean is_json: Whether the request body is a json.
        :return: The headers dictionary.
        """

        headers = {"Authorization": "Bearer " + self._get_token()}

        if self.client_type is not None:
            headers["Client-Type"] = self.client_type

        if self.client_version is not None:
            headers["Client-Version"] = self.client_version

        if self.client_metatag is not None:
            headers["Client-Metatag"] = self.client_metatag

        if is_json:
            headers['Content-Type'] = 'application/json'

        return headers


Instruction:
Create a Python 3 function for
determining whether the given response indicates that the token is expired.

Response:
def _is_expired_token_response(cls, response):
        """
        Determine whether the given response indicates that the token is expired.

        :param response: The response object.
        :return: True if the response indicates that the token is expired.
        """

        EXPIRED_MESSAGE = "Expired oauth2 access token"
        INVALID_MESSAGE = "Invalid oauth2 access token"

        if response.status_code == 400:
            try:
                body = response.json()
                if str(body.get('error_description')) in [EXPIRED_MESSAGE, INVALID_MESSAGE]:
                    return True
            except:
                pass
        return False


Instruction:
How would you explain what the following Python 3 function does
def request(self, method, path, headers=None, params=None, data=None, **kwargs):

        retry = self.retry
        attempted = False
        while not attempted or retry:

            # get headers and merge with headers from method parameter if it exists
            base_headers = self._get_headers(is_json=method in ["POST", "PUT"])
            if headers is not None:
                base_headers.update(headers)

            url = "{}/{}".format(self.base, path)

            # make request
            response = requests.request(method=method,
                                        url=url,
                                        headers=base_headers,
                                        verify=self.verify,
                                        params=params,
                                        data=data,
                                        proxies=self.proxies,
                                        **kwargs)
            self.last_response = response
            attempted = True

            # log request
            self.logger.debug("%s %s. Trace-Id: %s. Params: %s", method, url, response.headers.get('Trace-Id'), params)

            # refresh token if expired
            if self._is_expired_token_response(response):
                self._refresh_token()

            # if "too many requests" status code received, wait until next request will be allowed and retry
            elif retry and response.status_code == 429:
                wait_time = ceil(response.json().get('waitTime') / 1000)
                self.logger.debug("Waiting %d seconds until next request allowed." % wait_time)

                # if wait time exceeds max wait time, allow the exception to be thrown
                if wait_time <= self.max_wait_time:
                    time.sleep(wait_time)
                else:
                    retry = False

            # request cycle is complete
            else:
                retry = False

        # raise exception if status code indicates an error
        if 400 <= response.status_code < 600:

            # get response json body, if one exists
            resp_json = None
            try:
                resp_json = response.json()
            except:
                pass

            # get message from json body, if one exists
            if resp_json is not None and 'message' in resp_json:
                reason = resp_json['message']
            else:
                reason = "unknown cause"

            # construct error message
            message = "{} {} Error (Trace-Id: {}): {}".format(response.status_code,
                                                              "Client" if response.status_code < 500 else "Server",
                                                              self._get_trace_id(response),
                                                              reason)
            # raise HTTPError
            raise HTTPError(message, response=response)

        return response

Response: Wrapper around requests. request that handles API calls.


Instruction:
Given the following Python 3 function, write the documentation
def isosurface_from_data(data, isolevel, origin, spacing):
    spacing = np.array(extent/resolution)
    if isolevel >= 0:
        triangles = marching_cubes(data, isolevel)
    else: # Wrong traingle unwinding roder -- god only knows why
        triangles = marching_cubes(-data, -isolevel)
    faces = []
    verts = []
    for i, t in enumerate(triangles):
       faces.append([i * 3, i * 3 +1, i * 3 + 2])
       verts.extend(t)

    faces = np.array(faces)
    verts = origin + spacing/2 + np.array(verts)*spacing
    return verts, faces

Response: This function takes a numpy array of data and returns the vertices and faces of the isosurface that is used to feed into programs


Instruction:
How would you implement a function in Python 3 that
loads a GTF file generated by StringTie which contains transcript - level quantification of abundance. Returns a dictionary mapping transcript IDs of transcripts to FPKM values.

Response:
def load_transcript_fpkm_dict_from_gtf(
        gtf_path,
        transcript_id_column_name="reference_id",
        fpkm_column_name="FPKM",
        feature_column_name="feature"):
    """
    Load a GTF file generated by StringTie which contains transcript-level
    quantification of abundance. Returns a dictionary mapping Ensembl
    IDs of transcripts to FPKM values.
    """
    df = gtfparse.read_gtf(
        gtf_path,
        column_converters={fpkm_column_name: float})
    transcript_ids = _get_gtf_column(transcript_id_column_name, gtf_path, df)
    fpkm_values = _get_gtf_column(fpkm_column_name, gtf_path, df)
    features = _get_gtf_column(feature_column_name, gtf_path, df)
    logging.info("Loaded %d rows from %s" % (len(transcript_ids), gtf_path))
    logging.info("Found %s transcript entries" % sum(
        feature == "transcript" for feature in features))
    result = {
        transcript_id: float(fpkm)
        for (transcript_id, fpkm, feature)
        in zip(transcript_ids, fpkm_values, features)
        if (
            (transcript_id is not None) and
            (len(transcript_id) > 0) and
            (feature == "transcript")
        )
    }
    logging.info("Keeping %d transcript rows with reference IDs" % (
        len(result),))
    return result


Instruction:
How would you code a function in Python 3 to
predict the amino acid sequences in the specified named sequences.

Response:
def predict_from_named_sequences(
            self, name_to_sequence_dict):
        """
        Parameters
        ----------
        name_to_sequence_dict : (str->str) dict
            Dictionary mapping sequence names to amino acid sequences

        Returns pandas.DataFrame with the following columns:
            - source_sequence_name
            - peptide
            - peptide_offset
            - peptide_length
            - allele
            - affinity
            - percentile_rank
            - prediction_method_name
        """
        df = self.mhc_model.predict_subsequences_dataframe(name_to_sequence_dict)
        return df.rename(
            columns={
                "length": "peptide_length",
                "offset": "peptide_offset"})


Instruction:
Given the following Python 3 function, write the documentation
def predict_from_sequences(self, sequences):
        # make each sequence its own unique ID
        sequence_dict = {
            seq: seq
            for seq in sequences
        }
        df = self.predict_from_named_sequences(sequence_dict)
        return df.rename(columns={"source_sequence_name": "source_sequence"})

Response: Predict MHC ligands for sub - sequences of each input sequence.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def predict_from_mutation_effects(
            self,
            effects,
            transcript_expression_dict=None,
            gene_expression_dict=None):
        """Given a Varcode.EffectCollection of predicted protein effects,
        return predicted epitopes around each mutation.

        Parameters
        ----------
        effects : Varcode.EffectCollection

        transcript_expression_dict : dict
            Dictionary mapping transcript IDs to RNA expression estimates. Used
            both for transcript expression filtering and for selecting the
            most abundant transcript for a particular variant. If omitted then
            transcript selection is done using priority of variant effects and
            transcript length.

        gene_expression_dict : dict, optional
            Dictionary mapping gene IDs to RNA expression estimates

        Returns DataFrame with the following columns:
            - variant
            - gene
            - gene_id
            - transcript_id
            - transcript_name
            - effect
            - effect_type
            - peptide
            - peptide_offset
            - peptide_length
            - allele
            - affinity
            - percentile_rank
            - prediction_method_name
            - contains_mutant_residues
            - mutation_start_in_peptide
            - mutation_end_in_peptide

        Optionall will also include the following columns if corresponding
        expression dictionary inputs are provided:
            - gene_expression
            - transcript_expression
        """

        # we only care about effects which impact the coding sequence of a
        # protein
        effects = filter_silent_and_noncoding_effects(effects)

        effects = apply_effect_expression_filters(
            effects,
            transcript_expression_dict=transcript_expression_dict,
            transcript_expression_threshold=self.min_transcript_expression,
            gene_expression_dict=gene_expression_dict,
            gene_expression_threshold=self.min_gene_expression)

        # group by variants, so that we end up with only one mutant
        # sequence per mutation
        variant_effect_groups = effects.groupby_variant()

        if len(variant_effect_groups) == 0:
            logging.warn("No candidates for MHC binding prediction")
            return []

        if transcript_expression_dict:
            # if expression data is available, then for each variant
            # keep the effect annotation for the most abundant transcript
            top_effects = [
                variant_effects.top_expression_effect(
                    transcript_expression_dict)
                for variant_effects in variant_effect_groups.values()
            ]
        else:
            # if no transcript abundance data is available, then
            # for each variant keep the effect with the most significant
            # predicted effect on the protein sequence, along with using
            # transcript/CDS length as a tie-breaker for effects with the same
            # priority.
            top_effects = [
                variant_effects.top_priority_effect()
                for variant_effects in variant_effect_groups.values()
            ]

        # 1) dictionary mapping varcode effect objects to subsequences
        #    around each mutation
        # 2) dictionary mapping varcode effect to start offset of subsequence
        #    within the full mutant protein sequence
        effect_to_subsequence_dict, effect_to_offset_dict = \
            protein_subsequences_around_mutations(
                effects=top_effects,
                padding_around_mutation=self.padding_around_mutation)

        # since we know that each set of variant effects has been
        # reduced to a single 'top priority' effect, we can uniquely
        # identify each variant sequence by its original genomic variant
        variant_string_to_effect_dict = {
            effect.variant.short_description: effect
            for effect in effect_to_subsequence_dict.keys()
        }
        variant_string_to_subsequence_dict = {
            effect.variant.short_description: subseq
            for (effect, subseq) in effect_to_subsequence_dict.items()
        }
        variant_string_to_offset_dict = {
            effect.variant.short_description: subseq_offset
            for (effect, subseq_offset) in effect_to_offset_dict.items()
        }
        df = self.predict_from_named_sequences(variant_string_to_subsequence_dict)
        logging.info("MHC predictor returned %d peptide binding predictions" % (
            len(df)))

        # since we used variant descrptions as the name of each sequence
        # let's rename that column to be more informative
        df = df.rename(columns={"source_sequence_name": "variant"})

        # adjust offset to be relative to start of protein, rather
        # than whatever subsequence we used for prediction
        def compute_peptide_offset_relative_to_protein(row):
            subsequence_offset = variant_string_to_offset_dict[row.variant]
            return row.peptide_offset + subsequence_offset

        df["peptide_offset"] = df.apply(
            compute_peptide_offset_relative_to_protein,
            axis=1)

        if self.ic50_cutoff:
            df = df[df.affinity <= self.ic50_cutoff]
            logging.info("Kept %d predictions after filtering affinity <= %f" % (
                len(df), self.ic50_cutoff))

        if self.percentile_cutoff:
            df = df[df.percentile_rank <= self.percentile_cutoff]
            logging.info("Kept %d predictions after filtering percentile <= %f" % (
                len(df), self.percentile_cutoff))

        extra_columns = OrderedDict([
            ('gene', []),
            ('gene_id', []),
            ('transcript_id', []),
            ('transcript_name', []),
            ('effect', []),
            ('effect_type', []),
            ('contains_mutant_residues', []),
            ('mutation_start_in_peptide', []),
            ('mutation_end_in_peptide', []),
        ])
        if gene_expression_dict is not None:
            extra_columns["gene_expression"] = []
        if transcript_expression_dict is not None:
            extra_columns["transcript_expression"] = []

        for _, row in df.iterrows():
            effect = variant_string_to_effect_dict[row.variant]
            mutation_start_in_protein = effect.aa_mutation_start_offset
            mutation_end_in_protein = effect.aa_mutation_end_offset
            peptide_length = len(row.peptide)
            is_mutant = contains_mutant_residues(
                peptide_start_in_protein=row.peptide_offset,
                peptide_length=peptide_length,
                mutation_start_in_protein=mutation_start_in_protein,
                mutation_end_in_protein=mutation_end_in_protein)
            if is_mutant:
                mutation_start_in_peptide, mutation_end_in_peptide = peptide_mutation_interval(
                    peptide_start_in_protein=row.peptide_offset,
                    peptide_length=peptide_length,
                    mutation_start_in_protein=mutation_start_in_protein,
                    mutation_end_in_protein=mutation_end_in_protein)
            else:
                mutation_start_in_peptide = mutation_end_in_peptide = None

            extra_columns["gene"].append(effect.gene_name)
            gene_id = effect.gene_id
            extra_columns["gene_id"].append(gene_id)
            if gene_expression_dict is not None:
                extra_columns["gene_expression"].append(
                    gene_expression_dict.get(gene_id, 0.0))

            transcript_id = effect.transcript_id
            extra_columns["transcript_id"].append(transcript_id)
            extra_columns["transcript_name"].append(effect.transcript_name)
            if transcript_expression_dict is not None:
                extra_columns["transcript_expression"].append(
                    transcript_expression_dict.get(transcript_id, 0.0))

            extra_columns["effect"].append(effect.short_description)
            extra_columns["effect_type"].append(effect.__class__.__name__)

            extra_columns["contains_mutant_residues"].append(is_mutant)
            extra_columns["mutation_start_in_peptide"].append(mutation_start_in_peptide)
            extra_columns["mutation_end_in_peptide"].append(mutation_end_in_peptide)

        for col, values in extra_columns.items():
            df[col] = values

        # TODO: add extra boolean field
        #   novel = is_mutant | not_in_reference
        # Requires keeping a quick lookup structure for all peptides in
        # the reference proteome
        if self.only_novel_epitopes:
            df = df[df.contains_mutant_residues]

        return df

Response: Given a Varcode. EffectCollection of predicted protein effects return predicted epitopes around each mutation.


Instruction:
Here you have a function in Python 3, explain what it does
def predict_from_variants(
            self,
            variants,
            transcript_expression_dict=None,
            gene_expression_dict=None):
        """
        Predict epitopes from a Variant collection, filtering options, and
        optional gene and transcript expression data.

        Parameters
        ----------
        variants : varcode.VariantCollection

        transcript_expression_dict : dict
            Maps from Ensembl transcript IDs to FPKM expression values.

        gene_expression_dict : dict, optional
            Maps from Ensembl gene IDs to FPKM expression values.

        Returns DataFrame with the following columns:
            - variant
            - gene
            - gene_id
            - transcript_id
            - transcript_name
            - effect
            - effect_type
            - peptide
            - peptide_offset
            - peptide_length
            - allele
            - affinity
            - percentile_rank
            - prediction_method_name
            - contains_mutant_residues
            - mutation_start_in_peptide
            - mutation_end_in_peptide

        Optionall will also include the following columns if corresponding
        expression dictionary inputs are provided:
            - gene_expression
            - transcript_expression
        """
        # pre-filter variants by checking if any of the genes or
        # transcripts they overlap have sufficient expression.
        # I'm tolerating the redundancy of this code since it's much cheaper
        # to filter a variant *before* trying to predict its impact/effect
        # on the protein sequence.
        variants = apply_variant_expression_filters(
            variants,
            transcript_expression_dict=transcript_expression_dict,
            transcript_expression_threshold=self.min_transcript_expression,
            gene_expression_dict=gene_expression_dict,
            gene_expression_threshold=self.min_gene_expression)

        effects = variants.effects(raise_on_error=self.raise_on_error)

        return self.predict_from_mutation_effects(
            effects=effects,
            transcript_expression_dict=transcript_expression_dict,
            gene_expression_dict=gene_expression_dict)

Response: Predict epitopes from a VariantCollection.


Instruction:
Can you write a function in Python 3 where it
scripts entry - point to predict neo - epitopes from genomic variants using Topiary.

Response:
def main(args_list=None):
    """
    Script entry-point to predict neo-epitopes from genomic variants using
    Topiary.
    """
    args = parse_args(args_list)
    print("Topiary commandline arguments:")
    print(args)
    df = predict_epitopes_from_args(args)
    write_outputs(df, args)
    print("Total count: %d" % len(df))


Instruction:
Can you generate the documentation for the following Python 3 function
def render_povray(scene, filename='ipython', width=600, height=600,
                  antialiasing=0.01, extra_opts={}):
    '''Render the scene with povray for publication.

    :param dict scene: The scene to render
    :param string filename: Output filename or 'ipython' to render in the notebook.
    :param int width: Width in pixels.
    :param int height: Height in pixels.
    :param dict extra_opts: Dictionary to merge/override with the passed scene.
    '''
    if not vapory_available:
        raise Exception("To render with povray, you need to have the vapory"
                        " package installed.")

    # Adding extra options
    scene = normalize_scene(scene)
    scene.update(extra_opts)

    # Camera target
    aspect = scene['camera']['aspect']
    up = np.dot(rmatrixquaternion(scene['camera']['quaternion']), [0, 1, 0])
    v_fov = scene['camera']['vfov']  / 180.0 * np.pi
    h_fov = 2.0 * np.arctan(np.tan(v_fov/2.0) * aspect) / np.pi * 180
    # Setup camera position
    camera = vp.Camera( 'location', scene['camera']['location'],
                        'direction', [0, 0, -1],
                        'sky', up,
                        'look_at', scene['camera']['target'],
                        'angle', h_fov )

    global_settings = []
    # Setup global illumination
    if scene.get('radiosity', False):
        # Global Illumination
        radiosity = vp.Radiosity(
                    'brightness', 2.0,
                    'count', 100,
                    'error_bound', 0.15,
                    'gray_threshold', 0.0,
                    'low_error_factor', 0.2,
                    'minimum_reuse', 0.015,
                    'nearest_count', 10,
                    'recursion_limit', 1, #Docs say 1 is enough
                    'adc_bailout', 0.01,
                    'max_sample', 0.5,
                    'media off',
                    'normal off',
                    'always_sample', 1,
                    'pretrace_start', 0.08,
                    'pretrace_end', 0.01)

        light_sources = []
        global_settings.append(radiosity)
    else:
        # Lights
        light_sources = [
                        vp.LightSource( np.array([2,4,-3]) * 1000, 'color', [1,1,1] ),
                        vp.LightSource( np.array([-2,-4,3]) * 1000, 'color', [1,1,1] ),
                        vp.LightSource( np.array([-1,2,3]) * 1000, 'color', [1,1,1] ),
                        vp.LightSource( np.array([1,-2,-3]) * 1000, 'color', [1,1,1] )
        ]

    # Background -- white for now
    background = vp.Background([1, 1, 1])

    # Things to display
    stuff = _generate_objects(scene['representations'])

    scene = vp.Scene( camera, objects = light_sources + stuff + [background],
                      global_settings=global_settings)

    return scene.render(filename, width=width, height=height,
                        antialiasing = antialiasing)

Response: Render the scene with povray for publication.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rmatrixquaternion(q):
    assert np.allclose(math.sqrt(np.dot(q,q)), 1.0)

    x, y, z, w = q

    xx = x*x
    xy = x*y
    xz = x*z
    xw = x*w
    yy = y*y
    yz = y*z
    yw = y*w
    zz = z*z
    zw = z*w

    r00 = 1.0 - 2.0 * (yy + zz)
    r01 =       2.0 * (xy - zw)
    r02 =       2.0 * (xz + yw)

    r10 =       2.0 * (xy + zw)
    r11 = 1.0 - 2.0 * (xx + zz)
    r12 =       2.0 * (yz - xw)

    r20 =       2.0 * (xz - yw)
    r21 =       2.0 * (yz + xw)
    r22 = 1.0 - 2.0 * (xx + yy)

    R = np.array([[r00, r01, r02],
               [r10, r11, r12],
               [r20, r21, r22]], float)

    assert np.allclose(np.linalg.det(R), 1.0)
    return R

Response: Create a rotation matrix from a quaternion rotation.


Instruction:
Write a Python 3 script to
return a dictionary mapping Ensembl transcript IDs to FPKM expression values or None if neither Cufflinks tracking file nor StringTie GTF file were specified.

Response:
def rna_transcript_expression_dict_from_args(args):
    """
    Returns a dictionary mapping Ensembl transcript IDs to FPKM expression
    values or None if neither Cufflinks tracking file nor StringTie GTF file
    were specified.
    """
    if args.rna_transcript_fpkm_tracking_file:
        return load_cufflinks_fpkm_dict(args.rna_transcript_fpkm_tracking_file)
    elif args.rna_transcript_fpkm_gtf_file:
        return load_transcript_fpkm_dict_from_gtf(
            args.rna_transcript_fpkm_gtf_file)
    else:
        return None


Instruction:
Can you generate the documentation for the following Python 3 function
def get_total_pages(self):

        if self.total_elements is None or self.page_size is None:
            return None

        return math.ceil(float(self.total_elements) / float(self.page_size))

Response: Returns the total number of pages on the server.


Instruction:
Make a summary of the following Python 3 code
def has_more_pages(self):

        # if has_next property exists, it represents whether more pages exist
        if self.has_next is not None:
            return self.has_next

        # otherwise, try to compute whether or not more pages exist
        total_pages = self.get_total_pages()
        if self.page_number is None or total_pages is None:
            return None
        else:
            return self.page_number + 1 < total_pages

Response: Returns True if there are more pages available on the server.


Instruction:
Can you implement a function in Python 3 that
creates a |Page| object from a dictionary.

Response:
def from_dict(page, content_type=None):
        """
        Create a |Page| object from a dictionary.  This method is intended for internal use, to construct a
        |Page| object from the body of a response json from a paginated endpoint.

        :param page: The dictionary.
        :param content_type: The class that the contents should be deserialized into.
        :return: The resulting |Page| object.
        """

        result = Page(items=page.get('items'),
                      page_number=page.get('pageNumber'),
                      page_size=page.get('pageSize'),
                      total_elements=page.get('totalElements'),
                      has_next=page.get('hasNext'))

        if content_type is not None:
            if not issubclass(content_type, ModelBase):
                raise ValueError("'content_type' must be a subclass of ModelBase.")

            result.items = [content_type.from_dict(item) for item in result.items]

        return result


Instruction:
Create a Python 3 function for
creating a dictionary representation of the page.

Response:
def to_dict(self, remove_nones=False):
        """
        Creates a dictionary representation of the page.

        :param remove_nones: Whether ``None`` values should be filtered out of the dictionary.  Defaults to ``False``.
        :return: A dictionary representation of the page.
        """

        items = []

        # attempt to replace each item with its dictionary representation if possible
        for item in self.items:
            if hasattr(item, 'to_dict'):
                items.append(item.to_dict(remove_nones=remove_nones))
            else:
                items.append(item)

        return {
            'items': items,
            'pageNumber': self.page_number,
            'pageSize': self.page_size,
            'totalElements': self.total_elements,
            'hasNext': self.has_next
        }


Instruction:
Write a Python 3 script for
returning a generator that returns each successive page from a paginated endpoint.

Response:
def get_page_generator(func, start_page=0, page_size=None):
        """
        Constructs a generator for retrieving pages from a paginated endpoint.  This method is intended for internal
        use.

        :param func: Should take parameters ``page_number`` and ``page_size`` and return the corresponding |Page| object.
        :param start_page: The page to start on.
        :param page_size: The size of each page.
        :return: A generator that generates each successive page.
        """

        # initialize starting values
        page_number = start_page
        more_pages = True

        # continuously request the next page as long as more pages exist
        while more_pages:

            # get next page
            page = func(page_number=page_number, page_size=page_size)

            yield page

            # determine whether more pages exist
            more_pages = page.has_more_pages()
            page_number += 1


Instruction:
Can you implement a function in Python 3 that
returns a dictionary representation of the object.

Response:
def to_dict(self, remove_nones=False):
        """
        Creates a dictionary representation of the object.

        :param remove_nones: Whether ``None`` values should be filtered out of the dictionary.  Defaults to ``False``.
        :return: The dictionary representation.
        """

        if remove_nones:
            return {k: v for k, v in self.to_dict().items() if v is not None}
        else:
            raise NotImplementedError()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_dict(self, remove_nones=False):

        if remove_nones:
            report_dict = super().to_dict(remove_nones=True)
        else:
            report_dict = {
                'title': self.title,
                'reportBody': self.body,
                'timeBegan': self.time_began,
                'externalUrl': self.external_url,
                'distributionType': self._get_distribution_type(),
                'externalTrackingId': self.external_id,
                'enclaveIds': self.enclave_ids,
                'created': self.created,
                'updated': self.updated,
            }

        # id field might not be present
        if self.id is not None:
            report_dict['id'] = self.id
        else:
            report_dict['id'] = None

        return report_dict

Response: Returns a dictionary representation of the object.


Instruction:
Can you create a Python 3 function that
creates a report object from a dictionary.

Response:
def from_dict(cls, report):
        """
        Create a report object from a dictionary.  This method is intended for internal use, to construct a
        :class:`Report` object from the body of a response json.  It expects the keys of the dictionary to match those
        of the json that would be found in a response to an API call such as ``GET /report/{id}``.

        :param report: The dictionary.
        :return: The report object.
        """

        # determine distribution type
        distribution_type = report.get('distributionType')
        if distribution_type is not None:
            is_enclave = distribution_type.upper() != DistributionType.COMMUNITY
        else:
            is_enclave = None

        return Report(id=report.get('id'),
                      title=report.get('title'),
                      body=report.get('reportBody'),
                      time_began=report.get('timeBegan'),
                      external_id=report.get('externalTrackingId'),
                      external_url=report.get('externalUrl'),
                      is_enclave=is_enclave,
                      enclave_ids=report.get('enclaveIds'),
                      created=report.get('created'),
                      updated=report.get('updated'))


Instruction:
Can you implement a function in Python 3 that
makes a json - serializable dictionary from input dictionary by converting non - serializable data types such as numpy arrays.

Response:
def serialize_to_dict(dictionary):
    '''Make a json-serializable dictionary from input dictionary by converting
    non-serializable data types such as numpy arrays.'''
    retval = {}

    for k, v in dictionary.items():
        if isinstance(v, dict):
            retval[k] = serialize_to_dict(v)
        else:
            # This is when custom serialization happens
            if isinstance(v, np.ndarray):
                if v.dtype == 'float64':
                    # We don't support float64 on js side
                    v = v.astype('float32')

                retval[k] = encode_numpy(v)
            else:
                retval[k] = v

    return retval


Instruction:
Can you implement a function in Python 3 that
returns a dictionary of information about pkg and its recursive deps.

Response:
def make_graph(pkg):
    """Returns a dictionary of information about pkg & its recursive deps.

    Given a string, which can be parsed as a requirement specifier, return a
    dictionary where each key is the name of pkg or one of its recursive
    dependencies, and each value is a dictionary returned by research_package.
    (No, it's not really a graph.)
    """
    ignore = ['argparse', 'pip', 'setuptools', 'wsgiref']
    pkg_deps = recursive_dependencies(pkg_resources.Requirement.parse(pkg))

    dependencies = {key: {} for key in pkg_deps if key not in ignore}
    installed_packages = pkg_resources.working_set
    versions = {package.key: package.version for package in installed_packages}
    for package in dependencies:
        try:
            dependencies[package]['version'] = versions[package]
        except KeyError:
            warnings.warn("{} is not installed so we cannot compute "
                          "resources for its dependencies.".format(package),
                          PackageNotInstalledWarning)
            dependencies[package]['version'] = None

    for package in dependencies:
        package_data = research_package(package, dependencies[package]['version'])
        dependencies[package].update(package_data)

    return OrderedDict(
        [(package, dependencies[package]) for package in sorted(dependencies.keys())]
    )


Instruction:
Create a Python 3 function for
retrieving a report by its ID. Internal and external IDs are both allowed.

Response:
def get_report_details(self, report_id, id_type=None):
        """
        Retrieves a report by its ID.  Internal and external IDs are both allowed.

        :param str report_id: The ID of the incident report.
        :param str id_type: Indicates whether ID is internal or external.

        :return: The retrieved |Report| object.

        Example:

        >>> report = ts.get_report_details("1a09f14b-ef8c-443f-b082-9643071c522a")
        >>> print(report)
        {
          "id": "1a09f14b-ef8c-443f-b082-9643071c522a",
          "created": 1515571633505,
          "updated": 1515620420062,
          "reportBody": "Employee reported suspect email.  We had multiple reports of suspicious email overnight ...",
          "title": "Phishing Incident",
          "enclaveIds": [
            "ac6a0d17-7350-4410-bc57-9699521db992"
          ],
          "distributionType": "ENCLAVE",
          "timeBegan": 1479941278000
        }

        """

        params = {'idType': id_type}
        resp = self._client.get("reports/%s" % report_id, params=params)
        return Report.from_dict(resp.json())


Instruction:
Explain what the following Python 3 code does
def get_reports_page(self, is_enclave=None, enclave_ids=None, tag=None, excluded_tags=None,
                         from_time=None, to_time=None):
        """
        Retrieves a page of reports, filtering by time window, distribution type, enclave association, and tag.
        The results are sorted by updated time.
        This method does not take ``page_number`` and ``page_size`` parameters.  Instead, each successive page must be
        found by adjusting the ``from_time`` and ``to_time`` parameters.

        Note:  This endpoint will only return reports from a time window of maximum size of 2 weeks. If you give a
        time window larger than 2 weeks, it will pull reports starting at 2 weeks before the "to" date, through the
        "to" date.

        :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible
            reports are returned).
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by
            default reports from all of user's enclaves are returned)
        :param list(str) tag: Name (or list of names) of tag(s) to filter reports by.  Only reports containing
            ALL of these tags will be returned.
        :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results.
        :param int from_time: start of time window in milliseconds since epoch (optional)
        :param int to_time: end of time window in milliseconds since epoch (optional)

        :return: A |Page| of |Report| objects.

        """

        distribution_type = None

        # explicitly compare to True and False to distinguish from None (which is treated as False in a conditional)
        if is_enclave:
            distribution_type = DistributionType.ENCLAVE
        elif not is_enclave:
            distribution_type = DistributionType.COMMUNITY

        if enclave_ids is None:
            enclave_ids = self.enclave_ids

        params = {
            'from': from_time,
            'to': to_time,
            'distributionType': distribution_type,
            'enclaveIds': enclave_ids,
            'tags': tag,
            'excludedTags': excluded_tags
        }
        resp = self._client.get("reports", params=params)
        result = Page.from_dict(resp.json(), content_type=Report)

        # create a Page object from the dict
        return result

Response: This endpoint returns a page of reports from the server.


Instruction:
Can you tell what is the following Python 3 function doing
def submit_report(self, report):

        # make distribution type default to "enclave"
        if report.is_enclave is None:
            report.is_enclave = True

        if report.enclave_ids is None:
            # use configured enclave_ids by default if distribution type is ENCLAVE
            if report.is_enclave:
                report.enclave_ids = self.enclave_ids
            # if distribution type is COMMUNITY, API still expects non-null list of enclaves
            else:
                report.enclave_ids = []

        if report.is_enclave and len(report.enclave_ids) == 0:
            raise Exception("Cannot submit a report of distribution type 'ENCLAVE' with an empty set of enclaves.")

        # default time began is current time
        if report.time_began is None:
            report.set_time_began(datetime.now())

        data = json.dumps(report.to_dict())
        resp = self._client.post("reports", data=data, timeout=60)

        # get report id from response body
        report_id = resp.content

        if isinstance(report_id, bytes):
            report_id = report_id.decode('utf-8')

        report.id = report_id

        return report

Response: Submits a report to the user.


Instruction:
How would you explain what the following Python 3 function does
def update_report(self, report):

        # default to interal ID type if ID field is present
        if report.id is not None:
            id_type = IdType.INTERNAL
            report_id = report.id
        # if no ID field is present, but external ID field is, default to external ID type
        elif report.external_id is not None:
            id_type = IdType.EXTERNAL
            report_id = report.external_id
        # if no ID fields exist, raise exception
        else:
            raise Exception("Cannot update report without either an ID or an external ID.")

        # not allowed to update value of 'reportId', so remove it
        report_dict = {k: v for k, v in report.to_dict().items() if k != 'reportId'}

        params = {'idType': id_type}

        data = json.dumps(report.to_dict())
        self._client.put("reports/%s" % report_id, data=data, params=params)

        return report

Response: Updates the report identified by the report. id field.


Instruction:
How would you implement a function in Python 3 that
deletes the report with the given ID.

Response:
def delete_report(self, report_id, id_type=None):
        """
        Deletes the report with the given ID.

        :param report_id: the ID of the report to delete
        :param id_type: indicates whether the ID is internal or an external ID provided by the user
        :return: the response object

        Example:

        >>> response = ts.delete_report("4d1fcaee-5009-4620-b239-2b22c3992b80")
        """

        params = {'idType': id_type}
        self._client.delete("reports/%s" % report_id, params=params)


Instruction:
How would you implement a function in Python 3 that
gets a list of the IDs of all TruSTAR reports that contain the searched indicators.

Response:
def get_correlated_report_ids(self, indicators):
        """
        DEPRECATED!
        Retrieves a list of the IDs of all TruSTAR reports that contain the searched indicators.

        :param indicators: A list of indicator values to retrieve correlated reports for.
        :return: The list of IDs of reports that correlated.

        Example:

        >>> report_ids = ts.get_correlated_report_ids(["wannacry", "www.evil.com"])
        >>> print(report_ids)
        ["e3bc6921-e2c8-42eb-829e-eea8da2d3f36", "4d04804f-ff82-4a0b-8586-c42aef2f6f73"]
        """

        params = {'indicators': indicators}
        resp = self._client.get("reports/correlate", params=params)
        return resp.json()


Instruction:
Here you have a function in Python 3, explain what it does
def get_correlated_reports_page(self, indicators, enclave_ids=None, is_enclave=True,
                                    page_size=None, page_number=None):
        """
        Retrieves a page of all TruSTAR reports that contain the searched indicators.

        :param indicators: A list of indicator values to retrieve correlated reports for.
        :param enclave_ids: The enclaves to search in.
        :param is_enclave: Whether to search enclave reports or community reports.
        :param int page_number: the page number to get.
        :param int page_size: the size of the page to be returned.
        :return: The list of IDs of reports that correlated.

        Example:

        >>> reports = ts.get_correlated_reports_page(["wannacry", "www.evil.com"]).items
        >>> print([report.id for report in reports])
        ["e3bc6921-e2c8-42eb-829e-eea8da2d3f36", "4d04804f-ff82-4a0b-8586-c42aef2f6f73"]
        """

        if is_enclave:
            distribution_type = DistributionType.ENCLAVE
        else:
            distribution_type = DistributionType.COMMUNITY

        params = {
            'indicators': indicators,
            'enclaveIds': enclave_ids,
            'distributionType': distribution_type,
            'pageNumber': page_number,
            'pageSize': page_size
        }
        resp = self._client.get("reports/correlated", params=params)

        return Page.from_dict(resp.json(), content_type=Report)

Response: Retrieves a page of all TruSTAR reports that contain the searched indicators.


Instruction:
Write a Python 3 function that can
search for reports containing a search term.

Response:
def search_reports_page(self, search_term=None,
                            enclave_ids=None,
                            from_time=None,
                            to_time=None,
                            tags=None,
                            excluded_tags=None,
                            page_size=None,
                            page_number=None):
        """
        Search for reports containing a search term.

        :param str search_term: The term to search for.  If empty, no search term will be applied.  Otherwise, must
            be at least 3 characters.
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by
            default reports from all of user's enclaves are returned)
        :param int from_time: start of time window in milliseconds since epoch (optional)
        :param int to_time: end of time window in milliseconds since epoch (optional)
        :param list(str) tags: Name (or list of names) of tag(s) to filter reports by.  Only reports containing
            ALL of these tags will be returned. (optional)
        :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results.
        :param int page_number: the page number to get. (optional)
        :param int page_size: the size of the page to be returned.
        :return: a |Page| of |Report| objects.  *NOTE*:  The bodies of these reports will be ``None``.
        """

        body = {
            'searchTerm': search_term
        }

        params = {
            'enclaveIds': enclave_ids,
            'from': from_time,
            'to': to_time,
            'tags': tags,
            'excludedTags': excluded_tags,
            'pageSize': page_size,
            'pageNumber': page_number
        }

        resp = self._client.post("reports/search", params=params, data=json.dumps(body))
        page = Page.from_dict(resp.json(), content_type=Report)

        return page


Instruction:
How would you code a function in Python 3 to
return a generator that returns each successive page.

Response:
def _get_reports_page_generator(self, is_enclave=None, enclave_ids=None, tag=None, excluded_tags=None,
                                    from_time=None, to_time=None):
        """
        Creates a generator from the |get_reports_page| method that returns each successive page.

        :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible
            reports are returned).
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific
            enclaves (optional - by default reports from all enclaves are returned)
        :param str tag: name of tag to filter reports by.  if a tag with this name exists in more than one enclave
            indicated in ``enclave_ids``, the request will fail.  handle this by making separate requests for each
            enclave ID if necessary.
        :param int from_time: start of time window in milliseconds since epoch
        :param int to_time: end of time window in milliseconds since epoch (optional, defaults to current time)
        :return: The generator.
        """

        get_page = functools.partial(self.get_reports_page, is_enclave, enclave_ids, tag, excluded_tags)
        return get_time_based_page_generator(
            get_page=get_page,
            get_next_to_time=lambda x: x.items[-1].updated if len(x.items) > 0 else None,
            from_time=from_time,
            to_time=to_time
        )


Instruction:
Write a Python 3 function that can
use the |get_reports_page| method to create a generator that returns each successive report as a trustar report object. :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible reports are returned). :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by default reports from all enclaves are returned) :param list(str) tag: a list of tags; only reports containing ALL of these tags will be returned. If a tag with this name exists in more than one enclave in the list passed as the ``enclave_ids`` argument, the request will fail. Handle this by making separate requests for each enclave ID if necessary. :param list(str) excluded_tags: a list of tags; reports containing ANY of these tags will not be returned. :param int from_time: start of time window in milliseconds since epoch (optional) :param int to_time: end of time window in milliseconds since epoch (optional) :return: A generator of Report objects. Note: If a report contains all of the tags in the list passed as argument to the 'tag' parameter and also contains any (1 or more) of the tags in the list passed as argument to the 'excluded_tags' parameter, that report will not be returned by this function. Example: >>> page = ts.get_reports(is_enclave=True, tag="malicious", from_time=1425695711000, to_time=1514185311000) >>> for report in reports: print(report.id) '661583cb-a6a7-4cbd-8a90-01578fa4da89' 'da131660-2708-4c8a-926e-f91fb5dbbc62' '2e3400d6-fa37-4a8c-bc2f-155aaa02ae5a' '38064828-d3db-4fff-8ab8-e0e3b304ff44' 'dbf26104-cee5-4ca4-bdbf-a01d0178c007'

Response:
def get_reports(self, is_enclave=None, enclave_ids=None, tag=None, excluded_tags=None, from_time=None, to_time=None):
        """
        Uses the |get_reports_page| method to create a generator that returns each successive report as a trustar
        report object.

        :param boolean is_enclave: restrict reports to specific distribution type (optional - by default all accessible
            reports are returned).
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific
            enclaves (optional - by default reports from all enclaves are returned)
        :param list(str) tag: a list of tags; only reports containing ALL of these tags will be returned.
            If a tag with this name exists in more than one enclave in the list passed as the ``enclave_ids``
            argument, the request will fail.  Handle this by making separate requests for each
            enclave ID if necessary.
        :param list(str) excluded_tags: a list of tags; reports containing ANY of these tags will not be returned.
        :param int from_time: start of time window in milliseconds since epoch (optional)
        :param int to_time: end of time window in milliseconds since epoch (optional)
        :return: A generator of Report objects.

        Note:  If a report contains all of the tags in the list passed as argument to the 'tag' parameter and also
        contains any (1 or more) of the tags in the list passed as argument to the 'excluded_tags' parameter, that
        report will not be returned by this function.

        Example:

        >>> page = ts.get_reports(is_enclave=True, tag="malicious", from_time=1425695711000, to_time=1514185311000)
        >>> for report in reports: print(report.id)
        '661583cb-a6a7-4cbd-8a90-01578fa4da89'
        'da131660-2708-4c8a-926e-f91fb5dbbc62'
        '2e3400d6-fa37-4a8c-bc2f-155aaa02ae5a'
        '38064828-d3db-4fff-8ab8-e0e3b304ff44'
        'dbf26104-cee5-4ca4-bdbf-a01d0178c007'

        """

        return Page.get_generator(page_generator=self._get_reports_page_generator(is_enclave, enclave_ids, tag,
                                                                                excluded_tags, from_time, to_time))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_correlated_reports_page_generator(self, indicators, enclave_ids=None, is_enclave=True,
                                               start_page=0, page_size=None):
        """
        Creates a generator from the |get_correlated_reports_page| method that returns each
        successive page.

        :param indicators: A list of indicator values to retrieve correlated reports for.
        :param enclave_ids:
        :param is_enclave:
        :return: The generator.
        """

        get_page = functools.partial(self.get_correlated_reports_page, indicators, enclave_ids, is_enclave)
        return Page.get_page_generator(get_page, start_page, page_size)

Response:
Creates a generator from the |get_correlated_reports_page| method that returns each
        successive page.


Instruction:
Explain what the following Python 3 code does
def get_correlated_reports(self, indicators, enclave_ids=None, is_enclave=True):

        return Page.get_generator(page_generator=self._get_correlated_reports_page_generator(indicators,
                                                                                enclave_ids,
                                                                                is_enclave))

Response: Uses the |get_correlated_reports_page| method to create a generator that returns each successive report.


Instruction:
How would you implement a function in Python 3 that
creates a generator from the |search_reports_page| method that returns each successive page.

Response:
def _search_reports_page_generator(self, search_term=None,
                                       enclave_ids=None,
                                       from_time=None,
                                       to_time=None,
                                       tags=None,
                                       excluded_tags=None,
                                       start_page=0,
                                       page_size=None):
        """
        Creates a generator from the |search_reports_page| method that returns each successive page.

        :param str search_term: The term to search for.  If empty, no search term will be applied.  Otherwise, must
            be at least 3 characters.
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by
            default reports from all of user's enclaves are returned)
        :param int from_time: start of time window in milliseconds since epoch (optional)
        :param int to_time: end of time window in milliseconds since epoch (optional)
        :param list(str) tags: Name (or list of names) of tag(s) to filter reports by.  Only reports containing
            ALL of these tags will be returned. (optional)
        :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results.
        :param int start_page: The page to start on.
        :param page_size: The size of each page.
        :return: The generator.
        """

        get_page = functools.partial(self.search_reports_page, search_term, enclave_ids, from_time, to_time, tags,
                                     excluded_tags)
        return Page.get_page_generator(get_page, start_page, page_size)


Instruction:
Can you write a function in Python 3 where it
searches for reports in the user s enclave.

Response:
def search_reports(self, search_term=None,
                       enclave_ids=None,
                       from_time=None,
                       to_time=None,
                       tags=None,
                       excluded_tags=None):
        """
        Uses the |search_reports_page| method to create a generator that returns each successive report.

        :param str search_term: The term to search for.  If empty, no search term will be applied.  Otherwise, must
            be at least 3 characters.
        :param list(str) enclave_ids: list of enclave ids used to restrict reports to specific enclaves (optional - by
            default reports from all of user's enclaves are returned)
        :param int from_time: start of time window in milliseconds since epoch (optional)
        :param int to_time: end of time window in milliseconds since epoch (optional)
        :param list(str) tags: Name (or list of names) of tag(s) to filter reports by.  Only reports containing
            ALL of these tags will be returned. (optional)
        :param list(str) excluded_tags: Reports containing ANY of these tags will be excluded from the results.
        :return: The generator of Report objects.  Note that the body attributes of these reports will be ``None``.
        """

        return Page.get_generator(page_generator=self._search_reports_page_generator(search_term, enclave_ids,
                                                                                from_time, to_time, tags,
                                                                                excluded_tags))


Instruction:
Explain what the following Python 3 code does
def parse_boolean(value):

    if value is None:
        return None

    if isinstance(value, bool):
        return value

    if isinstance(value, string_types):
        value = value.lower()
        if value == 'false':
            return False
        if value == 'true':
            return True

    raise ValueError("Could not convert value to boolean: {}".format(value))

Response: Coerce a value to boolean.


Instruction:
Can you tell what is the following Python 3 function doing
def config_from_file(config_file_path, config_role):

        # read config file depending on filetype, parse into dictionary
        ext = os.path.splitext(config_file_path)[-1]
        if ext in ['.conf', '.ini']:
            config_parser = configparser.RawConfigParser()
            config_parser.read(config_file_path)
            roles = dict(config_parser)
        elif ext in ['.json', '.yml', '.yaml']:
            with open(config_file_path, 'r') as f:
                roles = yaml.safe_load(f)
        else:
            raise IOError("Unrecognized filetype for config file '%s'" % config_file_path)

        # ensure that config file has indicated role
        if config_role in roles:
            config = dict(roles[config_role])
        else:
            raise KeyError("Could not find role %s" % config_role)

        # parse enclave ids
        if 'enclave_ids' in config:
            # if id has all numeric characters, will be parsed as an int, so convert to string
            if isinstance(config['enclave_ids'], int):
                config['enclave_ids'] = str(config['enclave_ids'])
            # split comma separated list if necessary
            if isinstance(config['enclave_ids'], string_types):
                config['enclave_ids'] = config['enclave_ids'].split(',')
            elif not isinstance(config['enclave_ids'], list):
                raise Exception("'enclave_ids' must be a list or a comma-separated list")
            # strip out whitespace
            config['enclave_ids'] = [str(x).strip() for x in config['enclave_ids'] if x is not None]
        else:
            # default to empty list
            config['enclave_ids'] = []

        return config

Response: Create a configuration dictionary from a config file.


Instruction:
Make a summary of the following Python 3 code
def get_version(self):

        result = self._client.get("version").content

        if isinstance(result, bytes):
            result = result.decode('utf-8')

        return result.strip('\n')

Response: Get the version number of the API.


Instruction:
Can you create a Python 3 function that
gets the list of user enclaves that the user has access to.

Response:
def get_user_enclaves(self):
        """
        Gets the list of enclaves that the user has access to.

        :return: A list of |EnclavePermissions| objects, each representing an enclave and whether the requesting user
            has read, create, and update access to it.
        """

        resp = self._client.get("enclaves")
        return [EnclavePermissions.from_dict(enclave) for enclave in resp.json()]


Instruction:
Can you tell what is the following Python 3 function doing
def get_request_quotas(self):

        resp = self._client.get("request-quotas")
        return [RequestQuota.from_dict(quota) for quota in resp.json()]

Response: Gets the request quotas for the user s company.


Instruction:
Can you create a Python 3 function that
initializes logging configuration to defaults.

Response:
def configure_logging():
    """
    Initialize logging configuration to defaults.  If the environment variable DISABLE_TRUSTAR_LOGGING is set to true,
    this will be ignored.
    """

    if not parse_boolean(os.environ.get('DISABLE_TRUSTAR_LOGGING')):

        # configure
        dictConfig(DEFAULT_LOGGING_CONFIG)

        # construct error logger
        error_logger = logging.getLogger("error")

        # log all uncaught exceptions
        def log_exception(exc_type, exc_value, exc_traceback):
            error_logger.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))

        # register logging function as exception hook
        sys.excepthook = log_exception


Instruction:
Here you have a function in Python 3, explain what it does
def from_dict(cls, enclave):

        return Enclave(id=enclave.get('id'),
                       name=enclave.get('name'),
                       type=EnclaveType.from_string(enclave.get('type')))

Response: Create a enclave object from a dictionary.


Instruction:
Can you generate the documentation for the following Python 3 function
def to_dict(self, remove_nones=False):

        if remove_nones:
            return super().to_dict(remove_nones=True)

        return {
            'id': self.id,
            'name': self.name,
            'type': self.type
        }

Response: Returns a dictionary representation of the object.


Instruction:
Can you generate the documentation for the following Python 3 function
def from_dict(cls, d):

        enclave = super(cls, EnclavePermissions).from_dict(d)
        enclave_permissions = cls.from_enclave(enclave)

        enclave_permissions.read = d.get('read')
        enclave_permissions.create = d.get('create')
        enclave_permissions.update = d.get('update')

        return enclave_permissions

Response: Create a enclave object from a dictionary.


Instruction:
Can you tell what is the following Python 3 function doing
def to_dict(self, remove_nones=False):

        d = super().to_dict(remove_nones=remove_nones)

        d.update({
            'read': self.read,
            'create': self.create,
            'update': self.update
        })

        return d

Response: Returns a dictionary representation of the enclave.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_enclave(cls, enclave):

        return EnclavePermissions(id=enclave.id,
                                  name=enclave.name,
                                  type=enclave.type)

Response: Create an |EnclavePermissions| object from an |Enclave| object.


Instruction:
Can you write a function in Python 3 where it
retrieves all enclave tags present in a specific report.

Response:
def get_enclave_tags(self, report_id, id_type=None):
        """
        Retrieves all enclave tags present in a specific report.

        :param report_id: the ID of the report
        :param id_type: indicates whether the ID internal or an external ID provided by the user
        :return: A list of  |Tag| objects.
        """

        params = {'idType': id_type}
        resp = self._client.get("reports/%s/tags" % report_id, params=params)
        return [Tag.from_dict(indicator) for indicator in resp.json()]


Instruction:
Write a Python 3 script for
adding a tag to a specific report for a specific enclave.

Response:
def add_enclave_tag(self, report_id, name, enclave_id, id_type=None):
        """
        Adds a tag to a specific report, for a specific enclave.

        :param report_id: The ID of the report
        :param name: The name of the tag to be added
        :param enclave_id: ID of the enclave where the tag will be added
        :param id_type: indicates whether the ID internal or an external ID provided by the user
        :return: The ID of the tag that was created.
        """

        params = {
            'idType': id_type,
            'name': name,
            'enclaveId': enclave_id
        }
        resp = self._client.post("reports/%s/tags" % report_id, params=params)
        return str(resp.content)


Instruction:
Can you tell what is the following Python 3 function doing
def delete_enclave_tag(self, report_id, tag_id, id_type=None):

        params = {
            'idType': id_type
        }
        self._client.delete("reports/%s/tags/%s" % (report_id, tag_id), params=params)

Response: Deletes a tag from a specific report in a specific enclave.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_all_enclave_tags(self, enclave_ids=None):

        params = {'enclaveIds': enclave_ids}
        resp = self._client.get("reports/tags", params=params)
        return [Tag.from_dict(indicator) for indicator in resp.json()]

Response: Retrieves all tags present in the given enclaves.


Instruction:
Can you generate the documentation for the following Python 3 function
def add_indicator_tag(self, indicator_value, name, enclave_id):

        data = {
            'value': indicator_value,
            'tag': {
                'name': name,
                'enclaveId': enclave_id
            }
        }

        resp = self._client.post("indicators/tags", data=json.dumps(data))
        return Tag.from_dict(resp.json())

Response: Adds a tag to a specific indicator for a specific enclave.


Instruction:
Can you generate the documentation for the following Python 3 function
def delete_indicator_tag(self, indicator_value, tag_id):

        params = {
            'value': indicator_value
        }

        self._client.delete("indicators/tags/%s" % tag_id, params=params)

Response: Deletes a tag from an indicator in a specific enclave.


Instruction:
Can you write a function in Python 3 where it
creates a Tag object from a dictionary.

Response:
def from_dict(cls, tag):
        """
        Create a tag object from a dictionary.  This method is intended for internal use, to construct a
        :class:`Tag` object from the body of a response json.  It expects the keys of the dictionary to match those
        of the json that would be found in a response to an API call such as ``GET /enclave-tags``.

        :param tag: The dictionary.
        :return: The :class:`Tag` object.
        """

        return Tag(name=tag.get('name'),
                   id=tag.get('guid'),
                   enclave_id=tag.get('enclaveId'))


Instruction:
Given the following Python 3 function, write the documentation
def to_dict(self, remove_nones=False):

        if remove_nones:
            d = super().to_dict(remove_nones=True)
        else:
            d = {
                'name': self.name,
                'id': self.id,
                'enclaveId': self.enclave_id
            }

        return d

Response: Creates a dictionary representation of the tag.


Instruction:
Write a Python 3 script to
infer the most likely delimiter from a file.

Response:
def infer_delimiter(filename, comment_char="#", n_lines=3):
    """
    Given a file which contains data separated by one of the following:
        - commas
        - tabs
        - spaces
    Return the most likely separator by sniffing the first few lines
    of the file's contents.
    """
    lines = []
    with open(filename, "r") as f:
        for line in f:
            if line.startswith(comment_char):
                continue
            if len(lines) < n_lines:
                lines.append(line)
            else:
                break
    if len(lines) < n_lines:
        raise ValueError(
            "Not enough lines in %s to infer delimiter" % filename)
    candidate_delimiters = ["\t", ",", "\s+"]
    for candidate_delimiter in candidate_delimiters:
        counts = [len(re.split(candidate_delimiter, line)) for line in lines]
        first_line_count = counts[0]
        if all(c == first_line_count for c in counts) and first_line_count > 1:
            return candidate_delimiter
    raise ValueError("Could not determine delimiter for %s" % filename)


Instruction:
Here you have a function in Python 3, explain what it does
def check_required_columns(df, filename, required_columns):
    available_columns = set(df.columns)
    for column_name in required_columns:
        if column_name not in available_columns:
            raise ValueError("FPKM tracking file %s missing column '%s'" % (
                filename,
                column_name))

Response: Checks that all required columns are present in the given dataframe.


Instruction:
Can you tell what is the following Python 3 function doing
def topology_mdtraj(traj):
    '''Generate topology spec for the MolecularViewer from mdtraj.

    :param mdtraj.Trajectory traj: the trajectory
    :return: A chemview-compatible dictionary corresponding to the topology defined in mdtraj.

    '''
    import mdtraj as md

    top = {}
    top['atom_types'] = [a.element.symbol for a in traj.topology.atoms]
    top['atom_names'] = [a.name for a in traj.topology.atoms]
    top['bonds'] = [(a.index, b.index) for a, b in traj.topology.bonds]
    top['secondary_structure'] = md.compute_dssp(traj[0])[0]
    top['residue_types'] = [r.name for r in traj.topology.residues ]
    top['residue_indices'] = [ [a.index for a in r.atoms] for r in traj.topology.residues ]

    return top

Response: Generate the MolecularViewer topology from the mdtraj. Trajectory object.


Instruction:
Write a Python 3 function that can
encode a numpy array as a base64 encoded string to be JSON serialized.

Response:
def encode_numpy(array):
    '''Encode a numpy array as a base64 encoded string, to be JSON serialized.

    :return: a dictionary containing the fields:
                - *data*: the base64 string
                - *type*: the array type
                - *shape*: the array shape

    '''
    return {'data' : base64.b64encode(array.data).decode('utf8'),
            'type' : array.dtype.name,
            'shape': array.shape}


Instruction:
Can you write a function in Python 3 where it
loads a Cufflinks tracking file and returns a DataFrame containing the FPKM data.

Response:
def load_cufflinks_dataframe(
        filename,
        id_column=ID_COLUMN,
        fpkm_column=FPKM_COLUMN,
        status_column=STATUS_COLUMN,
        locus_column=LOCUS_COLUMN,
        gene_names_column=GENE_NAMES_COLUMN,
        drop_failed=True,
        drop_lowdata=False,
        drop_hidata=True,
        replace_hidata_fpkm_value=None,
        drop_nonchromosomal_loci=False,
        drop_novel=False,
        sep=None):
    """
    Loads a Cufflinks tracking file, which contains expression levels
    (in FPKM: Fragments Per Kilobase of transcript per Million fragments)
    for transcript isoforms or whole genes. These transcripts/genes may be
    previously known (in which case they have an Ensembl ID) or a novel
    assembly from the RNA-Seq data (in which case their IDs look like "CUFF.1")

    Parameters
    ----------

    filename : str
        Filename of tracking file e.g. "genes.tracking_fpkm"

    id_column : str, optional

    fpkm_column : str, optional

    status_column : str, optional
        Name of column which indicates the FPKM estimate status. The column
        name is typically "FPKM_status". Possible contained within this column
        will be OK, FAIL, LOWDATA, HIDATA.

    locus_column : str, optional

    gene_names_column : str, optional

    drop_failed : bool, optional
        Drop rows whose FPKM status is "FAIL" (default=True)

    drop_lowdata : bool, optional
        Drop rows whose FPKM status is "LOWDATA", meaning that Cufflinks thought
        there were too few reads to accurately estimate the FPKM (default=False)

    drop_hidata : bool, optional
        Drop rows whose FPKM status is "HIDATA", meaning that too many
        fragments aligned to a feature for Cufflinks to process. Dropping
        the most expressed genes seems like a stupid idea so: default=False

    replace_hidata_fpkm_value : float, optional
        If drop_hidata=False, the HIDATA entries will still have an FPKM=0.0,
        this argument lets you replace the FPKM with some known constant.

    drop_nonchromosomal_loci : bool, optional
        Drop rows whose location isn't on a canonical chromosome
        i.e. doesn't start with "chr" (default=False)

    drop_novel : bool, optional
        Drop genes or isoforms that aren't found in Ensembl (default = False)

    sep : str, optional
        Separator between data fields in the FPKM tracking file
        (default is to infer whether the file uses comma or whitespace)

    Returns DataFrame with columns:
        id : str
        novel : bool
        fpkm : float
        chr : str
        start : int
        end : int
        gene_names : str list
    """
    if sep is None:
        sep = infer_delimiter(filename)

    df = pd.read_csv(filename, sep=sep, engine="c")

    required_columns = {
        status_column,
        locus_column,
        id_column,
        gene_names_column,
        fpkm_column
    }
    check_required_columns(df, filename, required_columns)

    for flag, status_value in [
            (drop_failed, "FAIL"),
            (drop_lowdata, "LOWDATA"),
            (drop_hidata, "HIDATA")]:
        mask = df[status_column] == status_value
        mask_count = mask.sum()
        total_count = len(df)
        if flag and mask_count > 0:
            verb_str = "Dropping"
            df = df[~mask]
        else:
            verb_str = "Keeping"
        logging.info(
            "%s %d/%d entries from %s with status=%s",
            verb_str,
            mask_count,
            total_count,
            filename,
            status_value)

    if drop_nonchromosomal_loci:
        loci = df[locus_column]
        chromosomal_loci = loci.str.startswith("chr")
        n_dropped = (~chromosomal_loci).sum()
        if n_dropped > 0:
            logging.info("Dropping %d/%d non-chromosomal loci from %s" % (
                n_dropped, len(df), filename))
            df = df[chromosomal_loci]

    if replace_hidata_fpkm_value:
        hidata_mask = df[status_column] == "HIDATA"
        n_hidata = hidata_mask.sum()
        logging.info(
            "Setting FPKM=%s for %d/%d entries with status=HIDATA",
            replace_hidata_fpkm_value,
            n_hidata,
            len(df))
        df[fpkm_column][hidata_mask] = replace_hidata_fpkm_value

    if len(df) == 0:
        raise ValueError("Empty FPKM tracking file: %s" % filename)

    ids = df[id_column]
    known = ids.str.startswith("ENS")

    if known.sum() == 0:
        raise ValueError("No Ensembl IDs found in %s" % filename)

    if drop_novel:
        n_dropped = (~known).sum()
        if n_dropped > 0:
            logging.info(
                "Dropping %d/%d novel entries from %s",
                n_dropped,
                len(df),
                filename)
            df = df[known]
            known = np.ones(len(df), dtype='bool')

    loci = df[locus_column]
    chromosomes, starts, ends = parse_locus_column(df[locus_column])

    # gene names are given either as "-" or a comma separated list
    # e.g. "BRAF1,PFAM2"
    gene_names_strings = df[gene_names_column].copy()
    gene_names_strings[gene_names_strings == "-"] = ""
    # split each entry into a list of zero or more strings
    gene_names_lists = gene_names_strings.str.split(",")

    return pd.DataFrame({
        "id": df[id_column],
        "novel": ~known,
        "fpkm": df[fpkm_column],
        "chr": chromosomes,
        "start": starts,
        "end": ends,
        "gene_names": gene_names_lists
    })


Instruction:
Can you write a function in Python 3 where it
loads a Cufflinks table into a dictionary.

Response:
def load_cufflinks_dict(*args, **kwargs):
    """
    Returns dictionary mapping feature identifier (either transcript or gene ID)
    to a DataFrame row with fields:
        id : str
        novel : bool
        fpkm : float
        chr : str
        start : int
        end : int
        gene_names : str list
    """
    return {
        row.id: row
        for (_, row)
        in load_cufflinks_dataframe(*args, **kwargs).iterrows()
    }


Instruction:
Here you have a function in Python 3, explain what it does
def load_cufflinks_fpkm_dict(*args, **kwargs):
    return {
        row.id: row.fpkm
        for (_, row)
        in load_cufflinks_dataframe(*args, **kwargs).iterrows()
    }

Response: Load a Cufflinks FPKM expression value dictionary.


Instruction:
Can you tell what is the following Python 3 function doing
def enable_notebook(verbose=0):
    libs = ['objexporter.js',
            'ArcballControls.js', 'filesaver.js',
            'base64-arraybuffer.js', 'context.js',
            'chemview.js', 'three.min.js', 'jquery-ui.min.js',
            'context.standalone.css', 'chemview_widget.js',
            'trajectory_controls_widget.js', "layout_widget.js",
            "components/jquery-fullscreen/jquery.fullscreen.js",
            'scales.js']
    fns = [resource_filename('chemview', os.path.join('static', f)) for f in libs]

    [install_nbextension(fn, verbose=verbose, overwrite=True, user=True) for fn in fns]

Response: Enable IPython notebook widgets to be displayed.


Instruction:
Create a Python 3 function to
extract text from a pdf file

Response:
def extract_pdf(file_name):
    """
    Extract text from a pdf file
    :param file_name path to pdf to read
    :return text from pdf
    """

    rsrcmgr = pdfminer.pdfinterp.PDFResourceManager()
    sio = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, sio, codec='utf-8', laparams=laparams)
    interpreter = pdfminer.pdfinterp.PDFPageInterpreter(rsrcmgr, device)

    # Extract text from pdf file
    with open(file_name, 'rb') as fp:
        for page in PDFPage.get_pages(fp, maxpages=20):
            interpreter.process_page(page)

    text = sio.getvalue()

    # Cleanup
    device.close()
    sio.close()

    return text


Instruction:
Create a Python 3 function to
extract text from a file

Response:
def process_file(source_file):
    """
    Extract text from a file (pdf, txt, eml, csv, json)
    :param source_file path to file to read
    :return text from file
    """
    if source_file.endswith(('.pdf', '.PDF')):
        txt = extract_pdf(source_file)
    elif source_file.endswith(('.txt', '.eml', '.csv', '.json')):
        with open(source_file, 'r') as f:
            txt = f.read()
    else:
        logger.info("Unsupported file extension for file {}".format(source_file))
        return ""
    return txt


Instruction:
Explain what the following Python 3 code does
def from_string(cls, string):

        # find enum value
        for attr in dir(cls):
            value = getattr(cls, attr)
            if value == string:
                return value

        # if not found, log warning and return the value passed in
        logger.warning("{} is not a valid enum value for {}.".format(string, cls.__name__))
        return string

Response: Returns the value passed in as string.


Instruction:
Can you write a function in Python 3 where it
validates the given parameter value value.

Response:
def validate(self, value):
        """
        Validate (and possibly typecast) the given parameter value value.

        :param value: Parameter value
        :return: Typecast parameter value
        :raises ValidationErrors: if there were validation errors
        """
        errors = []
        value = self._validate_type(value, errors)
        self._validate_value(value, errors)

        if errors:
            raise ValidationErrors(errors)

        return value


Instruction:
How would you explain what the following Python 3 function does
def format_cli(self, value):
        if value is None or (self.type == 'flag' and not value):
            return None
        pass_as_bits = text_type(self.pass_as or self.default_pass_as).split()
        env = dict(name=self.name, value=value, v=value)
        return [bit.format(**env) for bit in pass_as_bits]

Response: Build a single parameter argument that can be used as a CLI string.


Instruction:
Write a Python 3 script for
wrapping the given value into a list.

Response:
def listify(value):
    """
    Wrap the given value into a list, with the below provisions:

    * If the value is a list or a tuple, it's coerced into a new list.
    * If the value is None, an empty list is returned.
    * Otherwise, a single-element list is returned, containing the value.

    :param value: A value.
    :return: a list!
    :rtype: list
    """
    if value is None:
        return []
    if isinstance(value, (list, tuple)):
        return list(value)
    return [value]