def decorator(func):<EOL><INDENT>@wraps(func)<EOL>def wrapped_func(request, *args, **kwargs):<EOL><INDENT>try:<EOL><INDENT>request_dict = json.loads(request.raw_post_data)<EOL><DEDENT>except ValueError as e:<EOL><INDENT>return JsonResponseBadRequest('<STR_LIT>' % e)<EOL><DEDENT>for k in required_fields:<EOL><INDENT>if k not in request_dict:<EOL><INDENT>return JsonResponseBadRequest(<EOL>'<STR_LIT>' % k)<EOL><DEDENT><DEDENT>return func(request, request_dict, *args, **kwargs)<EOL><DEDENT>return wrapped_func<EOL><DEDENT>return decorator<EOL>

Return a decorator that ensures that the request passed to the view
function/method has a valid JSON request body with the given required
fields.  The dict parsed from the JSON is then passed as the second
argument to the decorated function/method.  For example:

@json_request({'name', 'date'})
def view_func(request, request_dict):
    ...


def decorator(func):<EOL><INDENT>@wraps(func)<EOL>def wrapped_func(request, *args, **kwargs):<EOL><INDENT>if request.method not in ['<STR_LIT:GET>', '<STR_LIT:POST>']:<EOL><INDENT>return func(request, *args, **kwargs)<EOL><DEDENT>form_class = type('<STR_LIT>', (forms.Form,), fields.copy())<EOL>form = form_class(getattr(request, request.method))<EOL>if not form.is_valid():<EOL><INDENT>if settings.DEBUG:<EOL><INDENT>return JsonResponseBadRequest(<EOL>'<STR_LIT>' % dict(form.errors)<EOL>)<EOL><DEDENT>else:<EOL><INDENT>logger.warn(<EOL>'<STR_LIT>',<EOL>request.path,<EOL>dict(form.errors)<EOL>)<EOL>return func(request, *args, **kwargs)<EOL><DEDENT><DEDENT>for (field_name, field_instance) in fields.items():<EOL><INDENT>if isinstance(field_instance, models.Model):<EOL><INDENT>field_type = type(field_instance)<EOL>field_id = '<STR_LIT>' % field_name<EOL>if field_id not in request.REQUEST:<EOL><INDENT>return JsonResponseBadRequest(<EOL>'<STR_LIT>' % field_name<EOL>)<EOL><DEDENT>field_pk = int(request.REQUEST[field_id])<EOL>try:<EOL><INDENT>field_value = field_type.objects.get(pk=field_pk)<EOL><DEDENT>except field_type.DoesNotExist:<EOL><INDENT>return JsonResponseNotFound(<EOL>'<STR_LIT>' % (<EOL>field_type, field_pk<EOL>)<EOL>)<EOL><DEDENT>form.cleaned_data[field_name] = field_value<EOL><DEDENT><DEDENT>validated_request = ValidatedRequest(request, form)<EOL>return func(validated_request, *args, **kwargs)<EOL><DEDENT>return wrapped_func<EOL><DEDENT>return decorator<EOL>

Define the accept schema of an API (GET or POST).

'fields' is a dict of Django form fields keyed by field name that specifies
the form-urlencoded fields that the API accepts*.

The view function is then called with GET/POST data that has been cleaned
by the Django form.

In debug and test modes, failure to validate the fields will result in a
400 Bad Request response.
In production mode, failure to validate will just log a
warning, unless overwritten by a 'strict' setting.

For example:

@api_accepts({
    'x': forms.IntegerField(min_value=0),
    'y': forms.IntegerField(min_value=0),
})
def add(request, *args, **kwargs):
    x = request.POST['x']
    y = request.POST['y']

    # x and y are integers already.
    return HttpResponse('%d' % (x + y))

*: 'fields' can also include Django models as {'key': Model()}. If present,
api_accepts will look for the field keyed by '<key>-id'
and pick the object that has that primary key. For example, if the entry is
{'course': Course()}, it will search for the key course_id='course-id' in
the request object, and find the object Course.objects.get(pk=course_id)


url = self.API_PATH + '<STR_LIT>'<EOL>json_data = json.loads(self._getdata(url))<EOL>coins = []<EOL>for entry in json_data:<EOL><INDENT>coin = Coin()<EOL>coin.id = entry['<STR_LIT:id>']<EOL>coin.name = entry['<STR_LIT:name>']<EOL>coin.website = entry['<STR_LIT>']<EOL>coin.price_btc = entry['<STR_LIT>']<EOL>coin.volume_btc = entry['<STR_LIT>']<EOL>coins.append(coin)<EOL><DEDENT>return coins<EOL>

Use this function to list all coins with their data which are available on cryptocoincharts.
Usage: http://api.cryptocoincharts.info/listCoins


def _getdata(self, url, data = "<STR_LIT>"):

request = Request(url)<EOL>if data != "<STR_LIT>":<EOL><INDENT>request = Request(url, urlencode(data))<EOL><DEDENT>try:<EOL><INDENT>response = urlopen(request)<EOL><DEDENT>except HTTPError as e:<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>', e.code)<EOL><DEDENT>except URLError as e:<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>', e.code)<EOL><DEDENT>else:<EOL><INDENT>return response.read()<EOL><DEDENT>


def consistency(self, desired_version=None, include_package=False,<EOL>strictness=None):

