Instruction:
Make a summary of the following Python 3 code
def generate_fake_data(f='2*x-5', x=_n.linspace(-5,5,11), ey=1, ex=0, include_errors=False, **kwargs):

    # Make a fitter object, which handily interprets string functions
    # The "+0*x" is a trick to ensure the function takes x as an argument
    # (makes it a little more idiot proof).
    fitty = _s.data.fitter().set_functions(f+"+0*x",'')

    # Make sure both errors are arrays of the right length
    if not _s.fun.is_iterable(ex): ex = _n.array([ex]*len(x))
    if not _s.fun.is_iterable(ey): ey = _n.array([ey]*len(x))

    # Get the x and y exact values first, then randomize
    x = _n.array(x)
    y = fitty.f[0](x)

    x = _n.random.normal(_n.array(x),ex)
    y = _n.random.normal(y,          ey)

    # make a databox
    d = _s.data.databox()
    d['x']  = x
    d['y']  = y

    if include_errors:
        d['ey'] = ey
        d['ex'] = ex

    d.h(reality=f, ey=ey[0], ex=ex[0])

    return d

Response: Generate a set of fake data from the underlying reality function or mean behavior.


Instruction:
Explain what the following Python 3 code does
def get_shell_history():
    # try for ipython
    if 'get_ipython' in globals():
        a = list(get_ipython().history_manager.input_hist_raw)
        a.reverse()
        return a

    elif 'SPYDER_SHELL_ID' in _os.environ:
        try:
            p = _os.path.join(_settings.path_user, ".spyder2", "history.py")
            a = read_lines(p)
            a.reverse()
            return a
        except:
            pass

    # otherwise try pyshell or pycrust (requires wx)
    else:
        try:
            import wx
            for x in wx.GetTopLevelWindows():
                if type(x) in [wx.py.shell.ShellFrame, wx.py.crust.CrustFrame]:
                    a = x.shell.GetText().split(">>>")
                    a.reverse()
                    return a
        except:
            pass

    return ['shell history not available']

Response: Returns a list of history items for the current shell.


Instruction:
Can you generate the documentation for the following Python 3 function
def index(value, array):
    i = array.searchsorted(value)
    if i == len(array): return -1
    else:               return i

Response: Returns the index of the value in the array.


Instruction:
Create a Python 3 function to
return the index of the nearest value in an array

Response:
def index_nearest(value, array):
    """
    expects a _n.array
    returns the global minimum of (value-array)^2
    """

    a = (array-value)**2
    return index(a.min(), a)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def index_next_crossing(value, array, starting_index=0, direction=1):

    for n in range(starting_index, len(array)-1):
        if  (value-array[n]  )*direction >= 0         \
        and (value-array[n+1])*direction <  0: return n

    # no crossing found
    return -1

Response: Returns the index of the next crossing point with value in the array.


Instruction:
Write a Python 3 function that can
invert the increasing function f by f0.

Response:
def invert_increasing_function(f, f0, xmin, xmax, tolerance, max_iterations=100):
    """
    This will try try to qickly find a point on the f(x) curve between xmin and xmax that is
    equal to f0 within tolerance.
    """

    for n in range(max_iterations):
        # start at the middle
        x = 0.5*(xmin+xmax)

        df = f(x)-f0
        if _n.fabs(df) < tolerance: return x

        # if we're high, set xmin to x etc...
        if df > 0: xmin=x
        else:      xmax=x

    print("Couldn't find value!")
    return 0.5*(xmin+xmax)


Instruction:
How would you code a function in Python 3 to
return a single - sided power spectral density of the signal at time t and signal y.

Response:
def psd(t, y, pow2=False, window=None, rescale=False):
    """
    Single-sided power spectral density, assuming real valued inputs. This goes
    through the numpy fourier transform process, assembling and returning
    (frequencies, psd) given time and signal data y.

    Note it is defined such that sum(psd)*df, where df is the frequency
    spacing, is the variance of the original signal for any range of frequencies.
    This includes the DC and Nyquist components:

        sqrt(psd[0]*df)  = average value of original time trace

        sqrt(psd[-1]*df) = amplitude of Nyquist component (for even # points)

    Parameters
    ----------
    t,y
        Time (t) and signal (y) arrays with which to perform the PSD. Note the t
        array is assumed to be evenly spaced.

    pow2 = False
        Set this to true if you only want to keep the first 2^n data
        points (speeds up the FFT substantially)

    window = None
        can be set to any of the windowing functions in numpy,
        e.g. window='hanning'.

    rescale = False
        If True, the FFT will be rescaled by the square root of the ratio of
        variances before and after windowing, such that the integral
        sum(PSD)*df is the variance of the *original* time-domain data.

    returns frequencies, psd (y^2/Hz)
    """

    # do the actual fft
    f, Y = fft(t,y,pow2,window,rescale)

    # take twice the negative frequency branch, because it contains the
    # extra frequency point when the number of points is odd.
    f = _n.abs(f[int(len(f)/2)::-1])
    P = _n.abs(Y[int(len(Y)/2)::-1])**2 / (f[1]-f[0])

    # Since this is the same as the positive frequency branch, double the
    # appropriate frequencies. For even number of points, there is one
    # extra negative frequency to avoid doubling. For odd, you only need to
    # avoid the DC value.

    # For the even
    if len(t)%2 == 0: P[1:len(P)-1] = P[1:len(P)-1]*2
    else:             P[1:]         = P[1:]*2

    return f, P


Instruction:
Make a summary of the following Python 3 code
def replace_in_files(search, replace, depth=0, paths=None, confirm=True):

    # have the user select some files
    if paths==None:
        paths = _s.dialogs.MultipleFiles('DIS AND DAT|*.*')
    if paths == []: return

    for path in paths:
        lines = read_lines(path)

        if depth: N=min(len(lines),depth)
        else:     N=len(lines)

        for n in range(0,N):
            if lines[n].find(search) >= 0:
                lines[n] = lines[n].replace(search,replace)
                print(path.split(_os.path.pathsep)[-1]+ ': "'+lines[n]+'"')

        # only write if we're not confirming
        if not confirm:
            _os.rename(path, path+".backup")
            write_to_file(path, join(lines, ''))

    if confirm:
        if input("yes? ")=="yes":
            replace_in_files(search,replace,depth,paths,False)

    return

Response: Replaces the search string in the specified list of files.


Instruction:
Write a Python 3 function that can
find lines containing the search string and replaces the whole line with replacement_line.

Response:
def replace_lines_in_files(search_string, replacement_line):
    """
    Finds lines containing the search string and replaces the whole line with
    the specified replacement string.
    """

    # have the user select some files
    paths = _s.dialogs.MultipleFiles('DIS AND DAT|*.*')
    if paths == []: return

    for path in paths:
        _shutil.copy(path, path+".backup")
        lines = read_lines(path)
        for n in range(0,len(lines)):
            if lines[n].find(search_string) >= 0:
                print(lines[n])
                lines[n] = replacement_line.strip() + "\n"
        write_to_file(path, join(lines, ''))

    return


Instruction:
Here you have a function in Python 3, explain what it does
def reverse(array):
    l = list(array)
    l.reverse()
    return _n.array(l)

Response: returns a reversed numpy array


Instruction:
Can you tell what is the following Python 3 function doing
def round_sigfigs(x, n=2):
    iterable = is_iterable(x)
    if not iterable: x = [x]

    # make a copy to be safe
    x = _n.array(x)

    # loop over the elements
    for i in range(len(x)):

        # Handle the weird cases
        if not x[i] in [None, _n.inf, _n.nan]:
            sig_figs = -int(_n.floor(_n.log10(abs(x[i]))))+n-1
            x[i] = _n.round(x[i], sig_figs)

    if iterable: return x
    else:        return x[0]

Response: Rounds the number to the specified significant figures.


Instruction:
Explain what the following Python 3 code does
def shift_feature_to_x0(xdata, ydata, x0=0, feature=imax):
    i = feature(ydata)
    return xdata-xdata[i]+x0, ydata

Response: Shifts xdata ydata by x0


Instruction:
How would you code a function in Python 3 to
return smoothed [xdata, ydata, yerror]. Does not destroy the input arrays.

Response:
def smooth_data(xdata, ydata, yerror, amount=1):
    """
    Returns smoothed [xdata, ydata, yerror]. Does not destroy the input arrays.
    """

    new_xdata  = smooth_array(_n.array(xdata), amount)
    new_ydata  = smooth_array(_n.array(ydata), amount)
    if yerror is None:  new_yerror = None
    else:               new_yerror = smooth_array(_n.array(yerror), amount)

    return [new_xdata, new_ydata, new_yerror]


Instruction:
Create a Python 3 function for
sorting a matrix by n elements.

Response:
def sort_matrix(a,n=0):
    """
    This will rearrange the array a[n] from lowest to highest, and
    rearrange the rest of a[i]'s in the same way. It is dumb and slow.

    Returns a numpy array.
    """
    a = _n.array(a)
    return a[:,a[n,:].argsort()]


Instruction:
How would you explain what the following Python 3 function does
def submatrix(matrix,i1,i2,j1,j2):

    new = []
    for i in range(i1,i2+1):
        new.append(matrix[i][j1:j2+1])
    return _n.array(new)

Response: Returns the submatrix of the given matrix.


Instruction:
Can you implement a function in Python 3 that
removes all the data except that xdata is between xmin and xmax.

Response:
def trim_data(xmin, xmax, xdata, *args):
    """
    Removes all the data except that in which xdata is between xmin and xmax.
    This does not mutilate the input arrays, and additional arrays
    can be supplied via args (provided they match xdata in shape)

    xmin and xmax can be None
    """

    # make sure it's a numpy array
    if not isinstance(xdata, _n.ndarray): xdata = _n.array(xdata)

    # make sure xmin and xmax are numbers
    if xmin is None: xmin = min(xdata)
    if xmax is None: xmax = max(xdata)

    # get all the indices satisfying the trim condition
    ns = _n.argwhere((xdata >= xmin) & (xdata <= xmax)).transpose()[0]

    # trim the xdata
    output = []
    output.append(xdata[ns])

    # trim the rest
    for a in args:
        # make sure it's a numpy array
        if not isinstance(a, _n.ndarray): a = _n.array(a)
        output.append(a[ns])

    return output


Instruction:
Can you implement a function in Python 3 that
fetches a response from the Geocoding API.

Response:
def _fetch(self, searchtype, fields, **kwargs):
        '''Fetch a response from the Geocoding API.'''
        fields['vintage'] = self.vintage
        fields['benchmark'] = self.benchmark
        fields['format'] = 'json'

        if 'layers' in kwargs:
            fields['layers'] = kwargs['layers']

        returntype = kwargs.get('returntype', 'geographies')
        url = self._geturl(searchtype, returntype)

        try:
            with requests.get(url, params=fields, timeout=kwargs.get('timeout')) as r:
                content = r.json()
                if "addressMatches" in content.get('result', {}):
                    return AddressResult(content)

                if "geographies" in content.get('result', {}):
                    return GeographyResult(content)

                raise ValueError()

        except (ValueError, KeyError):
            raise ValueError("Unable to parse response from Census")

        except RequestException as e:
            raise e


