code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
if value is None: editor.show_message('tabstop=%i' % editor.tabstop) else: try: value = int(value) if value > 0: editor.tabstop = value else: editor.show_message('Argument must be positive') except ValueError: ...
def tab_stop(editor, value)
Set tabstop.
3.424933
3.348348
1.022873
if value is None: editor.show_message('scrolloff=%i' % editor.scroll_offset) else: try: value = int(value) if value >= 0: editor.scroll_offset = value else: editor.show_message('Argument must be positive') except Va...
def set_scroll_offset(editor, value)
Set scroll offset.
3.43106
3.331157
1.02999
b = get_app().current_buffer before_cursor = b.document.current_line_before_cursor return bool(not before_cursor or before_cursor[-1].isspace())
def whitespace_before_cursor_on_line()
Filter which evaluates to True when the characters before the cursor are whitespace, or we are at the start of te line.
5.098751
4.952333
1.029565
def walk(split): for c in split: if isinstance(c, (HSplit, VSplit)): for i in walk(c): yield i elif isinstance(c, Window): yield split, c return walk(self.root)
def _walk_through_windows(self)
Yields (Split, Window) tuples.
5.203825
3.526556
1.475611
def walk(split): for c in split: if isinstance(c, (HSplit, VSplit)): yield split, c for i in walk(c): yield i return walk(self.root)
def _walk_through_splits(self)
Yields (parent_split, child_plit) tuples.
5.779079
4.342404
1.330848
if editor_buffer is None: editor_buffer = self.active_window.editor_buffer active_split = self._get_active_split() index = active_split.index(self.active_window) new_window = Window(editor_buffer) if isinstance(active_split, split_cls): # Add ne...
def _split(self, split_cls, editor_buffer=None)
Split horizontal or vertical. (when editor_buffer is None, show the current buffer there as well.)
2.878739
2.838662
1.014118
assert isinstance(editor_buffer, EditorBuffer) self.active_window.editor_buffer = editor_buffer
def show_editor_buffer(self, editor_buffer)
Open this `EditorBuffer` in the active window.
4.413121
3.383899
1.304152
for split, window in self._walk_through_windows(): if window.editor_buffer == editor_buffer: self._close_window(window)
def close_editor_buffer(self, editor_buffer)
Close all the windows that have this editor buffer open.
6.928471
5.700073
1.215506
if window == self.active_window: self.close_active_window() else: original_active_window = self.active_window self.close_active_window() self.active_window = original_active_window
def _close_window(self, window)
Close this window.
2.872401
2.681717
1.071105
active_split = self._get_active_split() # First remove the active window from its split. index = active_split.index(self.active_window) del active_split[index] # Move focus. if len(active_split): new_active_window = active_split[max(0, index - 1)] ...
def close_active_window(self)
Close active window.
3.688854
3.633982
1.0151
windows = self.windows() new_index = (windows.index(self.active_window) + 1) % len(windows) self.active_window = windows[new_index]
def cycle_focus(self)
Cycle through all windows.
3.497994
2.719848
1.286099
for w in self.windows(): if w.editor_buffer.has_unsaved_changes: return True return False
def has_unsaved_changes(self)
True when any of the visible buffers in this tab has unsaved changes.
5.477178
4.33279
1.264123
if self.active_tab and self.active_tab.active_window: return self.active_tab.active_window.editor_buffer
def active_editor_buffer(self)
The active EditorBuffer or None.
3.780733
3.214528
1.176139
" The active prompt_toolkit layout Window. " if self.active_tab: w = self.active_tab.active_window if w: return w.pt_window
def active_pt_window(self)
The active prompt_toolkit layout Window.
9.093359
5.463953
1.664245
for eb in self.editor_buffers: if eb.location == location: return eb
def get_editor_buffer_for_location(self, location)
Return the `EditorBuffer` for this location. When this file was not yet loaded, return None
3.802092
3.41714
1.112653
for eb in self.editor_buffers: if eb.buffer_name == buffer_name: return eb
def get_editor_buffer_for_buffer_name(self, buffer_name)
Return the `EditorBuffer` for this buffer_name. When not found, return None
2.816665
2.703866
1.041718
if len(self.tab_pages) > 1: # Cannot close last tab. del self.tab_pages[self.active_tab_index] self.active_tab_index = max(0, self.active_tab_index - 1) # Clean up buffers. self._auto_close_new_empty_buffers()
def close_tab(self)
Close active tab.
4.442679
4.115137
1.079595
assert location is None or text is None or new is False # Don't pass two of them. if location or text or new: editor_buffer = self._get_or_create_editor_buffer(location=location, text=text) else: editor_buffer = None self.active_tab.hsplit(editor_buffer...
def hsplit(self, location=None, new=False, text=None)
Split horizontally.
5.09231
5.058255
1.006733
self.tab_pages = [TabPage(self.active_tab.active_window)] self.active_tab_index = 0
def keep_only_current_window(self)
Close all other windows, except the current one.
9.477144
8.122109
1.166833
if self.active_editor_buffer: # Find the active opened buffer. index = self.editor_buffers.index(self.active_editor_buffer) # Get index of new buffer. if _previous: new_index = (len(self.editor_buffers) + index - 1) % len(self.editor_buff...
def go_to_next_buffer(self, _previous=False)
Open next buffer in active window.
3.068424
2.887423
1.062686
self.active_tab_index = (self.active_tab_index - 1 + len(self.tab_pages)) % len(self.tab_pages)
def go_to_previous_tab(self)
Focus the previous tab.
3.042813
2.792587
1.089604
assert isinstance(buffer_name, string_types) for i, eb in enumerate(self.editor_buffers): if (eb.location == buffer_name or (buffer_name.isdigit() and int(buffer_name) == i)): self.show_editor_buffer(eb) break
def go_to_buffer(self, buffer_name)
Go to one of the open buffers.
4.052685
3.974318
1.019719
assert isinstance(editor_buffer, EditorBuffer) and editor_buffer not in self.editor_buffers # Add to list of EditorBuffers eb = self.active_editor_buffer if eb is None: self.editor_buffers.append(editor_buffer) else: # Append right after the curr...
def _add_editor_buffer(self, editor_buffer, show_in_current_window=False)
Insert this new buffer in the list of buffers, right after the active one.
3.196672
3.100291
1.031088
assert location is None or text is None # Don't pass two of them. assert location is None or isinstance(location, string_types) if location is None: # Create and add an empty EditorBuffer eb = EditorBuffer(self.editor, text=text) self._add_editor_bu...
def _get_or_create_editor_buffer(self, location=None, text=None)
Given a location, return the `EditorBuffer` instance that we have if the file is already open, or create a new one. When location is None, this creates a new buffer.
3.181492
3.007336
1.05791
eb = self._get_or_create_editor_buffer(location) if show_in_current_window: self.show_editor_buffer(eb)
def open_buffer(self, location=None, show_in_current_window=False)
Open/create a file, load it, and show it in a new buffer.
3.878756
4.214362
0.920366
# Get all visible EditorBuffers ebs = set() for t in self.tab_pages: ebs |= set(t.visible_editor_buffers()) # Remove empty/new buffers that are hidden. for eb in self.editor_buffers[:]: if eb.is_new and not eb.location and eb not in ebs and eb.bu...
def _auto_close_new_empty_buffers(self)
When there are new, empty buffers open. (Like, created when the editor starts without any files.) These can be removed at the point when there is no more window showing them. This should be called every time when a window is closed, or when the content of a window is replcaed by somethi...
5.462994
4.903984
1.113991
eb = self.active_editor_buffer # Remove this buffer. index = self.editor_buffers.index(eb) self.editor_buffers.remove(eb) # Close the active window. self.active_tab.close_active_window() # Close all the windows that still have this buffer open. ...
def close_buffer(self)
Close current buffer. When there are other windows showing the same buffer, they are closed as well. When no windows are left, the previous buffer or an empty buffer is shown.
2.97301
2.892749
1.027746
eb = self._get_or_create_editor_buffer(location) self.tab_pages.insert(self.active_tab_index + 1, TabPage(Window(eb))) self.active_tab_index += 1
def create_tab(self, location=None)
Create a new tab page.
5.56155
4.916718
1.131151
active_eb = self.active_editor_buffer visible_ebs = self.active_tab.visible_editor_buffers() def make_info(i, eb): return OpenBufferInfo( index=i, editor_buffer=eb, is_active=(eb == active_eb), is_visible=(eb i...
def list_open_buffers(self)
Return a `OpenBufferInfo` list that gives information about the open buffers.
3.434579
3.160721
1.086644
if not SPHINX_INSTALLED: docutils.parsers.rst.directives.register_directive('code', CodeBlockDirective) docutils.parsers.rst.directives.register_directive('code-block', CodeBloc...
def register_code_directive()
Register code directive.
2.599568
2.518607
1.032145
# Do this at call time rather than import time to avoid unnecessarily # mutating state. register_code_directive() ignore_sphinx() ignore = ignore or {} try: ignore.setdefault('languages', []).extend( find_ignored_languages(source) ) except Error as error: ...
def check(source, filename='<string>', report_level=docutils.utils.Reporter.INFO_LEVEL, ignore=None, debug=False)
Yield errors. Use lower report_level for noisier error output. Each yielded error is a tuple of the form: (line_number, message) Line numbers are indexed at 1 and are with respect to the full RST file. Each code block is checked asynchronously in a subprocess. Note that this function m...
5.668774
5.71671
0.991615
for (index, line) in enumerate(source.splitlines()): match = RSTCHECK_COMMENT_RE.match(line) if match: key_and_value = line[match.end():].strip().split('=') if len(key_and_value) != 2: raise Error('Expected "key=value" syntax', ...
def find_ignored_languages(source)
Yield ignored languages. Languages are ignored via comment. For example, to ignore C++, JSON, and Python: >>> list(find_ignored_languages(''' ... Example ... ======= ... ... .. rstcheck: ignore-language=cpp,json ... ... .. rstcheck: ignore-language=python ... ''')) ['cpp',...
3.209118
2.848358
1.126655
(filename, args) = parameters if filename == '-': contents = sys.stdin.read() else: with contextlib.closing( docutils.io.FileInput(source_path=filename) ) as input_file: contents = input_file.read() args = load_configuration_from_file( os.pa...
def _check_file(parameters)
Return list of errors.
3.819078
3.787936
1.008221
try: compile(code, '<string>', 'exec') except SyntaxError as exception: yield (int(exception.lineno), exception.msg)
def check_python(code)
Yield errors.
4.168662
3.616647
1.152632
try: json.loads(code) except ValueError as exception: message = '{}'.format(exception) line_number = 0 found = re.search(r': line\s+([0-9]+)[^:]*$', message) if found: line_number = int(found.group(1)) yield (int(line_number), message)
def check_json(code)
Yield errors.
3.883563
3.551723
1.093431
try: xml.etree.ElementTree.fromstring(code) except xml.etree.ElementTree.ParseError as exception: message = '{}'.format(exception) line_number = 0 found = re.search(r': line\s+([0-9]+)[^:]*$', message) if found: line_number = int(found.group(1)) ...
def check_xml(code)
Yield errors.
3.333787
3.105162
1.073628
filename = '<string>' for result in check(code, filename=filename, ignore=ignore): yield result
def check_rst(code, ignore)
Yield errors in nested RST code.
8.889294
7.636069
1.164119
if SPHINX_INSTALLED: sphinx_directives = list(sphinx.domains.std.StandardDomain.directives) sphinx_roles = list(sphinx.domains.std.StandardDomain.roles) for domain in [sphinx.domains.c.CDomain, sphinx.domains.cpp.CPPDomain, sphinx.domains.j...
def _get_directives_and_roles_from_sphinx()
Return a tuple of Sphinx directive and roles.
2.834909
2.767654
1.0243
(directives, roles) = _get_directives_and_roles_from_sphinx() directives += [ 'centered', 'include', 'deprecated', 'index', 'no-code-block', 'literalinclude', 'hlist', 'seealso', 'toctree', 'todo', 'versionadded', ...
def ignore_sphinx()
Register Sphinx directives and roles to ignore.
5.63866
5.113578
1.102684
directory_or_file = os.path.realpath(directory_or_file) if os.path.isfile(directory_or_file): if debug: print('using config file {}'.format(directory_or_file), file=sys.stderr) return directory_or_file directory = directory_or_file while directory: ...
def find_config(directory_or_file, debug=False)
Return configuration filename. If `directory_or_file` is a file, return the real-path of that file. If it is a directory, find the configuration (any file name in CONFIG_FILES) in that directory or its ancestors.
1.764118
1.723585
1.023517
args = copy.copy(args) directory_or_file = directory if args.config is not None: directory_or_file = args.config options = _get_options(directory_or_file, debug=args.debug) args.report = options.get('report', args.report) threshold_dictionary = docutils.frontend.OptionParser.thre...
def load_configuration_from_file(directory, args)
Return new ``args`` with configuration loaded from file.
2.644992
2.560104
1.033158
for directive in directives: docutils.parsers.rst.directives.register_directive(directive, IgnoredDirective) for role in roles: docutils.parsers.rst.roles.register_local_role(role, _ignore_role)
def ignore_directives_and_roles(directives, roles)
Ignore directives/roles in docutils.
2.903584
2.558339
1.134949
run = run_in_subprocess(code, '.bash', ['bash', '-n'], working_directory=working_directory) def run_check(): result = run() if result: (output, filename) = result prefix = filename + ': line ' for line in output.split...
def bash_checker(code, working_directory)
Return checker.
3.723148
3.741017
0.995224
return gcc_checker(code, '.c', [os.getenv('CC', 'gcc'), '-std=c99'] + INCLUDE_FLAGS, working_directory=working_directory)
def c_checker(code, working_directory)
Return checker.
6.30663
6.523101
0.966815
return gcc_checker(code, '.cpp', [os.getenv('CXX', 'g++'), '-std=c++0x'] + INCLUDE_FLAGS, working_directory=working_directory)
def cpp_checker(code, working_directory)
Return checker.
5.794998
6.066574
0.955234
run = run_in_subprocess(code, filename_suffix, arguments + ['-pedantic', '-fsyntax-only'], working_directory=working_directory) def run_check(): result = run() if result: (output, filen...
def gcc_checker(code, filename_suffix, arguments, working_directory)
Return checker.
4.964881
4.942533
1.004522
colons = 2 if has_column else 1 prefix = filename + ':' if not message.startswith(prefix): raise ValueError() message = message[len(prefix):] split_message = message.split(':', colons) line_number = int(split_message[0]) return (line_number, split_message[colons].str...
def parse_gcc_style_error_message(message, filename, has_column=True)
Parse GCC-style error message. Return (line_number, message). Raise ValueError if message cannot be parsed.
3.001732
2.759191
1.087903
temporary_file = tempfile.NamedTemporaryFile(mode='wb', suffix=filename_suffix) temporary_file.write(code.encode('utf-8')) temporary_file.flush() process = subprocess.Popen(arguments + [temporary_file.name], stdout=sub...
def run_in_subprocess(code, filename_suffix, arguments, working_directory)
Return None on success.
2.700515
2.630801
1.026499
if SPHINX_INSTALLED and not is_code_node: delta = len(node.non_default_attributes()) current_line_contents = full_contents.splitlines()[line_number:] blank_lines = next( (i for (i, x) in enumerate(current_line_contents) if x), 0) return ( line...
def beginning_of_code_block(node, line_number, full_contents, is_code_node)
Return line number of beginning of code block.
3.988325
3.90963
1.020129
threshold_choices = docutils.frontend.OptionParser.threshold_choices parser = argparse.ArgumentParser( description=__doc__ + (' Sphinx is enabled.' if SPHINX_INSTALLED else ''), prog='rstcheck') parser.add_argument('files', nargs='+', type=decode_filenam...
def parse_args()
Return parsed command-line arguments.
2.817746
2.770188
1.017168
if file.encoding is None: # If the output file does not support Unicode, encode it to a byte # string. On some machines, this occurs when Python is redirecting to # file (or piping to something like Vim). text = text.encode('utf-8') print(text, file=file)
def output_message(text, file=sys.stderr)
Output message to terminal.
7.839825
7.79145
1.006209
if SPHINX_INSTALLED: srcdir = tempfile.mkdtemp() outdir = os.path.join(srcdir, '_build') try: sphinx.application.Sphinx(srcdir=srcdir, confdir=None, outdir=outdir, ...
def enable_sphinx_if_possible()
Register Sphinx directives and roles.
2.982249
2.973471
1.002952
base_name = os.path.basename(filename) if base_name.startswith('.'): return False if not os.path.isdir(filename) and not filename.lower().endswith('.rst'): return False return True
def match_file(filename)
Return True if file is okay for modifying/recursing.
3.062505
2.845836
1.076135
args = parse_args() if not args.files: return 0 with enable_sphinx_if_possible(): status = 0 pool = multiprocessing.Pool(multiprocessing.cpu_count()) try: if len(args.files) > 1: results = pool.map( _check_file, ...
def main()
Return 0 on success.
3.831547
3.704652
1.034253
try: language = self.arguments[0] except IndexError: language = '' code = '\n'.join(self.content) literal = docutils.nodes.literal_block(code, code) literal['classes'].append('code-block') literal['language'] = language return [lit...
def run(self)
Run directive.
4.149233
3.522663
1.177868
# For "..code-block:: language" language = node.get('language', None) is_code_node = False if not language: # For "..code:: language" is_code_node = True classes = node.get('classes') if 'code' in classes: language ...
def visit_literal_block(self, node)
Check syntax of code block.
3.127264
3.058008
1.022647
find = re.search(r'\[[^\]]+\]\([^\)]+\)', node.rawsource) if find is not None: self.document.reporter.warning( '(rst) Link is formatted in Markdown style.', base_node=node)
def visit_paragraph(self, node)
Check syntax of reStructuredText.
8.263881
6.999199
1.18069
def run_check(): all_results = run() if all_results is not None: if all_results: for result in all_results: error_offset = result[0] - 1 line_number = getattr(node, 'line', None) ...
def _add_check(self, node, run, language, is_code_node)
Add checker that will be run.
4.40417
4.152634
1.060573
visitor = CheckTranslator(self.document, contents=self.contents, filename=self.filename, ignore=self.ignore) self.document.walkabout(visitor) self.checkers += visitor.checkers
def translate(self)
Run CheckTranslator.
6.767551
5.123044
1.321002
parsed = urlparse(url) kwargs = parse_qsl(parsed.query) # TCP redis connection if parsed.scheme == 'redis': details = {'host': parsed.hostname} if parsed.port: details['port'] = parsed.port if parsed.password: details['password'] = parsed.password ...
def parse_url(url)
Parse the argument url and return a redis connection. Two patterns of url are supported: * redis://host:port[/db][?options] * redis+socket:///path/to/redis.sock[?options] A ValueError is raised if the URL is not recognized.
2.569855
2.289504
1.122451
acquired = Lock( self.redis, key, timeout=timeout, blocking=self.blocking, blocking_timeout=self.blocking_timeout ).acquire() if not acquired: # Time remaining in milliseconds # https://redis.io/command...
def raise_or_lock(self, key, timeout)
Checks if the task is locked and raises an exception, else locks the task. By default, the tasks and the key expire after 60 minutes. (meaning it will not be executed and the lock will clear).
4.009092
4.066173
0.985962
backend_name = config['backend'] path = backend_name.split('.') backend_mod_name, backend_class_name = '.'.join(path[:-1]), path[-1] backend_mod = importlib.import_module(backend_mod_name) backend_class = getattr(backend_mod, backend_class_name) return backend_class(config['settings'])
def import_backend(config)
Imports and initializes the Backend class.
2.029726
2.025751
1.001962
if isinstance(kwargs, dict): # Context: https://github.com/cameronmaske/celery-once/issues/58 # Keep equivalent to string of dict for backwards compatibility. return order_dict_to_string(OrderedDict( (force_string(key), force_string(value)) for key, value in item...
def force_string(kwargs)
Force key in dict or list to a string. Fixes: https://github.com/cameronmaske/celery-once/issues/11
4.287628
3.450231
1.242707
kwargs_list = [] # Kwargs are sorted in alphabetic order by their keys. # Taken from http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/ for k, v in items_sorted_by_key(kwargs): kwargs_list.append(str(k) + '-' + str(force_string(v))) return kwargs_list
def kwargs_to_list(kwargs)
Turns {'a': 1, 'b': 2} into ["a-1", "b-2"]
3.812329
3.314444
1.150217
keys = ['qo', force_string(name)] # Restrict to only the keys allowed in keys. if restrict_to is not None: restrict_kwargs = {key: kwargs[key] for key in restrict_to} keys += kwargs_to_list(restrict_kwargs) else: keys += kwargs_to_list(kwargs) key = "_".join(keys) re...
def queue_once_key(name, kwargs, restrict_to=None)
Turns a list the name of the task, the kwargs and allowed keys into a redis key.
4.300143
4.118979
1.043983
lock_path = self._get_lock_path(key) try: # Create lock file, raise exception if it exists fd = os.open(lock_path, os.O_CREAT | os.O_EXCL) except OSError as error: if error.errno == errno.EEXIST: # File already exists, check its modifi...
def raise_or_lock(self, key, timeout)
Check the lock file and create one if it does not exist.
2.66481
2.580145
1.032814
lock_path = self._get_lock_path(key) os.remove(lock_path)
def clear_lock(self, key)
Remove the lock file.
3.392921
2.697414
1.257842
once_options = options.get('once', {}) once_graceful = once_options.get( 'graceful', self.once.get('graceful', False)) once_timeout = once_options.get( 'timeout', self.once.get('timeout', self.default_timeout)) if not options.get('retries'): ...
def apply_async(self, args=None, kwargs=None, **options)
Attempts to queues a task. Will raises an AlreadyQueued exception if already queued. :param \*args: positional arguments passed on to the task. :param \*\*kwargs: keyword arguments passed on to the task. :keyword \*\*once: (optional) :param: graceful: (optional) ...
3.922744
3.796883
1.033148
restrict_to = self.once.get('keys', None) args = args or {} kwargs = kwargs or {} call_args = getcallargs( getattr(self, '_orig_run', self.run), *args, **kwargs) # Remove the task instance from the kwargs. This only happens when the # task has the...
def get_key(self, args=None, kwargs=None)
Generate the key from the name of the task (e.g. 'tasks.example') and args/kwargs.
7.7092
7.517
1.025569
# Only clear the lock after the task's execution if the # "unlock_before_run" option is False if not self.unlock_before_run(): key = self.get_key(args, kwargs) self.once_backend.clear_lock(key)
def after_return(self, status, retval, task_id, args, kwargs, einfo)
After a task has run (both succesfully or with a failure) clear the lock if "unlock_before_run" is False.
7.597231
5.074319
1.497192
logger.info("Saving DataFrame to %s", csv_path) df.to_csv(csv_path, index=False, chunksize=chunksize)
def _write_csv(self, df, csv_path, chunksize=10**5)
Parameters ---------- df : pandas.DataFrame csv_path : str chunksize : int Number of rows to write at a time. Helps to limit memory consumption while writing a CSV.
2.628807
3.347488
0.785308
if not csv_path.endswith(".csv"): raise ValueError("Invalid path '%s', must be a CSV file" % csv_path) if csv_path in self._memory_cache: return self._memory_cache[csv_path] if exists(csv_path) and not self.is_empty(csv_path): df = self._read_csv(cs...
def cached_dataframe(self, csv_path, compute_fn)
If a CSV path is in the _memory_cache, then return that cached value. If we've already saved the DataFrame as a CSV then load it. Otherwise run the provided `compute_fn`, and store its result in memory and and save it as a CSV.
2.235965
2.08279
1.073543
if path in self._memory_cache: return self._memory_cache[path] if exists(path) and not self.is_empty(path): obj = load_pickle(path) else: obj = compute_fn() dump_pickle(obj, path) self._memory_cache[path] = obj return obj
def cached_object(self, path, compute_fn)
If `cached_object` has already been called for a value of `path` in this running Python instance, then it should have a cached value in the _memory_cache; return that value. If this function was never called before with a particular value of `path`, then call compute_fn, and pickle it ...
2.538559
2.565034
0.989679
if type(line) is not binary_type: raise TypeError("Expected header line to be of type %s but got %s" % ( binary_type, type(line))) if len(line) <= 1: raise ValueError("No identifier on FASTA line") # split line at first space to get the unique identifier for # this seq...
def _parse_header_id(line)
Pull the transcript or protein identifier from the header line which starts with '>'
4.525469
4.279696
1.057428
fasta_dictionary = {} for (identifier, sequence) in self.iterate_over_file(fasta_path): fasta_dictionary[identifier] = sequence return fasta_dictionary
def read_file(self, fasta_path)
Read the contents of a FASTA file into a dictionary
3.745932
3.24668
1.153773
with self._open(fasta_path) as f: for line in f: line = line.rstrip() if len(line) == 0: continue # have to slice into a bytes object or else I get a single integer first_char = line[0:1] ...
def iterate_over_file(self, fasta_path)
Generator that yields identifiers paired with sequences.
4.336646
4.219553
1.02775
if fasta_path.endswith("gz") or fasta_path.endswith("gzip"): return GzipFile(fasta_path, 'rb') else: return open(fasta_path, 'rb')
def _open(self, fasta_path)
Open either a text file or compressed gzip file as a stream of bytes.
2.390723
2.106528
1.134912
lower_name = name.strip().lower() for reference in Species._reference_names_to_species.keys(): if reference.lower() == lower_name: return reference raise ValueError("Reference genome '%s' not found" % name)
def normalize_reference_name(name)
Search the dictionary of species-specific references to find a reference name that matches aside from capitalization. If no matching reference is found, raise an exception.
5.170602
4.359942
1.185934
reference_name = normalize_reference_name(reference_name) species = find_species_by_reference(reference_name) (min_ensembl_release, max_ensembl_release) = \ species.reference_assemblies[reference_name] if allow_older_downloaded_release: # go through candidate releases in descending ...
def genome_for_reference_name( reference_name, allow_older_downloaded_release=True)
Given a genome reference name, such as "GRCh38", returns the corresponding Ensembl Release object. If `allow_older_downloaded_release` is True, and some older releases have been downloaded, then return the most recent locally available release. Otherwise, return the newest release of Ensembl (even if...
3.798839
3.825787
0.992956
lower_name = name.lower().strip() # if given a common name such as "human", look up its latin equivalent if lower_name in Species._common_names_to_species: return Species._common_names_to_species[lower_name].latin_name return lower_name.replace(" ", "_")
def normalize_species_name(name)
If species name was "Homo sapiens" then replace spaces with underscores and return "homo_sapiens". Also replace common names like "human" with "homo_sapiens".
4.335474
3.68183
1.177532
if isinstance(species_name_or_object, Species): return species_name_or_object elif isinstance(species_name_or_object, str): return find_species_by_name(species_name_or_object) else: raise ValueError("Unexpected type for species: %s : %s" % ( species_name_or_object, t...
def check_species_object(species_name_or_object)
Helper for validating user supplied species names or objects.
1.824526
1.803761
1.011512
species = Species( latin_name=latin_name, synonyms=synonyms, reference_assemblies=reference_assemblies) cls._latin_names_to_species[species.latin_name] = species for synonym in synonyms: if synonym in cls._common_names_to_species: ...
def register(cls, latin_name, synonyms, reference_assemblies)
Create a Species object from the given arguments and enter into all the dicts used to look the species up by its fields.
1.73368
1.629249
1.064097
for species_name in cls.all_registered_latin_names(): species = cls._latin_names_to_species[species_name] for _, release_range in species.reference_assemblies.items(): for release in range(release_range[0], release_range[1] + 1): yield species...
def all_species_release_pairs(cls)
Generator which yields (species, release) pairs for all possible combinations.
3.8687
3.974714
0.973328
if position > self.end or position < self.start: raise ValueError( "Position %d outside valid range %d..%d of %s" % ( position, self.start, self.end, self)) elif self.on_forward_strand: return position - self.start else: ...
def offset(self, position)
Offset of given position from stranded start of this locus. For example, if a Locus goes from 10..20 and is on the negative strand, then the offset of position 13 is 7, whereas if the Locus is on the positive strand, then the offset is 3.
3.328412
2.854088
1.166191
assert start <= end, \ "Locations should always have start < end, got start=%d, end=%d" % ( start, end) if start < self.start or end > self.end: raise ValueError("Range (%d, %d) falls outside %s" % ( start, end, self)) if self.on...
def offset_range(self, start, end)
Database start/end entries are always ordered such that start < end. This makes computing a relative position (e.g. of a stop codon relative to its transcript) complicated since the "end" position of a backwards locus is actually earlir on the strand. This function correctly selects a st...
3.01782
2.765773
1.091131
return (self.on_contig(contig) and (strand is None or self.on_strand(strand)))
def can_overlap(self, contig, strand=None)
Is this locus on the same contig and (optionally) on the same strand?
4.483616
3.390565
1.32238
if self.start > end: # interval is before this exon return self.start - end elif self.end < start: # exon is before the interval return start - self.end else: return 0
def distance_to_interval(self, start, end)
Find the distance between intervals [start1, end1] and [start2, end2]. If the intervals overlap then the distance is 0.
3.879766
3.686711
1.052365
return ( self.can_overlap(contig, strand) and self.distance_to_interval(start, end) == 0)
def overlaps(self, contig, start, end, strand=None)
Does this locus overlap with a given range of positions? Since locus position ranges are inclusive, we should make sure that e.g. chr1:10-10 overlaps with chr1:10-10
6.468866
7.138526
0.906191
cache_key_list = [] # hack to get around the unhashability of lists, # add a special case to convert them to tuples for arg in args: if type(arg) is list: cache_key_list.append(tuple(arg)) else: cache_key_list.append(arg) for (k, v) in sorted(kwargs.item...
def _memoize_cache_key(args, kwargs)
Turn args tuple and kwargs dictionary into a hashable key. Expects that all arguments to a memoized function are either hashable or can be uniquely identified from type(arg) and repr(arg).
2.368812
2.378452
0.995947
cache = {} @wraps(fn) def wrapped_fn(*args, **kwargs): cache_key = _memoize_cache_key(args, kwargs) try: return cache[cache_key] except KeyError: value = fn(*args, **kwargs) cache[cache_key] = value return value def clear_cac...
def memoize(fn)
Simple reset-able memoization decorator for functions and methods, assumes that all arguments to the function can be hashed and compared.
3.459898
3.638202
0.950991
release = check_release_number(release) species = check_species_object(species) return (release, species, server)
def normalize_init_values(cls, release, species, server)
Normalizes the arguments which uniquely specify an EnsemblRelease genome.
5.264882
4.739664
1.110813
init_args_tuple = cls.normalize_init_values(release, species, server) if init_args_tuple in cls._genome_cache: genome = cls._genome_cache[init_args_tuple] else: genome = cls._genome_cache[init_args_tuple] = cls(*init_args_tuple) return genome
def cached( cls, release=MAX_ENSEMBL_RELEASE, species=human, server=ENSEMBL_FTP_SERVER)
Construct EnsemblRelease if it's never been made before, otherwise return an old instance.
3.092169
3.062467
1.009698
if reference_name is None: reference_name = "" if annotation_name is None: annotation_name = "" if annotation_version is None: annotation_version = "" reference_dir = join(CACHE_BASE_SUBDIR, reference_name) annotation_dir = "%s%s" % (annotation_name, annotation_version) ...
def cache_subdirectory( reference_name=None, annotation_name=None, annotation_version=None)
Which cache subdirectory to use for a given annotation database over a particular reference. All arguments can be omitted to just get the base subdirectory for all pyensembl cached datasets.
2.062493
2.04071
1.010674
return ( ('reference_name', self.reference_name,), ('annotation_name', self.annotation_name), ('annotation_version', self.annotation_version), ('cache_directory_path', self.cache_directory_path), ('decompress_on_download', self.decompress_on_d...
def _fields(self)
Fields used for hashing, string representation, equality comparison
3.401756
3.170291
1.073011
for ext in [".gz", ".gzip", ".zip"]: if filename.endswith(ext): return filename[:-len(ext)] return filename
def _remove_compression_suffix_if_present(self, filename)
If the given filename ends in one of the compression suffixes that datacache knows how to deal with, remove the suffix (since we expect the result of downloading to be a decompressed file)
3.066753
3.299944
0.929335
assert path_or_url, "Expected non-empty string for path_or_url" remote_filename = split(path_or_url)[1] if self.is_url_format(path_or_url): # passing `decompress=False` since there is logic below # for stripping decompression extensions for both local ...
def cached_path(self, path_or_url)
When downloading remote files, the default behavior is to name local files the same as their remote counterparts.
4.950798
4.720345
1.048821
cached_path = self.cached_path(url) missing = not exists(cached_path) if (missing or overwrite) and download_if_missing: logger.info("Fetching %s from URL %s", cached_path, url) datacache.download._download_and_decompress_if_necessary( full_path=c...
def _download_if_necessary(self, url, download_if_missing, overwrite)
Return local cached path to a remote file, download it if necessary.
4.306239
3.861739
1.115104
local_path = abspath(local_path) if not exists(local_path): raise MissingLocalFile(local_path) elif not self.copy_local_files_to_cache: return local_path else: cached_path = self.cached_path(local_path) if exists(cached_path) and n...
def _copy_if_necessary(self, local_path, overwrite)
Return cached path to local file, copying it to the cache if necessary.
2.46447
2.144148
1.149393
assert path_or_url, "Expected non-empty string for path_or_url" if self.is_url_format(path_or_url): return self._download_if_necessary( path_or_url, download_if_missing, overwrite) else: return self._copy_if_necessa...
def download_or_copy_if_necessary( self, path_or_url, download_if_missing=False, overwrite=False)
Download a remote file or copy Get the local path to a possibly remote file. Download if file is missing from the cache directory and `download_if_missing` is True. Download even if local file exists if both `download_if_missing` and `overwrite` are True. If the file is on the ...
2.341789
3.083736
0.7594
for filename in listdir(self.cache_directory_path): delete = ( any([filename.endswith(ext) for ext in suffixes]) or any([filename.startswith(pre) for pre in prefixes])) if delete: path = join(self.cache_directory_path, filename) ...
def delete_cached_files(self, prefixes=[], suffixes=[])
Deletes any cached files matching the prefixes or suffixes given
2.541379
2.578537
0.98559