keys_to_check = list(self.versions.keys())<EOL>if not include_package and '<STR_LIT>' in keys_to_check:<EOL><INDENT>keys_to_check.remove('<STR_LIT>')<EOL><DEDENT>if desired_version is None:<EOL><INDENT>try:<EOL><INDENT>desired_version = self.versions['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>desired_version = self.versions[keys_to_check[<NUM_LIT:0>]]<EOL><DEDENT><DEDENT>if strictness is None:<EOL><INDENT>strictness = self.strictness<EOL><DEDENT>desired = self._version(desired_version, strictness)<EOL>error_keys = []<EOL>for key in keys_to_check:<EOL><INDENT>test = self._version(self.versions[key], strictness)<EOL>if test != desired:<EOL><INDENT>error_keys += [key]<EOL><DEDENT><DEDENT>msg = "<STR_LIT>"<EOL>for key in error_keys:<EOL><INDENT>msg += "<STR_LIT>".format(<EOL>d=str(desired),<EOL>v=str(self.versions[key]),<EOL>k=str(key)<EOL>)<EOL><DEDENT>return msg<EOL>

Checks that the versions are consistent

        Parameters
        ----------
        desired_version: str
            optional; the version that all of these should match
        include_package: bool
            whether to check the special 'package' version for consistency
            (default False)
        strictness: str


def __call__(self, method, *args, **kwargs):

self.output.write(str(method.__name__) + "<STR_LIT>")<EOL>msg = method(*args, **kwargs)<EOL>fail = <NUM_LIT:0><EOL>if bool(msg):<EOL><INDENT>fail = <NUM_LIT:1><EOL>self.output.write("<STR_LIT>")<EOL>self.output.write(self.wrapper.fill(msg) + '<STR_LIT:\n>')<EOL><DEDENT>else:<EOL><INDENT>self.output.write("<STR_LIT>")<EOL><DEDENT>return fail<EOL>

Generic method to turn other methods into tests.


async def user_from_token(self, token: str) -> User:

return await User.from_token(self, token)<EOL>

Create a user session from a token.

        This code is equivelent to `User.from_token(client, token)`

        Parameters
        ----------
        token : str
            The token to attatch the user session to.

        Returns
        -------
        user : User
            The user from the ID


def oauth2_url(self, redirect_uri: str, scope: Optional[str] = None, state: Optional[str] = None) -> str:

return OAuth2.url_(self.http.client_id, redirect_uri, scope=scope, state=state)<EOL>

Generate an outh2 url for user authentication.

        Parameters
        ----------
        redirect_uri : str
            Where spotify should redirect the user to after authentication.
        scope : Optional[str]
            Space seperated spotify scopes for different levels of access.
        state : Optional[str]
            Using a state value can increase your assurance that an incoming connection is the result of an authentication request.

        Returns
        -------
        url : str
            The OAuth2 url.


async def get_tracks(self, *, limit=<NUM_LIT:20>, offset=<NUM_LIT:0>) -> List[Track]:

data = await self.user.http.saved_tracks(limit=limit, offset=offset)<EOL>return [Track(self.__client, item['<STR_LIT>']) for item in data['<STR_LIT>']]<EOL>

Get a list of the songs saved in the current Spotify user’s ‘Your Music’ library.

        Parameters
        ----------
        limit : Optional[int]
            The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optional[int]
            The index of the first item to return. Default: 0


async def contains_albums(self, *albums: Sequence[Union[str, Album]]) -> List[bool]:

_albums = [(obj if isinstance(obj, str) else obj.id) for obj in albums]<EOL>return await self.user.http.is_saved_album(_albums)<EOL>

Check if one or more albums is already saved in the current Spotify user’s ‘Your Music’ library.

        Parameters
        ----------
        albums : Union[Album, str]
            A sequence of artist objects or spotify IDs


return self.__client.http.track_audio_analysis(self.id)<EOL>

Get a detailed audio analysis for the track.


async def set_repeat(self, state, *, device: Optional[SomeDevice] = None):

await self._user.http.repeat_playback(state, device_id=str(device))<EOL>

Set the repeat mode for the user’s playback.

        Parameters
        ----------
        state : str
            Options are repeat-track, repeat-context, and off
        device : Optional[:obj:`SomeDevice`]
            The Device object or id of the device this command is targeting.
            If not supplied, the user’s currently active device is the target.


async def pause(self, *, device: Optional[SomeDevice] = None):

await self._user.http.pause_playback(device_id=str(device))<EOL>

Pause playback on the user’s account.

        Parameters
        ----------
        device : Optional[:obj:`SomeDevice`]
            The Device object or id of the device this command is targeting.
            If not supplied, the user’s currently active device is the target.


async def set_volume(self, volume: int, *, device: Optional[SomeDevice] = None):

await self._user.http.set_playback_volume(volume, device_id=str(device))<EOL>

Set the volume for the user’s current playback device.

        Parameters
        ----------
        volume : int
            The volume to set. Must be a value from 0 to 100 inclusive.
        device : Optional[:obj:`SomeDevice`]
            The Device object or id of the device this command is targeting.
            If not supplied, the user’s currently active device is the target.


async def resume(self, *, device: Optional[SomeDevice] = None):

await self._user.http.play_playback(None, device_id=str(device))<EOL>

Resume playback on the user's account.

        Parameters
        ----------
        device : Optional[:obj:`SomeDevice`]
            The Device object or id of the device this command is targeting.
            If not supplied, the user’s currently active device is the target.


async def get_all_tracks(self) -> List[PlaylistTrack]:

if isinstance(self._tracks, PartialTracks):<EOL><INDENT>return await self._tracks.build()<EOL><DEDENT>_tracks = []<EOL>offset = <NUM_LIT:0><EOL>while len(self.tracks) < self.total_tracks:<EOL><INDENT>data = await self.__client.http.get_playlist_tracks(self.owner.id, self.id, limit=<NUM_LIT:50>, offset=offset)<EOL>_tracks += [PlaylistTrack(self.__client, item) for item in data['<STR_LIT>']]<EOL>offset += <NUM_LIT:50><EOL><DEDENT>self.total_tracks = len(self._tracks)<EOL>return list(self._tracks)<EOL>

Get all playlist tracks from the playlist.

        Returns
        -------
        tracks : List[PlaylistTrack]
            The playlists tracks.


async def top_artists(self, **data) -> List[Artist]:

return await self._get_top(Artist, data)<EOL>

Get the current user’s top artists based on calculated affinity.

        Parameters
        ----------
        limit : Optional[int]
            The number of entities to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optional[int]
            The index of the first entity to return. Default: 0
        time_range : Optional[str]
            Over what time frame the affinities are computed. (long_term, short_term, medium_term)

        Returns
        -------
        tracks : List[Artist]
            The top artists for the user.


@ensure_http<EOL><INDENT>async def get_player(self) -> Player:<DEDENT>

self._player = player = Player(self.__client, self, await self.http.current_player())<EOL>return player<EOL>

Get information about the users current playback.

        Returns
        -------
        player : Player
            A player object representing the current playback.


async def get_playlists(self, *, limit=<NUM_LIT:20>, offset=<NUM_LIT:0>):

if hasattr(self, '<STR_LIT:http>'):<EOL><INDENT>http = self.http<EOL><DEDENT>else:<EOL><INDENT>http = self.__client.http<EOL><DEDENT>data = await http.get_playlists(self.id, limit=limit, offset=offset)<EOL>return [Playlist(self.__client, playlist_data) for playlist_data in data['<STR_LIT>']]<EOL>

get the users playlists from spotify.

        Parameters
        ----------
         limit : Optional[int]
             The limit on how many playlists to retrieve for this user (default is 20).
         offset : Optional[int]
             The offset from where the api should start from in the playlists.

        Returns
        -------
        playlists : List[Playlist]
            A list of the users playlists.


@ensure_http<EOL><INDENT>async def get_devices(self) -> List[Device]:<DEDENT>

data = await self.http.available_devices()<EOL>return [Device(item) for item in data['<STR_LIT>']]<EOL>

Get information about the users avaliable devices.

        Returns
        -------
        devices : List[Device]
            The devices the user has available.


async def top_tracks(self, **data) -> List[Track]:

return await self._get_top(Track, data)<EOL>

Get the current user’s top tracks based on calculated affinity.

        Parameters
        ----------
        limit : Optional[int]
            The number of entities to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optional[int]
            The index of the first entity to return. Default: 0
        time_range : Optional[str]
            Over what time frame the affinities are computed. (long_term, short_term, medium_term)

        Returns
        -------
        tracks : List[Track]
            The top tracks for the user.


async def get_albums(self, *, limit: Optional[int] = <NUM_LIT:20>, offset: Optional[int] = <NUM_LIT:0>, include_groups=None, market: Optional[str] = None) -> List[Album]:

from .album import Album<EOL>data = await self.__client.http.artist_albums(self.id, limit=limit, offset=offset, include_groups=include_groups, market=market)<EOL>return list(Album(self.__client, item) for item in data['<STR_LIT>'])<EOL>

Get the albums of a Spotify artist.

        Parameters
        ----------
        limit : Optional[int]
            The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optiona[int]
            The offset of which Spotify should start yielding from.
        include_groups : INCLUDE_GROUPS_TP
            INCLUDE_GROUPS
        market : Optional[str]
            An ISO 3166-1 alpha-2 country code.

        Returns
        -------
        albums : List[Album]
            The albums of the artist.


def following_artists_or_users(self, ids, *, type='<STR_LIT>'):

route = Route('<STR_LIT:GET>', '<STR_LIT>')<EOL>payload = {'<STR_LIT>': ids, '<STR_LIT:type>': type}<EOL>return self.request(route, params=payload)<EOL>

Check to see if the current user is following one or more artists or other Spotify users.

        Parameters
        ----------
        ids : List[str]
            A comma-separated list of the artist or the user Spotify IDs to check.
            A maximum of 50 IDs can be sent in one request.
        type : Optional[str]
            The ID type: either "artist" or "user".
            Default: "artist"


def category_playlists(self, category_id, limit=<NUM_LIT:20>, offset=<NUM_LIT:0>, country=None):

route = Route('<STR_LIT:GET>', '<STR_LIT>', category_id=category_id)<EOL>payload = {'<STR_LIT>': limit, '<STR_LIT>': offset}<EOL>if country:<EOL><INDENT>payload['<STR_LIT>'] = country<EOL><DEDENT>return self.request(route, params=payload)<EOL>

Get a list of Spotify playlists tagged with a particular category.

        Parameters
        ----------
        category_id : str
            The Spotify category ID for the category.
        limit : Optional[int]
            The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optional[int]
            The index of the first item to return. Default: 0
        country : COUNTRY_TP
            COUNTRY


def album(self, spotify_id, market='<STR_LIT>'):

route = Route('<STR_LIT:GET>', '<STR_LIT>', spotify_id=spotify_id)<EOL>payload = {}<EOL>if market:<EOL><INDENT>payload['<STR_LIT>'] = market<EOL><DEDENT>return self.request(route, params=payload)<EOL>

Get a spotify album by its ID.

        Parameters
        ----------
        spotify_id : str
            The spotify_id to search by.
        market : Optional[str]
            An ISO 3166-1 alpha-2 country code.

        Returns
        -------
        album : Dict
            The album object.


def album_tracks(self, spotify_id, limit=<NUM_LIT:20>, offset=<NUM_LIT:0>, market='<STR_LIT>'):

route = Route('<STR_LIT:GET>', '<STR_LIT>', spotify_id=spotify_id)<EOL>payload = {'<STR_LIT>': limit, '<STR_LIT>': offset}<EOL>if market:<EOL><INDENT>payload['<STR_LIT>'] = market<EOL><DEDENT>return self.request(route, params=payload)<EOL>

Get an albums tracks by an ID.

        Parameters
        ----------
        spotify_id : str
            The spotify_id to search by.
        limit : Optional[int]
            The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optiona[int]
            The offset of which Spotify should start yielding from.
        market : Optional[str]
            An ISO 3166-1 alpha-2 country code.


def categories(self, limit=<NUM_LIT:20>, offset=<NUM_LIT:0>, country=None, locale=None):

route = Route('<STR_LIT:GET>', '<STR_LIT>')<EOL>payload = {'<STR_LIT>': limit, '<STR_LIT>': offset}<EOL>if country:<EOL><INDENT>payload['<STR_LIT>'] = country<EOL><DEDENT>if locale:<EOL><INDENT>payload['<STR_LIT>'] = locale<EOL><DEDENT>return self.request(route, params=payload)<EOL>

Get a list of categories used to tag items in Spotify.

        Parameters
        ----------
        limit : Optional[int]
            The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
        offset : Optional[int]
            The index of the first item to return. Default: 0
        country : COUNTRY_TP
            COUNTRY
        locale : LOCALE_TP
            LOCALE


return OAuth2.url_(*args, **kwargs)<EOL>

Construct a OAuth2 URL instead of an OAuth2 object.


@property<EOL><INDENT>def url(self) -> str:<DEDENT>

return self._BASE.format(parameters=self.parameters)<EOL>


def assert_hasattr(attr: str, msg: str, tp: BaseException = SpotifyException) -> Callable:

def decorator(func: Callable) -> Callable:<EOL><INDENT>@functools.wraps(func)<EOL>def decorated(self, *args, **kwargs):<EOL><INDENT>if not hasattr(self, attr):<EOL><INDENT>raise tp(msg)<EOL><DEDENT>return func(self, *args, **kwargs)<EOL><DEDENT>if inspect.iscoroutinefunction(func):<EOL><INDENT>@functools.wraps(func)<EOL>async def decorated(*args, **kwargs):<EOL><INDENT>return await decorated(*args, **kwargs)<EOL><DEDENT><DEDENT>return decorated<EOL><DEDENT>return decorator<EOL>

decorator to assert an object has an attribute when run.


string = string.strip()<EOL>match = _URI_RE.match(string)<EOL>if match is None:<EOL><INDENT>match = _OPEN_RE.match(string)<EOL>if match is None:<EOL><INDENT>return string<EOL><DEDENT>else:<EOL><INDENT>return match.group(<NUM_LIT:2>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return match.group(<NUM_LIT:1>)<EOL><DEDENT>

Get a spotify ID from a URI or open.spotify URL.

    Paramters
    ---------
    string : str
        The string to operate on.

    Returns
    -------
    id : str
        The Spotify ID from the string.


return self._get_current_item(self)<EOL>

Get the current active navigation Item if any.

        .. versionadded:: 0.2.0


return self._url is None<EOL>

``True`` if the target url is internal of current app.


return ItemReference(self.endpoint, self.args)<EOL>

The identity of this item.

        :type: :class:`~flask.ext.navigation.Navigation.ItemReference`


return bool(request and self.is_current)<EOL>

``True`` if the item should be presented as active, and ``False``
        always if the request context is not bound.


if not self.is_internal:<EOL><INDENT>return False  <EOL><DEDENT>has_same_endpoint = (request.endpoint == self.endpoint)<EOL>has_same_args = (request.view_args == self.args)<EOL>return has_same_endpoint and has_same_args<EOL>

``True`` if current request has same endpoint with the item.

        The property should be used in a bound request context, or the
        :class:`RuntimeError` may be raised.


pairs = dict_.items()<EOL>key_getter = operator.itemgetter(<NUM_LIT:0>)<EOL>return tuple(sorted(pairs, key=key_getter))<EOL>

Freezes ``dict`` into ``tuple``.

    A typical usage is packing ``dict`` into hashable.

    e.g.::

        >>> freeze_dict({'a': 1, 'b': 2})
        (('a', 1), ('b', 2))


print('<STR_LIT>')<EOL>return <NUM_LIT><EOL>

This description will show up in the --help. Try it!


payload = kwargs.copy()<EOL>payload['<STR_LIT>'] = self.api_key<EOL>payload['<STR_LIT>'] = self.api_secret<EOL>to = payload.pop('<STR_LIT:to>', None)<EOL>if to:<EOL><INDENT>if isinstance(to, basestring):<EOL><INDENT>payload['<STR_LIT:to>'] = to<EOL><DEDENT>else:<EOL><INDENT>for num_i, fax_num in enumerate(to):<EOL><INDENT>payload['<STR_LIT>' % num_i] = fax_num<EOL><DEDENT><DEDENT><DEDENT>files = payload.pop('<STR_LIT>', [])<EOL>if not isinstance(files, (list, tuple)): files = (files,)<EOL>req_files = {}<EOL>for file_i, f in enumerate(files):<EOL><INDENT>if isinstance(f, basestring):<EOL><INDENT>req_files['<STR_LIT>' % file_i] = open(f, '<STR_LIT:rb>')<EOL><DEDENT>else:<EOL><INDENT>f.seek(<NUM_LIT:0>)<EOL>req_files['<STR_LIT>' % file_i] = f<EOL><DEDENT><DEDENT>url = '<STR_LIT>' % (self.BASE_URL, self.VERSION, method)<EOL>r = requests.post(url, data=payload, files=req_files)<EOL>return self.parse_response(r)<EOL>

Builds the url for the specified method and arguments and returns
        the response as a dictionary.


rv = self.lib.C_CloseSession(self.session)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>


if type in (CKA_WRAP_TEMPLATE,<EOL>CKA_UNWRAP_TEMPLATE):<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

is the type a attribute list value?

:param type: PKCS#11 type like `CKA_WRAP_TEMPLATE`
:rtype: bool


low_seed = ckbytelist(seed)<EOL>rv = self.lib.C_SeedRandom(self.session, low_seed)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>

C_SeedRandom

:param seed: seed material
:type seed: iterable


def sign(self, key, data, mecha=MechanismRSAPKCS1):

m = mecha.to_native()<EOL>signature = ckbytelist()<EOL>data1 = ckbytelist(data)<EOL>rv = self.lib.C_SignInit(self.session, m, key)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>rv = self.lib.C_Sign(self.session, data1, signature)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>rv = self.lib.C_Sign(self.session, data1, signature)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>return signature<EOL>

C_SignInit/C_Sign

:param key: a key handle, obtained calling :func:`findObjects`.
:type key: integer
:param data: the data to be signed
:type data:  (binary) string or list/tuple of bytes
:param mecha: the signing mechanism to be used
  (use `MechanismRSAPKCS1` for `CKM_RSA_PKCS`)
:type mecha: :class:`Mechanism`
:return: the computed signature
:rtype: list of bytes

:note: the returned value is an instance of :class:`ckbytelist`.
  You can easly convert it to a binary string with:
  ``bytes(ckbytelistSignature)``
  or, for Python 2:
  ``''.join(chr(i) for i in ckbytelistSignature)``


if not isinstance(pykcs11, PyKCS11Lib):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if not isinstance(session, LowLevel.CK_SESSION_HANDLE):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self.pykcs11 = pykcs11<EOL>self.session = session<EOL>

:param pykcs11: PyKCS11 library object
:type pykcs11: PyKCS11Lib
:param session: session handle
:type session: instance of :class:`CK_SESSION_HANDLE`


return self.pykcs11.lib<EOL>

Get the low level lib of the owning PyKCS11Lib


if self.value in CKR:<EOL><INDENT>if self.value < <NUM_LIT:0>:<EOL><INDENT>return CKR[self.value] + "<STR_LIT>" % self.text<EOL><DEDENT>else:<EOL><INDENT>return CKR[self.value] + "<STR_LIT>" % self.value<EOL><DEDENT><DEDENT>elif self.value & CKR_VENDOR_DEFINED:<EOL><INDENT>return "<STR_LIT>" % (self.value & <NUM_LIT> & ~CKR_VENDOR_DEFINED)<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>" % self.value<EOL><DEDENT>

The text representation of a PKCS#11 error is something like:
"CKR_DEVICE_ERROR (0x00000030)"


def getAttributeValue_fragmented(self, obj_id, attr, allAsBinary=False):

<EOL>valTemplate = PyKCS11.LowLevel.ckattrlist(<NUM_LIT:1>)<EOL>res = []<EOL>for x in range(len(attr)):<EOL><INDENT>valTemplate[<NUM_LIT:0>].Reset()<EOL>valTemplate[<NUM_LIT:0>].SetType(attr[x])<EOL>rv = self.lib.C_GetAttributeValue(self.session, obj_id,<EOL>valTemplate)<EOL>if rv in (CKR_ATTRIBUTE_TYPE_INVALID,<EOL>CKR_ATTRIBUTE_SENSITIVE, CKR_ARGUMENTS_BAD):<EOL><INDENT>res.append(None)<EOL>continue<EOL><DEDENT>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>rv = self.lib.C_GetAttributeValue(self.session, obj_id,<EOL>valTemplate)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>if allAsBinary:<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetBin())<EOL><DEDENT>elif valTemplate[<NUM_LIT:0>].IsNum():<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetNum())<EOL><DEDENT>elif valTemplate[<NUM_LIT:0>].IsBool():<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetBool())<EOL><DEDENT>elif valTemplate[<NUM_LIT:0>].IsString():<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetString())<EOL><DEDENT>elif valTemplate[<NUM_LIT:0>].IsBin():<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetBin())<EOL><DEDENT>elif valTemplate[<NUM_LIT:0>].IsAttributeList():<EOL><INDENT>res.append(valTemplate[<NUM_LIT:0>].GetBin())<EOL><DEDENT>else:<EOL><INDENT>raise PyKCS11Error(-<NUM_LIT:2>)<EOL><DEDENT><DEDENT>return res<EOL>

Same as :func:`getAttributeValue` except that when some attribute
is sensitive or unknown an empty value (None) is returned.

Note: this is achived by getting attributes one by one.

:see: :func:`getAttributeValue`


def __init__(self, hashAlg, mgf, label=None):

self._param = PyKCS11.LowLevel.CK_RSA_PKCS_OAEP_PARAMS()<EOL>self._param.hashAlg = hashAlg<EOL>self._param.mgf = mgf<EOL>self._source = None<EOL>self._param.src = CKZ_DATA_SPECIFIED<EOL>if label:<EOL><INDENT>self._source = ckbytelist(label)<EOL>self._param.ulSourceDataLen = len(self._source)<EOL><DEDENT>else:<EOL><INDENT>self._param.ulSourceDataLen = <NUM_LIT:0><EOL><DEDENT>self._param.pSourceData = self._source<EOL>self._mech = PyKCS11.LowLevel.CK_MECHANISM()<EOL>self._mech.mechanism = CKM_RSA_PKCS_OAEP<EOL>self._mech.pParameter = self._param<EOL>self._mech.ulParameterLen = PyKCS11.LowLevel.CK_RSA_PKCS_OAEP_PARAMS_LENGTH<EOL>

:param hashAlg: the hash algorithm to use (like `CKM_SHA256`)
:param mgf: the mask generation function to use (like
  `CKG_MGF1_SHA256`)
:param label: the (optional) label to use


low_rand = ckbytelist([<NUM_LIT:0>] * size)<EOL>rv = self.lib.C_GenerateRandom(self.session, low_rand)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>return low_rand<EOL>

C_GenerateRandom

:param size: number of random bytes to get
:type size: integer

:note: the returned value is an instance of :class:`ckbytelist`.
  You can easly convert it to a binary string with:
  ``bytes(random)``
  or, for Python 2:
  ``''.join(chr(i) for i in random)``