Instruction:
How would you explain what the following Python 3 function does
def coordinates(self, x, y, **kwargs):
        '''Geocode a (lon, lat) coordinate.'''
        kwargs['returntype'] = 'geographies'
        fields = {
            'x': x,
            'y': y
        }

        return self._fetch('coordinates', fields, **kwargs)

Response: Geocode a ( lon lat ) coordinate.


Instruction:
Explain what the following Python 3 code does
def onelineaddress(self, address, **kwargs):
        '''Geocode an an address passed as one string.
        e.g. "4600 Silver Hill Rd, Suitland, MD 20746"
        '''
        fields = {
            'address': address,
        }

        return self._fetch('onelineaddress', fields, **kwargs)

Response: Geocode an address passed as one string.


Instruction:
Can you implement a function in Python 3 that
sends either a CSV file or a list of dicts with the above fields.

Response:
def addressbatch(self, data, **kwargs):
        '''
        Send either a CSV file or data to the addressbatch API.
        According to the Census, "there is currently an upper limit of 1000 records per batch file."
        If a file, must have no header and fields id,street,city,state,zip
        If data, should be a list of dicts with the above fields (although ID is optional)
        '''
        # Does data quack like a file handle?
        if hasattr(data, 'read'):
            return self._post_batch(f=data, **kwargs)

        # Check if it's a string file
        elif isinstance(data, string_types):
            with open(data, 'rb') as f:
                return self._post_batch(f=f, **kwargs)

        else:
            # Otherwise, assume a list of dicts
            return self._post_batch(data=data, **kwargs)


Instruction:
Can you create a Python 3 function that
loads a colormap from the supplied name.

Response:
def load_colormap(self, name=None):
        """
        Loads a colormap of the supplied name. None means used the internal
        name. (See self.get_name())
        """

        if name == None: name = self.get_name()
        if name == "" or not type(name)==str: return "Error: Bad name."

        # assemble the path to the colormap
        path = _os.path.join(_settings.path_home, "colormaps", name+".cmap")

        # make sure the file exists
        if not _os.path.exists(path):
            print("load_colormap(): Colormap '"+name+"' does not exist. Creating.")
            self.save_colormap(name)
            return

        # open the file and get the lines
        f = open(path, 'r')
        x = f.read()
        f.close()

        try:
            self._colorpoint_list = eval(x)
        except:
            print("Invalid colormap. Overwriting.")
            self.save_colormap()

        # update the image
        self.update_image()

        return self


Instruction:
Here you have a function in Python 3, explain what it does
def save_colormap(self, name=None):
        if name == None: name = self.get_name()
        if name == "" or not type(name)==str: return "Error: invalid name."

        # get the colormaps directory
        colormaps = _os.path.join(_settings.path_home, 'colormaps')

        # make sure we have the colormaps directory
        _settings.MakeDir(colormaps)

        # assemble the path to the colormap
        path = _os.path.join(_settings.path_home, 'colormaps', name+".cmap")

        # open the file and overwrite
        f = open(path, 'w')
        f.write(str(self._colorpoint_list))
        f.close()

        return self

Response: Saves the colormap with the specified name.


Instruction:
Make a summary of the following Python 3 code
def delete_colormap(self, name=None):
        if name == None: name = self.get_name()
        if name == "" or not type(name)==str: return "Error: invalid name."

        # assemble the path to the colormap
        path = _os.path.join(_settings.path_home, 'colormaps', name+".cmap")
        _os.unlink(path)

        return self

Response: Deletes the colormap with the specified name.


Instruction:
Explain what the following Python 3 code does
def set_name(self, name="My Colormap"):
        if not type(name)==str:
            print("set_name(): Name must be a string.")
            return
        self._name = name
        return self

Response: Sets the name of the object.


Instruction:
Can you implement a function in Python 3 that
sets which pylab image to tweak.

Response:
def set_image(self, image='auto'):
        """
        Set which pylab image to tweak.
        """
        if image=="auto": image = _pylab.gca().images[0]
        self._image=image
        self.update_image()


Instruction:
Can you generate the documentation for the following Python 3 function
def update_image(self):
        if self._image:
            self._image.set_cmap(self.get_cmap())
            _pylab.draw()

Response: Update the image s cmap.


Instruction:
Write a Python 3 script to
remove and returns the specified colorpoint. Will always leave two behind.

Response:
def pop_colorpoint(self, n=0):
        """
        Removes and returns the specified colorpoint. Will always leave two behind.
        """

        # make sure we have more than 2; otherwise don't pop it, just return it
        if len(self._colorpoint_list) > 2:

            # do the popping
            x = self._colorpoint_list.pop(n)

            # make sure the endpoints are 0 and 1
            self._colorpoint_list[0][0]  = 0.0
            self._colorpoint_list[-1][0] = 1.0

            # update the image
            self.update_image()

            return x

        # otherwise just return the indexed item
        else: return self[n]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def insert_colorpoint(self, position=0.5, color1=[1.0,1.0,0.0], color2=[1.0,1.0,0.0]):
        L = self._colorpoint_list

        # if position = 0 or 1, push the end points inward
        if   position <= 0.0:
            L.insert(0,[0.0,color1,color2])

        elif position >= 1.0:
            L.append([1.0,color1,color2])

        # otherwise, find the position where it belongs
        else:

            # loop over all the points
            for n in range(len(self._colorpoint_list)):
                # check if it's less than the next one
                if position <= L[n+1][0]:
                    # found the place to insert it
                    L.insert(n+1,[position,color1,color2])
                    break

        # update the image with the new cmap
        self.update_image()

        return self

Response: Insert a colorpoint into the list of colorpoints.


Instruction:
How would you code a function in Python 3 to
modify the colorpoint n in the image.

Response:
def modify_colorpoint(self, n, position=0.5, color1=[1.0,1.0,1.0], color2=[1.0,1.0,1.0]):
        """
        Changes the values of an existing colorpoint, then updates the colormap.
        """
        if    n==0.0                         : position = 0.0
        elif  n==len(self._colorpoint_list)-1: position = 1.0
        else: position = max(self._colorpoint_list[n-1][0], position)

        self._colorpoint_list[n] = [position, color1, color2]
        self.update_image()
        self.save_colormap("Last Used")


Instruction:
Write a Python 3 function that can
generate a pylab colormap object from the colorpoint data.

Response:
def get_cmap(self):
        """
        Generates a pylab cmap object from the colorpoint data.
        """

        # now generate the colormap from the ordered list
        r = []
        g = []
        b = []
        for p in self._colorpoint_list:
            r.append((p[0], p[1][0]*1.0, p[2][0]*1.0))
            g.append((p[0], p[1][1]*1.0, p[2][1]*1.0))
            b.append((p[0], p[1][2]*1.0, p[2][2]*1.0))

        # store the formatted dictionary
        c = {'red':r, 'green':g, 'blue':b}

        # now set the dang thing
        return _mpl.colors.LinearSegmentedColormap('custom', c)


Instruction:
Create a Python 3 function for
building the gui for the current locale.

Response:
def _build_gui(self):
        """
        Removes all existing sliders and rebuilds them based on the colormap.
        """
        # remove all widgets (should destroy all children too)
        self._central_widget.deleteLater()

        # remove all references to other controls
        self._sliders               = []
        self._buttons_top_color     = []
        self._buttons_bottom_color  = []
        self._checkboxes            = []
        self._buttons_plus          = []
        self._buttons_minus         = []
        self._color_dialogs_top     = []
        self._color_dialogs_bottom  = []

        # create the new central widget
        self._central_widget = _qtw.QWidget()
        self._window.setCentralWidget(self._central_widget)

        # layout for main widget
        self._layout = _qtw.QGridLayout(self._central_widget)
        self._central_widget.setLayout(self._layout)

        # add the list of cmaps
        self._combobox_cmaps = _qtw.QComboBox(self._central_widget)
        self._combobox_cmaps.setEditable(True)
        self._load_cmap_list()

        # add the save and delete buttons
        self._button_save   = _qtw.QPushButton("Save",   self._central_widget)
        self._button_delete = _qtw.QPushButton("Delete", self._central_widget)
        self._button_save.setFixedWidth(70)
        self._button_delete.setFixedWidth(70)

        # layouts
        self._layout.addWidget(self._combobox_cmaps, 1,1, 1,3, _qtcore.Qt.Alignment(0))
        self._layout.addWidget(self._button_save,    1,5, 1,1, _qtcore.Qt.Alignment(1))
        self._layout.addWidget(self._button_delete,  1,6, 1,2, _qtcore.Qt.Alignment(1))

        # actions
        self._combobox_cmaps.currentIndexChanged.connect(self._signal_load)
        self._button_save  .clicked.connect(self._button_save_clicked)
        self._button_delete.clicked.connect(self._button_delete_clicked)

        # ensmallen the window
        self._window.resize(10,10)

        # now create a control set for each color point
        for n in range(len(self._colorpoint_list)):

            c1 = self._colorpoint_list[n][1]
            c2 = self._colorpoint_list[n][2]

            # create a top-color button
            self._buttons_top_color.append(_qtw.QPushButton(self._central_widget))
            self._buttons_top_color[-1].setStyleSheet("background-color: rgb("+str(int(c2[0]*255))+","+str(int(c2[1]*255))+","+str(int(c2[2]*255))+"); border-radius: 3px;")

            # create a bottom-color button
            self._buttons_bottom_color.append(_qtw.QPushButton(self._central_widget))
            self._buttons_bottom_color[-1].setStyleSheet("background-color: rgb("+str(int(c1[0]*255))+","+str(int(c1[1]*255))+","+str(int(c1[2]*255))+"); border-radius: 3px;")

            # create color dialogs
            self._color_dialogs_top.append(_qtw.QColorDialog(self._central_widget))
            self._color_dialogs_top[-1].setCurrentColor(self._buttons_top_color[-1].palette().color(1))

            self._color_dialogs_bottom.append(_qtw.QColorDialog(self._central_widget))
            self._color_dialogs_bottom[-1].setCurrentColor(self._buttons_top_color[-1].palette().color(1))

            # create link checkboxes
            self._checkboxes.append(_qtw.QCheckBox(self._central_widget))
            self._checkboxes[-1].setChecked(c1==c2)

            # create a slider
            self._sliders.append(_qtw.QSlider(self._central_widget))
            self._sliders[-1].setOrientation(_qtcore.Qt.Horizontal)
            self._sliders[-1].setMaximum(1000)
            self._sliders[-1].setValue(int(self._colorpoint_list[n][0]*1000))
            self._sliders[-1].setFixedWidth(250)

            # create + and - buttons
            self._buttons_plus.append(_qtw.QPushButton(self._central_widget))
            self._buttons_plus[-1].setText("+")
            self._buttons_plus[-1].setFixedWidth(25)

            self._buttons_minus.append(_qtw.QPushButton(self._central_widget))
            self._buttons_minus[-1].setText("-")
            self._buttons_minus[-1].setFixedWidth(25)

            # layout
            self._layout.addWidget(self._buttons_bottom_color[-1], n+3,1,      _qtcore.Qt.AlignCenter)
            self._layout.addWidget(self._checkboxes[-1],           n+3,2, 1,1, _qtcore.Qt.AlignCenter)
            self._layout.addWidget(self._buttons_top_color[-1],    n+3,3,      _qtcore.Qt.AlignCenter)
            self._layout.addWidget(self._sliders[-1],              n+3,4, 1,2, _qtcore.Qt.AlignCenter)
            self._layout.setColumnStretch(5,100)
            self._layout.addWidget(self._buttons_minus[-1],        n+3,7,      _qtcore.Qt.AlignCenter)
            self._layout.addWidget(self._buttons_plus[-1],         n+3,6,      _qtcore.Qt.AlignCenter)

            # connect the buttons and slider actions to the calls
            self._buttons_bottom_color[-1]            .clicked.connect(_partial(self._color_button_clicked, n, 0))
            self._buttons_top_color[-1]               .clicked.connect(_partial(self._color_button_clicked, n, 1))
            self._color_dialogs_bottom[-1].currentColorChanged.connect(_partial(self._color_dialog_changed, n, 0))
            self._color_dialogs_top[-1]   .currentColorChanged.connect(_partial(self._color_dialog_changed, n, 1))

            self._buttons_plus[-1]        .clicked.connect(_partial(self._button_plus_clicked,  n))
            self._buttons_minus[-1]       .clicked.connect(_partial(self._button_minus_clicked, n))
            self._sliders[-1]        .valueChanged.connect(_partial(self._slider_changed,   n))

        # disable the appropriate sliders
        self._sliders[0] .setDisabled(True)
        self._sliders[-1].setDisabled(True)


