<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>REBELSCIENCE</title><link>https://rebelscience.club/</link><description>Bioinformatics, Programming and Open-Source Science</description><item><title>Genome Toolkit. Part 1: Project Setup</title><link>https://rebelscience.club/2022/10/genome-toolkit-part-1/</link><guid isPermaLink="true">https://rebelscience.club/2022/10/genome-toolkit-part-1/</guid><pubDate>Sun, 02 Oct 2022 16:08:35 GMT</pubDate><description>Welcome to the new series, called “Genome Toolkit”. In this series, we will write a set of tools, that will help us find and build statistical data around any DNA, RNA and Protein sequences.
</description><content:encoded><![CDATA[
<h1 class="wp-block-heading">Introduction</h1>



<p class="wp-block-paragraph">Welcome to the new series, called &#8220;Genome Toolkit&#8221;. In this series, we will write a set of tools, that will help us find and build statistical data around any DNA, RNA, and Protein sequences. We will start by looking at the basic needs of any biologist, and write algorithms around their needs.</p>



<p class="wp-block-paragraph">This will include:</p>



<ul class="wp-block-list">
<li>Searching for repeating patterns/subsequences, also called k-mers, in genomes</li>



<li>Building and displaying graphs for GC content in a given sequence</li>



<li>Using graph theory to assemble genomes</li>



<li>And much, much more&#8230;</li>
</ul>



<p class="wp-block-paragraph">This is going to be our second, and much longer series, after the DNA Toolkit series. We will use DNA Toolkit code in our Genome Toolkit class. If you have not started that series, or have not yet finished it, I suggest you do before starting the Genome Toolkit series. In the DNA Toolkit, we have built a very basic set of tools to work with DNA/RNA sequences. We will be expanding the DNA Toolkit class as we develop the Genome Toolkit.</p>



<p class="wp-block-paragraph">The main goal of this series is to build a set of algorithms, and wrap them into a usable bioinformatics tool, that is easy to use, modify, and extend.</p>



<p class="wp-block-paragraph">This series is built on information from a lot of amazing sources, primarily a Bioinformatics course from University of California San Diego. You can find it on Coursera or in my &#8220;Getting started in Bioinformatics: A step-by-step guide&#8221; article <a href="https://rebelscience.club/2020/08/getting-started-in-bioinformatics-a-step-by-step-guide/">here</a>. Algorithms and solutions from a few good books were also included. In our series, we will try to combine a lot of good sources, and build a usable tool that can also be used as a part of your portfolio when applying for a job in the field of bioinformatics. Make sure you don&#8217;t just copy the code. Make sure you understand it, and can modify it to fit your needs, if needed.</p>



<p class="wp-block-paragraph">We are going to follow a very basic structure, as we established in our DNA Toolkit series:</p>



<ul class="wp-block-list">
<li>Lesson article</li>



<li>Lesson video</li>



<li>GitHub repository with the code from videos</li>



<li>Downloadable slides, presented in videos (included in GitHub repository)</li>
</ul>



<p class="wp-block-paragraph">As we build our first set of algorithms that are applicable in real world scenarios, we will see how we can integrate them into our &#8220;DNA Engine&#8221; GUI application. We will be building the &#8220;DNA Engine&#8221; in parallel within our &#8220;Bioinformatics Tools Programming in Python with Qt&#8221; series.</p>



<p class="wp-block-paragraph">Alright. Now, that you know what to expect from the new series, let&#8217;s start by setting up our new working directory, creating the base class, and activating a virtual environment.</p>



<hr class="wp-block-separator has-alpha-channel-opacity" />



<h3 id="part-1-setting-up-the-project" class="wp-block-heading">Part 1: Setting up the project</h3>



<p class="wp-block-paragraph">We will start by creating our project structure as follows:</p>



<ul class="wp-block-list">
<li>Folder with <code>genome_toolkit.py</code> and <code>application.py</code> files</li>



<li>Basic <code>Genome Toolkit</code> class</li>



<li>Virtual environment (pipenv) for the project folder</li>



<li>Follow the Python naming convention</li>
</ul>



<p class="wp-block-paragraph">Yes, we will start using classes. If you have never used classes, or need a refresher on how they work, Cory&#8217;s videos <a rel="noreferrer noopener" href="https://youtu.be/ZDa-Z5JzLYM?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc" target="_blank">here</a> can help with that. Classes will allow us to structure our project much better, as it will be much larger than the &#8220;DNA Toolkit&#8221; project. Many useful functions, like plotting, file reading/writing, and database access, can be added to the class as we progress.</p>



<p class="wp-block-paragraph">Virtual environment (pipenv) will help to make sure our project is isolated from any other projects or software packages you are running on your computer. When you commit changes to your project, add various packages to it, and you want to make sure anyone else working with your code will be able to run it with no issues, you can do this by activating the virtual environment. You don&#8217;t have to use pipenv, just skip the <code>pipenv</code> part, and run your code as it is. But, I strongly recommend you do use pipenv. As always, Cory has an amazing video about <code>pipenv</code> <a href="https://youtu.be/zDYL22QNiWk" target="_blank" rel="noreferrer noopener">here</a>.</p>



<p class="wp-block-paragraph">We will continue using VSCode/VSCodium code editor, as it is very light, does not have overwhelming amount of functionality, and it is very easy to set up (I have a video <a href="https://youtu.be/81Eb_YXmV4g" target="_blank" rel="noreferrer noopener">here</a>). I strongly recommend using the exact same tools and programs your instructor is using, no matter what course or tutorials you are following. This will help you to avoid having issues with your code editor, and other tools, and you can just focus on writing code and learning new things. We had far too many cases when someone was using editors like PyCharm, Atom, or some other tool, and they could not even run the very first simple function. They would see no output, output that was unexpected, or just an error. It is hard, or even impossible, to help that person in that case, as everyone has a very different hardware/software setup on their local computers. You can, of course, use any tool or editor, as long as you understand what you are doing, and you can debug and solve issues on your own. For everyone else, I suggest using VSCode.</p>



<hr class="wp-block-separator has-alpha-channel-opacity" />



<p class="wp-block-paragraph">Alright, let&#8217;s do it!</p>



<ol class="wp-block-list">
<li>Create a new folder, and call it &#8220;genome_toolkit&#8221; (create it in the directory you usually keep your school/project files)</li>



<li>Open that folder in your code editor (File ⇾ Open Folder) and add two files:<br><code>genome_toolkit.py</code> &#8211; our main class file, where most of the core functionality will &#8220;live&#8221;<br><code>application.py</code> <em>&#8211;</em> will be used to test our new functionality and build a small demo application.</li>



<li>If you are not going to use virtual environments, you can skip this part.<br>While in the editor, open the built-in Terminal (Mac: <code>Cmd + ~</code>, Windows/Linux: <code>Ctrl + ~</code>), confirm you are in the correct directory (terminal path matches your genome_toolkit directory path), and execute the <code>pipenv shell</code> command. You should see a<br><code>✔ Successfully created virtual environment!</code> message, and a new <code>Pipfile</code> file will also be added to our project.</li>



<li>In step 3, we have created a virtual environment, and it might not be selected by default. If it is not, let&#8217;s first close and re-open our VSCode editor and our project folder. Now we should be able to select the environment we created by clicking on the Python version in the bottom-right corner, and selecting the virtual one (note, you might have a different Python version, like 3.9 or 3.8, and that is just fine):</li>
</ol>



<figure class="wp-block-image alignwide size-full"><a href="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_175033.png"><img decoding="async" width="1043" height="619" src="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_175033.png" alt="" class="wp-image-1191" srcset="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_175033.png 1043w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_175033-485x288.png 485w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_175033-768x456.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">Python version should change to the one in our virtual environment:</p>



<figure class="wp-block-image size-full"><a href="https://rebelscience.club/wp-content/uploads/2022/09/image.png"><img decoding="async" width="1024" height="30" src="https://rebelscience.club/wp-content/uploads/2022/09/image.png" alt="" class="wp-image-1192" srcset="https://rebelscience.club/wp-content/uploads/2022/09/image.png 1024w, https://rebelscience.club/wp-content/uploads/2022/09/image-512x15.png 512w, https://rebelscience.club/wp-content/uploads/2022/09/image-768x23.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">All the steps, when executed correctly, should look something like this:</p>



<figure class="wp-block-image size-large"><a href="https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-1071x720.gif"><img decoding="async" width="1071" height="720" src="https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-1071x720.gif" alt="" class="wp-image-1270" srcset="https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-1071x720.gif 1071w, https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-429x288.gif 429w, https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-768x516.gif 768w, https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-24x16.gif 24w, https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-36x24.gif 36w, https://rebelscience.club/wp-content/uploads/2022/10/pipenv-1-48x32.gif 48w" sizes="(max-width: 1071px) 100vw, 1071px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<ol start="5" class="wp-block-list">
<li>Now, that we have created the project directory, and virtual environment activated, let&#8217;s just do a quick test to see if it works. Let&#8217;s print the &#8220;Genome Toolkit!&#8221; text from the <code>application.py</code> file:</li>
</ol>



<figure class="wp-block-image alignwide size-full"><a href="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_180047.png"><img decoding="async" width="1043" height="619" src="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_180047.png" alt="" class="wp-image-1193" srcset="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_180047.png 1043w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_180047-485x288.png 485w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_180047-768x456.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">Save (Ctrl + S) the file, and run it. If you see the above result, it means our project is set up, and we are ready to start working on our code. You might see this message when you try running your code for the first time:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://rebelscience.club/wp-content/uploads/2022/09/image-1.png"><img decoding="async" width="591" height="113" src="https://rebelscience.club/wp-content/uploads/2022/09/image-1.png" alt="" class="wp-image-1197" srcset="https://rebelscience.club/wp-content/uploads/2022/09/image-1.png 591w, https://rebelscience.club/wp-content/uploads/2022/09/image-1-512x98.png 512w" sizes="(max-width: 591px) 100vw, 591px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>
</div>


<p class="wp-block-paragraph">If you do, just select <code>Yes</code>. This will add <code>autopep8</code> code formatter to your virtual environment. This tool will format and clean our code structure. A new file, called <code>Pipfile.lock</code> will be added to our project. This file will keep track of all packages we will use in our project</p>



<hr class="wp-block-separator has-alpha-channel-opacity" />



<h2 id="part-2-creating-class-structure" class="wp-block-heading">Part 2: Creating <em>class</em> structure</h2>



<p class="wp-block-paragraph">As I mentioned above, we will be using <em>classes</em> in this series, but don&#8217;t get discouraged. We will only use the minimal amount of code related to <em>classes</em>. Mostly we are doing this for better code structure and reusability. On the other hand, you have a chance to start learning and practicing <em>classes</em>, if you never used them before. I also bet, being an OOP (Object-Oriented Programmer) will increase your chances of getting a job with Python.</p>



<p class="wp-block-paragraph">Let&#8217;s add our <em>class</em> to <code>genome_toolkit.py</code> file, and add a default constructor. Constructor is a function that is triggered every time we create an instance of a <em>class</em>. We will see how this works, in our next step:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">class genomeToolkit:
    def __init__(self):
        print(&quot;Genome Toolkit initiated&quot;)</pre></div>



<p class="wp-block-paragraph">Now we need to test our base <em>class,</em> by importing the above file into our <code>application.py</code>, and create a test instance of that class:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text/x-python&quot;,&quot;theme&quot;:&quot;monokai&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Python&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}">from genome_toolkit import genomeToolkit

# Creating an instance of our genomeToolkit class
gt = genomeToolkit()</pre></div>



<p class="wp-block-paragraph">If we run our code now, we should see a &#8220;Genome Toolkit initiated&#8221; message. Just make sure the <code>application.py</code> file is active when you run code:</p>



<figure class="wp-block-image alignwide size-large"><a href="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906-1280x664.png"><img decoding="async" width="1280" height="664" src="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906-1280x664.png" alt="" class="wp-image-1198" srcset="https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906-1280x664.png 1280w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906-512x266.png 512w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906-768x398.png 768w, https://rebelscience.club/wp-content/uploads/2022/09/Screenshot_20220917_203906.png 1469w" sizes="(max-width: 1280px) 100vw, 1280px" /></a><figcaption class="wp-element-caption">Click to enlarge/download</figcaption></figure>



<p class="wp-block-paragraph">In the above example, we created a <em>class,</em> imported it into our application, and created an instance of that <em>class.</em> When the instance was created, on line 3 in the <code>application.py</code> file, it triggered the constructor which is on line 2-3 in the <code>genomeToolkit</code> <em>class.</em> If you are not very familiar with the concept of constructors and destructors, the above example should provide a basic example, and you can imagine how many interesting things we can do with that. Just a few examples: load a file automatically when we create an instance of our <em>class</em>, verify our DNA/RNA/Protein sequence when we create it, etc. We will be adding more functionality to that <em>class</em> as we develop our tool.</p>



<p class="wp-block-paragraph">This is it for this article.</p>



<p class="wp-block-paragraph">This is only the beginning. Now that we have our project configured, and base <em>class</em> created, we are ready to add our first simple, but powerful function. We have set up our project, and we will start adding a lot of cool functionality in our upcoming articles. This is exactly what we will do in our next article. The next articles will be much shorter, as we will focus on one algorithm at a time.</p>



<p class="wp-block-paragraph">Git repository for this lesson: <a href="https://github.com/rebelC0der/Genome_Toolkit/commit/e06ec19a739fd1988ae12d4b3b3cc56e1ddde958">https://github.com/rebelC0der/Genome_Toolkit/commit/e06ec19a739fd1988ae12d4b3b3cc56e1ddde958</a></p>



<p class="wp-block-paragraph">And as always, this article is available in a video from here:</p>



<figure class="wp-block-embed aligncenter is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Genome Toolkit. Part 1: project setup" width="640" height="360" src="https://www.youtube.com/embed/SggIKLyFRtA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h3 class="wp-block-heading">Until next time, rebelCoder, signing out.</h3>



<hr class="wp-block-separator has-alpha-channel-opacity" />
]]></content:encoded></item></channel></rss>