def unwrapKey(self, unwrappingKey, wrappedKey, template,<EOL>mecha=MechanismRSAPKCS1):

m = mecha.to_native()<EOL>data1 = ckbytelist(wrappedKey)<EOL>handle = PyKCS11.LowLevel.CK_OBJECT_HANDLE()<EOL>attrs = self._template2ckattrlist(template)<EOL>rv = self.lib.C_UnwrapKey(self.session, m, unwrappingKey,<EOL>data1, attrs, handle)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>return handle<EOL>

C_UnwrapKey

:param unwrappingKey: the unwrapping key handle
:type unwrappingKey: integer
:param wrappedKey: the bytes of the wrapped key
:type wrappedKey:  (binary) string or list/tuple of bytes
:param template: template for the unwrapped key
:param mecha: the decrypt mechanism to be used (use
  `MechanismRSAPKCS1` for `CKM_RSA_PKCS`)
:type mecha: :class:`Mechanism`
:return: the unwrapped key object
:rtype: integer


return (not self.isBool(type))and (not self.isString(type))and (not self.isNum(type))<EOL>

is the type a byte array value?

:param type: PKCS#11 type like `CKA_MODULUS`
:rtype: bool


def wrapKey(self, wrappingKey, key, mecha=MechanismRSAPKCS1):

wrapped = ckbytelist()<EOL>native = mecha.to_native()<EOL>rv = self.lib.C_WrapKey(self.session, native, wrappingKey, key,<EOL>wrapped)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>rv = self.lib.C_WrapKey(self.session, native, wrappingKey, key,<EOL>wrapped)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>return wrapped<EOL>

C_WrapKey

:param wrappingKey: a wrapping key handle
:type wrappingKey: integer
:param key: a handle of the key to be wrapped
:type key: integer
:param mecha: the encrypt mechanism to be used
  (use `MechanismRSAPKCS1` for `CKM_RSA_PKCS`)
:type mecha: :class:`Mechanism`
:return: the wrapped key bytes
:rtype: list of bytes

:note: the returned value is an instance of :class:`ckbytelist`.
  You can easily convert it to a binary string with:
  ``bytes(ckbytelistData)``
  or, for Python 2:
  ``''.join(chr(i) for i in ckbytelistData)``


tokeninfo = PyKCS11.LowLevel.CK_TOKEN_INFO()<EOL>rv = self.lib.C_GetTokenInfo(slot, tokeninfo)<EOL>if rv != CKR_OK:<EOL><INDENT>raise PyKCS11Error(rv)<EOL><DEDENT>t = CK_TOKEN_INFO()<EOL>t.label = tokeninfo.GetLabel()<EOL>t.manufacturerID = tokeninfo.GetManufacturerID()<EOL>t.model = tokeninfo.GetModel()<EOL>t.serialNumber = tokeninfo.GetSerialNumber()<EOL>t.flags = tokeninfo.flags<EOL>t.ulMaxSessionCount = tokeninfo.ulMaxSessionCount<EOL>if t.ulMaxSessionCount == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulMaxSessionCount = -<NUM_LIT:1><EOL><DEDENT>t.ulSessionCount = tokeninfo.ulSessionCount<EOL>if t.ulSessionCount == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulSessionCount = -<NUM_LIT:1><EOL><DEDENT>t.ulMaxRwSessionCount = tokeninfo.ulMaxRwSessionCount<EOL>if t.ulMaxRwSessionCount == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulMaxRwSessionCount = -<NUM_LIT:1><EOL><DEDENT>t.ulRwSessionCount = tokeninfo.ulRwSessionCount<EOL>if t.ulRwSessionCount == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulRwSessionCount = -<NUM_LIT:1><EOL><DEDENT>t.ulMaxPinLen = tokeninfo.ulMaxPinLen<EOL>t.ulMinPinLen = tokeninfo.ulMinPinLen<EOL>t.ulTotalPublicMemory = tokeninfo.ulTotalPublicMemory<EOL>if t.ulTotalPublicMemory == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulTotalPublicMemory = -<NUM_LIT:1><EOL><DEDENT>t.ulFreePublicMemory = tokeninfo.ulFreePublicMemory<EOL>if t.ulFreePublicMemory == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulFreePublicMemory = -<NUM_LIT:1><EOL><DEDENT>t.ulTotalPrivateMemory = tokeninfo.ulTotalPrivateMemory<EOL>if t.ulTotalPrivateMemory == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulTotalPrivateMemory = -<NUM_LIT:1><EOL><DEDENT>t.ulFreePrivateMemory = tokeninfo.ulFreePrivateMemory<EOL>if t.ulFreePrivateMemory == CK_UNAVAILABLE_INFORMATION:<EOL><INDENT>t.ulFreePrivateMemory = -<NUM_LIT:1><EOL><DEDENT>t.hardwareVersion = (tokeninfo.hardwareVersion.major,<EOL>tokeninfo.hardwareVersion.minor)<EOL>t.firmwareVersion = (tokeninfo.firmwareVersion.major,<EOL>tokeninfo.firmwareVersion.minor)<EOL>t.utcTime = tokeninfo.GetUtcTime().replace('<STR_LIT>', '<STR_LIT:U+0020>')<EOL>return t<EOL>

C_GetTokenInfo

:param slot: slot number returned by :func:`getSlotList`
:type slot: integer
:return: a :class:`CK_TOKEN_INFO` object


def __init__(self, mecha, hashAlg, mgf, sLen):

self._param = PyKCS11.LowLevel.CK_RSA_PKCS_PSS_PARAMS()<EOL>self._param.hashAlg = hashAlg<EOL>self._param.mgf = mgf<EOL>self._param.sLen = sLen<EOL>self._mech = PyKCS11.LowLevel.CK_MECHANISM()<EOL>self._mech.mechanism = mecha<EOL>self._mech.pParameter = self._param<EOL>self._mech.ulParameterLen = PyKCS11.LowLevel.CK_RSA_PKCS_PSS_PARAMS_LENGTH<EOL>

:param mecha: the mechanism to use (like
  `CKM_SHA384_RSA_PKCS_PSS`)
:param hashAlg: the hash algorithm to use (like `CKM_SHA384`)
:param mgf: the mask generation function to use (like
  `CKG_MGF1_SHA384`)
:param sLen: length, in bytes, of the salt value used in the PSS
  encoding (like 0 or the message length)


if self._oid_set[<NUM_LIT:0>]:<EOL><INDENT>oid_ptr = None<EOL>if isinstance(new_oid, OID):<EOL><INDENT>oid_ptr = ffi.addressof(new_oid._oid)<EOL><DEDENT>elif isinstance(new_oid, ffi.CData):<EOL><INDENT>if ffi.typeof(new_oid) == ffi.typeof('<STR_LIT>'):<EOL><INDENT>oid_ptr = ffi.addressof(new_oid)<EOL><DEDENT>elif ffi.typeof(new_oid) == ffi.typeof('<STR_LIT>'):<EOL><INDENT>oid_ptr = new_oid<EOL><DEDENT><DEDENT>if oid_ptr is None:<EOL><INDENT>raise TypeError("<STR_LIT>" + str(type(new_oid)))<EOL><DEDENT>minor_status = ffi.new('<STR_LIT>')<EOL>retval = C.gss_add_oid_set_member(minor_status, oid_ptr, self._oid_set)<EOL>if GSS_ERROR(retval):<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise GSSException("<STR_LIT>")<EOL><DEDENT>

Adds another :class:`OID` to this set.

:param new_oid: the OID to add.
:type new_oid: :class:`OID`


super(OIDSet, self).__init__()<EOL>if isinstance(oid_set, ffi.CData) and ffi.typeof(oid_set) == ffi.typeof('<STR_LIT>'):<EOL><INDENT>self._oid_set = ffi.gc(oid_set, _release_OID_set)<EOL><DEDENT>elif oid_set is None:<EOL><INDENT>self._oid_set = ffi.new('<STR_LIT>')<EOL>minor_status = ffi.new('<STR_LIT>')<EOL>try:<EOL><INDENT>retval = C.gss_create_empty_oid_set(minor_status, self._oid_set)<EOL>if GSS_ERROR(retval):<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>])<EOL><DEDENT>self._oid_set = ffi.gc(self._oid_set, _release_OID_set)<EOL><DEDENT>except:<EOL><INDENT>_release_OID_set(self._oid_set)<EOL>raise<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>" + str(type(oid_set)))<EOL><DEDENT>

Wraps a gss_OID_set. This can be returned from methods like gss_inquire_cred
        where it shouldn't be modified by the caller, since it's immutable.


minor_status = ffi.new('<STR_LIT>')<EOL>mech_set = ffi.new('<STR_LIT>')<EOL>try:<EOL><INDENT>retval = C.gss_indicate_mechs(minor_status, mech_set)<EOL>if GSS_ERROR(retval):<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>except:<EOL><INDENT>_release_OID_set(mech_set)<EOL>raise<EOL><DEDENT>return OIDSet(oid_set=mech_set)<EOL>

Return an :class:`OIDSet` of all the mechanisms supported by the underlying GSSAPI
implementation.


return self._inquire(False, True, False, False)[<NUM_LIT:1>]<EOL>

The lifetime in seconds for which this credential is valid.


def verify_mic(self, message, mic, supplementary=False):