Instruction:
How would you code a function in Python 3 to
load the selected cmap.

Response:
def _signal_load(self):
        """
        Load the selected cmap.
        """

        # set our name
        self.set_name(str(self._combobox_cmaps.currentText()))

        # load the colormap
        self.load_colormap()

        # rebuild the interface
        self._build_gui()

        self._button_save.setEnabled(False)


Instruction:
Write a Python 3 function that can
save the selected cmap.

Response:
def _button_save_clicked(self):
        """
        Save the selected cmap.
        """
        self.set_name(str(self._combobox_cmaps.currentText()))
        self.save_colormap()
        self._button_save.setEnabled(False)
        self._load_cmap_list()


Instruction:
Explain what the following Python 3 code does
def _button_delete_clicked(self):
        name = str(self._combobox_cmaps.currentText())
        self.delete_colormap(name)
        self._combobox_cmaps.setEditText("")
        self._load_cmap_list()

Response: Delete the selected cmap.


Instruction:
Write a Python 3 function that can
update the color of the slider.

Response:
def _color_dialog_changed(self, n, top, c):
        """
        Updates the color of the slider.
        """
        self._button_save.setEnabled(True)

        cp = self._colorpoint_list[n]

        # if they're linked, set both
        if self._checkboxes[n].isChecked():
            self.modify_colorpoint(n, cp[0], [c.red()/255.0, c.green()/255.0, c.blue()/255.0],
                                             [c.red()/255.0, c.green()/255.0, c.blue()/255.0])
            self._buttons_top_color   [n].setStyleSheet("background-color: rgb("+str(c.red())+","+str(c.green())+","+str(c.green())+"); border-radius: 3px;")
            self._buttons_bottom_color[n].setStyleSheet("background-color: rgb("+str(c.red())+","+str(c.green())+","+str(c.green())+"); border-radius: 3px;")

        elif top:
            self.modify_colorpoint(n, cp[0], cp[1], [c.red()/255.0, c.green()/255.0, c.blue()/255.0])
            self._buttons_top_color   [n].setStyleSheet("background-color: rgb("+str(c.red())+","+str(c.green())+","+str(c.green())+"); border-radius: 3px;")

        else:
            self.modify_colorpoint(n, cp[0], [c.red()/255.0, c.green()/255.0, c.blue()/255.0], cp[2])
            self._buttons_bottom_color[n].setStyleSheet("background-color: rgb("+str(c.red())+","+str(c.green())+","+str(c.green())+"); border-radius: 3px;")


Instruction:
Write a Python 3 function that can
create a new colorpoint and insert it into the GUI

Response:
def _button_plus_clicked(self, n):
        """
        Create a new colorpoint.
        """
        self._button_save.setEnabled(True)

        self.insert_colorpoint(self._colorpoint_list[n][0],
                               self._colorpoint_list[n][1],
                               self._colorpoint_list[n][2])
        self._build_gui()


Instruction:
Write a Python 3 function for
removing a new colorpoint.

Response:
def _button_minus_clicked(self, n):
        """
        Remove a new colorpoint.
        """
        self._button_save.setEnabled(True)

        self.pop_colorpoint(n)
        self._build_gui()


Instruction:
Given the following Python 3 function, write the documentation
def _slider_changed(self, n):
        self._button_save.setEnabled(True)

        self.modify_colorpoint(n, self._sliders[n].value()*0.001, self._colorpoint_list[n][1], self._colorpoint_list[n][2])

Response: Updates the colormap and plot


Instruction:
Given the following Python 3 function, write the documentation
def _load_cmap_list(self):
        # store the current name
        name = self.get_name()

        # clear the list
        self._combobox_cmaps.blockSignals(True)
        self._combobox_cmaps.clear()

        # list the existing contents
        paths = _settings.ListDir('colormaps')

        # loop over the paths and add the names to the list
        for path in paths:
            self._combobox_cmaps.addItem(_os.path.splitext(path)[0])

        # try to select the current name
        self._combobox_cmaps.setCurrentIndex(self._combobox_cmaps.findText(name))
        self._combobox_cmaps.blockSignals(False)

Response: Searches the colormaps directory for all files and populates the list.


Instruction:
How would you implement a function in Python 3 that
displays a save dialog and returns the path of the selected file.

Response:
def save(filters='*.*', text='Save THIS, facehead!', default_directory='default_directory', force_extension=None):
    """
    Pops up a save dialog and returns the string path of the selected file.

    Parameters
    ----------
    filters='*.*'
        Which file types should appear in the dialog.
    text='Save THIS, facehead!'
        Title text for the dialog.
    default_directory='default_directory'
        Key for the spinmob.settings default directory. If you use a name, e.g.
        'my_defaultypoo', for one call of this function, the next time you use
        the same name, it will start in the last dialog's directory by default.
    force_extension=None
        Setting this to a string, e.g. 'txt', will enforce that the filename
        will have this extension.
    """
    # make sure the filters contains "*.*" as an option!
    if not '*' in filters.split(';'): filters = filters + ";;All files (*)"

    # if this type of pref doesn't exist, we need to make a new one
    if default_directory in _settings.keys(): default = _settings[default_directory]
    else:                                     default = ""

    # pop up the dialog
    result = _qtw.QFileDialog.getSaveFileName(None,text,default,filters)

    # If Qt5, take the zeroth element
    if _s._qt.VERSION_INFO[0:5] == "PyQt5": result = result[0]

    # Make sure it's a string
    result = str(result)

    # Enforce the extension if necessary
    if  not force_extension == None:

        # In case the user put "*.txt" instead of just "txt"
        force_extension = force_extension.replace('*','').replace('.','')

        # If it doesn't end with the right extension, add this.
        if not _os.path.splitext(result)[-1][1:] == force_extension:
            result = result + '.' + force_extension

    if result == '': return None
    else:
        _settings[default_directory] = _os.path.split(result)[0]
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def load(filters="*.*", text='Select a file, FACEFACE!', default_directory='default_directory'):
    # make sure the filters contains "*.*" as an option!
    if not '*' in filters.split(';'): filters = filters + ";;All files (*)"

    # if this type of pref doesn't exist, we need to make a new one
    if default_directory in _settings.keys(): default = _settings[default_directory]
    else:                                     default = ""

    # pop up the dialog
    result = _qtw.QFileDialog.getOpenFileName(None,text,default,filters)

    # If Qt5, take the zeroth element
    if _s._qt.VERSION_INFO[0:5] == "PyQt5": result = result[0]

    # Make sure it's a string
    result = str(result)

    if result == '': return None
    else:
        _settings[default_directory] = _os.path.split(result)[0]
        return result

Response: Open a single file in a directory. Returns a string path or None.


Instruction:
Make a summary of the following Python 3 code
def load_multiple(filters="*.*", text='Select some files, FACEFACE!', default_directory='default_directory'):
    # make sure the filters contains "*.*" as an option!
    if not '*' in filters.split(';'): filters = filters + ";;All files (*)"

    # if this type of pref doesn't exist, we need to make a new one
    if default_directory in _settings.keys(): default = _settings[default_directory]
    else:                                     default = ""

    # pop up the dialog
    results = _qtw.QFileDialog.getOpenFileNames(None,text,default,filters)

    # If Qt5, take the zeroth element
    if _s._qt.VERSION_INFO[0:5] == "PyQt5": results = results[0]

    # Make sure it's a string
    result = []
    for r in results: result.append(str(r))

    if len(result)==0: return
    else:
        _settings[default_directory] = _os.path.split(result[0])[0]
        return result

Response: Open multiple files. Returns a list of string paths or None.


Instruction:
Can you generate the documentation for the following Python 3 function
def Dump(self):
        prefs_file = open(self.prefs_path, 'w')
        for n in range(0,len(self.prefs)):
            if len(list(self.prefs.items())[n]) > 1:
                prefs_file.write(str(list(self.prefs.items())[n][0]) + ' = ' +
                                 str(list(self.prefs.items())[n][1]) + '\n')
        prefs_file.close()

Response: Dumps the current preferences to the preferences. txt file.


Instruction:
Here you have a function in Python 3, explain what it does
def link(self):

        type_specs = {}
        types = []

        for name, type_spec in self.scope.type_specs.items():
            type_spec = type_spec.link(self.scope)
            type_specs[name] = type_spec

            if type_spec.surface is not None:
                self.scope.add_surface(name, type_spec.surface)
                types.append(type_spec.surface)

        self.scope.type_specs = type_specs
        self.scope.add_surface('__types__', tuple(types))

Response: Resolve and link all types in the scope.


Instruction:
Given the following Python 3 function, write the documentation
def install(path, name=None):
    if name is None:
        name = os.path.splitext(os.path.basename(path))[0]

    callermod = inspect.getmodule(inspect.stack()[1][0])
    name = '%s.%s' % (callermod.__name__, name)

    if name in sys.modules:
        return sys.modules[name]

    if not os.path.isabs(path):
        callerfile = callermod.__file__
        path = os.path.normpath(
            os.path.join(os.path.dirname(callerfile), path)
        )

    sys.modules[name] = mod = load(path, name=name)
    return mod

Response: Compiles a Thrift file and installs it as a submodule of the caller.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def loads(self, name, document):
        return self.compiler.compile(name, document).link().surface

Response: Parse and compile the given Thrift IDL into a surface IDL.


Instruction:
Can you tell what is the following Python 3 function doing
def load(self, path, name=None):
        if name is None:
            name = os.path.splitext(os.path.basename(path))[0]
            # TODO do we care if the file extension is .thrift?

        with open(path, 'r') as f:
            document = f.read()

        return self.compiler.compile(name, document, path).link().surface

Response: Load and compile the given Thrift file.


Instruction:
Implement a Python 3 function for
getting the URL for implicit flow user.

