<commit_before>from ..schema_utils import (
    load_schema,
)
from ..contentbase import (
    location,
)
from .base import (
    ACCESSION_KEYS,
    ALIAS_KEYS,
    Collection,
    paths_filtered_by_status,
)
from itertools import chain
from pyramid.traversal import (
    find_resource,
)
from urllib import quote_plus
from urlparse import urljoin


def file_is_revoked(root, path):
    item = find_resource(root, path)
    return item.upgrade_properties()['status'] == 'revoked'


@location('datasets')
class Dataset(Collection):
    item_type = 'dataset'
    schema = load_schema('dataset.json')
    properties = {
        'title': 'Datasets',
        'description': 'Listing of datasets',
    }

    class Item(Collection.Item):
        template = {
            # XXX Still needed?
            'original_files': (
                lambda root, original_files: paths_filtered_by_status(root, original_files)
            ),
            'files': (
                lambda root, original_files, related_files: paths_filtered_by_status(
                    root, chain(original_files, related_files),
                    exclude=('revoked', 'deleted', 'replaced'),
                )
            ),
            'revoked_files': (
                lambda root, original_files, related_files: [
                    path for path in chain(original_files, related_files)
                    if file_is_revoked(root, path)
                ]
            ),
            'hub': {
                '$value': '{item_uri}@@hub/hub.txt',
                '$condition': 'assembly',
            },
            'assembly': {
                '$value': '{assembly}',
                '$condition': 'assembly',
            },
        }
        embedded = [
            'files',
            'files.replicate',
            'files.replicate.experiment',
            'files.replicate.experiment.lab',
            'files.replicate.experiment.target',
            'files.submitted_by',
            'revoked_files',
            'revoked_files.replicate',
            'revoked_files.replicate.experiment',
            'revoked_files.replicate.experiment.lab',
            'revoked_files.replicate.experiment.target',
            'revoked_files.submitted_by',
            'submitted_by',
            'lab',
            'award',
            'documents.lab',
            'documents.award',
            'documents.submitted_by',
        ]
        name_key = 'accession'
        keys = ACCESSION_KEYS + ALIAS_KEYS
        rev = {
            'original_files': ('file', 'dataset'),
        }

        def template_namespace(self, properties, request=None):
            ns = super(Dataset.Item, self).template_namespace(properties, request)
            root = ns['root']
            for link in ns['original_files'] + ns['related_files']:
                f = find_resource(root, link)
                if f.properties['file_format'] in ['bigWig', 'bigBed', 'narrowPeak', 'broadPeak'] and \
                        f.properties['status'] in ['released']:
                    if 'assembly' in f.properties:
                        ns['assembly'] = f.properties['assembly']
                        break
            return ns

        @classmethod
        def expand_page(cls, request, properties):
            properties = super(Dataset.Item, cls).expand_page(request, properties)
            if 'hub' in properties:
                hub_url = urljoin(request.resource_url(request.root), properties['hub'])
                properties = properties.copy()
                hgTracks = 'http://genome.ucsc.edu/cgi-bin/hgTracks?'
                properties['visualize_ucsc'] = hgTracks + '&'.join([
                    'db=' + quote_plus(properties['assembly']),
                    'hubUrl=' + quote_plus(hub_url, ':/@'),
                ])
            return properties<commit_msg>Move assembly to template<commit_after>from ..schema_utils import (
    load_schema,
)
from ..contentbase import (
    location,
)
from .base import (
    ACCESSION_KEYS,
    ALIAS_KEYS,
    Collection,
    paths_filtered_by_status,
)
from itertools import chain
from pyramid.traversal import (
    find_resource,
)
from urllib import quote_plus
from urlparse import urljoin


def file_is_revoked(root, path):
    item = find_resource(root, path)
    return item.upgrade_properties()['status'] == 'revoked'


def assembly(root, original_files, related_files):
    for path in chain(original_files, related_files):
        f = find_resource(root, path)
        if f.properties['file_format'] in ['bigWig', 'bigBed', 'narrowPeak', 'broadPeak'] and \
                f.properties['status'] in ['released']:
            if 'assembly' in f.properties:
                return f.properties['assembly']
    return None


@location('datasets')
class Dataset(Collection):
    item_type = 'dataset'
    schema = load_schema('dataset.json')
    properties = {
        'title': 'Datasets',
        'description': 'Listing of datasets',
    }

    class Item(Collection.Item):
        template = {
            # XXX Still needed?
            'original_files': (
                lambda root, original_files: paths_filtered_by_status(root, original_files)
            ),
            'files': (
                lambda root, original_files, related_files: paths_filtered_by_status(
                    root, chain(original_files, related_files),
                    exclude=('revoked', 'deleted', 'replaced'),
                )
            ),
            'revoked_files': (
                lambda root, original_files, related_files: [
                    path for path in chain(original_files, related_files)
                    if file_is_revoked(root, path)
                ]
            ),
            'hub': {
                '$value': '{item_uri}@@hub/hub.txt',
                '$condition': 'assembly',
            },
            'assembly': {
                '$value': assembly,
                '$condition': assembly,
            },
        }
        embedded = [
            'files',
            'files.replicate',
            'files.replicate.experiment',
            'files.replicate.experiment.lab',
            'files.replicate.experiment.target',
            'files.submitted_by',
            'revoked_files',
            'revoked_files.replicate',
            'revoked_files.replicate.experiment',
            'revoked_files.replicate.experiment.lab',
            'revoked_files.replicate.experiment.target',
            'revoked_files.submitted_by',
            'submitted_by',
            'lab',
            'award',
            'documents.lab',
            'documents.award',
            'documents.submitted_by',
        ]
        name_key = 'accession'
        keys = ACCESSION_KEYS + ALIAS_KEYS
        rev = {
            'original_files': ('file', 'dataset'),
        }

        @classmethod
        def expand_page(cls, request, properties):
            properties = super(Dataset.Item, cls).expand_page(request, properties)
            if 'hub' in properties:
                hub_url = urljoin(request.resource_url(request.root), properties['hub'])
                properties = properties.copy()
                hgTracks = 'http://genome.ucsc.edu/cgi-bin/hgTracks?'
                properties['visualize_ucsc'] = hgTracks + '&'.join([
                    'db=' + quote_plus(properties['assembly']),
                    'hubUrl=' + quote_plus(hub_url, ':/@'),
                ])
            return properties
{'fraction_non_alphanumeric': 0.11598872331856624, 'fraction_numerical': 0.0, 'mean_word_length': 1.8070836473247929, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6512504', 'n_tokens_mistral': 2083, 'n_tokens_neox': 2004, 'n_words': 389}