if not (self.flags & C.GSS_C_INTEG_FLAG):<EOL><INDENT>raise GSSException("<STR_LIT>")<EOL><DEDENT>if not (self.established or (self.flags & C.GSS_C_PROT_READY_FLAG)):<EOL><INDENT>raise GSSException("<STR_LIT>")<EOL><DEDENT>minor_status = ffi.new('<STR_LIT>')<EOL>message_buffer = ffi.new('<STR_LIT>')<EOL>message_buffer[<NUM_LIT:0>].length = len(message)<EOL>c_str_message = ffi.new('<STR_LIT>', message)<EOL>message_buffer[<NUM_LIT:0>].value = c_str_message<EOL>mic_buffer = ffi.new('<STR_LIT>')<EOL>mic_buffer[<NUM_LIT:0>].length = len(mic)<EOL>c_str_mic = ffi.new('<STR_LIT>', mic)<EOL>mic_buffer[<NUM_LIT:0>].value = c_str_mic<EOL>qop_state = ffi.new('<STR_LIT>')<EOL>retval = C.gss_verify_mic(<EOL>minor_status,<EOL>self._ctx[<NUM_LIT:0>],<EOL>message_buffer,<EOL>mic_buffer,<EOL>qop_state<EOL>)<EOL>if GSS_ERROR(retval):<EOL><INDENT>if minor_status[<NUM_LIT:0>] and self.mech_type:<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>], self.mech_type)<EOL><DEDENT>else:<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>])<EOL><DEDENT><DEDENT>supp_bits = _status_bits(retval)<EOL>if supplementary:<EOL><INDENT>return qop_state[<NUM_LIT:0>], supp_bits<EOL><DEDENT>elif len(supp_bits) > <NUM_LIT:0>:<EOL><INDENT>raise _exception_for_status(retval, minor_status[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>return qop_state[<NUM_LIT:0>]<EOL><DEDENT>

Takes a message integrity code (MIC) that has been generated by the peer application for a
given message, and verifies it against a message, using this security context's
cryptographic keys.

The `supplementary` parameter determines how this method deals with replayed, unsequential,
too-old or missing tokens, as follows:

If the `supplementary` parameter is False (the default), and if a replayed or otherwise
out-of-sequence token is detected, this method raises a :exc:`~gssapi.error.GSSCException`.
If no replay or out-of-sequence token is detected, this method does not raise an exception
and returns the ``qop_state`` only.

If `supplementary` is True, instead of raising an exception when a replayed or
out-of-sequence token is detected, this method returns a tuple
``(qop_state, supplementary_info)`` where ``supplementary_info`` is a tuple containing zero
or more of the constants :const:`~gssapi.S_DUPLICATE_TOKEN`, :const:`~gssapi.S_OLD_TOKEN`,
:const:`~gssapi.S_UNSEQ_TOKEN` and :const:`~gssapi.S_GAP_TOKEN`. The supplementary info
tells the caller whether a replayed or out-of-sequence message was detected. The caller
must check this and decide how to handle the message if any of the flags are set. For a
reference to the meaning of the flags, check
`RFC 2744 Section 3.9.1 <http://tools.ietf.org/html/rfc2744#section-3.9.1>` for the
corresponding GSS_S_OLD_TOKEN, etc, constants.

:param message: The message the MIC was calculated for
:type message: bytes
:param mic: The MIC calculated by the peer
:type mic: bytes
:param supplementary: Whether to also return supplementary info.
:type supplementary: bool
:returns: ``qop_state`` if `supplementary` is False, or ``(qop_state,
    supplementary_info)`` if `supplementary` is True.
:raises: :exc:`~gssapi.error.GSSException` if :attr:`integrity_negotiated` is false, or
    :exc:`~gssapi.error.GSSCException` if the verification fails indicating the message was
    modified, replayed or out-of-sequence.


return bool(self.flags & C.GSS_C_TRANS_FLAG)<EOL>

True if the context can be transferred between processes using :meth:`export` and
:meth:`imprt`, False otherwise.


return bool(self.flags & C.GSS_C_ANON_FLAG)<EOL>

After :meth:`step` has been called, this property will be set to
True if the initiator did not reveal its name to the acceptor, False if the initiator's
name is revealed and authenticated to the acceptor. If True, the
:attr:`~AcceptContext.peer_name` property of the :class:`AcceptContext` will be an
anonymous internal name.


options = _parse_args()<EOL>tarball = download_setuptools(download_base=options.download_base,<EOL>downloader_factory=options.downloader_factory)<EOL>return _install(tarball, _build_install_args(options))<EOL>

Install or upgrade setuptools and EasyInstall


install_args = []<EOL>if options.user_install:<EOL><INDENT>if sys.version_info < (<NUM_LIT:2>, <NUM_LIT:6>):<EOL><INDENT>log.warn("<STR_LIT>")<EOL>raise SystemExit(<NUM_LIT:1>)<EOL><DEDENT>install_args.append('<STR_LIT>')<EOL><DEDENT>return install_args<EOL>

Build the arguments to 'python setup.py install' on the setuptools package


parser = optparse.OptionParser()<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', metavar="<STR_LIT>",<EOL>default=DEFAULT_URL,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT>',<EOL>const=lambda: download_file_insecure, default=get_best_downloader,<EOL>help='<STR_LIT>'<EOL>)<EOL>options, args = parser.parse_args()<EOL>return options<EOL>


def parse_token_stream(stream, soft_delimiter, hard_delimiter):

return [<EOL>[sum(len(token) for token in sentence_it) <EOL>for sentence_it in split_at(block_it, soft_delimiter)]<EOL>for block_it in split_at(stream, hard_delimiter)]<EOL>

Parses a stream of tokens and splits it into sentences (using C{soft_delimiter} tokens)
    and blocks (using C{hard_delimiter} tokens) for use with the L{align_texts} function.


def _chunk_iterator(first):<EOL><INDENT>v = first<EOL>while v != split_value:<EOL><INDENT>yield v<EOL>v = next(it)<EOL><DEDENT><DEDENT>while True:<EOL><INDENT>yield _chunk_iterator(next(it))<EOL><DEDENT>

Splits an iterator C{it} at values of C{split_value}.

    Each instance of C{split_value} is swallowed. The iterator produces
    subiterators which need to be consumed fully before the next subiterator
    can be used.


document_lines = []<EOL>for variant in variants:<EOL><INDENT>line = []<EOL>position = variant.get('<STR_LIT>')<EOL>change = '<STR_LIT:>>'.join([variant.get('<STR_LIT>'),variant.get('<STR_LIT>')])<EOL>line.append(position)<EOL>line.append(change)<EOL>line.append(str(position)+change)<EOL>genes = []<EOL>prot_effect = []<EOL>for gene in variant.get('<STR_LIT>'):<EOL><INDENT>genes.append(gene.get('<STR_LIT>','<STR_LIT>'))<EOL>for transcript in gene.get('<STR_LIT>'):<EOL><INDENT>if transcript.get('<STR_LIT>') and transcript.get('<STR_LIT>'):<EOL><INDENT>prot_effect.append(urllib.parse.unquote(transcript.get('<STR_LIT>')))<EOL><DEDENT><DEDENT><DEDENT>line.append('<STR_LIT:U+002C>'.join(prot_effect))<EOL>line.append('<STR_LIT:U+002C>'.join(genes))<EOL>ref_ad = '<STR_LIT>'<EOL>alt_ad = '<STR_LIT>'<EOL>for sample in variant['<STR_LIT>']:<EOL><INDENT>if sample.get('<STR_LIT>') == sample_id:<EOL><INDENT>ref_ad = sample['<STR_LIT>'][<NUM_LIT:0>]<EOL>alt_ad = sample['<STR_LIT>'][<NUM_LIT:1>]<EOL><DEDENT><DEDENT>line.append(ref_ad)<EOL>line.append(alt_ad)<EOL>document_lines.append(line)<EOL><DEDENT>return document_lines<EOL>

Export mitochondrial variants for a case to create a MT excel report

    Args:
        variants(list): all MT variants for a case, sorted by position
        sample_id(str) : the id of a sample within the case

    Returns:
        document_lines(list): list of lines to include in the document


def export_panels(adapter, panels, versions=None, build='<STR_LIT>'):

if versions and (len(versions) != len(panels)):<EOL><INDENT>raise SyntaxError("<STR_LIT>")<EOL><DEDENT>headers = []<EOL>build_string = ("<STR_LIT>")<EOL>headers.append(build_string.format(build))<EOL>header_string = ("<STR_LIT>")<EOL>contig_string = ("<STR_LIT>")<EOL>bed_string = ("<STR_LIT>")<EOL>panel_geneids = set()<EOL>chromosomes_found = set()<EOL>hgnc_geneobjs = []<EOL>for i,panel_id in enumerate(panels):<EOL><INDENT>version = None<EOL>if versions:<EOL><INDENT>version = versions[i]<EOL><DEDENT>panel_obj = adapter.gene_panel(panel_id, version=version)<EOL>if not panel_obj:<EOL><INDENT>LOG.warning("<STR_LIT>".format(panel_id, version))<EOL>continue<EOL><DEDENT>headers.append(header_string.format(<EOL>panel_obj['<STR_LIT>'],<EOL>panel_obj['<STR_LIT:version>'],<EOL>panel_obj['<STR_LIT:date>'].date(),<EOL>panel_obj['<STR_LIT>'],<EOL>))<EOL>for gene_obj in panel_obj['<STR_LIT>']:<EOL><INDENT>panel_geneids.add(gene_obj['<STR_LIT>'])<EOL><DEDENT><DEDENT>gene_objs = adapter.hgncid_to_gene(build=build)<EOL>for hgnc_id in panel_geneids:<EOL><INDENT>hgnc_geneobj = gene_objs.get(hgnc_id)<EOL>if hgnc_geneobj is None:<EOL><INDENT>LOG.warn("<STR_LIT>", hgnc_id)<EOL>continue<EOL><DEDENT>chrom = hgnc_geneobj['<STR_LIT>']<EOL>start = hgnc_geneobj['<STR_LIT:start>']<EOL>chrom_int = CHROMOSOME_INTEGERS.get(chrom)<EOL>if not chrom_int:<EOL><INDENT>LOG.warn("<STR_LIT>", chrom)<EOL>continue<EOL><DEDENT>hgnc_geneobjs.append((chrom_int, start, hgnc_geneobj))<EOL>chromosomes_found.add(chrom)<EOL><DEDENT>hgnc_geneobjs.sort(key=lambda tup: (tup[<NUM_LIT:0>], tup[<NUM_LIT:1>]))<EOL>for chrom in CHROMOSOMES:<EOL><INDENT>if chrom in chromosomes_found:<EOL><INDENT>headers.append(contig_string.format(chrom))<EOL><DEDENT><DEDENT>headers.append("<STR_LIT>")<EOL>for header in headers:<EOL><INDENT>yield header<EOL><DEDENT>for hgnc_gene in hgnc_geneobjs:<EOL><INDENT>gene_obj = hgnc_gene[-<NUM_LIT:1>]<EOL>gene_line = bed_string.format(gene_obj['<STR_LIT>'], gene_obj['<STR_LIT:start>'],<EOL>gene_obj['<STR_LIT:end>'], gene_obj['<STR_LIT>'],<EOL>gene_obj['<STR_LIT>'])<EOL>yield gene_line<EOL><DEDENT>

Export all genes in gene panels

    Exports the union of genes in one or several gene panels to a bed like format with coordinates.

    Args:
        adapter(scout.adapter.MongoAdapter)
        panels(iterable(str)): Iterable with panel ids
        bed(bool): If lines should be bed formated


LOG.info("<STR_LIT>")<EOL>for gene_obj in adapter.all_genes(build=build):<EOL><INDENT>yield gene_obj<EOL><DEDENT>


try:<EOL><INDENT>import smtplib<EOL>try:<EOL><INDENT>from email.utils import formatdate<EOL><DEDENT>except ImportError:<EOL><INDENT>formatdate = self.date_time<EOL><DEDENT>port = self.mailport<EOL>if not port:<EOL><INDENT>port = smtplib.SMTP_PORT<EOL><DEDENT>smtp = smtplib.SMTP(self.mailhost, port)<EOL>msg = self.format(record)<EOL>msg = "<STR_LIT>" % (<EOL>self.fromaddr,<EOL>'<STR_LIT:U+002C>'.join(self.toaddrs),<EOL>self.getSubject(record),<EOL>formatdate(), msg<EOL>)<EOL>if self.username:<EOL><INDENT>smtp.ehlo()  <EOL>smtp.starttls()  <EOL>smtp.ehlo()  <EOL>smtp.login(self.username, self.password)<EOL><DEDENT>smtp.sendmail(self.fromaddr, self.toaddrs, msg)<EOL>smtp.quit()<EOL><DEDENT>except (KeyboardInterrupt, SystemExit):<EOL><INDENT>raise<EOL><DEDENT>except:<EOL><INDENT>self.handleError(record)<EOL><DEDENT>

Emit a record.
        Format the record and send it to the specified addressees.


def build_variant(variant, institute_id, gene_to_panels = None,<EOL>hgncid_to_gene=None, sample_info=None):

gene_to_panels = gene_to_panels or {}<EOL>hgncid_to_gene = hgncid_to_gene or {}<EOL>sample_info = sample_info or {}<EOL>variant_obj = dict(<EOL>_id = variant['<STR_LIT>']['<STR_LIT>'],<EOL>document_id=variant['<STR_LIT>']['<STR_LIT>'],<EOL>variant_id=variant['<STR_LIT>']['<STR_LIT>'],<EOL>display_name=variant['<STR_LIT>']['<STR_LIT>'],<EOL>variant_type=variant['<STR_LIT>'],<EOL>case_id=variant['<STR_LIT>'],<EOL>chromosome=variant['<STR_LIT>'],<EOL>reference=variant['<STR_LIT>'],<EOL>alternative=variant['<STR_LIT>'],<EOL>institute=institute_id,<EOL>)<EOL>variant_obj['<STR_LIT>'] = False<EOL>variant_obj['<STR_LIT>'] = int(variant['<STR_LIT>'])<EOL>variant_obj['<STR_LIT>'] = float(variant['<STR_LIT>'])<EOL>end = variant.get('<STR_LIT:end>')<EOL>if end:<EOL><INDENT>variant_obj['<STR_LIT:end>'] = int(end)<EOL><DEDENT>length = variant.get('<STR_LIT>')<EOL>if length:<EOL><INDENT>variant_obj['<STR_LIT>'] = int(length)<EOL><DEDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>'].get('<STR_LIT>')<EOL>variant_obj['<STR_LIT>'] = float(variant['<STR_LIT>']) if variant['<STR_LIT>'] else None<EOL>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL>variant_obj['<STR_LIT>'] = variant.get('<STR_LIT>')<EOL>variant_obj['<STR_LIT>'] = variant.get('<STR_LIT>')<EOL>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL>variant_obj['<STR_LIT>'] = variant.get('<STR_LIT>')<EOL>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>gt_types = []<EOL>for sample in variant.get('<STR_LIT>', []):<EOL><INDENT>gt_call = build_genotype(sample)<EOL>gt_types.append(gt_call)<EOL>if sample_info:<EOL><INDENT>sample_id = sample['<STR_LIT>']<EOL>if sample_info[sample_id] == '<STR_LIT>':<EOL><INDENT>key = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>key = '<STR_LIT>'<EOL><DEDENT>variant_obj[key] = {<EOL>'<STR_LIT>': sample['<STR_LIT>'],<EOL>'<STR_LIT>': sample['<STR_LIT>'],<EOL>'<STR_LIT>': sample['<STR_LIT>'],<EOL>'<STR_LIT>': sample['<STR_LIT>'],<EOL>'<STR_LIT>': sample_id<EOL>}<EOL><DEDENT><DEDENT>variant_obj['<STR_LIT>'] = gt_types<EOL>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>compounds = []<EOL>for compound in variant.get('<STR_LIT>', []):<EOL><INDENT>compound_obj = build_compound(compound)<EOL>compounds.append(compound_obj)<EOL><DEDENT>if compounds:<EOL><INDENT>variant_obj['<STR_LIT>'] = compounds<EOL><DEDENT>genes = []<EOL>for index, gene in enumerate(variant.get('<STR_LIT>', [])):<EOL><INDENT>if gene.get('<STR_LIT>'):<EOL><INDENT>gene_obj = build_gene(gene, hgncid_to_gene)<EOL>genes.append(gene_obj)<EOL>if index > <NUM_LIT:30>:<EOL><INDENT>variant_obj['<STR_LIT>'] = True<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if genes:<EOL><INDENT>variant_obj['<STR_LIT>'] = genes<EOL><DEDENT>if '<STR_LIT>' in variant:<EOL><INDENT>variant_obj['<STR_LIT>'] = [hgnc_id for hgnc_id in variant['<STR_LIT>'] if hgnc_id]<EOL><DEDENT>hgnc_symbols = []<EOL>for hgnc_id in variant_obj['<STR_LIT>']:<EOL><INDENT>gene_obj = hgncid_to_gene.get(hgnc_id)<EOL>if gene_obj:<EOL><INDENT>hgnc_symbols.append(gene_obj['<STR_LIT>'])<EOL><DEDENT><DEDENT>if hgnc_symbols:<EOL><INDENT>variant_obj['<STR_LIT>'] = hgnc_symbols<EOL><DEDENT>panel_names = set()<EOL>for hgnc_id in variant_obj['<STR_LIT>']:<EOL><INDENT>gene_panels = gene_to_panels.get(hgnc_id, set())<EOL>panel_names = panel_names.union(gene_panels)<EOL><DEDENT>if panel_names:<EOL><INDENT>variant_obj['<STR_LIT>'] = list(panel_names)<EOL><DEDENT>clnsig_objects = []<EOL>for entry in variant.get('<STR_LIT>', []):<EOL><INDENT>clnsig_obj = build_clnsig(entry)<EOL>clnsig_objects.append(clnsig_obj)<EOL><DEDENT>if clnsig_objects:<EOL><INDENT>variant_obj['<STR_LIT>'] = clnsig_objects<EOL><DEDENT>call_info = variant.get('<STR_LIT>', {})<EOL>for caller in call_info:<EOL><INDENT>if call_info[caller]:<EOL><INDENT>variant_obj[caller] = call_info[caller]<EOL><DEDENT><DEDENT>conservation_info = variant.get('<STR_LIT>', {})<EOL>if conservation_info.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = conservation_info['<STR_LIT>']<EOL><DEDENT>if conservation_info.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = conservation_info['<STR_LIT>']<EOL><DEDENT>if conservation_info.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = conservation_info['<STR_LIT>']<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>frequencies = variant.get('<STR_LIT>', {})<EOL>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = float(frequencies['<STR_LIT>'])<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] =  variant['<STR_LIT>']<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>if frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>elif frequencies.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = frequencies['<STR_LIT>']<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = variant['<STR_LIT>']<EOL><DEDENT>rank_results = []<EOL>for category in variant.get('<STR_LIT>', []):<EOL><INDENT>rank_result = {<EOL>'<STR_LIT>': category,<EOL>'<STR_LIT>': variant['<STR_LIT>'][category]<EOL>}<EOL>rank_results.append(rank_result)<EOL><DEDENT>if rank_results:<EOL><INDENT>variant_obj['<STR_LIT>'] = rank_results<EOL><DEDENT>if variant.get('<STR_LIT>'):<EOL><INDENT>variant_obj['<STR_LIT>'] = True<EOL><DEDENT>return variant_obj<EOL>

Build a variant object based on parsed information

        Args:
            variant(dict)
            institute_id(str)
            gene_to_panels(dict): A dictionary with
                {<hgnc_id>: {
                    'panel_names': [<panel_name>, ..],
                    'disease_associated_transcripts': [<transcript_id>, ..]
                    }
                    .
                    .
                }

            hgncid_to_gene(dict): A dictionary with
                {<hgnc_id>: <hgnc_gene info>
                    .
                    .
                }
            sample_info(dict): A dictionary with info about samples.
                               Strictly for cancer to tell which is tumor

        Returns:
            variant_obj(dict)

        variant = dict(
            # document_id is a md5 string created by institute_genelist_caseid_variantid:
            _id = str, # required, same as document_id
            document_id = str, # required
            # variant_id is a md5 string created by chrom_pos_ref_alt (simple_id)
            variant_id = str, # required
            # display name is variant_id (no md5)
            display_name = str, # required

            # chrom_pos_ref_alt
            simple_id = str,
            # The variant can be either research or clinical.
            # For research variants we display all the available information while
            # the clinical variants have limited annotation fields.
            variant_type = str, # required, choices=('research', 'clinical'))

            category = str, # choices=('sv', 'snv', 'str')
            sub_category = str, # choices=('snv', 'indel', 'del', 'ins', 'dup', 'inv', 'cnv', 'bnd')
            mate_id = str, # For SVs this identifies the other end

            case_id = str, # case_id is a string like owner_caseid
            chromosome = str, # required
            position = int, # required
            end = int, # required
            length = int, # required
            reference = str, # required
            alternative = str, # required

            rank_score = float, # required
            variant_rank = int, # required
            rank_score_results = list, # List if dictionaries
            variant_rank = int, # required

            institute = str, # institute_id, required

            sanger_ordered = bool,
            validation = str, # Sanger validation, choices=('True positive', 'False positive')

            quality = float,
            filters = list, # list of strings
            samples = list, # list of dictionaries that are <gt_calls>
            genetic_models = list, # list of strings choices=GENETIC_MODELS
            compounds = list, # sorted list of <compound> ordering='combined_score'

            genes = list, # list with <gene>
            dbsnp_id = str,

            # Gene ids:
            hgnc_ids = list, # list of hgnc ids (int)
            hgnc_symbols = list, # list of hgnc symbols (str)
            panels = list, # list of panel names that the variant ovelapps

            # Frequencies:
            thousand_genomes_frequency = float,
            thousand_genomes_frequency_left = float,
            thousand_genomes_frequency_right = float,
            exac_frequency = float,
            max_thousand_genomes_frequency = float,
            max_exac_frequency = float,
            local_frequency = float,
            local_obs_old = int,
            local_obs_hom_old = int,
            local_obs_total_old = int, # default=638

            # Predicted deleteriousness:
            cadd_score = float,
            clnsig = list, # list of <clinsig>
            spidex = float,

            missing_data = bool, # default False

            # STR specific information
            str_repid = str, repeat id generally corresponds to gene symbol
            str_ru = str, used e g in PanelApp naming of STRs
            str_ref = int, reference copy number
            str_len = int, number of repeats found in case
            str_status = str, this indicates the severity of the expansion level

            # Callers
            gatk = str, # choices=VARIANT_CALL, default='Not Used'
            samtools = str, # choices=VARIANT_CALL, default='Not Used'
            freebayes = str, # choices=VARIANT_CALL, default='Not Used'

            # Conservation:
            phast_conservation = list, # list of str, choices=CONSERVATION
            gerp_conservation = list, # list of str, choices=CONSERVATION
            phylop_conservation = list, # list of str, choices=CONSERVATION
            # Database options:
            gene_lists = list,
            manual_rank = int, # choices=[0, 1, 2, 3, 4, 5]
            dismiss_variant = list,

            acmg_evaluation = str, # choices=ACMG_TERMS
        )


try:<EOL><INDENT>hpo_id = hpo_info['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError("<STR_LIT>")<EOL><DEDENT>LOG.debug("<STR_LIT>", hpo_id)<EOL>try:<EOL><INDENT>description = hpo_info['<STR_LIT:description>']<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError("<STR_LIT>")<EOL><DEDENT>hpo_obj = HpoTerm(<EOL>hpo_id = hpo_id,<EOL>description = description<EOL>)<EOL>hgnc_ids = hpo_info.get('<STR_LIT>', set())<EOL>if hgnc_ids:<EOL><INDENT>hpo_obj['<STR_LIT>'] = list(hgnc_ids)<EOL><DEDENT>return hpo_obj<EOL>

Build a hpo_term object

    Check that the information is correct and add the correct hgnc ids to the
    array of genes.

        Args:
            hpo_info(dict)

        Returns:
            hpo_obj(scout.models.HpoTerm): A dictionary with hpo information


LOG.info("<STR_LIT>")<EOL>hgnc_gene_lines = fetch_hgnc() <EOL>header = None<EOL>genes_found = <NUM_LIT:0><EOL>for i,line in enumerate(hgnc_gene_lines):<EOL><INDENT>line = line.rstrip()<EOL>if not len(line) > <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>if i == <NUM_LIT:0>:<EOL><INDENT>header = line.split('<STR_LIT:\t>')<EOL>yield line<EOL>continue<EOL><DEDENT>gene = parse_hgnc_line(line, header)<EOL>if not gene:<EOL><INDENT>continue<EOL><DEDENT>hgnc_id = int(gene['<STR_LIT>'])<EOL>if hgnc_id in genes:<EOL><INDENT>genes_found += <NUM_LIT:1><EOL>yield line<EOL><DEDENT><DEDENT>LOG.info("<STR_LIT>", genes_found)<EOL>

Generate lines from a file with reduced hgnc information

    Args:
        genes(dict): A dictionary with hgnc_id as key and hgnc_symbol as value
        outpath(str): Defaults to hgnc_reduced_path

    Yields:
        print_line(str): Lines from the reduced file


chrom = request.args.get('<STR_LIT>')<EOL>if chrom == '<STR_LIT>':<EOL><INDENT>chrom = '<STR_LIT:M>'<EOL><DEDENT>start = request.args.get('<STR_LIT:start>')<EOL>stop = request.args.get('<STR_LIT>')<EOL>locus = "<STR_LIT>".format(chrom,start,stop)<EOL>LOG.debug('<STR_LIT>', locus)<EOL>chromosome_build = request.args.get('<STR_LIT>')<EOL>LOG.debug('<STR_LIT>', chromosome_build)<EOL>samples = request.args.getlist('<STR_LIT>')<EOL>bam_files = request.args.getlist('<STR_LIT>')<EOL>bai_files = request.args.getlist('<STR_LIT>')<EOL>LOG.debug('<STR_LIT>', bam_files)<EOL>display_obj={}<EOL>fastaURL = '<STR_LIT>'<EOL>indexURL = '<STR_LIT>'<EOL>cytobandURL = '<STR_LIT>'<EOL>gene_track_format = '<STR_LIT>'<EOL>gene_track_URL = '<STR_LIT>'<EOL>gene_track_indexURL = '<STR_LIT>'<EOL>if chromosome_build == "<STR_LIT>" or chrom == '<STR_LIT:M>':<EOL><INDENT>fastaURL = '<STR_LIT>'<EOL>indexURL = '<STR_LIT>'<EOL>cytobandURL = '<STR_LIT>'<EOL>gene_track_format = '<STR_LIT>'<EOL>gene_track_URL = '<STR_LIT>'<EOL>gene_track_indexURL = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>fastaURL = '<STR_LIT>'<EOL>indexURL = '<STR_LIT>'<EOL>cytobandURL = '<STR_LIT>'<EOL>gene_track_format = '<STR_LIT>'<EOL>gene_track_URL = '<STR_LIT>'<EOL>gene_track_indexURL = '<STR_LIT>'<EOL><DEDENT>display_obj['<STR_LIT>'] = {<EOL>'<STR_LIT>' : fastaURL,<EOL>'<STR_LIT>' : indexURL,<EOL>'<STR_LIT>' : cytobandURL<EOL>}<EOL>display_obj['<STR_LIT>'] = {<EOL>'<STR_LIT:name>' : '<STR_LIT>',<EOL>'<STR_LIT:type>' : '<STR_LIT>',<EOL>'<STR_LIT>': gene_track_format,<EOL>'<STR_LIT>': '<STR_LIT:file>',<EOL>'<STR_LIT:url>' : gene_track_URL,<EOL>'<STR_LIT>' : gene_track_indexURL,<EOL>'<STR_LIT>' : '<STR_LIT>'<EOL>}<EOL>sample_tracks = []<EOL>counter = <NUM_LIT:0><EOL>for sample in samples:<EOL><INDENT>if bam_files[counter]:<EOL><INDENT>sample_tracks.append({ '<STR_LIT:name>' : sample, '<STR_LIT:url>' : bam_files[counter],<EOL>'<STR_LIT>' : bai_files[counter],<EOL>'<STR_LIT>' : <NUM_LIT>, '<STR_LIT>' : <NUM_LIT>})<EOL><DEDENT>counter += <NUM_LIT:1><EOL><DEDENT>display_obj['<STR_LIT>'] = sample_tracks<EOL>if request.args.get('<STR_LIT>'):<EOL><INDENT>display_obj['<STR_LIT>'] = True<EOL><DEDENT>else:<EOL><INDENT>display_obj['<STR_LIT>'] = False<EOL><DEDENT>return render_template('<STR_LIT>', locus=locus, **display_obj )<EOL>

Visualize BAM alignments using igv.js (https://github.com/igvteam/igv.js)


self.roles = []<EOL>for key, value in user_data.items():<EOL><INDENT>setattr(self, key, value)<EOL><DEDENT>


user_objs = list(store.users())<EOL>total_events = store.user_events().count()<EOL>for user_obj in user_objs:<EOL><INDENT>if user_obj.get('<STR_LIT>'):<EOL><INDENT>user_obj['<STR_LIT>'] = [store.institute(inst_id) for inst_id in user_obj.get('<STR_LIT>')]<EOL><DEDENT>else:<EOL><INDENT>user_obj['<STR_LIT>'] = []<EOL><DEDENT>user_obj['<STR_LIT>'] = store.user_events(user_obj).count()<EOL>user_obj['<STR_LIT>'] = event_rank(user_obj['<STR_LIT>'])<EOL><DEDENT>return dict(<EOL>users=sorted(user_objs, key=lambda user: -user['<STR_LIT>']),<EOL>total_events=total_events,<EOL>)<EOL>

Display a list of all users and which institutes they belong to.


def update_panel(store, panel_name, csv_lines, option):

new_genes= []<EOL>panel_obj = store.gene_panel(panel_name)<EOL>if panel_obj is None:<EOL><INDENT>return None<EOL><DEDENT>try:<EOL><INDENT>new_genes = parse_genes(csv_lines) <EOL><DEDENT>except SyntaxError as error:<EOL><INDENT>flash(error.args[<NUM_LIT:0>], '<STR_LIT>')<EOL>return None<EOL><DEDENT>if option == '<STR_LIT:replace>':<EOL><INDENT>for gene in panel_obj['<STR_LIT>']:<EOL><INDENT>gene['<STR_LIT>'] = gene['<STR_LIT>']<EOL>store.add_pending(panel_obj, gene, action='<STR_LIT>', info=None)<EOL><DEDENT><DEDENT>for new_gene in new_genes:<EOL><INDENT>if not new_gene['<STR_LIT>']:<EOL><INDENT>flash("<STR_LIT>".format(new_gene['<STR_LIT>']),'<STR_LIT>')<EOL>continue<EOL><DEDENT>gene_obj = store.hgnc_gene(new_gene['<STR_LIT>'])<EOL>if gene_obj is None:<EOL><INDENT>flash("<STR_LIT>".format(new_gene['<STR_LIT>'], new_gene['<STR_LIT>']),'<STR_LIT>')<EOL>continue<EOL><DEDENT>if new_gene['<STR_LIT>'] and gene_obj['<STR_LIT>'] != new_gene['<STR_LIT>']:<EOL><INDENT>flash("<STR_LIT>".format(<EOL>gene_obj['<STR_LIT>'], new_gene['<STR_LIT>']), '<STR_LIT>')<EOL><DEDENT>info_data = {<EOL>'<STR_LIT>': new_gene['<STR_LIT>'],<EOL>'<STR_LIT>': new_gene['<STR_LIT>'],<EOL>'<STR_LIT>': new_gene['<STR_LIT>'],<EOL>'<STR_LIT>': new_gene['<STR_LIT>'],<EOL>'<STR_LIT>': new_gene['<STR_LIT>'],<EOL>}<EOL>if option == '<STR_LIT:replace>': <EOL><INDENT>action = '<STR_LIT>'<EOL><DEDENT>else: <EOL><INDENT>existing_genes = {gene['<STR_LIT>'] for gene in panel_obj['<STR_LIT>']}<EOL>action = '<STR_LIT>' if gene_obj['<STR_LIT>'] in existing_genes else '<STR_LIT>'<EOL><DEDENT>store.add_pending(panel_obj, gene_obj, action=action, info=info_data)<EOL><DEDENT>return panel_obj<EOL>

Update an existing gene panel with genes.

    Args:
        store(scout.adapter.MongoAdapter)
        panel_name(str)
        csv_lines(iterable(str)): Stream with genes
        option(str): 'add' or 'replace'

    Returns:
        panel_obj(dict)


@variants_bp.route('<STR_LIT>')<EOL>@templated('<STR_LIT>')<EOL>def sv_variant(institute_id, case_name, variant_id):

data = controllers.sv_variant(store, institute_id, case_name, variant_id)<EOL>return data<EOL>


page = int(request.form.get('<STR_LIT>', <NUM_LIT:1>))<EOL>variant_type = request.args.get('<STR_LIT>', '<STR_LIT>')<EOL>institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>form = SvFiltersForm(request.form)<EOL>default_panels = []<EOL>for panel in case_obj['<STR_LIT>']:<EOL><INDENT>if panel['<STR_LIT>']:<EOL><INDENT>default_panels.append(panel['<STR_LIT>'])<EOL><DEDENT><DEDENT>request.form.get('<STR_LIT>')<EOL>if bool(request.form.get('<STR_LIT>')):<EOL><INDENT>clinical_filter = MultiDict({<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': ['<STR_LIT>','<STR_LIT>'],<EOL>'<STR_LIT>': SEVERE_SO_TERMS,<EOL>'<STR_LIT>': str(institute_obj['<STR_LIT>']),<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': <NUM_LIT:10>,<EOL>'<STR_LIT>': <NUM_LIT:10>,<EOL>'<STR_LIT:size>': <NUM_LIT:100>,<EOL>'<STR_LIT>': default_panels<EOL>})<EOL><DEDENT>if(request.method == "<STR_LIT:POST>"):<EOL><INDENT>if bool(request.form.get('<STR_LIT>')):<EOL><INDENT>form = SvFiltersForm(clinical_filter)<EOL>form.csrf_token = request.args.get('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>form = SvFiltersForm(request.form)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>form = SvFiltersForm(request.args)<EOL><DEDENT>available_panels = case_obj.get('<STR_LIT>', []) + [<EOL>{'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': '<STR_LIT>'}]<EOL>panel_choices = [(panel['<STR_LIT>'], panel['<STR_LIT>'])<EOL>for panel in available_panels]<EOL>form.gene_panels.choices = panel_choices<EOL>if form.data['<STR_LIT>'] == ['<STR_LIT>']:<EOL><INDENT>hpo_symbols = list(set(term_obj['<STR_LIT>'] for term_obj in<EOL>case_obj['<STR_LIT>']))<EOL>form.hgnc_symbols.data = hpo_symbols<EOL><DEDENT>if case_obj['<STR_LIT:status>'] == '<STR_LIT>' and not current_user.is_admin:<EOL><INDENT>flash('<STR_LIT>', '<STR_LIT:info>')<EOL>user_obj = store.user(current_user.email)<EOL>case_link = url_for('<STR_LIT>', institute_id=institute_obj['<STR_LIT>'],<EOL>case_name=case_obj['<STR_LIT>'])<EOL>store.update_status(institute_obj, case_obj, user_obj, '<STR_LIT>', case_link)<EOL><DEDENT>variants_query = store.variants(case_obj['<STR_LIT>'], category='<STR_LIT>',<EOL>query=form.data)<EOL>data = {}<EOL>if request.form.get('<STR_LIT>'):<EOL><INDENT>document_header = controllers.variants_export_header(case_obj)<EOL>export_lines = []<EOL>export_lines = controllers.variant_export_lines(store, case_obj, variants_query.limit(<NUM_LIT>))<EOL>def generate(header, lines):<EOL><INDENT>yield header + '<STR_LIT:\n>'<EOL>for line in lines:<EOL><INDENT>yield line + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>headers = Headers()<EOL>headers.add('<STR_LIT>','<STR_LIT>', filename=str(case_obj['<STR_LIT>'])+'<STR_LIT>')<EOL>return Response(generate("<STR_LIT:U+002C>".join(document_header), export_lines), mimetype='<STR_LIT>', headers=headers) <EOL><DEDENT>else:<EOL><INDENT>data = controllers.sv_variants(store, institute_obj, case_obj,<EOL>variants_query, page)<EOL><DEDENT>return dict(institute=institute_obj, case=case_obj, variant_type=variant_type,<EOL>form=form, severe_so_terms=SEVERE_SO_TERMS, page=page, **data)<EOL>


@variants_bp.route('<STR_LIT>', methods=['<STR_LIT:POST>'])<EOL>def upload_panel(institute_id, case_name):

file = form.symbol_file.data<EOL>if file.filename == '<STR_LIT>':<EOL><INDENT>flash('<STR_LIT>', '<STR_LIT>')<EOL>return redirect(request.referrer)<EOL><DEDENT>try:<EOL><INDENT>stream = io.StringIO(file.stream.read().decode('<STR_LIT:utf-8>'), newline=None)<EOL><DEDENT>except UnicodeDecodeError as error:<EOL><INDENT>flash("<STR_LIT>", '<STR_LIT>')<EOL>return redirect(request.referrer)<EOL><DEDENT>category = request.args.get('<STR_LIT>')<EOL>if(category == '<STR_LIT>'):<EOL><INDENT>form = SvFiltersForm(request.args)<EOL><DEDENT>else:<EOL><INDENT>form = FiltersForm(request.args)<EOL><DEDENT>hgnc_symbols = set(form.hgnc_symbols.data)<EOL>new_hgnc_symbols = controllers.upload_panel(store, institute_id, case_name, stream)<EOL>hgnc_symbols.update(new_hgnc_symbols)<EOL>form.hgnc_symbols.data = '<STR_LIT:U+002C>'.join(hgnc_symbols)<EOL>form.gene_panels.data = '<STR_LIT>'<EOL>if(category == '<STR_LIT>'):<EOL><INDENT>return redirect(url_for('<STR_LIT>', institute_id=institute_id, case_name=case_name,<EOL>**form.data), code=<NUM_LIT>)<EOL><DEDENT>else:<EOL><INDENT>return redirect(url_for('<STR_LIT>', institute_id=institute_id, case_name=case_name,<EOL>**form.data), code=<NUM_LIT>)<EOL><DEDENT>

Parse gene panel file and fill in HGNC symbols for filter.


user_obj = store.user(current_user.email)<EOL>user_institutes = user_obj.get('<STR_LIT>')<EOL>temp_excel_dir = os.path.join(variants_bp.static_folder, '<STR_LIT>')<EOL>os.makedirs(temp_excel_dir, exist_ok=True)<EOL>written_files = controllers.verified_excel_file(store, user_institutes, temp_excel_dir)<EOL>if written_files:<EOL><INDENT>today = datetime.datetime.now().strftime('<STR_LIT>')<EOL>data = io.BytesIO()<EOL>with zipfile.ZipFile(data, mode='<STR_LIT:w>') as z:<EOL><INDENT>for f_name in pathlib.Path(temp_excel_dir).iterdir():<EOL><INDENT>zipfile.ZipFile<EOL>z.write(f_name, os.path.basename(f_name))<EOL><DEDENT><DEDENT>data.seek(<NUM_LIT:0>)<EOL>shutil.rmtree(temp_excel_dir)<EOL>return send_file(<EOL>data,<EOL>mimetype='<STR_LIT>',<EOL>as_attachment=True,<EOL>attachment_filename='<STR_LIT:_>'.join(['<STR_LIT>', '<STR_LIT>', today])+'<STR_LIT>'<EOL>)<EOL><DEDENT>else:<EOL><INDENT>flash("<STR_LIT>", '<STR_LIT>')<EOL>return redirect(request.referrer)<EOL><DEDENT>

Download all verified variants for user's cases


@variants_bp.route('<STR_LIT>')<EOL>@templated('<STR_LIT>')<EOL>def str_variant(institute_id, case_name, variant_id):

data = controllers.str_variant(store, institute_id, case_name, variant_id)<EOL>return data<EOL>


for clinsig_obj in variant_obj['<STR_LIT>']:<EOL><INDENT>if isinstance(clinsig_obj['<STR_LIT>'], int):<EOL><INDENT>link = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>link = "<STR_LIT>"<EOL><DEDENT>human_str = '<STR_LIT>'<EOL>if clinsig_obj.get('<STR_LIT:value>'):<EOL><INDENT>try:<EOL><INDENT>int(clinsig_obj['<STR_LIT:value>'])<EOL>human_str = CLINSIG_MAP.get(clinsig_obj['<STR_LIT:value>'], '<STR_LIT>')<EOL><DEDENT>except ValueError:<EOL><INDENT>human_str = clinsig_obj['<STR_LIT:value>']<EOL><DEDENT><DEDENT>clinsig_obj['<STR_LIT>'] = human_str<EOL>clinsig_obj['<STR_LIT>'] = link.format(clinsig_obj['<STR_LIT>'])<EOL>yield clinsig_obj<EOL><DEDENT>

Convert to human readable version of CLINSIG evaluation.


def variant_acmg(store, institute_id, case_name, variant_id):

institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>variant_obj = store.variant(variant_id)<EOL>return dict(institute=institute_obj, case=case_obj, variant=variant_obj,<EOL>CRITERIA=ACMG_CRITERIA, ACMG_OPTIONS=ACMG_OPTIONS)<EOL>

Collect data relevant for rendering ACMG classification form.


def parse_transcript(gene_obj, tx_obj, build=None):

build = build or <NUM_LIT><EOL>add_tx_links(tx_obj, build)<EOL>if tx_obj.get('<STR_LIT>'):<EOL><INDENT>gene_name = (gene_obj['<STR_LIT>']['<STR_LIT>'] if gene_obj['<STR_LIT>'] else<EOL>gene_obj['<STR_LIT>'])<EOL>tx_obj['<STR_LIT>'] = transcript_str(tx_obj, gene_name)<EOL><DEDENT>


data = {'<STR_LIT>': []}<EOL>for gene_obj in genes:<EOL><INDENT>if not gene_obj.get('<STR_LIT>'):<EOL><INDENT>tx = gene_obj['<STR_LIT>'][<NUM_LIT:0>]<EOL>tx_id = tx['<STR_LIT>']<EOL>exon = tx.get('<STR_LIT>', '<STR_LIT:->')<EOL>c_seq = tx.get('<STR_LIT>', '<STR_LIT:->')<EOL><DEDENT>else:<EOL><INDENT>tx_id = gene_obj['<STR_LIT>']<EOL>exon = gene_obj.get('<STR_LIT>', '<STR_LIT:->')<EOL>c_seq = gene_obj.get('<STR_LIT>', '<STR_LIT:->')<EOL><DEDENT>if len(c_seq) > <NUM_LIT:20>:<EOL><INDENT>c_seq = c_seq[:<NUM_LIT:20>] + '<STR_LIT>'<EOL><DEDENT>if len(genes) == <NUM_LIT:1>:<EOL><INDENT>value = '<STR_LIT::>'.join([tx_id,exon,c_seq])<EOL><DEDENT>else:<EOL><INDENT>gene_id = gene_obj.get('<STR_LIT>') or str(gene_obj['<STR_LIT>'])<EOL>value = '<STR_LIT::>'.join([gene_id, tx_id,exon,c_seq])<EOL><DEDENT>data['<STR_LIT>'].append(value)<EOL><DEDENT>return data<EOL>


most_common_frequency = max(variant_obj.get('<STR_LIT>') or <NUM_LIT:0>,<EOL>variant_obj.get('<STR_LIT>') or <NUM_LIT:0>)<EOL>if most_common_frequency > <NUM_LIT>:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif most_common_frequency > <NUM_LIT>:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

Returns a judgement on the overall frequency of the variant.

    Combines multiple metrics into a single call.


def variant_export_lines(store, case_obj, variants_query):

export_variants = []<EOL>for variant in variants_query:<EOL><INDENT>variant_line = []<EOL>position = variant['<STR_LIT>']<EOL>change = variant['<STR_LIT>']+'<STR_LIT:>>'+variant['<STR_LIT>']<EOL>variant_line.append(variant['<STR_LIT>'])<EOL>variant_line.append(variant['<STR_LIT>'])<EOL>variant_line.append(position)<EOL>variant_line.append(change)<EOL>variant_line.append('<STR_LIT:_>'.join([str(position), change]))<EOL>gene_list = variant.get('<STR_LIT>') <EOL>gene_ids = []<EOL>gene_names = []<EOL>hgvs_c = []<EOL>if len(gene_list) > <NUM_LIT:0>:<EOL><INDENT>for gene_obj in gene_list:<EOL><INDENT>hgnc_id = gene_obj['<STR_LIT>']<EOL>gene_name = gene(store, hgnc_id)['<STR_LIT>']<EOL>gene_ids.append(hgnc_id)<EOL>gene_names.append(gene_name)<EOL>hgvs_nucleotide = '<STR_LIT:->'<EOL>transcripts_list = gene_obj.get('<STR_LIT>')<EOL>for transcript_obj in transcripts_list:<EOL><INDENT>if transcript_obj.get('<STR_LIT>') and transcript_obj.get('<STR_LIT>') is True:<EOL><INDENT>hgvs_nucleotide = str(transcript_obj.get('<STR_LIT>'))<EOL><DEDENT><DEDENT>hgvs_c.append(hgvs_nucleotide)<EOL><DEDENT>variant_line.append('<STR_LIT:;>'.join( str(x) for x in  gene_ids))<EOL>variant_line.append('<STR_LIT:;>'.join( str(x) for x in  gene_names))<EOL>variant_line.append('<STR_LIT:;>'.join( str(x) for x in  hgvs_c))<EOL><DEDENT>else:<EOL><INDENT>while i < <NUM_LIT:4>:<EOL><INDENT>variant_line.append('<STR_LIT:->') <EOL>i = i+<NUM_LIT:1><EOL><DEDENT><DEDENT>variant_gts = variant['<STR_LIT>'] <EOL>for individual in case_obj['<STR_LIT>']:<EOL><INDENT>for variant_gt in variant_gts:<EOL><INDENT>if individual['<STR_LIT>'] == variant_gt['<STR_LIT>']:<EOL><INDENT>variant_line.append(variant_gt['<STR_LIT>'][<NUM_LIT:0>]) <EOL>variant_line.append(variant_gt['<STR_LIT>'][<NUM_LIT:1>]) <EOL>variant_line.append(variant_gt['<STR_LIT>'])<EOL><DEDENT><DEDENT><DEDENT>variant_line = [str(i) for i in variant_line]<EOL>export_variants.append("<STR_LIT:U+002C>".join(variant_line))<EOL><DEDENT>return export_variants<EOL>

Get variants info to be exported to file, one list (line) per variant.

        Args:
            store(scout.adapter.MongoAdapter)
            case_obj(scout.models.Case)
            variants_query: a list of variant objects, each one is a dictionary

        Returns:
            export_variants: a list of strings. Each string  of the list corresponding to the fields
                             of a variant to be exported to file, separated by comma


def clinvar_export(store, institute_id, case_name, variant_id):

institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>pinned = [store.variant(variant_id) or variant_id for variant_id in<EOL>case_obj.get('<STR_LIT>', [])]<EOL>variant_obj = store.variant(variant_id)<EOL>return dict(<EOL>today = str(date.today()),<EOL>institute=institute_obj,<EOL>case=case_obj,<EOL>variant=variant_obj,<EOL>pinned_vars=pinned<EOL>)<EOL>

Gather the required data for creating the clinvar submission form

        Args:
            store(scout.adapter.MongoAdapter)
            institute_id(str): Institute ID
            case_name(str): case ID
            variant_id(str): variant._id

        Returns:
            a dictionary with all the required data (case and variant level) to pre-fill in fields in the clinvar submission form


def parse_variant(store, institute_obj, case_obj, variant_obj, update=False, genome_build='<STR_LIT>',<EOL>get_compounds = True):

has_changed = False<EOL>compounds = variant_obj.get('<STR_LIT>', [])<EOL>if compounds and get_compounds:<EOL><INDENT>if '<STR_LIT>' not in compounds[<NUM_LIT:0>]:<EOL><INDENT>new_compounds = store.update_variant_compounds(variant_obj)<EOL>variant_obj['<STR_LIT>'] = new_compounds<EOL>has_changed = True<EOL><DEDENT>variant_obj['<STR_LIT>'] = sorted(variant_obj['<STR_LIT>'],<EOL>key=lambda compound: -compound['<STR_LIT>'])<EOL><DEDENT>variant_genes = variant_obj.get('<STR_LIT>')<EOL>if variant_genes is not None:<EOL><INDENT>for gene_obj in variant_genes:<EOL><INDENT>if not gene_obj['<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>if gene_obj.get('<STR_LIT>') is None:<EOL><INDENT>hgnc_gene = store.hgnc_gene(gene_obj['<STR_LIT>'], build=genome_build)<EOL>if not hgnc_gene:<EOL><INDENT>continue<EOL><DEDENT>has_changed = True<EOL>gene_obj['<STR_LIT>'] = hgnc_gene['<STR_LIT>']<EOL><DEDENT><DEDENT><DEDENT>if update and has_changed:<EOL><INDENT>variant_obj = store.update_variant(variant_obj)<EOL><DEDENT>variant_obj['<STR_LIT>'] = store.events(institute_obj, case=case_obj,<EOL>variant_id=variant_obj['<STR_LIT>'], comments=True)<EOL>if variant_genes:<EOL><INDENT>variant_obj.update(get_predictions(variant_genes))<EOL>if variant_obj.get('<STR_LIT>') == '<STR_LIT>':<EOL><INDENT>variant_obj.update(get_variant_info(variant_genes))<EOL><DEDENT><DEDENT>for compound_obj in compounds:<EOL><INDENT>compound_obj.update(get_predictions(compound_obj.get('<STR_LIT>', [])))<EOL><DEDENT>if isinstance(variant_obj.get('<STR_LIT>'), int):<EOL><INDENT>acmg_code = ACMG_MAP[variant_obj['<STR_LIT>']]<EOL>variant_obj['<STR_LIT>'] = ACMG_COMPLETE_MAP[acmg_code]<EOL><DEDENT>variant_length = variant_obj.get('<STR_LIT>')<EOL>variant_obj['<STR_LIT>'] = {<NUM_LIT>: '<STR_LIT>', -<NUM_LIT:1>: '<STR_LIT>'}.get(variant_length, variant_length)<EOL>if not '<STR_LIT>' in variant_obj:<EOL><INDENT>variant_obj['<STR_LIT>'] = variant_obj['<STR_LIT>']<EOL><DEDENT>return variant_obj<EOL>

Parse information about variants.

    - Adds information about compounds
    - Updates the information about compounds if necessary and 'update=True'

    Args:
        store(scout.adapter.MongoAdapter)
        institute_obj(scout.models.Institute)
        case_obj(scout.models.Case)
        variant_obj(scout.models.Variant)
        update(bool): If variant should be updated in database
        genome_build(str)


def sv_variants(store, institute_obj, case_obj, variants_query, page=<NUM_LIT:1>, per_page=<NUM_LIT:50>):

skip_count = (per_page * max(page - <NUM_LIT:1>, <NUM_LIT:0>))<EOL>more_variants = True if variants_query.count() > (skip_count + per_page) else False<EOL>genome_build = case_obj.get('<STR_LIT>', '<STR_LIT>')<EOL>if genome_build not in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>genome_build = '<STR_LIT>'<EOL><DEDENT>return {<EOL>'<STR_LIT>': (parse_variant(store, institute_obj, case_obj, variant, genome_build=genome_build) for variant in<EOL>variants_query.skip(skip_count).limit(per_page)),<EOL>'<STR_LIT>': more_variants,<EOL>}<EOL>


data = {<EOL>'<STR_LIT>': [],<EOL>'<STR_LIT>': [],<EOL>'<STR_LIT>': [],<EOL>'<STR_LIT>': []<EOL>}<EOL>for gene_obj in genes:<EOL><INDENT>for pred_key in data:<EOL><INDENT>gene_key = pred_key[:-<NUM_LIT:1>]<EOL>if len(genes) == <NUM_LIT:1>:<EOL><INDENT>value = gene_obj.get(gene_key, '<STR_LIT:->')<EOL><DEDENT>else:<EOL><INDENT>gene_id = gene_obj.get('<STR_LIT>') or str(gene_obj['<STR_LIT>'])<EOL>value = '<STR_LIT::>'.join([gene_id, gene_obj.get(gene_key, '<STR_LIT:->')])<EOL><DEDENT>data[pred_key].append(value)<EOL><DEDENT><DEDENT>return data<EOL>


def variant_verification(store, mail, institute_obj, case_obj, user_obj, variant_obj, sender, variant_url, order, comment, url_builder=url_for):

recipients = institute_obj['<STR_LIT>']<EOL>if len(recipients) == <NUM_LIT:0>:<EOL><INDENT>raise MissingSangerRecipientError()<EOL><DEDENT>view_type = None<EOL>email_subject = None<EOL>category = variant_obj.get('<STR_LIT>')<EOL>display_name = None<EOL>chromosome = variant_obj['<STR_LIT>']<EOL>end_chrom = variant_obj.get('<STR_LIT>', chromosome)<EOL>breakpoint_1 = '<STR_LIT::>'.join([chromosome, str(variant_obj['<STR_LIT>'])])<EOL>breakpoint_2 = '<STR_LIT::>'.join([end_chrom, str(variant_obj.get('<STR_LIT:end>'))])<EOL>variant_size = variant_obj.get('<STR_LIT>')<EOL>panels = '<STR_LIT:U+002CU+0020>'.join(variant_obj.get('<STR_LIT>', []))<EOL>hgnc_symbol = '<STR_LIT:U+002CU+0020>'.join(variant_obj['<STR_LIT>'])<EOL>email_subj_gene_symbol = None<EOL>if len(variant_obj['<STR_LIT>']) > <NUM_LIT:3>:<EOL><INDENT>email_subj_gene_symbol = '<STR_LIT:U+0020>'.join([ str(len(variant_obj['<STR_LIT>'])) + '<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>email_subj_gene_symbol = hgnc_symbol<EOL><DEDENT>gtcalls = ["<STR_LIT>".format(sample_obj['<STR_LIT>'],<EOL>sample_obj['<STR_LIT>'])<EOL>for sample_obj in variant_obj['<STR_LIT>']]<EOL>tx_changes = []<EOL>if category == '<STR_LIT>': <EOL><INDENT>view_type = '<STR_LIT>'<EOL>display_name = variant_obj.get('<STR_LIT>')<EOL>tx_changes = []<EOL>for gene_obj in variant_obj.get('<STR_LIT>', []):<EOL><INDENT>for tx_obj in gene_obj['<STR_LIT>']:<EOL><INDENT>parse_transcript(gene_obj, tx_obj)<EOL>if not tx_obj.get('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>for refseq_id in tx_obj.get('<STR_LIT>'):<EOL><INDENT>transcript_line = []<EOL>if "<STR_LIT>" in gene_obj:<EOL><INDENT>transcript_line.append(gene_obj['<STR_LIT>']['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>transcript_line.append(gene_obj['<STR_LIT>'])<EOL><DEDENT>transcript_line.append('<STR_LIT:->'.join([refseq_id, tx_obj['<STR_LIT>']]))<EOL>if "<STR_LIT>" in tx_obj:<EOL><INDENT>transcript_line.append('<STR_LIT>'.join([ "<STR_LIT>", tx_obj["<STR_LIT>"]]))<EOL><DEDENT>elif "<STR_LIT>" in tx_obj:<EOL><INDENT>transcript_line.append('<STR_LIT>'.join([ "<STR_LIT>", tx_obj["<STR_LIT>"]]))<EOL><DEDENT>else:<EOL><INDENT>transcript_line.append('<STR_LIT>')<EOL><DEDENT>if "<STR_LIT>" in tx_obj:<EOL><INDENT>transcript_line.append(urllib.parse.unquote(tx_obj['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>transcript_line.append('<STR_LIT>')<EOL><DEDENT>if "<STR_LIT>" in tx_obj:<EOL><INDENT>transcript_line.append(urllib.parse.unquote(tx_obj['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>transcript_line.append('<STR_LIT>')<EOL><DEDENT>tx_changes.append("<STR_LIT>".format('<STR_LIT::>'.join(transcript_line)))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else: <EOL><INDENT>view_type = '<STR_LIT>'<EOL>display_name = '<STR_LIT:_>'.join([breakpoint_1, variant_obj.get('<STR_LIT>').upper()])<EOL><DEDENT>html = verification_email_body(<EOL>case_name = case_obj['<STR_LIT>'],<EOL>url = variant_url, <EOL>display_name = display_name,<EOL>category = category.upper(),<EOL>subcategory = variant_obj.get('<STR_LIT>').upper(),<EOL>breakpoint_1 = breakpoint_1,<EOL>breakpoint_2 = breakpoint_2,<EOL>hgnc_symbol = hgnc_symbol,<EOL>panels = panels,<EOL>gtcalls = '<STR_LIT>'.join(gtcalls),<EOL>tx_changes = '<STR_LIT>'.join(tx_changes) or '<STR_LIT>',<EOL>name = user_obj['<STR_LIT:name>'].encode('<STR_LIT:utf-8>'),<EOL>comment = comment<EOL>)<EOL>local_link = url_builder(view_type, institute_id=institute_obj['<STR_LIT>'],<EOL>case_name=case_obj['<STR_LIT>'],<EOL>variant_id=variant_obj['<STR_LIT>'])<EOL>if order == '<STR_LIT:True>': <EOL><INDENT>if case_obj.get('<STR_LIT>') is None or variant_obj['<STR_LIT>'] not in case_obj['<STR_LIT>']:<EOL><INDENT>store.pin_variant(institute_obj, case_obj, user_obj, local_link, variant_obj)<EOL><DEDENT>email_subject = "<STR_LIT>".format( category.upper(), display_name, email_subj_gene_symbol)<EOL>store.order_verification(institute=institute_obj, case=case_obj, user=user_obj, link=local_link, variant=variant_obj)<EOL><DEDENT>else: <EOL><INDENT>email_subject = "<STR_LIT>".format(category.upper(), display_name, email_subj_gene_symbol)<EOL>store.cancel_verification(institute=institute_obj, case=case_obj, user=user_obj, link=local_link, variant=variant_obj)<EOL><DEDENT>kwargs = dict(subject=email_subject, html=html, sender=sender, recipients=recipients,<EOL>cc=[user_obj['<STR_LIT:email>']])<EOL>message = Message(**kwargs)<EOL>mail.send(message)<EOL>

Sand a verification email and register the verification in the database

        Args:
            store(scout.adapter.MongoAdapter)
            mail(scout.server.extensions.mail): an instance of flask_mail.Mail
            institute_obj(dict): an institute object
            case_obj(dict): a case object
            user_obj(dict): a user object
            variant_obj(dict): a variant object (snv or sv)
            sender(str): current_app.config['MAIL_USERNAME']
            variant_url(str): the complete url to the variant (snv or sv), a link that works from outside scout domain.
            order(str): False == cancel order, True==order verification
            comment(str): sender's entered comment from form
            url_builder(flask.url_for): for testing purposes, otherwise test verification email fails because out of context


institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>case_url = url_for('<STR_LIT>', institute_id=institute_id, case_name=case_name)<EOL>is_group = request.args.get('<STR_LIT>') == '<STR_LIT:yes>'<EOL>user_obj = store.user(current_user.email)<EOL>if phenotype_id:<EOL><INDENT>store.remove_phenotype(institute_obj, case_obj, user_obj, case_url,<EOL>phenotype_id, is_group=is_group)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>phenotype_term = request.form['<STR_LIT>']<EOL>if phenotype_term.startswith('<STR_LIT>') or len(phenotype_term) == <NUM_LIT:7>:<EOL><INDENT>hpo_term = phenotype_term.split('<STR_LIT>', <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>store.add_phenotype(institute_obj, case_obj, user_obj, case_url,<EOL>hpo_term=hpo_term, is_group=is_group)<EOL><DEDENT>else:<EOL><INDENT>store.add_phenotype(institute_obj, case_obj, user_obj, case_url,<EOL>omim_term=phenotype_term)<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>return abort(<NUM_LIT>, ("<STR_LIT>".format(phenotype_term)))<EOL><DEDENT><DEDENT>return redirect(case_url)<EOL>


institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>data = controllers.case_report_content(store, institute_obj, case_obj)<EOL>return dict(institute=institute_obj, case=case_obj, format='<STR_LIT:html>', **data)<EOL>


institute_obj = institute_and_case(store, institute_id)<EOL>query = request.args.get('<STR_LIT>')<EOL>limit = <NUM_LIT:100><EOL>if request.args.get('<STR_LIT>'):<EOL><INDENT>limit = int(request.args.get('<STR_LIT>'))<EOL><DEDENT>skip_assigned = request.args.get('<STR_LIT>')<EOL>is_research = request.args.get('<STR_LIT>')<EOL>all_cases = store.cases(collaborator=institute_id, name_query=query,<EOL>skip_assigned=skip_assigned, is_research=is_research)<EOL>data = controllers.cases(store, all_cases, limit)<EOL>sanger_unevaluated = controllers.get_sanger_unevaluated(store, institute_id, current_user.email)<EOL>if len(sanger_unevaluated)> <NUM_LIT:0>:<EOL><INDENT>data['<STR_LIT>'] = sanger_unevaluated<EOL><DEDENT>return dict(institute=institute_obj, skip_assigned=skip_assigned,<EOL>is_research=is_research, query=query, **data)<EOL>

Display a list of cases for an institute.


@cases_bp.route('<STR_LIT>', methods=['<STR_LIT:POST>'])<EOL>def pin_variant(institute_id, case_name, variant_id):

institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>variant_obj = store.variant(variant_id)<EOL>user_obj = store.user(current_user.email)<EOL>link = url_for('<STR_LIT>', institute_id=institute_id, case_name=case_name,<EOL>variant_id=variant_id)<EOL>if request.form['<STR_LIT:action>'] == '<STR_LIT>':<EOL><INDENT>store.pin_variant(institute_obj, case_obj, user_obj, link, variant_obj)<EOL><DEDENT>elif request.form['<STR_LIT:action>'] == '<STR_LIT>':<EOL><INDENT>store.unpin_variant(institute_obj, case_obj, user_obj, link, variant_obj)<EOL><DEDENT>return redirect(request.referrer or link)<EOL>

Pin and unpin variants to/from the list of suspects.


@cases_bp.route('<STR_LIT>', methods=['<STR_LIT:POST>'])<EOL>@cases_bp.route('<STR_LIT>', methods=['<STR_LIT:POST>'])<EOL>def events(institute_id, case_name, event_id=None):

institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>link = request.form.get('<STR_LIT>')<EOL>content = request.form.get('<STR_LIT:content>')<EOL>variant_id = request.args.get('<STR_LIT>')<EOL>user_obj = store.user(current_user.email)<EOL>if event_id:<EOL><INDENT>store.delete_event(event_id)<EOL><DEDENT>else:<EOL><INDENT>if variant_id:<EOL><INDENT>variant_obj = store.variant(variant_id)<EOL>level = request.form.get('<STR_LIT>', '<STR_LIT>')<EOL>store.comment(institute_obj, case_obj, user_obj, link,<EOL>variant=variant_obj, content=content, comment_level=level)<EOL><DEDENT>else:<EOL><INDENT>store.comment(institute_obj, case_obj, user_obj, link, content=content)<EOL><DEDENT><DEDENT>return redirect(request.referrer)<EOL>


@cases_bp.route('<STR_LIT>', methods=['<STR_LIT:POST>'])<EOL>def matchmaker_delete(institute_id, case_name):

<EOL>user_obj = store.user(current_user.email)<EOL>if '<STR_LIT>' not in user_obj['<STR_LIT>']:<EOL><INDENT>flash('<STR_LIT>', '<STR_LIT>')<EOL>return redirect(request.referrer)<EOL><DEDENT>institute_obj, case_obj = institute_and_case(store, institute_id, case_name)<EOL>mme_base_url = current_app.config.get('<STR_LIT>')<EOL>mme_token = current_app.config.get('<STR_LIT>')<EOL>if not mme_base_url or not mme_token:<EOL><INDENT>flash('<STR_LIT>', '<STR_LIT>')<EOL>return redirect(request.referrer)<EOL><DEDENT>delete_result = controllers.mme_delete(case_obj, mme_base_url, mme_token)<EOL>n_deleted = <NUM_LIT:0><EOL>category = '<STR_LIT>'<EOL>for resp in delete_result:<EOL><INDENT>if resp['<STR_LIT>'] == <NUM_LIT:200>:<EOL><INDENT>n_deleted += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>flash(resp['<STR_LIT:message>'], category)<EOL><DEDENT><DEDENT>if n_deleted:<EOL><INDENT>category = '<STR_LIT:success>'<EOL>user_obj = store.user(current_user.email)<EOL>store.case_mme_delete(case_obj=case_obj, user_obj=user_obj)<EOL><DEDENT>flash('<STR_LIT>'.format(n_deleted, len(delete_result)), category)<EOL>return redirect(request.referrer)<EOL>


def gene_variants(store, variants_query, page=<NUM_LIT:1>, per_page=<NUM_LIT:50>):

variant_count = variants_query.count()<EOL>skip_count = per_page * max(page - <NUM_LIT:1>, <NUM_LIT:0>)<EOL>more_variants = True if variant_count > (skip_count + per_page) else False<EOL>variant_res = variants_query.skip(skip_count).limit(per_page)<EOL>my_institutes = list(inst['<STR_LIT>'] for inst in user_institutes(store, current_user))<EOL>variants = []<EOL>for variant_obj in variant_res:<EOL><INDENT>if variant_obj['<STR_LIT>'] not in my_institutes:<EOL><INDENT>LOG.warning("<STR_LIT>".format(variant_obj['<STR_LIT>']))<EOL>continue<EOL><DEDENT>variant_case_obj = store.case(case_id=variant_obj['<STR_LIT>'])<EOL>if not variant_case_obj:<EOL><INDENT>continue<EOL><DEDENT>case_display_name = variant_case_obj.get('<STR_LIT>')<EOL>variant_obj['<STR_LIT>'] = case_display_name<EOL>genome_build = variant_case_obj.get('<STR_LIT>', '<STR_LIT>')<EOL>if genome_build not in ['<STR_LIT>','<STR_LIT>']:<EOL><INDENT>genome_build = '<STR_LIT>'<EOL><DEDENT>variant_genes = variant_obj.get('<STR_LIT>')<EOL>if variant_genes is not None:<EOL><INDENT>for gene_obj in variant_genes:<EOL><INDENT>if not gene_obj['<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>if gene_obj.get('<STR_LIT>') is None or gene_obj.get('<STR_LIT:description>') is None:<EOL><INDENT>hgnc_gene = store.hgnc_gene(gene_obj['<STR_LIT>'], build=genome_build)<EOL>if not hgnc_gene:<EOL><INDENT>continue<EOL><DEDENT>gene_obj['<STR_LIT>'] = hgnc_gene['<STR_LIT>']<EOL>gene_obj['<STR_LIT:description>'] = hgnc_gene['<STR_LIT:description>']<EOL><DEDENT><DEDENT><DEDENT>gene_ids = []<EOL>gene_symbols = []<EOL>hgvs_c = []<EOL>hgvs_p = []<EOL>variant_genes = variant_obj.get('<STR_LIT>')<EOL>if variant_genes is not None:<EOL><INDENT>functional_annotation = '<STR_LIT>'<EOL>for gene_obj in variant_genes:<EOL><INDENT>hgnc_id = gene_obj['<STR_LIT>']<EOL>gene_symbol = gene(store, hgnc_id)['<STR_LIT>']<EOL>gene_ids.append(hgnc_id)<EOL>gene_symbols.append(gene_symbol)<EOL>hgvs_nucleotide = '<STR_LIT:->'<EOL>transcripts_list = gene_obj.get('<STR_LIT>')<EOL>for transcript_obj in transcripts_list:<EOL><INDENT>if transcript_obj.get('<STR_LIT>') and transcript_obj.get('<STR_LIT>') is True:<EOL><INDENT>hgvs_nucleotide = str(transcript_obj.get('<STR_LIT>'))<EOL>hgvs_protein = str(transcript_obj.get('<STR_LIT>'))<EOL><DEDENT><DEDENT>hgvs_c.append(hgvs_nucleotide)<EOL>hgvs_p.append(hgvs_protein)<EOL><DEDENT>if len(gene_symbols) == <NUM_LIT:1>:<EOL><INDENT>if(hgvs_p[<NUM_LIT:0>] != "<STR_LIT:None>"):<EOL><INDENT>hgvs = hgvs_p[<NUM_LIT:0>]<EOL><DEDENT>elif(hgvs_c[<NUM_LIT:0>] != "<STR_LIT:None>"):<EOL><INDENT>hgvs = hgvs_c[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>hgvs = "<STR_LIT:->"<EOL><DEDENT>variant_obj['<STR_LIT>'] = hgvs<EOL><DEDENT>variant_obj.update(get_predictions(variant_genes))<EOL><DEDENT>variants.append(variant_obj)<EOL><DEDENT>return {<EOL>'<STR_LIT>': variants,<EOL>'<STR_LIT>': more_variants,<EOL>}<EOL>


def mt_excel_files(store, case_obj, temp_excel_dir):

today = datetime.datetime.now().strftime('<STR_LIT>')<EOL>samples = case_obj.get('<STR_LIT>')<EOL>query = {'<STR_LIT>':'<STR_LIT>'}<EOL>mt_variants = list(store.variants(case_id=case_obj['<STR_LIT>'], query=query, nr_of_variants= -<NUM_LIT:1>, sort_key='<STR_LIT>'))<EOL>written_files = <NUM_LIT:0><EOL>for sample in samples:<EOL><INDENT>sample_id = sample['<STR_LIT>']<EOL>sample_lines = export_mt_variants(variants=mt_variants, sample_id=sample_id)<EOL>document_name = '<STR_LIT:.>'.join([case_obj['<STR_LIT>'], sample_id, today]) + '<STR_LIT>'<EOL>workbook = Workbook(os.path.join(temp_excel_dir,document_name))<EOL>Report_Sheet = workbook.add_worksheet()<EOL>row = <NUM_LIT:0><EOL>for col,field in enumerate(MT_EXPORT_HEADER):<EOL><INDENT>Report_Sheet.write(row,col,field)<EOL><DEDENT>for row, line in enumerate(sample_lines,<NUM_LIT:1>): <EOL><INDENT>for col, field in enumerate(line): <EOL><INDENT>Report_Sheet.write(row,col,field)<EOL><DEDENT><DEDENT>workbook.close()<EOL>if os.path.exists(os.path.join(temp_excel_dir,document_name)):<EOL><INDENT>written_files += <NUM_LIT:1><EOL><DEDENT><DEDENT>return written_files<EOL>

Collect MT variants and format line of a MT variant report
    to be exported in excel format

    Args:
        store(adapter.MongoAdapter)
        case_obj(models.Case)
        temp_excel_dir(os.Path): folder where the temp excel files are written to

    Returns:
        written_files(int): the number of files written to temp_excel_dir


def get_analysis_types(adapter, total_cases, institute_id=None, slice_query=None):

<EOL>query = {}<EOL>subquery = {}<EOL>if institute_id and slice_query:<EOL><INDENT>subquery = adapter.cases(owner=institute_id, name_query=slice_query,<EOL>yield_query=True)<EOL><DEDENT>elif institute_id:<EOL><INDENT>subquery = adapter.cases(owner=institute_id, yield_query=True)<EOL><DEDENT>elif slice_query:<EOL><INDENT>subquery = adapter.cases(name_query=slice_query, yield_query=True)<EOL><DEDENT>query = {'<STR_LIT>': subquery}<EOL>pipeline = []<EOL>if query:<EOL><INDENT>pipeline.append(query)<EOL><DEDENT>pipeline.append({'<STR_LIT>': '<STR_LIT>'})<EOL>pipeline.append({'<STR_LIT>': {'<STR_LIT>': '<STR_LIT>', '<STR_LIT:count>': {'<STR_LIT>': <NUM_LIT:1>}}})<EOL>analysis_query = adapter.case_collection.aggregate(pipeline)<EOL>analysis_types = [{'<STR_LIT:name>': group['<STR_LIT>'], '<STR_LIT:count>': group['<STR_LIT:count>']} for group in analysis_query]<EOL>return analysis_types<EOL>

Return information about analysis types.
        Group cases based on analysis type for the individuals.
    Args:
        adapter(adapter.MongoAdapter)
        total_cases(int): Total number of cases
        institute_id(str)
        slice_query(str): Query to filter cases to obtain statistics for.
    Returns:
        analysis_types array of hashes with name: analysis_type(str), count: count(int)


def decorator(f):<EOL><INDENT>@wraps(f)<EOL>def decorated_function(*args, **kwargs):<EOL><INDENT>template_name = template<EOL>if template_name is None:<EOL><INDENT>template_name = request.endpoint.replace('<STR_LIT:.>', '<STR_LIT:/>') + '<STR_LIT>'<EOL><DEDENT>context = f(*args, **kwargs)<EOL>if context is None:<EOL><INDENT>context = {}<EOL><DEDENT>elif not isinstance(context, dict):<EOL><INDENT>return context<EOL><DEDENT>return render_template(template_name, **context)<EOL><DEDENT>return decorated_function<EOL><DEDENT>return decorator<EOL>

Template decorator.

    Ref: http://flask.pocoo.org/docs/patterns/viewdecorators/


reader = csv.DictReader(csv_stream, delimiter='<STR_LIT:;>', quoting=csv.QUOTE_NONE)<EOL>genes = []<EOL>for gene_row in reader:<EOL><INDENT>if not gene_row['<STR_LIT>'].strip().isdigit():<EOL><INDENT>continue<EOL><DEDENT>transcripts_raw = gene_row.get('<STR_LIT>')<EOL>if transcripts_raw:<EOL><INDENT>transcripts_list = [tx.split('<STR_LIT::>', <NUM_LIT:1>)[-<NUM_LIT:1>].strip() for tx in transcripts_raw.split('<STR_LIT:U+002C>')]<EOL><DEDENT>else:<EOL><INDENT>transcripts_list = []<EOL><DEDENT>models_raw = gene_row.get('<STR_LIT>')<EOL>models_list = [model.strip() for model in models_raw.split('<STR_LIT:U+002C>')] if models_raw else []<EOL>panel_gene = dict(<EOL>symbol=gene_row['<STR_LIT>'].strip() if gene_row.get('<STR_LIT>') else None,<EOL>hgnc_id=int(gene_row['<STR_LIT>'].strip()),<EOL>disease_associated_transcripts=transcripts_list,<EOL>reduced_penetrance=True if gene_row.get('<STR_LIT>') else None,<EOL>mosaicism=True if gene_row.get('<STR_LIT>') else None,<EOL>inheritance_models=models_list,<EOL>database_entry_version=gene_row.get('<STR_LIT>'),<EOL>)<EOL>genes.append(panel_gene)<EOL><DEDENT>return genes<EOL>