Response:
def get_url_implicit_flow_user(client_id, scope,
                               redirect_uri='https://oauth.vk.com/blank.html', display='page',
                               response_type='token', version=None, state=None, revoke=1):
    """
    https://vk.com/dev/implicit_flow_user

    :return: url
    """
    url = "https://oauth.vk.com/authorize"
    params = {
        "client_id": client_id,
        "scope": scope,
        "redirect_uri": redirect_uri,
        "display": display,
        "response_type": response_type,
        "version": version,
        "state": state,
        "revoke": revoke
    }

    params = {key: value for key, value in params.items() if value is not None}
    return u"{url}?{params}".format(url=url, params=urlencode(params))


Instruction:
Given the following Python 3 function, write the documentation
def get_url_authcode_flow_user(client_id, redirect_uri, display="page", scope=None, state=None):
    url = "https://oauth.vk.com/authorize"
    params = {
        "client_id": client_id,
        "redirect_uri": redirect_uri,
        "display": display,
        "response_type": "code"
    }

    if scope:
        params['scope'] = scope

    if state:
        params['state'] = state

    return u"{url}?{params}".format(url=url, params=urlencode(params))

Response: This function returns a URL that can be used to get the authorization code for a user access token.


Instruction:
Implement a function in Python 3 to
call when someone presses the fire button.

Response:
def get_fake_data(*a):
    """
    Called whenever someone presses the "fire" button.
    """
    # add columns of data to the databox
    d['x'] = _n.linspace(0,10,100)
    d['y'] = _n.cos(d['x']) + 0.1*_n.random.rand(100)

    # update the curve
    c.setData(d['x'], d['y'])


Instruction:
Can you create a Python 3 function that
sets the behavior of the SpinBox.

Response:
def setOpts(self, **opts):
        """
        Changes the behavior of the SpinBox. Accepts most of the arguments
        allowed in :func:`__init__ <pyqtgraph.SpinBox.__init__>`.

        """
        #print opts
        for k in opts:
            if k == 'bounds':
                #print opts[k]
                self.setMinimum(opts[k][0], update=False)
                self.setMaximum(opts[k][1], update=False)
                #for i in [0,1]:
                    #if opts[k][i] is None:
                        #self.opts[k][i] = None
                    #else:
                        #self.opts[k][i] = D(unicode(opts[k][i]))
            elif k in ['step', 'minStep']:
                self.opts[k] = D(asUnicode(opts[k]))
            elif k == 'value':
                pass   ## don't set value until bounds have been set
            else:
                self.opts[k] = opts[k]
        if 'value' in opts:
            self.setValue(opts['value'])

        ## If bounds have changed, update value to match
        if 'bounds' in opts and 'value' not in opts:
            self.setValue()

        ## sanity checks:
        if self.opts['int']:
            if 'step' in opts:
                step = opts['step']
                ## not necessary..
                #if int(step) != step:
                    #raise Exception('Integer SpinBox must have integer step size.')
            else:
                self.opts['step'] = int(self.opts['step'])

            if 'minStep' in opts:
                step = opts['minStep']
                if int(step) != step:
                    raise Exception('Integer SpinBox must have integer minStep size.')
            else:
                ms = int(self.opts.get('minStep', 1))
                if ms < 1:
                    ms = 1
                self.opts['minStep'] = ms

        if 'delay' in opts:
            self.proxy.setDelay(opts['delay'])

        self.updateText()


Instruction:
Write a Python 3 function that can
set the maximum allowed value for this option

Response:
def setMaximum(self, m, update=True):
        """Set the maximum allowed value (or None for no limit)"""
        if m is not None:
            m = D(asUnicode(m))
        self.opts['bounds'][1] = m
        if update:
            self.setValue()


Instruction:
Explain what the following Python 3 code does
def selectNumber(self):
        le = self.lineEdit()
        text = asUnicode(le.text())
        if self.opts['suffix'] == '':
            le.setSelection(0, len(text))
        else:
            try:
                index = text.index(' ')
            except ValueError:
                return
            le.setSelection(0, index)

Response: Select the numerical portion of the text to allow quick editing by the user.


Instruction:
Create a Python 3 function to
return the value of this SpinBox.

Response:
def value(self):
        """
        Return the value of this SpinBox.

        """
        if self.opts['int']:
            return int(self.val)
        else:
            return float(self.val)


Instruction:
Implement a Python 3 function for
setting the value of the current spin.

Response:
def setValue(self, value=None, update=True, delaySignal=False):
        """
        Set the value of this spin.
        If the value is out of bounds, it will be clipped to the nearest boundary.
        If the spin is integer type, the value will be coerced to int.
        Returns the actual value set.

        If value is None, then the current value is used (this is for resetting
        the value after bounds, etc. have changed)
        """

        if value is None:
            value = self.value()

        bounds = self.opts['bounds']
        if bounds[0] is not None and value < bounds[0]:
            value = bounds[0]
        if bounds[1] is not None and value > bounds[1]:
            value = bounds[1]

        if self.opts['int']:
            value = int(value)

        value = D(asUnicode(value))
        if value == self.val:
            return
        prev = self.val

        self.val = value
        if update:
            self.updateText(prev=prev)

        self.sigValueChanging.emit(self, float(self.val))  ## change will be emitted in 300ms if there are no subsequent changes.
        if not delaySignal:
            self.emitChanged()

        return value


Instruction:
Can you write a function in Python 3 where it
returns value of text. Return False if text is invalid raise exception if text is intermediate

Response:
def interpret(self):
        """Return value of text. Return False if text is invalid, raise exception if text is intermediate"""
        strn = self.lineEdit().text()
        suf = self.opts['suffix']
        if len(suf) > 0:
            if strn[-len(suf):] != suf:
                return False
            #raise Exception("Units are invalid.")
            strn = strn[:-len(suf)]
        try:
            val = fn.siEval(strn)
        except:
            #sys.excepthook(*sys.exc_info())
            #print "invalid"
            return False
        #print val
        return val


Instruction:
Can you generate the documentation for the following Python 3 function
def editingFinishedEvent(self):
        #print "Edit finished."
        if asUnicode(self.lineEdit().text()) == self.lastText:
            #print "no text change."
            return
        try:
            val = self.interpret()
        except:
            return

        if val is False:
            #print "value invalid:", str(self.lineEdit().text())
            return
        if val == self.val:
            #print "no value change:", val, self.val
            return
        self.setValue(val, delaySignal=False)

Response: Edit has finished ; set value.


Instruction:
How would you implement a function in Python 3 that
creates a factory for the given class.

Response:
def make_factory(self, cls, count):
        """ Get the generators from the Scaffolding class within the model.
        """
        field_names = cls._meta.get_all_field_names()
        fields = {}
        text = []
        finalizer = None
        scaffold = scaffolding.scaffold_for_model(cls)

        for field_name in field_names:
            generator = getattr(scaffold, field_name, None)
            if generator:
                if hasattr(generator, 'set_up'):
                    generator.set_up(cls, count)
                fields[field_name] = generator
                text.append(u'%s: %s; ' % (field_name, fields[field_name]))
        try:
            self.stdout.write(u'Generator for %s: %s\n' % (cls, u''.join(text)))
        except models.ObjectDoesNotExist:
            self.stdout.write(u'Generator for %s\n' % u''.join(text))

        if hasattr(scaffold, 'finalize') and hasattr(scaffold.finalize, '__call__'):
            finalizer = scaffold.finalize

        return fields, finalizer


Instruction:
Write a Python 3 script to
compute the indel length accounting for wether it is an insertion or deletion.

Response:
def compute_indel_length(fs_df):
    """Computes the indel length accounting for wether it is an insertion or
    deletion.

    Parameters
    ----------
    fs_df : pd.DataFrame
        mutation input as dataframe only containing indel mutations

    Returns
    -------
    indel_len : pd.Series
        length of indels
    """
    indel_len = pd.Series(index=fs_df.index)
    indel_len[fs_df['Reference_Allele']=='-'] = fs_df['Tumor_Allele'][fs_df['Reference_Allele']=='-'].str.len()
    indel_len[fs_df['Tumor_Allele']=='-'] = fs_df['Reference_Allele'][fs_df['Tumor_Allele']=='-'].str.len()
    indel_len = indel_len.fillna(0).astype(int)
    return indel_len


Instruction:
Explain what the following Python 3 code does
def keep_indels(mut_df,
                indel_len_col=True,
                indel_type_col=True):
    """Filters out all mutations that are not indels.

    Requires that one of the alleles have '-' indicating either an insertion
    or deletion depending if found in reference allele or somatic allele
    columns, respectively.

    Parameters
    ----------
    mut_df : pd.DataFrame
        mutation input file as a dataframe in standard format
    indel_len_col : bool
        whether or not to add a column indicating the length of the indel

    Returns
    -------
    mut_df : pd.DataFrame
        mutations with only frameshift mutations kept
    """
    # keep only frameshifts
    mut_df = mut_df[is_indel_annotation(mut_df)]

    if indel_len_col:
        # calculate length
        mut_df.loc[:, 'indel len'] = compute_indel_length(mut_df)

    if indel_type_col:
        is_ins = mut_df['Reference_Allele']=='-'
        is_del = mut_df['Tumor_Allele']=='-'
        mut_df['indel type'] = ''
        mut_df.loc[is_ins, 'indel type'] = 'INS'
        mut_df.loc[is_del, 'indel type'] = 'DEL'

    return mut_df

Response: Filters out all mutations that are not indels.


Instruction:
Here you have a function in Python 3, explain what it does
def keep_frameshifts(mut_df,
                     indel_len_col=True):
    """Filters out all mutations that are not frameshift indels.

    Requires that one of the alleles have '-' indicating either an insertion
    or deletion depending if found in reference allele or somatic allele
    columns, respectively.

    Parameters
    ----------
    mut_df : pd.DataFrame
        mutation input file as a dataframe in standard format
    indel_len_col : bool
        whether or not to add a column indicating the length of the frameshift

    Returns
    -------
    mut_df : pd.DataFrame
        mutations with only frameshift mutations kept
    """
    # keep only frameshifts
    mut_df = mut_df[is_frameshift_annotation(mut_df)]

    if indel_len_col:
        # calculate length
        mut_df.loc[:, 'indel len'] = compute_indel_length(mut_df)
    return mut_df

Response: Filters out all mutations that are not frameshift indels.


Instruction:
Can you tell what is the following Python 3 function doing
def get_frameshift_lengths(num_bins):
    fs_len = []
    i = 1
    tmp_bins = 0
    while(tmp_bins<num_bins):
        if i%3:
            fs_len.append(i)
            tmp_bins += 1
        i += 1
    return fs_len

Response: Simple function that returns the lengths for each frameshift category in the tree.


Instruction:
Write a Python 3 script to
initialize the attributes defining the mutation contexts and their positions.

