{"body": "<h1>Source code for iCallSV.sortbamByReadName</h1><div class=\"highlight\"><pre>\n<span></span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">sortbamByReadName</span>\n<span class=\"sd\">~~~~~~~~~~~~~~~~~</span>\n\n<span class=\"sd\">:Description: This module will sort bam file by name</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">&#39;&#39;&#39;</span>\n<span class=\"sd\">Created on Mar 18, 2015</span>\n<span class=\"sd\">Description: This module will sort bam file by name</span>\n<span class=\"sd\">@author: Ronak H Shah</span>\n<span class=\"sd\">&#39;&#39;&#39;</span>\n\n<span class=\"kn\">import</span> <span class=\"nn\">os</span>\n<span class=\"kn\">import</span> <span class=\"nn\">sys</span>\n<span class=\"kn\">import</span> <span class=\"nn\">pysam</span>\n<span class=\"kn\">import</span> <span class=\"nn\">logging</span>\n<span class=\"kn\">import</span> <span class=\"nn\">coloredlogs</span>\n\n<span class=\"n\">logger</span> <span class=\"o\">=</span> <span class=\"n\">logging</span><span class=\"o\">.</span><span class=\"n\">getLogger</span><span class=\"p\">(</span><span class=\"s1\">&#39;iCallSV.sortbamByReadName&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">coloredlogs</span><span class=\"o\">.</span><span class=\"n\">install</span><span class=\"p\">(</span><span class=\"n\">level</span><span class=\"o\">=</span><span class=\"s1\">&#39;DEBUG&#39;</span><span class=\"p\">)</span>\n\n<div class=\"viewcode-block\" id=\"sortBam\"><a class=\"viewcode-back\" href=\"../../../iCallSV/#iCallSV.sortbamByReadName.sortBam\">[docs]</a><span class=\"k\">def</span> <span class=\"nf\">sortBam</span><span class=\"p\">(</span><span class=\"n\">inputBam</span><span class=\"p\">,</span> <span class=\"n\">outputBamName</span><span class=\"p\">,</span> <span class=\"n\">outputDir</span><span class=\"p\">):</span>\n    <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;sortbamByReadName: Trying to sort BAM file by Read Name&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">isdir</span><span class=\"p\">(</span><span class=\"n\">outputDir</span><span class=\"p\">)):</span>\n        <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;sortbamByReadName: The output directory </span><span class=\"si\">%s</span><span class=\"s2\"> exists&quot;</span><span class=\"p\">,</span> <span class=\"n\">outputDir</span><span class=\"p\">)</span>\n        <span class=\"n\">outputFile</span> <span class=\"o\">=</span> <span class=\"n\">outputDir</span> <span class=\"o\">+</span> <span class=\"s2\">&quot;/&quot;</span> <span class=\"o\">+</span> <span class=\"n\">outputBamName</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;sortbamByReadName: The output directory </span><span class=\"si\">%s</span><span class=\"s2\"> does not exists !!&quot;</span><span class=\"p\">,</span> <span class=\"n\">outputDir</span><span class=\"p\">)</span>\n        <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">exit</span><span class=\"p\">()</span>\n\n    <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">isfile</span><span class=\"p\">(</span><span class=\"n\">inputBam</span><span class=\"p\">)):</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"n\">pysam</span><span class=\"o\">.</span><span class=\"n\">sort</span><span class=\"p\">(</span><span class=\"s2\">&quot;-n&quot;</span><span class=\"p\">,</span> <span class=\"n\">inputBam</span><span class=\"p\">,</span> <span class=\"n\">outputFile</span><span class=\"p\">)</span>\n        <span class=\"k\">except</span> <span class=\"ne\">IndexError</span> <span class=\"k\">as</span> <span class=\"n\">err</span><span class=\"p\">:</span>\n            <span class=\"n\">exception</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;Index error(</span><span class=\"si\">{0}</span><span class=\"s2\">): </span><span class=\"si\">{1}</span><span class=\"s2\">&quot;</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span><span class=\"n\">err</span><span class=\"o\">.</span><span class=\"n\">errno</span><span class=\"p\">,</span> <span class=\"n\">err</span><span class=\"o\">.</span><span class=\"n\">strerror</span><span class=\"p\">)</span>\n            <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"n\">exception</span><span class=\"p\">)</span>\n        <span class=\"k\">except</span> <span class=\"ne\">IOError</span> <span class=\"k\">as</span> <span class=\"n\">err</span><span class=\"p\">:</span>\n            <span class=\"n\">exception</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;I/O error(</span><span class=\"si\">{0}</span><span class=\"s2\">): </span><span class=\"si\">{1}</span><span class=\"s2\">&quot;</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span><span class=\"n\">err</span><span class=\"o\">.</span><span class=\"n\">errno</span><span class=\"p\">,</span> <span class=\"n\">err</span><span class=\"o\">.</span><span class=\"n\">strerror</span><span class=\"p\">)</span>\n            <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;</span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"n\">exception</span><span class=\"p\">)</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;sortbamByReadName:Bam File </span><span class=\"si\">%s</span><span class=\"s2\"> does not exists !!&quot;</span><span class=\"p\">,</span> <span class=\"n\">inputBam</span><span class=\"p\">)</span>\n        <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">exit</span><span class=\"p\">()</span>\n    <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span><span class=\"s2\">&quot;sortbamByReadName: Finished sorting BAM file by Read Name.&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span><span class=\"p\">(</span><span class=\"n\">outputFile</span><span class=\"p\">)</span></div>\n</pre></div>", "title": "iCallSV.sortbamByReadName", "customsidebar": null, "current_page_name": "_modules/iCallSV/sortbamByReadName", "sidebars": null, "parents": [{"link": "../../", "title": "Module code"}], "alabaster_version": "0.7.9"}