475 lines
340 KiB
HTML
475 lines
340 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang="en" class="light TridactylThemeDefault"><head>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<meta charset="utf-8">
|
||
|
<link rel="icon" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/favicon.png">
|
||
|
<link rel="manifest" href="https://chat.depeuter.dev/manifest.json">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||
|
<script>
|
||
|
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
|
||
|
if (
|
||
|
localStorage.theme === 'light' ||
|
||
|
(!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: light)').matches)
|
||
|
) {
|
||
|
document.documentElement.classList.add('light');
|
||
|
} else if (localStorage.theme) {
|
||
|
localStorage.theme.split(' ').forEach((e) => {
|
||
|
document.documentElement.classList.add(e);
|
||
|
});
|
||
|
} else {
|
||
|
document.documentElement.classList.add('dark');
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/entry/start.a008403e.js">
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/chunks/scheduler.f6668761.js">
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/chunks/singletons.0af75cc8.js">
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/chunks/index.afef706d.js">
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/entry/app.fb362f1a.js">
|
||
|
<link rel="modulepreload" href="https://chat.depeuter.dev/_app/immutable/chunks/index.65c87b32.js">
|
||
|
<style type="text/css">@media print {
|
||
|
.TridactylStatusIndicator {
|
||
|
display: none !important;
|
||
|
}
|
||
|
}</style><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/nodes/0.a3180f91.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/constants.affb70c1.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/navigation.4d675c85.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/Toaster.svelte_svelte_type_style_lang.4f4e956a.js"><link rel="stylesheet" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/Toaster.3a6d0da3.css"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/each.2cd6bd47.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.cce326ae.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.a0e4648e.js"><link rel="stylesheet" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/0.de36d200.css"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/nodes/1.439e4b1a.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/stores.96135f2d.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/nodes/2.e9fd4742.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/FileSaver.min.898eb36f.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/_commonjsHelpers.de833af9.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.cb1db698.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.150eea5c.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.447671ac.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.1ab02664.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/Modal.68c1a0a5.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/AdvancedParams.020cee97.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.94bf9275.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.4062ae9e.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.03ddf5e5.js"><link rel="stylesheet" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/2.c7820e19.css"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/nodes/5.17634acd.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/index.5af3041e.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/Tags.4457bfcc.js"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/chunks/dayjs.min.1e504c00.js"><link rel="stylesheet" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/index.8a27bb7e.css"><script src="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/page-script.js" id="bw-fido2-page-script"></script><title>Open WebUI</title><link rel="stylesheet" type="text/css" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/rosepine.css"><link rel="stylesheet" type="text/css" href="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/rosepine-dawn.css"><link rel="modulepreload" as="script" crossorigin="" href="https://chat.depeuter.dev/_app/immutable/nodes/3.3da18224.js"></head>
|
||
|
<body data-sveltekit-preload-data="hover" style="overflow: unset;">
|
||
|
<div style="display: contents"> <div class="hidden lg:flex fixed bottom-0 right-0 px-3 py-3 z-10"><button id="show-shortcuts-button" class="text-gray-600 dark:text-gray-300 bg-gray-300/20 w-6 h-6 flex items-center justify-center text-xs rounded-full">?</button></div> <div class="app relative"> <div class="text-gray-700 dark:text-gray-100 bg-white dark:bg-gray-900 min-h-screen overflow-auto flex flex-row"><div class="h-screen max-h-[100dvh] min-h-screen -translate-x-[260px] w-[0px] bg-black text-gray-200 shadow-2xl text-sm transition z-40 fixed top-0 left-0"><div class="py-2.5 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] invisible"><div class="px-2.5 flex justify-center space-x-2"><button id="sidebar-new-chat-button" class="flex-grow flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition"><div class="flex self-center"><div class="self-center mr-1.5"><img src="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/favicon.png" class="w-7 -translate-x-1.5 rounded-full" alt="logo"></div> <div class="self-center font-medium text-sm">New Chat</div></div> <div class="self-center"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4"><path d="M5.433 13.917l1.262-3.155A4 4 0 017.58 9.42l6.92-6.918a2.121 2.121 0 013 3l-6.92 6.918c-.383.383-.84.685-1.343.886l-3.154 1.262a.5.5 0 01-.65-.65z"></path><path d="M3.5 5.75c0-.69.56-1.25 1.25-1.25H10A.75.75 0 0010 3H4.75A2.75 2.75 0 002 5.75v9.5A2.75 2.75 0 004.75 18h9.5A2.75 2.75 0 0017 15.25V10a.75.75 0 00-1.5 0v5.25c0 .69-.56 1.25-1.25 1.25h-9.5c-.69 0-1.25-.56-1.25-1.25v-9.5z"></path></svg></div></button></div> <div class="relative flex flex-col flex-1 overflow-y-auto"> <div class="px-2.5 mt-1 mb-2 flex justify-center space-x-2"><div class="flex w-full" id="chat-search"><div class="self-center pl-3 py-2 rounded-l bg-gray-950"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4"><path fill-rule="evenodd" d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z" clip-rule="evenodd"></path></svg></div> <input class="w-full rounded-r py-1.5 pl-2.5 pr-4 text-sm text-gray-300 bg-gray-950 outline-none" placeholder="Search"></div></div> <div class="pl-2.5 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto"><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Is there a more efficient way to implement this loop?</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[120px]">
|
||
|
The header for this code can be improved by following the PEP 8 style
|
||
|
guide and using more descriptive variable names. Here's a revised
|
||
|
version of the header with improved naming conventions:
|
||
|
```python
|
||
|
# https://dodona.be/nl/courses/3363/series/36080/activities/1128421449
|
||
|
|
||
|
from Bio import SeqIO
|
||
|
|
||
|
nucleotides = ['A', 'C', 'G', 'T']
|
||
|
|
||
|
|
||
|
def most_frequent_kmers(sequence: str, k: int, d: int) -> set:
|
||
|
"""Find the most frequent k-mers in a given DNA sequence.
|
||
|
|
||
|
Args:
|
||
|
sequence (str): The DNA sequence to search for k-mers.
|
||
|
k (int): The length of the k-mers.
|
||
|
d (int): The maximum Hamming distance between k-mers.
|
||
|
|
||
|
Returns:
|
||
|
A set containing the most frequent k-mers in the given sequence.
|
||
|
|
||
|
>>> most_frequent_kmers('ACGTTGCATGTCGCATGATGCATGAGAGCT',
|
||
|
4, 1)
|
||
|
{'ACAT', 'ATGT'}
|
||
|
>>> most_frequent_kmers('AACAAGCTGATAAACATTTAAAGAG', 5, 1)
|
||
|
{'TTAAA', 'TTTAA', 'TTTTA', 'TAAAA'}
|
||
|
>>> most_frequent_kmers('AAAATTTTTTTT', 3, 0)
|
||
|
{}
|
||
|
>>> most_frequent_kmers('CAT', 1, 0)
|
||
|
{}
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> most_frequent_kmers(*SeqIO.parse('data/09-data.fna',
|
||
|
'fasta'), 10, 2)
|
||
|
{'CCGGCGGCCG', 'CGGCCGCCGG'}
|
||
|
"""
|
||
|
```</div></div></button> <div class="absolute right-[22px] top-[10px]"><div class="flex self-center space-x-1.5"><button id="delete-chat-button" class="hidden"></button> <button class="self-center hover:text-white transition"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"></path></svg></button> <button class="self-center hover:text-white transition"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"></path></svg></button></div></div> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Header: "Flatten recursion"</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Improving the running time of this function can be done by reducing the
|
||
|
number of iterations through the suffix_neighbors set and by using a
|
||
|
more efficient algorithm for computing the Hamming distance between two
|
||
|
strings. Here are a few ways to optimize the function:
|
||
|
|
||
|
1. Use a more efficient data structure for storing the d-neighborhood,
|
||
|
such as a list or a dictionary, rather than a set. This can help reduce
|
||
|
the time it takes to add and remove elements from the neighborhood.
|
||
|
2. Replace the inner loop with a more efficient algorithm for computing
|
||
|
the Hamming distance between two strings. One way to do this is to use
|
||
|
the Levenshtein distance algorithm, which has a time complexity of
|
||
|
O(n+m) instead of O(nm), where n and m are the lengths of the two
|
||
|
strings.
|
||
|
3. Use memoization or dynamic programming to cache the results of
|
||
|
previous calls to the function, so that if the same d-neighborhood is
|
||
|
requested multiple times, it can be returned more quickly from memory
|
||
|
instead of being recomputed each time. This can help reduce the overall
|
||
|
running time of the function by avoiding redundant computations.
|
||
|
|
||
|
Here's an example of how you could modify the function to use a list for
|
||
|
storing the d-neighborhood and implement memoization:
|
||
|
```python
|
||
|
import collections
|
||
|
|
||
|
def neighbors(s: str, d: int) -> set:
|
||
|
"""Return the d-neighborhood of s
|
||
|
The d-neighborhood of a string s is the set of all k-mers whose
|
||
|
Hamming distance from s does not exceed d.
|
||
|
|
||
|
>>> neighbors('ACG', 1)
|
||
|
{'ACG', 'ACC', 'AAG', 'AGG', 'CCG', 'TCG', 'GCG', 'ACT', 'ATG',
|
||
|
'ACA'}
|
||
|
"""
|
||
|
|
||
|
if isinstance(s, SeqIO.SeqRecord):
|
||
|
s = s.seq
|
||
|
|
||
|
if d == 0:
|
||
|
return {str(s)}
|
||
|
if len(s) == 1:
|
||
|
return {'A', 'C', 'G', 'T'}
|
||
|
|
||
|
neighborhood = []
|
||
|
suffix_neighbors = neighbors(s[1:], d)
|
||
|
|
||
|
for text in suffix_neighbors:
|
||
|
hamming_distance = LevenshteinDistance.distance(s[1:], text)
|
||
|
if hamming_distance < d:
|
||
|
neighborhood += ['A' + text, 'C' + text, 'G' + text, 'T' +
|
||
|
text]
|
||
|
else:
|
||
|
neighborhood += [s[0] + text]
|
||
|
|
||
|
return neighborhood
|
||
|
|
||
|
# Memoization cache for storing the results of previous calls to the
|
||
|
function
|
||
|
neighborhood_cache = collections.defaultdict(list)
|
||
|
|
||
|
def memoized_neighbors(s: str, d: int):
|
||
|
"""Return the d-neighborhood of s using memoization"""
|
||
|
if (s, d) in neighborhood_cache:
|
||
|
return neighborhood_cache[(s, d)]
|
||
|
else:
|
||
|
neighborhood = neighbors(s, d)
|
||
|
neighborhood_cache[(s, d)] = neighborhood
|
||
|
return neighborhood
|
||
|
```</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> "The Artistry of Bread Making: A User's Guide"
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Header: "Settings Error"</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Optimize this code:
|
||
|
```python
|
||
|
# Optimized
|
||
|
from Bio import SeqIO
|
||
|
|
||
|
def most_frequent_kmers(s: str, k: int, d: int) -> set:
|
||
|
"""Returns the set of most frequent k-mers with up to d mismatches in s
|
||
|
|
||
|
>>> most_frequent_kmers('ACGTTGCATGTCGCATGATGCATGAGAGCT', 4, 1)
|
||
|
{'GATG', 'ATGT', 'ATGC'}
|
||
|
>>> most_frequent_kmers('AACAAGCTGATAAACATTTAAAGAG', 5, 1)
|
||
|
{'AAAAA'}
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> most_frequent_kmers(*SeqIO.parse('8-data.fna', 'fasta'), 10, 2)
|
||
|
{'GCACACAGAC', 'GCGCACACAC'}
|
||
|
"""
|
||
|
|
||
|
if isinstance(s, SeqIO.SeqRecord):
|
||
|
s = s.seq
|
||
|
|
||
|
# Calculate the number of possible k-mers in the string
|
||
|
n_kmers = 4 ** k
|
||
|
|
||
|
# Create a dictionary to store the counts for each k-mer
|
||
|
kmer_counts = {}
|
||
|
|
||
|
# Iterate through each k-mer in the string
|
||
|
for i in range(len(s) - k + 1):
|
||
|
kmer = s[i:i+k]
|
||
|
if kmer not in kmer_counts:
|
||
|
kmer_counts[kmer] = 0
|
||
|
kmer_counts[kmer] += 1
|
||
|
|
||
|
# Find the maximum count of any k-mer
|
||
|
max_count = max(kmer_counts.values())
|
||
|
|
||
|
# Return the set of k-mers with the highest count, up to d mismatches
|
||
|
return {kmer for kmer, count in kmer_counts.items() if count == max_count}
|
||
|
```</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Optimize Pattern Count</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> Hello World!
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> Meme title: ?
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Rewrite using list comprehension:
|
||
|
```python
|
||
|
skew_over_time = [0] + [(i == 'G' and 1) or (i == 'C' and -1) for i in s]
|
||
|
```</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> "The Basics of Chatbots"
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> "Home Assistant Integration"
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Plant Life</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> Greetings from AI!
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
"Discord Rich Presence Updater"</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Backup ZFS with Restic</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
This line uses `curl` with the `--netrc`, `-k`, and `-a` options to transfer a file using SFTP.</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
Meaning of dmesg message</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]"> Greetings! How can I assist you today?
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
"""
|
||
|
|
||
|
</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
AI in LaTeX</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">
|
||
|
"Terminal Closes with Stack Smashing Error"</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">New Chat</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Compose row with icon.</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Android Button w/ Icon</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"></path></svg></div> <div class="text-left self-center overflow-hidden w-[180px]">Git selective line commit.</div></div></button> </div><div class="w-full pr-2 relative"><button class="w-full flex justify-between rounded-md px-3 py-2 hover:bg-gray-900 transition whitespace-nowrap text-ellipsis"><div class="flex self-center flex-1"><div class="self-center mr-3"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.22
|
||
|
The header for this code can be improved by following the PEP 8 style
|
||
|
guide and using more descriptive variable names. Here's a revised
|
||
|
version of the header with improved naming conventions:
|
||
|
```python
|
||
|
# https://dodona.be/nl/courses/3363/series/36080/activities/1128421449
|
||
|
|
||
|
from Bio import SeqIO
|
||
|
|
||
|
nucleotides = ['A', 'C', 'G', 'T']
|
||
|
|
||
|
|
||
|
def most_frequent_kmers(sequence: str, k: int, d: int) -> set:
|
||
|
"""Find the most frequent k-mers in a given DNA sequence.
|
||
|
|
||
|
Args:
|
||
|
sequence (str): The DNA sequence to search for k-mers.
|
||
|
k (int): The length of the k-mers.
|
||
|
d (int): The maximum Hamming distance between k-mers.
|
||
|
|
||
|
Returns:
|
||
|
A set containing the most frequent k-mers in the given sequence.
|
||
|
|
||
|
>>> most_frequent_kmers('ACGTTGCATGTCGCATGATGCATGAGAGCT',
|
||
|
4, 1)
|
||
|
{'ACAT', 'ATGT'}
|
||
|
>>> most_frequent_kmers('AACAAGCTGATAAACATTTAAAGAG', 5, 1)
|
||
|
{'TTAAA', 'TTTAA', 'TTTTA', 'TAAAA'}
|
||
|
>>> most_frequent_kmers('AAAATTTTTTTT', 3, 0)
|
||
|
{}
|
||
|
>>> most_frequent_kmers('CAT', 1, 0)
|
||
|
{}
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> most_frequent_kmers(*SeqIO.parse('data/09-data.fna',
|
||
|
'fasta'), 10, 2)
|
||
|
{'CCGGCGGCCG', 'CGGCCGCCGG'}
|
||
|
"""
|
||
|
```</div></div> <div class="pl-2 self-center flex items-center space-x-2"><div class="flex flex-row space-x-0.5 line-clamp-1"> <div class="flex space-x-1 pl-1.5"><div class="flex items-center"><input class="cursor-pointer self-center text-xs h-fit bg-transparent outline-none line-clamp-1 w-[4rem]" placeholder="Add a tag"> <button type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-3 h-3"><path fill-rule="evenodd" d="M12.416 3.376a.75.75 0 0 1 .208 1.04l-5 7.5a.75.75 0 0 1-1.154.114l-3-3a.75.75 0 0 1 1.06-1.06l2.353 2.353 4.493-6.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></button></div> <button class="cursor-pointer self-center p-0.5 space-x-1 flex h-fit items-center dark:hover:bg-gray-700 rounded-full transition border dark:border-gray-600 border-dashed" type="button"><div class="m-auto self-center"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-3 h-3 rotate-45 transition-all transform"><path d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"></path></svg></div></button></div></div> <button class="cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition border dark:border-gray-600"><div class="m-auto self-center"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-4 h-4"><path fill-rule="evenodd" d="M15.75 4.5a3 3 0 1 1 .825 2.066l-8.421 4.679a3.002 3.002 0 0 1 0 1.51l8.421 4.679a3 3 0 1 1-.729 1.31l-8.421-4.678a3 3 0 1 1 0-4.132l8.421-4.679a3 3 0 0 1-.096-.755Z" clip-rule="evenodd"></path></svg></div></button></div></div></div></nav> <div class="flex flex-col flex-auto"><div class="pb-2.5 flex flex-col justify-between w-full flex-auto overflow-auto h-0" id="messages-container"><div class="max-w-2xl md:px-0 mx-auto w-full px-4"><div class="flex flex-col my-2"><div class="flex"><select id="models" class="outline-none bg-transparent text-lg font-semibold rounded-lg block w-full placeholder-gray-400" disabled="disabled"><option class="text-gray-700" value="" disabled="disabled">Select a model</option><option value="btc_trader_one:latest" class="text-gray-700 text-lg">btc_trader_one:latest (3.8GB)</option><option value="codellama:latest" class="text-gray-700 text-lg" selected="selected">codellama:latest (3.6GB)</option><option value="dolphin-mixtral:latest" class="text-gray-700 text-lg">dolphin-mixtral:latest (24.6GB)</option><option value="javascript-console:latest" class="text-gray-700 text-lg">javascript-console:latest (3.6GB)</option><option value="llama2:13b" class="text-gray-700 text-lg">llama2:13b (6.9GB)</option><option value="llama2:latest" class="text-gray-700 text-lg">llama2:latest (3.6GB)</option><option value="llava:latest" class="text-gray-700 text-lg">llava:latest (4.1GB)</option><option value="mario:latest" class="text-gray-700 text-lg">mario:latest (3.6GB)</option><option value="mistral:latest" class="text-gray-700 text-lg">mistral:latest (3.8GB)</option><option value="phi:latest" class="text-gray-700 text-lg">phi:latest (1.5GB)</option><option value="stable-code:latest" class="text-gray-700 text-lg">stable-code:latest (1.5GB)</option></select> <button class="self-center mr-3 disabled:text-gray-600 disabled:hover:text-gray-600" disabled="disabled"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m6-6H6"></path></svg></button> <button class="self-center dark:hover:text-gray-300" id="open-settings-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M10.343 3.94c.09-.542.56-.94 1.11-.94h1.093c.55 0 1.02.398 1.11.94l.149.894c.07.424.384.764.78.93.398.164.855.142 1.205-.108l.737-.527a1.125 1.125 0 011.45.12l.773.774c.39.389.44 1.002.12 1.45l-.527.737c-.25.35-.272.806-.107 1.204.165.397.505.71.93.7
|
||
|
```python
|
||
|
# https://dodona.be/nl/courses/3363/series/36080/activities/1128421449
|
||
|
|
||
|
from Bio import SeqIO
|
||
|
|
||
|
nucleotides = ['A', 'C', 'G', 'T']
|
||
|
|
||
|
|
||
|
def most_frequent_kmers(s: str, k: int, d: int) -> set:
|
||
|
"""
|
||
|
>>> most_frequent_kmers('ACGTTGCATGTCGCATGATGCATGAGAGCT', 4, 1)
|
||
|
{'ACAT', 'ATGT'}
|
||
|
>>> most_frequent_kmers('AACAAGCTGATAAACATTTAAAGAG', 5, 1)
|
||
|
{'TTAAA', 'TTTAA', 'TTTTA', 'TAAAA'}
|
||
|
>>> most_frequent_kmers('AAAATTTTTTTT', 3, 0)
|
||
|
{}
|
||
|
>>> most_frequent_kmers('CAT', 1, 0)
|
||
|
{}
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> most_frequent_kmers(*SeqIO.parse('data/09-data.fna', 'fasta'), 10, 2)
|
||
|
{'CCGGCGGCCG', 'CGGCCGCCGG'}
|
||
|
"""
|
||
|
|
||
|
if isinstance(s, SeqIO.SeqRecord):
|
||
|
s = s.seq
|
||
|
|
||
|
neighborhoods = []
|
||
|
for i in range(len(s) - k + 1):
|
||
|
# neighborhoods.extend(neighbors(s[i:i + k], d))
|
||
|
|
||
|
neighborhood = neighbors(s[i:i + k], d)
|
||
|
neighborhoods.extend(neighborhood)
|
||
|
neighborhoods.extend(map(reverse_complement, neighborhood))
|
||
|
|
||
|
# neighborhoods.extend(map(reverse_complement, neighborhoods))
|
||
|
|
||
|
count = [1 for _ in range(len(neighborhoods))]
|
||
|
index = [pattern_to_number(neighborhood) for _, neighborhood in enumerate(neighborhoods)]
|
||
|
|
||
|
index.sort()
|
||
|
|
||
|
for i in range(len(neighborhoods) - 1):
|
||
|
if index[i] == index[i + 1]:
|
||
|
count[i + 1] = count[i] + 1
|
||
|
|
||
|
max_count = max(count)
|
||
|
return {number_to_pattern(index[i], k) for i in range(len(neighborhoods)) if count[i] == max_count}
|
||
|
|
||
|
|
||
|
def neighbors(s: str, d: int):
|
||
|
if d == 0:
|
||
|
return {s}
|
||
|
if len(s) == 1:
|
||
|
return {'A', 'C', 'G', 'T'}
|
||
|
|
||
|
neighborhood = set()
|
||
|
suffix_neighbors = neighbors(s[1:], d)
|
||
|
|
||
|
for text in suffix_neighbors:
|
||
|
if hamming_distance(s[1:], text) < d:
|
||
|
for x in ['A', 'C', 'G', 'T']:
|
||
|
neighborhood.add(x + text)
|
||
|
else:
|
||
|
neighborhood.add(s[0] + text)
|
||
|
|
||
|
return neighborhood
|
||
|
|
||
|
|
||
|
def pattern_to_number(pattern):
|
||
|
result = 0
|
||
|
for _, symbol in enumerate(pattern):
|
||
|
result = (result * 4) + nucleotides.index(symbol)
|
||
|
return result
|
||
|
|
||
|
|
||
|
def number_to_pattern(index, k):
|
||
|
if k == 1:
|
||
|
return nucleotides[index]
|
||
|
return number_to_pattern(index // 4, k - 1) + nucleotides[index % 4]
|
||
|
|
||
|
|
||
|
def approximate_pattern_count(s: str, pattern: str, d: int) -> int:
|
||
|
count = 0
|
||
|
for i in range(len(s) - len(pattern) + 1):
|
||
|
pattern_ = s[i:i + len(pattern)]
|
||
|
count += hamming_distance(pattern, pattern_) <= d
|
||
|
return count
|
||
|
|
||
|
|
||
|
def hamming_distance(s: {str, SeqIO}, t: {str, SeqIO}) -> int:
|
||
|
"""Return how many mismatches there are between two strings. This is called the Hamming distance
|
||
|
|
||
|
>>> hamming_distance('GGGCCGTTGGT', 'GGACCGTTGAC')
|
||
|
3
|
||
|
>>> hamming_distance('AAAA', 'TTTT')
|
||
|
4
|
||
|
>>> hamming_distance('ACGTACGT', 'TACGTACG')
|
||
|
8
|
||
|
>>> hamming_distance('ACGTACGT', 'CCCCCCCC')
|
||
|
6
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> hamming_distance(*SeqIO.parse('data/06-data.fna', 'fasta'))
|
||
|
859
|
||
|
"""
|
||
|
|
||
|
# Convert SeqRecords to strings
|
||
|
if isinstance(s, SeqIO.SeqRecord):
|
||
|
s = s.seq
|
||
|
t = t.seq
|
||
|
|
||
|
# https://stackoverflow.com/a/27109562
|
||
|
return sum(s_ != t_ for s_, t_ in zip(s, t))
|
||
|
|
||
|
|
||
|
def reverse_complement(s):
|
||
|
"""Return the reverse complement
|
||
|
|
||
|
>>> reverse_complement('GTCA')
|
||
|
'TGAC'
|
||
|
>>> reverse_complement('CGATATATCCATAG')
|
||
|
'CTATGGATATATCG'
|
||
|
|
||
|
>>> from Bio import SeqIO
|
||
|
>>> reverse_complement(*SeqIO.parse('data/02-data.fna', 'fasta'))
|
||
|
'ACCGGGTTTT'
|
||
|
"""
|
||
|
|
||
|
complement = dict(zip('ACGT', 'TGCA'))
|
||
|
return ''.join([complement[base] for base in s[::-1]])
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
import doctest
|
||
|
|
||
|
doctest.testmod()
|
||
|
|
||
|
```</pre> <div class="flex justify-start space-x-1"><div class="flex self-center"><button class="self-center"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4"><path fill-rule="evenodd" d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z" clip-rule="evenodd"></path></svg></button> <div class="text-xs font-bold self-center">3 / 3</div> <button class="self-center"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-4 h-4"><path fill-rule="evenodd" d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z" clip-rule="evenodd"></path></svg></button></div> <button class="invisible group-hover:visible p-1 rounded dark:hover:text-white transition edit-user-message-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"></path></svg></button> <button class="invisible group-hover:visible p-1 rounded dark:hover:text-white transition"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"></path></svg></button></div></div></div></div></div> </div></div><div class="w-full"><div class="flex flex-col justify-between px-5 mb-3 max-w-3xl mx-auto rounded-lg group"><div class="flex w-full message-d7c5989d-fa77-473d-9dff-0562eb03dbb5 svelte-1u5gq5j"><div class="mr-4"><img src="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/favicon.png" class="max-w-[28px] object-cover rounded-full" alt="profile" draggable="false"></div> <div class="w-full overflow-hidden"><div class="self-center font-bold mb-0.5 capitalize line-clamp-1"> codellama:latest <span class="invisible group-hover:visible text-gray-400 text-xs font-medium">20/02/2024 11:52</span></div> <div class="prose chat-assistant w-full max-w-full dark:prose-invert prose-headings:my-0 prose-p:m-0 prose-p:-mb-6 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-8 prose-li:-mb-4 whitespace-pre-line svelte-1u5gq5j"><div><div class="w-full"><p>The code is well-structured and follows the PEP 8 style guide, with consistent spacing and indentation. However, there are a few suggestions to make it even more Pythonic:</p>
|
||
|
<ol>
|
||
|
<li>Use type hints for function parameters and return values. This helps other developers understand the intended use of the functions and catch errors at compile time. For example, the <code>most_frequent_kmers</code> function can be annotated with types like this:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre"><span class="hljs-keyword">def</span> <span class="hljs-title function_">most_frequent_kmers</span>(<span class="hljs-params">s: <span class="hljs-built_in">str</span>, k: <span class="hljs-built_in">int</span>, d: <span class="hljs-built_in">int</span></span>) -> <span class="hljs-built_in">set</span>:
|
||
|
<span class="hljs-comment"># ...</span></code></pre></div><ol start="2">
|
||
|
<li>Use a consistent naming convention for variables and functions. The code currently uses both camelCase and snake_case, which can be confusing to read. It's recommended to stick with one style throughout the code.</li>
|
||
|
<li>Add type hints for the variables in the <code>most_frequent_kmers</code> function. For example, the <code>neighborhoods</code> variable is initialized as an empty list and then extended with strings from the <code>neighbors</code> function. It would be more clear to declare it as a list of strings from the start:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre">neighborhoods: <span class="hljs-type">List</span>[<span class="hljs-built_in">str</span>] = []</code></pre></div><ol start="4">
|
||
|
<li>Use the <code>f-string</code> notation for string formatting, instead of concatenating with <code>+</code>. For example, in the <code>number_to_pattern</code> function, you can use:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre"><span class="hljs-keyword">return</span> <span class="hljs-string">f'<span class="hljs-subst">{index}</span><span class="hljs-subst">{nucleotides[index % <span class="hljs-number">4</span>]}</span>'</span></code></pre></div><ol start="5">
|
||
|
<li>Use the <code>enumerate</code> built-in function to iterate over both the index and value of a sequence at the same time, instead of using two separate for loops:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre"><span class="hljs-keyword">for</span> i, neighborhood <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(neighborhoods):
|
||
|
count[i] = approximate_pattern_count(s, neighborhood, d)</code></pre></div><ol start="6">
|
||
|
<li>Use the <code>max</code> function to find the maximum value in a list of integers, instead of iterating over the list and comparing values:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre">max_count = <span class="hljs-built_in">max</span>(count)</code></pre></div><ol start="7">
|
||
|
<li>Use the <code>set</code> constructor instead of the <code>{}</code> notation to create a set from a sequence:</li>
|
||
|
</ol>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre"><span class="hljs-keyword">return</span> {number_to_pattern(index, k) <span class="hljs-keyword">for</span> index <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(neighborhoods)) <span class="hljs-keyword">if</span> count[i] == max_count}</code></pre></div><ol start="8">
|
||
|
<li>Use the <code>doctest</code> module to test your functions instead of running them manually. This makes it easier to maintain and update the code.</li>
|
||
|
<li>Consider adding a docstring for each function, explaining what they do and their parameters.</li>
|
||
|
<li>Consider using the <code>SeqIO</code> module to parse FASTA files instead of writing your own parser.</li>
|
||
|
</ol>
|
||
|
<p>Here's the updated code with these suggestions:</p>
|
||
|
<div class="mb-4"><div class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"><div class="p-1">python</div> <button class="copy-code-button bg-none border-none p-1">Copy Code</button></div> <pre class="rounded-b-lg hljs p-4 px-5 overflow-x-auto rounded-t-none"><code class="language-python rounded-t-none whitespace-pre"><span class="hljs-keyword">import</span> doctest
|
||
|
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>, <span class="hljs-type">Set</span>, <span class="hljs-type">Tuple</span>
|
||
|
|
||
|
nucleotides = [<span class="hljs-string">"A"</span>, <span class="hljs-string">"C"</span>, <span class="hljs-string">"G"</span>, <span class="hljs-string">"T"</span>]
|
||
|
|
||
|
<span class="hljs-keyword">def</span> <span class="hljs-title function_">most_frequent_kmers</span>(<span class="hljs-params">s: <span class="hljs-built_in">str</span>, k: <span class="hljs-built_in">int</span>, d: <span class="hljs-built_in">int</span></span>) -> <span class="hljs-type">Set</span>[<span class="hljs-built_in">str</span>]:
|
||
|
<span class="hljs-string">"""Find the most frequent k-mers in a string s.
|
||
|
|
||
|
Args:
|
||
|
s (str): The input string.
|
||
|
k (int): The length of the k-mer.
|
||
|
d (int): The maximum distance between two kmers to be considered similar.
|
||
|
|
||
|
Returns:
|
||
|
A set of most frequent k-mers in the string.
|
||
|
"""</span>
|
||
|
<span class="hljs-comment"># Create a list of all possible k-mers</span>
|
||
|
all_kmers = [s[i:i+k] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(s) - k + <span class="hljs-number">1</span>)]
|
||
|
|
||
|
<span class="hljs-comment"># Count the number of occurrences of each k-mer</span>
|
||
|
kmer_count = {kmer: s.count(kmer) <span class="hljs-keyword">for</span> kmer <span class="hljs-keyword">in</span> all_kmers}
|
||
|
|
||
|
<span class="hljs-comment"># Sort the k-mers by their count and return the most frequent ones</span>
|
||
|
sorted_kmers = <span class="hljs-built_in">sorted</span>(kmer_count, key=<span class="hljs-keyword">lambda</span> x: kmer_count[x], reverse=<span class="hljs-literal">True</span>)[:d]
|
||
|
<span class="hljs-keyword">return</span> {sorted_kmer <span class="hljs-keyword">for</span> sorted_kmer <span class="hljs-keyword">in</span> sorted_kmers}
|
||
|
|
||
|
<span class="hljs-keyword">def</span> <span class="hljs-title function_">hamming_distance</span>(<span class="hljs-params">s1: <span class="hljs-built_in">str</span>, s2: <span class="hljs-built_in">str</span></span>) -> <span class="hljs-built_in">int</span>:
|
||
|
<span class="hljs-string">"""Return how many mismatches there are between two strings. This is called the Hamming distance.
|
||
|
|
||
|
Args:
|
||
|
s1 (str): The first input string.
|
||
|
s2 (str): The second input string.
|
||
|
|
||
|
Returns:
|
||
|
An integer representing the number of mismatches.
|
||
|
"""</span>
|
||
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">sum</span>(c1 != c2 <span class="hljs-keyword">for</span> c1, c2 <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(s1, s2))
|
||
|
|
||
|
<span class="hljs-keyword">def</span> <span class="hljs-title function_">reverse_complement</span>(<span class="hljs-params">s: <span class="hljs-built_in">str</span></span>) -> <span class="hljs-built_in">str</span>:
|
||
|
<span class="hljs-string">"""Return the reverse complement of a DNA string.
|
||
|
|
||
|
Args:
|
||
|
s (str): The input DNA string.
|
||
|
|
||
|
Returns:
|
||
|
The reverse complement of the input string.
|
||
|
"""</span>
|
||
|
<span class="hljs-keyword">return</span> <span class="hljs-string">""</span>.join([nucleotides[<span class="hljs-number">3</span> - i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)])
|
||
|
|
||
|
<span class="hljs-keyword">def</span> <span class="hljs-title function_">test_most_frequent_kmers</span>():
|
||
|
s = <span class="hljs-string">"GGTCAGGAGCTACCGTCATT"</span>
|
||
|
k = <span class="hljs-number">5</span>
|
||
|
d = <span class="hljs-number">2</span>
|
||
|
expected = {<span class="hljs-string">"GGTCA"</span>, <span class="hljs-string">"GGTAG"</span>}
|
||
|
<span class="hljs-keyword">assert</span> most_frequent_kmers(s, k, d) == expected
|
||
|
|
||
|
<span class="hljs-keyword">def</span> <span class="hljs-title function_">test_hamming_distance</span>():
|
||
|
s1 = <span class="hljs-string">"GGACCGTTGAC"</span>
|
||
|
s2 = <span class="hljs-string">"GGGCCGTTGGT"</span>
|
||
|
expected = <span class="hljs-number">3</span>
|
||
|
<span class="hljs-keyword">assert</span> hamming_distance(s1, s2) == expected
|
||
|
|
||
|
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
|
||
|
doctest.testmod()</code></pre></div> <div class="flex justify-start space-x-1 -mt-1 overflow-x-auto buttons text-gray-700 dark:text-gray-500 svelte-1u5gq5j"> <button class="visible p-1 rounded dark:hover:text-white hover:text-black transition"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"></path></svg></button> <button class="visible p-1 rounded dark:hover:text-white hover:text-black transition copy-response-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"></path></svg></button> <button class="visible p-1 rounded dark:hover:text-white hover:text-black transition"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="w-4 h-4" xmlns="http://www.w3.org/2000/svg"><path d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"></path></svg></button> <button class="visible p-1 rounded dark:hover:text-white hover:text-black transition"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="w-4 h-4" xmlns="http://www.w3.org/2000/svg"><path d="M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17"></path></svg></button> <button id="speak-button-d7c5989d-fa77-473d-9dff-0562eb03dbb5" class="visible p-1 rounded dark:hover:text-white hover:text-black transition"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M19.114 5.636a9 9 0 010 12.728M16.463 8.288a5.25 5.25 0 010 7.424M6.75 8.25l4.72-4.72a.75.75 0 011.28.53v15.88a.75.75 0 01-1.28.53l-4.72-4.72H4.51c-.88 0-1.704-.507-1.938-1.354A9.01 9.01 0 012.25 12c0-.83.112-1.633.322-2.396C2.806 8.756 3.63 8.25 4.51 8.25H6.75z"></path></svg></button> <button class="visible p-1 rounded dark:hover:text-white hover:text-black transition whitespace-pre-wrap" id="info-d7c5989d-fa77-473d-9dff-0562eb03dbb5"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l.041-.021M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9-3.75h.008v.008H12V8.25z"></path></svg></button> <button type="button" class="visible p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"></path><path stroke-linecap="round" stroke-linejoin="round" d="M15.91 11.672a.375.375 0 0 1 0 .656l-5.603 3.113a.375.375 0 0 1-.557-.328V8.887c0-.286.307-.466.557-.327l5.603 3.112Z"></path></svg></button> <button type="button" class="visible p-1 rounded dark:hover:text-white hover:text-black transition regenerate-response-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" st
|
||
|
|
||
|
|
||
|
<span class="cleanslate TridactylStatusIndicator TridactylModenormal" style="border: #37adff var(--tridactyl-indicator-border-style, solid) var(--tridactyl-indicator-border-width, 1.5px) !important">normal</span></body><iframe class="cleanslate hidden" src="21cd8134-ccb4-4d3d-86e0-70f085290d33_files/commandline.html" id="cmdline_iframe" loading="lazy" style="height: 0px !important;"></iframe></html>
|