Response:
def _init_context(self, gene_seq):
        """Initializes attributes defining mutation contexts and their position.

        The self.context2pos and self.pos2context dictionaries map from
        sequence context to sequence position and sequence position to
        sequence context, respectively. These attributes allow for randomly
        sampling of mutation positions while respecting sequence context in the
        randomization-based test.

        Parameters
        ----------
        gene_seq : GeneSequence
            GeneSequence object from the gene_sequence module
        """
        self.context2pos, self.pos2context = {}, {}
        gene_len = len(gene_seq.exon_seq)  # get length of CDS
        five_ss_len = 2*len(gene_seq.five_prime_seq)  # total length of 5' splice sites
        three_ss_len = 2*len(gene_seq.three_prime_seq)  # total length of 3' splice sites

        if gene_seq.nuc_context in [1, 2]:
            # case where context matters
            index_context = int(gene_seq.nuc_context) - 1  # subtract 1 since python is zero-based index
            for i in range(index_context, gene_len):
                nucs = gene_seq.exon_seq[i-index_context:i+1]
                self.context2pos.setdefault(nucs, [])
                self.context2pos[nucs].append(i)
                self.pos2context[i] = nucs

            # sequence context for five prime splice site
            for i, five_ss in enumerate(gene_seq.five_prime_seq):
                first_nucs = five_ss[1-index_context:1+1]
                second_nucs = five_ss[2-index_context:2+1]
                first_pos = 2*i + gene_len
                second_pos = 2*i + gene_len + 1
                self.context2pos.setdefault(first_nucs, [])
                self.context2pos[first_nucs].append(first_pos)
                self.context2pos.setdefault(second_nucs, [])
                self.context2pos[second_nucs].append(second_pos)
                self.pos2context[first_pos] = first_nucs
                self.pos2context[second_pos] = second_nucs
            # sequence context for three prime splice site
            for i, three_ss in enumerate(gene_seq.three_prime_seq):
                first_nucs = three_ss[1-index_context:1+1]
                second_nucs = three_ss[2-index_context:2+1]
                first_pos = 2*i + gene_len + five_ss_len
                second_pos = 2*i + gene_len + five_ss_len + 1
                self.context2pos.setdefault(first_nucs, [])
                self.context2pos[first_nucs].append(first_pos)
                self.context2pos.setdefault(second_nucs, [])
                self.context2pos[second_nucs].append(second_pos)
                self.pos2context[first_pos] = first_nucs
                self.pos2context[second_pos] = second_nucs

            # hack solution for context for first nuc
            if gene_seq.exon_seq and gene_seq.nuc_context > 1:
                self.pos2context[0] = gene_seq.exon_seq[0] * 2
                self.context2pos.setdefault(gene_seq.exon_seq[0]*2, [])
                self.context2pos[gene_seq.exon_seq[0]*2].append(0)
        elif gene_seq.nuc_context in [1.5, 3]:
            # use the nucleotide context from chasm if nuc
            # context is 1.5 otherwise always use a three
            # nucleotide context
            ncontext = gene_seq.nuc_context
            for i in range(1, len(gene_seq.exon_seq)-1):
                nucs = gene_seq.exon_seq[i-1:i+2]
                if ncontext == 1.5:
                    context = prob2020.python.mutation_context.get_chasm_context(nucs)
                else:
                    context = nucs
                self.context2pos.setdefault(context, [])
                self.context2pos[context].append(i)
                self.pos2context[i] = context

            # sequence context for five prime splice site
            for i, five_ss in enumerate(gene_seq.five_prime_seq):
                first_nucs = five_ss[:3]
                second_nucs = five_ss[1:4]
                first_pos = 2*i + gene_len
                second_pos = 2*i + gene_len + 1
                if ncontext == 1.5:
                    first_context = prob2020.python.mutation_context.get_chasm_context(first_nucs)
                    second_context = prob2020.python.mutation_context.get_chasm_context(second_nucs)
                else:
                    first_context = first_nucs
                    second_context = second_nucs
                self.context2pos.setdefault(first_context, [])
                self.context2pos[first_context].append(first_pos)
                self.context2pos.setdefault(second_context, [])
                self.context2pos[second_context].append(second_pos)
                self.pos2context[first_pos] = first_context
                self.pos2context[second_pos] = second_context
            # sequence context for three prime splice site
            for i, three_ss in enumerate(gene_seq.three_prime_seq):
                first_nucs = three_ss[:3]
                second_nucs = three_ss[1:4]
                first_pos = 2*i + gene_len + five_ss_len
                second_pos = 2*i + gene_len + five_ss_len + 1
                if ncontext == 1.5:
                    first_context = prob2020.python.mutation_context.get_chasm_context(first_nucs)
                    second_context = prob2020.python.mutation_context.get_chasm_context(second_nucs)
                else:
                    first_context = first_nucs
                    second_context = second_nucs
                self.context2pos.setdefault(first_context, [])
                self.context2pos[first_context].append(first_pos)
                self.context2pos.setdefault(second_context, [])
                self.context2pos[second_context].append(second_pos)
                self.pos2context[first_pos] = first_context
                self.pos2context[second_pos] = second_context

            # hack solution for context for first nuc
            if gene_seq.exon_seq:
                first_nuc = gene_seq.exon_seq[0] + gene_seq.exon_seq[:2]
                if ncontext == 1.5:
                    first_context = prob2020.python.mutation_context.get_chasm_context(first_nuc)
                else:
                    first_context = first_nuc
                self.pos2context[0] = first_context
                self.context2pos.setdefault(first_context, [])
                self.context2pos[first_context].append(0)
                last_nuc = gene_seq.exon_seq[-2:] + gene_seq.exon_seq[-1]
                if ncontext == 1.5:
                    last_context = prob2020.python.mutation_context.get_chasm_context(last_nuc)
                else:
                    last_context = last_nuc
                last_pos = len(gene_seq.exon_seq) - 1
                self.pos2context[last_pos] = first_context
                self.context2pos.setdefault(last_context, [])
                self.context2pos[last_context].append(last_pos)
        else:
            # case where there is no context,
            # mutations occur with uniform probability at each
            # position
            for i in range(gene_len + five_ss_len + three_ss_len):
                self.pos2context[i] = 'None'
            self.context2pos['None'] = range(gene_len + five_ss_len + three_ss_len)


Instruction:
Given the following Python 3 function, write the documentation
def random_context_pos(self, num, num_permutations, context):
        # make sure provide context is valid
        if not self.is_valid_context(context):
            error_msg = 'Context ({0}) was never seen in sequence.'.format(context)
            raise ValueError(error_msg)

        # make sure sampling is a positive integer
        if num < 1:
            error_msg = ('There must be at least one sample (specified {0}) '
                         'for a context'.format(num))
            raise ValueError(error_msg)

        # randomly select from available positions that fit the specified context
        available_pos = self.context2pos[context]
        random_pos = self.prng_dict[context].choice(available_pos, (num_permutations, num))
        return random_pos

Response:
Samples with replacement available positions matching the individual
            sequence context.


Instruction:
Write a Python 3 script to
return a list of random positions w/ replacement which match sequence context.

Response:
def random_pos(self, context_iterable, num_permutations):
        """Obtains random positions w/ replacement which match sequence context.

        Parameters
        ----------
        context_iterable: iterable containing two element tuple
            Records number of mutations in each context. context_iterable
            should be something like [('AA', 5), ...].
        num_permutations : int
            Number of permutations used in the permutation test.

        Returns
        -------
        position_list : list
            Contains context string and the randomly chosen positions
            for that context.
        """
        position_list = []
        for contxt, n in context_iterable:
            pos_array = self.random_context_pos(n, num_permutations, contxt)
            position_list.append([contxt, pos_array])
        return position_list


Instruction:
How would you code a function in Python 3 to
handle parallelization of permutations by splitting work by chromosome.

Response:
def multiprocess_permutation(bed_dict, mut_df, opts, indel_df=None):
    """Handles parallelization of permutations by splitting work
    by chromosome.
    """
    chroms = sorted(bed_dict.keys(), key=lambda x: len(bed_dict[x]), reverse=True)
    multiprocess_flag = opts['processes']>0
    if multiprocess_flag:
        num_processes = opts['processes']
    else:
        num_processes = 1
    #file_handle = open(opts['output'], 'w')
    file_handle = opts['handle']
    mywriter = csv.writer(file_handle, delimiter='\t', lineterminator='\n')
    if opts['maf'] and opts['num_iterations']:
        header = ['Gene', 'strand', 'Chromosome', 'Start_Position',
                  'End_Position', 'Reference_Allele', 'Tumor_Allele',
                  'Context', 'DNA_Change', 'Protein_Change', 'Variant_Classification']
    elif opts['maf']:
        header = ['Gene', 'strand', 'Chromosome', 'Start_Position',
                  'End_Position', 'Reference_Allele', 'Tumor_Allele',
                  'DNA_Change', 'Protein_Change', 'Variant_Classification',
                  'Tumor_Sample', 'Tumor_Type']
    else:
        header = ['Gene', 'ID', 'gene length', 'non-silent snv', 'silent snv', 'nonsense', 'lost stop',
                  'splice site', 'lost start', 'missense', 'recurrent missense',
                  'normalized missense position entropy',]
        # add column header for scores, is user provided one
        if opts['score_dir']:
            header += ['Total Missense MGAEntropy', 'Total Missense VEST Score']
        # add indel columns
        header += ['frameshift indel', 'inframe indel', 'normalized mutation entropy']
    mywriter.writerow(header)
    num_iterations = opts['num_iterations']

    # simulate indel counts
    if opts['summary'] and num_iterations:
        fs_cts, inframe_cts, gene_names = indel.simulate_indel_counts(indel_df,
                                                                      bed_dict,
                                                                      num_iterations,
                                                                      opts['seed'])
        name2ix = {gene_names[z]: z for z in range(len(gene_names))}
    # just count observed indels
    elif opts['summary']:
        # get gene names
        gene_names = [mybed.gene_name
                      for chrom in bed_dict
                      for mybed in bed_dict[chrom]]
        name2ix = {gene_names[z]: z for z in range(len(gene_names))}

        # initiate count vectors
        inframe_cts = np.zeros((1, len(gene_names)))
        fs_cts = np.zeros((1, len(gene_names)))

        # populate observed counts
        indel_cts_dict = indel_df['Gene'].value_counts().to_dict()
        fs_cts_dict = indel_df[indel.is_frameshift_annotation(indel_df)]['Gene'].value_counts().to_dict()
        for mygene in indel_cts_dict:
            if mygene in name2ix:
                # gene should be found in BED file annotation
                ix = name2ix[mygene]
                fs_cts[0, ix] = 0 if mygene not in fs_cts_dict else fs_cts_dict[mygene]
                inframe_cts[0, ix] = indel_cts_dict[mygene] - fs_cts[0, ix]

    # simulate snvs
    obs_result = []
    for i in range(0, len(chroms), num_processes):
        if multiprocess_flag:
            pool = Pool(processes=num_processes)
            tmp_num_proc = len(chroms) - i if i + num_processes > len(chroms) else num_processes
            info_repeat = ((bed_dict[chroms[tmp_ix]], mut_df, opts)
                            for tmp_ix in range(i, i+tmp_num_proc))
            process_results = pool.imap(singleprocess_permutation, info_repeat)
            process_results.next = utils.keyboard_exit_wrapper(process_results.next)
            try:
                # iterate through each chromosome result
                for chrom_result in process_results:
                    # add columns for indels
                    if opts['summary']:
                        tmp_chrom_result = []
                        for gname, grp in it.groupby(chrom_result, lambda x: x[0]):
                            for l, row in enumerate(grp):
                                gene_ix = name2ix[gname]
                                fs_count = fs_cts[l, gene_ix]
                                inframe_count = inframe_cts[l, gene_ix]
                                missense_pos_ct = list(row.pop(-1).values())  # missense codon counts
                                silent_pos_ct = [1 for l in range(row[4])]
                                inactivating_ct = sum(row[5:9]) + fs_count
                                tmp_count_list = missense_pos_ct + silent_pos_ct + [inactivating_ct, inframe_count]
                                norm_ent = math.normalized_mutation_entropy(tmp_count_list)
                                tmp_chrom_result.append(row+[fs_count, inframe_count, norm_ent])
                        chrom_result = tmp_chrom_result

                    # write output to file
                    mywriter.writerows(chrom_result)
            except KeyboardInterrupt:
                pool.close()
                pool.join()
                logger.info('Exited by user. ctrl-c')
                sys.exit(0)
            pool.close()
            pool.join()
        else:
            # perform simulation
            info = (bed_dict[chroms[i]], mut_df, opts)
            chrom_results = singleprocess_permutation(info)

            # add indel columns
            if opts['summary']:
                tmp_chrom_result = []
                for gname, grp in it.groupby(chrom_results, lambda x: x[0]):
                    for l, row in enumerate(grp):
                        gene_ix = name2ix[gname]
                        fs_count = fs_cts[l, gene_ix]
                        inframe_count = inframe_cts[l, gene_ix]
                        missense_pos_ct = list(row.pop(-1).values())  # missense codon counts
                        silent_pos_ct = [1 for l in range(row[4])]
                        inactivating_ct = sum(row[5:9]) + fs_count
                        tmp_count_list = missense_pos_ct + silent_pos_ct + [inactivating_ct, inframe_count]
                        norm_ent = math.normalized_mutation_entropy(tmp_count_list)
                        tmp_chrom_result.append(row+[fs_count, inframe_count, norm_ent])
                chrom_results = tmp_chrom_result

            # write to file
            mywriter.writerows(chrom_results)


Instruction:
Make a summary of the following Python 3 code
def multiprocess_permutation(bed_dict, mut_df, opts):
    chroms = sorted(bed_dict.keys())
    multiprocess_flag = opts['processes']>0
    if multiprocess_flag:
        num_processes = opts['processes']
    else:
        num_processes = 1
    num_permutations = opts['num_permutations']
    if not opts['by_sample']:
        obs_result = []
    else:
        uniq_samp = mut_df['Tumor_Sample'].unique()
        obs_result = pd.DataFrame(np.zeros((len(uniq_samp), len(cols))),
                                  index=uniq_samp, columns=cols)

    # initialize list containing output
    if not opts['score_dir']:
        result_list = [[0, 0, 0, 0, 0, 0, 0] for k in range(num_permutations)]
    else:
        result_list = [[0, 0, 0, 0, 0, 0, 0, 0, 0] for k in range(num_permutations)]

    # iterate over each chromosome
    for i in range(0, len(chroms), num_processes):
        if multiprocess_flag:
            pool = Pool(processes=num_processes)
            tmp_num_proc = len(chroms) - i if i + num_processes > len(chroms) else num_processes
            info_repeat = ((bed_dict[chroms[tmp_ix]], mut_df, opts)
                            for tmp_ix in range(i, i+tmp_num_proc))
            process_results = pool.imap(singleprocess_permutation, info_repeat)
            process_results.next = utils.keyboard_exit_wrapper(process_results.next)
            try:
                for chrom_result, obs_mutations in process_results:
                    for j in range(num_permutations):
                        result_list[j][0] += chrom_result[j][0]
                        result_list[j][1] += chrom_result[j][1]
                        result_list[j][2] += chrom_result[j][2]
                        result_list[j][3] += chrom_result[j][3]
                        result_list[j][4] += chrom_result[j][4]
                        result_list[j][5] += chrom_result[j][5]
                        result_list[j][6] += chrom_result[j][6]
                        if opts['score_dir']:
                            result_list[j][7] += chrom_result[j][7]
                            result_list[j][8] += chrom_result[j][8]

                    if not opts['by_sample']:
                        obs_result.append(obs_mutations)
                    else:
                        obs_result = obs_result + obs_mutations
            except KeyboardInterrupt:
                pool.close()
                pool.join()
                logger.info('Exited by user. ctrl-c')
                sys.exit(0)
            pool.close()
            pool.join()
        else:
            info = (bed_dict[chroms[i]], mut_df, opts)
            chrom_result, obs_mutations = singleprocess_permutation(info)
            for j in range(num_permutations):
                result_list[j][0] += chrom_result[j][0]
                result_list[j][1] += chrom_result[j][1]
                result_list[j][2] += chrom_result[j][2]
                result_list[j][3] += chrom_result[j][3]
                result_list[j][4] += chrom_result[j][4]
                result_list[j][5] += chrom_result[j][5]
                result_list[j][6] += chrom_result[j][6]
                if opts['score_dir']:
                    result_list[j][7] += chrom_result[j][7]
                    result_list[j][8] += chrom_result[j][8]
            if not opts['by_sample']:
                obs_result.append(obs_mutations)
            else:
                obs_result = obs_result + obs_mutations

    return result_list, obs_result

Response: Performs a single - process permutation on a set of work items.


Instruction:
Given the following Python 3 function, write the documentation
def retrieve_scores(gname, sdir,
                    codon_pos, germ_aa, somatic_aa,
                    default_mga=5., default_vest=0,
                    no_file_flag=-1):
    """Retrieves scores from pickle files.

    Used by summary script.

    """
    # get variant types
    #var_class = cutils.get_variant_classification(germ_aa, somatic_aa, codon_pos)

    # get information about MGA entropy
    mga_path = os.path.join(sdir, gname+".mgaentropy.pickle")
    if os.path.exists(mga_path):
        if sys.version_info < (3,):
            # python 2.7 way
            with open(mga_path) as handle:
                mga_ent = pickle.load(handle)
        else:
            # python 3.X way
            with open(mga_path, 'rb') as handle:
                mga_ent = pickle.load(handle, encoding='latin-1')
    else:
        mga_ent = None
    missense_pos = [p for i, p in enumerate(codon_pos)
                    if (germ_aa[i]!=somatic_aa[i]) and
                       (germ_aa[i] not in ['-', '*', 'Splice_Site']) and
                       (somatic_aa[i] not in ['-', '*', 'Splice_Site'])]
    total_mga_ent = compute_mga_entropy_stat(mga_ent, missense_pos, sum, default_mga)

        #mga_ent_ixs = [codon_pos[i] for i in range(len(var_class))
                       #if var_class[i] == 'Missense_Mutation']
        #len_mga_ent = len(mga_ent)
        #mga_ent_scores = [mga_ent[ix] for ix in mga_ent_ixs if ix < len_mga_ent]
        #if mga_ent_scores:
            #total_mga_ent = sum(mga_ent_scores)
        #else:
            #total_mga_ent = default_mga
    #else:
        #total_mga_ent = no_file_flag

    # get information about VEST scores
    vest_path = os.path.join(sdir, gname+".vest.pickle")
    if os.path.exists(vest_path):
        if sys.version_info < (3,):
            # python 2.7 way
            with open(vest_path) as handle:
                vest_score = pickle.load(handle)
        else:
            # python 3.X way
            with open(vest_path, 'rb') as handle:
                vest_score = pickle.load(handle, encoding='latin-1')
    else:
        vest_score = None
    total_vest = compute_vest_stat(vest_score,
                                   germ_aa, somatic_aa, codon_pos,
                                   stat_func=sum, default_val=default_vest)
        #vest_scores = [vest_score.get(codon_pos[i]+1, {}).get(germ_aa[i], {}).get(somatic_aa[i], default_vest)
                        #for i in range(len(var_class))
                        #if var_class[i] == 'Missense_Mutation']
        #total_vest = sum(vest_scores)
    #else:
        #total_vest = no_file_flag
    return total_mga_ent, total_vest

Response: Retrieves scores from pickle files.


Instruction:
Can you generate the documentation for the following Python 3 function
def read_vest_pickle(gname, score_dir):
    vest_path = os.path.join(score_dir, gname+".vest.pickle")
    if os.path.exists(vest_path):
        if sys.version_info < (3,):
            with open(vest_path) as handle:
                gene_vest = pickle.load(handle)
        else:
            with open(vest_path, 'rb') as handle:
                gene_vest = pickle.load(handle, encoding='latin-1')
        return gene_vest
    else:
        return None

Response: Reads in VEST scores for given gene.


Instruction:
How would you code a function in Python 3 to
compute missense VEST score statistic for a single mutation list.

Response:
def compute_vest_stat(vest_dict, ref_aa, somatic_aa, codon_pos,
                      stat_func=np.mean,
                      default_val=0.0):
    """Compute missense VEST score statistic.

    Note: non-missense mutations are intentially not filtered out and will take
    a default value of zero.

    Parameters
    ----------
    vest_dict : dict
        dictionary containing vest scores across the gene of interest
    ref_aa: list of str
        list of reference amino acids
    somatic_aa: list of str
        somatic mutation aa
    codon_pos : list of int
        position of codon in protein sequence
    stat_func : function, default=np.mean
        function that calculates a statistic
    default_val : float
        default value to return if there are no mutations

    Returns
    -------
    score_stat : float
        vest score statistic for provided mutation list
    """
    # return default value if VEST scores are missing
    if vest_dict is None:
        return default_val

    # fetch scores
    myscores = fetch_vest_scores(vest_dict, ref_aa, somatic_aa, codon_pos)

    # calculate mean score
    if myscores:
        score_stat = stat_func(myscores)
    else:
        score_stat = default_val

    return score_stat


Instruction:
Can you generate a brief explanation for the following Python 3 code
def compute_mga_entropy_stat(mga_vec, codon_pos,
                             stat_func=np.mean,
                             default_val=0.0):
    """Compute MGA entropy conservation statistic

    Parameters
    ----------
    mga_vec : np.array
        numpy vector containing MGA Entropy conservation scores for residues
    codon_pos : list of int
        position of codon in protein sequence
    stat_func : function, default=np.mean
        function that calculates a statistic
    default_val : float
        default value to return if there are no mutations

    Returns
    -------
    score_stat : float
        MGA entropy score statistic for provided mutation list
    """
    # return default value if VEST scores are missing
    if mga_vec is None:
        return default_val

    # fetch scores
    myscores = fetch_mga_scores(mga_vec, codon_pos)

    # calculate mean score
    if myscores is not None and len(myscores):
        score_stat = stat_func(myscores)
    else:
        score_stat = default_val

    return score_stat

Response: Compute the MGA Entropy statistic for a given codon.


Instruction:
Implement a Python 3 function for
getting VEST scores for a given mutation.

Response:
def fetch_vest_scores(vest_dict,
                      ref_aa, somatic_aa, codon_pos,
                      default_vest=0.0):
    """Get VEST scores from pre-computed scores in dictionary.

    Note: either all mutations should be missense or non-missense intended
    to have value equal to default.

    Parameters
    ----------
    vest_dict : dict
        dictionary containing vest scores across the gene of interest
    ref_aa: list of str
        list of reference amino acids
    somatic_aa: list of str
        somatic mutation aa
    codon_pos: list of int
        position of codon in protein sequence
    default_vest: float, default=0.0
        value to use if VEST score not available for a given mutation

    Returns
    -------
    vest_score_list: list
        score results for mutations
    """
    vest_score_list = []
    for i in range(len(somatic_aa)):
        # make sure position is valid
        if codon_pos[i] is not None:
            tmp_score = vest_dict.get(codon_pos[i]+1, {}).get(ref_aa[i], {}).get(somatic_aa[i], default_vest)
        else:
            tmp_score = 0.0
        vest_score_list.append(tmp_score)
    return vest_score_list


Instruction:
Make a summary of the following Python 3 code
def fetch_mga_scores(mga_vec,
                     codon_pos,
                     default_mga=None):
    """Get MGAEntropy scores from pre-computed scores in array.

    Parameters
    ----------
    mga_vec : np.array
        numpy vector containing MGA Entropy conservation scores for residues
    codon_pos: list of int
        position of codon in protein sequence
    default_mga: float or None, default=None
        value to use if MGA entropy score not available for a given mutation.
        Drop mutations if no default specified.

    Returns
    -------
    mga_ent_scores : np.array
        score results for MGA entropy conservation
    """
    # keep only positions in range of MGAEntropy scores
    len_mga = len(mga_vec)
    good_codon_pos = [p for p in codon_pos if p < len_mga]

    # get MGAEntropy scores
    if good_codon_pos:
        mga_ent_scores = mga_vec[good_codon_pos]
    else:
        mga_ent_scores = None

    return mga_ent_scores

Response: Get MGA Entropy conservation scores for a given codon.


Instruction:
Make a summary of the following Python 3 code
def read_neighbor_graph_pickle(gname, graph_dir):
    graph_path = os.path.join(graph_dir, gname+".pickle")
    if os.path.exists(graph_path):
        with open(graph_path) as handle:
            gene_graph = pickle.load(handle)
        return gene_graph
    else:
        return None

Response: Reads in neighbor graph for given gene.


Instruction:
Make a summary of the following Python 3 code
def compute_ng_stat(gene_graph, pos_ct, alpha=.5):
    # skip if there are no missense mutations
    if not len(pos_ct):
        return 1.0, 0

    max_pos = max(gene_graph)
    codon_vals = np.zeros(max_pos+1)

    # smooth out mutation counts
    for pos in pos_ct:
        mut_count = pos_ct[pos]

        # update neighbor values
        neighbors = list(gene_graph[pos])
        num_neighbors = len(neighbors)
        codon_vals[neighbors] += alpha*mut_count

        # update self-value
        codon_vals[pos] += (1-alpha)*mut_count

    # compute the normalized entropy
    #total_cts = float(np.count_nonzero(codon_vals))
    #graph_score = mymath.normalized_mutation_entropy(codon_vals, total_cts=total_cts)

    # compute regular entropy
    p = codon_vals / np.sum(codon_vals)
    graph_score = mymath.shannon_entropy(p)

    # get coverage
    coverage = np.count_nonzero(p)

    return graph_score, coverage

Response: Compute the clustering score for a gene on its neighbor graph.


Instruction:
How would you implement a function in Python 3 that
counts the total number of frameshifts in a single mutation.

Response:
def count_frameshift_total(mut_df,
                           bed_path,
                           use_unmapped=False,
                           to_zero_based=False):
    """Count frameshifts for each gene.

    Parameters
    ----------
    mut_df : pd.DataFrame
        mutation input
    bed_path : str
        path to BED file containing reference tx for genes
    use_unmapped : Bool
        flag indicating whether to include frameshifts not mapping
        to reference tx
    to_zero_based : Bool
        whether to convert end-coordinate to zero based for analysis

    Returns
    -------
    fs_cts_df : pd.DataFrame
        contains both total frameshift counts and frameshift counts
        not mappable to the reference transcript.
    """
    if to_zero_based:
        mut_df['Start_Position'] = mut_df['Start_Position'] - 1

    fs_cts = {}  # frameshift count information for each gene
    fs_df = indel.keep_frameshifts(mut_df)

    for bed in utils.bed_generator(bed_path):
        gene_df = fs_df[fs_df['Gene']==bed.gene_name]

        # find it frameshift actually is on gene annotation
        fs_pos = []
        for ix, row in gene_df.iterrows():
            indel_pos = [row['Start_Position'], row['End_Position']]
            coding_pos = bed.query_position(bed.strand, row['Chromosome'], indel_pos)
            fs_pos.append(coding_pos)

        # mark frameshifts that could not be mapped to reference tx
        gene_df['unmapped'] = [(1 if x is None else 0) for x in fs_pos]
        total_fs = len(gene_df)
        unmapped_fs = len(gene_df[gene_df['unmapped']==1])

        # filter out frameshifts that did not match reference tx
        if not use_unmapped:
            gene_df = gene_df[gene_df['unmapped']==0]
            total_fs -= unmapped_fs

        info = [total_fs, unmapped_fs,]
        fs_cts[bed.gene_name] = info

    # prepare counts into a dataframe
    fs_cts_df = pd.DataFrame.from_dict(fs_cts, orient='index')
    cols = ['total', 'unmapped',]
    fs_cts_df.columns = cols

    return fs_cts_df


Instruction:
Here you have a function in Python 3, explain what it does
def _fetch_3ss_fasta(fasta, gene_name, exon_num,
                     chrom, strand, start, end):
    """Retreives the 3' SS sequence flanking the specified exon.

    Returns a string in fasta format with the first line containing
    a ">" and the second line contains the two base pairs of 3' SS.

    Parameters
    ----------
    fasta : pysam.Fastafile
        fasta object from pysam
    gene_name : str
        gene name used for fasta seq id
    exon_num : int
        the `exon_num` exon, used for seq id
    chrom : str
        chromsome
    strand : str
        strand, {'+', '-'}
    start : int
        0-based start position
    end : int
        0-based end position

    Returns
    -------
    ss_fasta : str
        string in fasta format with first line being seq id
    """

    if strand == '-':
        ss_seq = fasta.fetch(reference=chrom,
                             start=end-1,
                             end=end+3)
        ss_seq = utils.rev_comp(ss_seq)
    elif strand == '+':
        ss_seq = fasta.fetch(reference=chrom,
                             start=start-3,
                             end=start+1)
    ss_fasta = '>{0};exon{1};3SS\n{2}\n'.format(gene_name,
                                                exon_num,
                                                ss_seq.upper())
    return ss_fasta

Response: Retreives the 3 SS sequence flanking the specified exon.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fetch_gene_fasta(gene_bed, fasta_obj):
    gene_fasta = ''
    strand = gene_bed.strand
    exons = gene_bed.get_exons()
    if strand == '-':
        exons.reverse()  # order exons 5' to 3', so reverse if '-' strand

    # iterate over exons
    for i, exon in enumerate(exons):
        exon_seq = fasta_obj.fetch(reference=gene_bed.chrom,
                                   start=exon[0],
                                   end=exon[1]).upper()
        if strand == '-':
            exon_seq = utils.rev_comp(exon_seq)
        exon_fasta = '>{0};exon{1}\n{2}\n'.format(gene_bed.gene_name,
                                                  i, exon_seq)

        # get splice site sequence
        if len(exons) == 1:
            # splice sites don't matter if there is no splicing
            ss_fasta = ''
        elif i == 0:
            # first exon only, get 3' SS
            ss_fasta = _fetch_5ss_fasta(fasta_obj, gene_bed.gene_name, i,
                                        gene_bed.chrom, strand, exon[0], exon[1])
        elif i == (len(exons) - 1):
            # last exon only, get 5' SS
            ss_fasta = _fetch_3ss_fasta(fasta_obj, gene_bed.gene_name, i,
                                        gene_bed.chrom, strand, exon[0], exon[1])
        else:
            # middle exon, get bot 5' and 3' SS
            fasta_3ss = _fetch_3ss_fasta(fasta_obj, gene_bed.gene_name, i,
                                         gene_bed.chrom, strand, exon[0], exon[1])
            fasta_5ss = _fetch_5ss_fasta(fasta_obj, gene_bed.gene_name, i,
                                         gene_bed.chrom, strand, exon[0], exon[1])
            ss_fasta = fasta_5ss + fasta_3ss

        gene_fasta += exon_fasta + ss_fasta

    return gene_fasta

Response: Retreive a single gene in FASTA format.


Instruction:
Can you tell what is the following Python 3 function doing
def _reset_seq(self):
        exon_seq_list, five_ss_seq_list, three_ss_seq_list = self._fetch_seq()
        self.exon_seq = ''.join(exon_seq_list)
        self.three_prime_seq = three_ss_seq_list
        self.five_prime_seq =  five_ss_seq_list
        self._to_upper()

Response: Updates the attributes of the object to the default values.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_germline_variants(self, germline_nucs, coding_pos):
        if len(germline_nucs) != len(coding_pos):
            raise ValueError('Each germline nucleotide should have a coding position')

        es = list(self.exon_seq)
        for i in range(len(germline_nucs)):
            gl_nuc, cpos = germline_nucs[i].upper(), coding_pos[i]
            if not utils.is_valid_nuc(gl_nuc):
                raise ValueError('{0} is not a valid nucleotide'.format(gl_nuc))
            if cpos >= 0:
                es[cpos] = gl_nuc
        self.exon_seq = ''.join(es)

Response: This method adds potential germline variants into the gene sequence.


Instruction:
Implement a Python 3 function for
converting sequences to upper case.

Response:
def _to_upper(self):
        """Convert sequences to upper case."""
        self.exon_seq = self.exon_seq.upper()
        self.three_prime_seq = [s.upper() for s in self.three_prime_seq]
        self.five_prime_seq = [s.upper() for s in self.five_prime_seq]


Instruction:
Make a summary of the following Python 3 code
def _fetch_seq(self):
        exons = []
        three_prime_ss = []
        five_prime_ss = []
        num_exons = self.bed.get_num_exons()
        for i in range(num_exons):
            # add exon sequence
            tmp_id = '{0};exon{1}'.format(self.bed.gene_name, i)
            tmp_exon = self.fasta.fetch(reference=tmp_id)
            exons.append(tmp_exon)

            # add splice site sequence
            tmp_id_3ss = '{0};3SS'.format(tmp_id)
            tmp_id_5ss = '{0};5SS'.format(tmp_id)
            if num_exons == 1:
                pass
            elif i == 0:
                tmp_5ss = self.fasta.fetch(tmp_id_5ss)
                five_prime_ss.append(tmp_5ss)
            elif i == (num_exons - 1):
                tmp_3ss = self.fasta.fetch(tmp_id_3ss)
                three_prime_ss.append(tmp_3ss)
            else:
                tmp_3ss = self.fasta.fetch(tmp_id_3ss)
                tmp_5ss = self.fasta.fetch(tmp_id_5ss)
                three_prime_ss.append(tmp_3ss)
                five_prime_ss.append(tmp_5ss)
        return exons, five_prime_ss, three_prime_ss

Response: Fetches gene sequence from PySAM fasta object.


Instruction:
Explain what the following Python 3 code does
def correct_chrom_names(chroms):
    chrom_list = []
    for chrom in chroms:
        # fix chrom numbering
        chrom = str(chrom)
        chrom = chrom.replace('23', 'X')
        chrom = chrom.replace('24', 'Y')
        chrom = chrom.replace('25', 'Mt')
        if not chrom.startswith('chr'):
            chrom = 'chr' + chrom
        chrom_list.append(chrom)
    return chrom_list

Response: Make sure chromosome names follow UCSC chr convention.


Instruction:
How would you explain what the following Python 3 function does
def cummin(x):
    for i in range(1, len(x)):
        if x[i-1] < x[i]:
            x[i] = x[i-1]
    return x

Response: A python implementation of the cummin function in R


Instruction:
Create a Python 3 function for
calculating the p - value for the number of inactivating SNV mutations.

Response:
def calc_deleterious_p_value(mut_info,
                             unmapped_mut_info,
                             sc,
                             gs,
                             bed,
                             num_permutations,
                             stop_thresh,
                             del_threshold,
                             pseudo_count,
                             seed=None):
    """Calculates the p-value for the number of inactivating SNV mutations.

    Calculates p-value based on how many simulations exceed the observed value.

    Parameters
    ----------
    mut_info : dict
        contains codon and amino acid residue information for mutations mappable
        to provided reference tx.
    unmapped_mut_info : dict
        contains codon/amino acid residue info for mutations that are NOT mappable
        to provided reference tx.
    fs_ct : int
        number of frameshifts for gene
    prob_inactive : float
        proportion of inactivating mutations out of total over all genes
    sc : SequenceContext
        object contains the nucleotide contexts for a gene such that new random
        positions can be obtained while respecting nucleotide context.
    gs : GeneSequence
        contains gene sequence
    bed : BedLine
        just used to return gene name
    num_permutations : int
        number of permutations to perform to estimate p-value. more permutations
        means more precision on the p-value.
    seed : int (Default: None)
        seed number to random number generator (None to be randomly set)
    """
    #prng = np.random.RandomState(seed)
    if len(mut_info) > 0:
        mut_info['Coding Position'] = mut_info['Coding Position'].astype(int)
        mut_info['Context'] = mut_info['Coding Position'].apply(lambda x: sc.pos2context[x])

        # group mutations by context
        cols = ['Context', 'Tumor_Allele']
        unmapped_mut_df = pd.DataFrame(unmapped_mut_info)
        tmp_df = pd.concat([mut_info[cols], unmapped_mut_df[cols]])
        context_cts = tmp_df['Context'].value_counts()
        context_to_mutations = dict((name, group['Tumor_Allele'])
                                    for name, group in tmp_df.groupby('Context'))

        # get deleterious info for actual mutations
        aa_mut_info = mc.get_aa_mut_info(mut_info['Coding Position'],
                                         mut_info['Tumor_Allele'].tolist(),
                                         gs)
        ref_aa = aa_mut_info['Reference AA'] + unmapped_mut_info['Reference AA']
        somatic_aa = aa_mut_info['Somatic AA'] + unmapped_mut_info['Somatic AA']
        codon_pos = aa_mut_info['Codon Pos'] + unmapped_mut_info['Codon Pos']
        num_del = cutils.calc_deleterious_info(ref_aa, somatic_aa, codon_pos)
        #num_del = fs_ct + num_snv_del

        # skip permutation test if number of deleterious mutations is not at
        # least meet some user-specified threshold
        if num_del >= del_threshold:
            # perform permutations
            del_p_value = pm.deleterious_permutation(num_del,
                                                     context_cts,
                                                     context_to_mutations,
                                                     sc,  # sequence context obj
                                                     gs,  # gene sequence obj
                                                     num_permutations,
                                                     stop_thresh,
                                                     pseudo_count)
        else:
            del_p_value = None
    else:
        num_del = 0
        del_p_value = None

    result = [bed.gene_name, num_del, del_p_value]
    return result


Instruction:
Can you tell what is the following Python 3 function doing
def calc_protein_p_value(mut_info,
                         unmapped_mut_info,
                         sc,
                         gs,
                         bed,
                         graph_dir,
                         num_permutations,
                         stop_thresh,
                         min_recurrent,
                         min_fraction):
    """Computes the p-value for clustering on a neighbor graph composed
    of codons connected with edges if they are spatially near in 3D protein
    structure.

    Parameters
    ----------

    Returns
    -------

    """
    if len(mut_info) > 0:
        mut_info['Coding Position'] = mut_info['Coding Position'].astype(int)
        mut_info['Context'] = mut_info['Coding Position'].apply(lambda x: sc.pos2context[x])

        # group mutations by context
        cols = ['Context', 'Tumor_Allele']
        unmapped_mut_df = pd.DataFrame(unmapped_mut_info)
        tmp_df = pd.concat([mut_info[cols], unmapped_mut_df[cols]])
        context_cts = tmp_df['Context'].value_counts()
        context_to_mutations = dict((name, group['Tumor_Allele'])
                                    for name, group in tmp_df.groupby('Context'))

        # get vest scores for gene if directory provided
        if graph_dir:
            gene_graph = scores.read_neighbor_graph_pickle(bed.gene_name, graph_dir)
            if gene_graph is None:
                logger.warning('Could not find neighbor graph for {0}, skipping . . .'.format(bed.gene_name))
        else:
            gene_graph = None

        # get recurrent info for actual mutations
        aa_mut_info = mc.get_aa_mut_info(mut_info['Coding Position'],
                                         mut_info['Tumor_Allele'].tolist(),
                                         gs)
        codon_pos = aa_mut_info['Codon Pos'] + unmapped_mut_info['Codon Pos']
        ref_aa = aa_mut_info['Reference AA'] + unmapped_mut_info['Reference AA']
        somatic_aa = aa_mut_info['Somatic AA'] + unmapped_mut_info['Somatic AA']
        num_recurrent, pos_ent, delta_pos_ent, pos_ct = cutils.calc_pos_info(codon_pos,
                                                                             ref_aa,
                                                                             somatic_aa,
                                                                             min_frac=min_fraction,
                                                                             min_recur=min_recurrent)
        try:
            # get vest score for actual mutations
            graph_score, coverage = scores.compute_ng_stat(gene_graph, pos_ct)

            # perform simulations to get p-value
            protein_p_value, norm_graph_score = pm.protein_permutation(
                graph_score, len(pos_ct), context_cts,
                context_to_mutations,
                sc,  # sequence context obj
                gs,  # gene sequence obj
                gene_graph, num_permutations, stop_thresh
            )
        except Exception as err:
            exc_info = sys.exc_info()
            norm_graph_score = 0.0
            protein_p_value = 1.0
            logger.warning('Codon numbering problem with '+bed.gene_name)

    else:
        norm_graph_score = 0.0
        protein_p_value = 1.0
        num_recurrent = 0

    result = [bed.gene_name, num_recurrent, norm_graph_score, protein_p_value]
    return result

Response: Calculates the p - value for clustering on a neighbor graph composed of codons connected with edges.


Instruction:
Can you tell what is the following Python 3 function doing
def shannon_entropy(p):
    return -np.sum(np.where(p!=0, p * np.log2(p), 0))

Response: Calculates the shannon entropy in bits.


Instruction:
Create a Python 3 function for
calculating the normalized mutation entropy based on a list of mutation counts.

Response:
def normalized_mutation_entropy(counts, total_cts=None):
    """Calculate the normalized mutation entropy based on a list/array
    of mutation counts.

    Note: Any grouping of mutation counts together should be done before hand

    Parameters
    ----------
    counts : np.array_like
        array/list of mutation counts

    Returns
    -------
    norm_ent : float
        normalized entropy of mutation count distribution.
    """
    cts = np.asarray(counts, dtype=float)
    if total_cts is None:
        total_cts = np.sum(cts)
    if total_cts > 1:
        p = cts / total_cts
        ent = shannon_entropy(p)
        max_ent = max_shannon_entropy(total_cts)
        norm_ent = ent / max_ent
    else:
        norm_ent = 1.0
    return norm_ent


Instruction:
How would you code a function in Python 3 to
compute the Kullback - Leibler divergence for discrete distributions.

Response:
def kl_divergence(p, q):
    """Compute the Kullback-Leibler (KL) divergence for discrete distributions.

    Parameters
    ----------
    p : np.array
        "Ideal"/"true" Probability distribution
    q : np.array
        Approximation of probability distribution p

    Returns
    -------
    kl : float
        KL divergence of approximating p with the distribution q
    """
    # make sure numpy arrays are floats
    p = p.astype(float)
    q = q.astype(float)

    # compute kl divergence
    kl = np.sum(np.where(p!=0, p*np.log2(p/q), 0))
    return kl


Instruction:
Can you generate a brief explanation for the following Python 3 code
def js_divergence(p, q):
    m = .5 * (p+q)
    js_div = .5*kl_divergence(p, m) + .5*kl_divergence(q, m)
    return js_div

Response: Compute the Jensen - Shannon Divergence between two discrete distributions.


Instruction:
Explain what the following Python 3 code does
def js_distance(p, q):
    js_dist = np.sqrt(js_divergence(p, q))
    return js_dist

Response: Compute the Jensen - Shannon distance between two discrete distributions.


Instruction:
How would you implement a function in Python 3 that
filters out UTR regions from the exon list.

Response:
def _filter_utr(self, ex):
        """Filter out UTR regions from the exon list (ie retain only coding regions).

        Coding regions are defined by the thickStart and thickEnd attributes.

        Parameters
        ----------
        ex : list of tuples
            list of exon positions, [(ex1_start, ex1_end), ...]

        Returns
        -------
        filtered_exons : list of tuples
            exons with UTR regions "chopped" out
        """
        # define coding region
        coding_start = int(self.bed_tuple.thickStart)
        coding_end = int(self.bed_tuple.thickEnd)
        if (coding_end - coding_start) < 3:
            # coding regions should have at least one codon, otherwise the
            # region is invalid and does not indicate an actually coding region
            logger.debug('{0} has an invalid coding region specified by thickStart '
                         'and thickEnd (only {1} bps long). This gene is possibly either '
                         'a non-coding transcript or a pseudo gene.'.format(self.gene_name,
                                                                            coding_end-coding_start))
            return []

        filtered_exons = []
        for exon in ex:
            if exon[0] > coding_end and exon[1] > coding_end:
                # exon has no coding region
                pass
            elif exon[0] < coding_start and exon[1] < coding_start:
                # exon has no coding region
                pass
            elif exon[0] <= coding_start and exon[1] >= coding_end:
                # coding region entirely contained within one exon
                filtered_exons.append((coding_start, coding_end))
            elif exon[0] <= coding_start and exon[1] < coding_end:
                # only beginning of exon contains UTR
                filtered_exons.append((coding_start, exon[1]))
            elif exon[0] > coding_start and exon[1] >= coding_end:
                # only end part of exon contains UTR
                filtered_exons.append((exon[0], coding_end))
            elif exon[0] > coding_start and exon[1] < coding_end:
                # entire exon is coding
                filtered_exons.append(exon)
            else:
                # exon is only a UTR
                pass
        return filtered_exons