{"body": "<h1>Source code for iCallSV.dellyVcf2Tab</h1><div class=\"highlight\"><pre>\n<span></span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">dellyVcf2Tab</span>\n<span class=\"sd\">~~~~~~~~~~~~</span>\n\n<span class=\"sd\">:Description: This module converts the Delly Vcf file having tumor normal, to tab-delimited format for input to iAnnotateSV</span>\n\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 converts the Delly Vcf file having tumor normal, to tab-delimited format for input to iAnnotateSV</span>\n<span class=\"sd\">@author: Ronak H Shah</span>\n<span class=\"sd\">::Input::</span>\n<span class=\"sd\">vcfFile: Input vcf file to convert</span>\n<span class=\"sd\">outputFileName: Name of the output file</span>\n<span class=\"sd\">OutputDir: Directory for output file</span>\n<span class=\"sd\">::Output::</span>\n<span class=\"sd\">outputFile: Tab-delimited file containing:</span>\n<span class=\"sd\">chr1: Its the chromosome name for first break point [1,2,3,4,5,6,7 etc..],</span>\n<span class=\"sd\">pos1: Its the chromosome loaction for first break point [1-based],</span>\n<span class=\"sd\">str1: Its the read direction for the first break point [0=top/plus/reference, 1=bottom/minus/complement],</span>\n<span class=\"sd\">chr2: Its the chromosome name for second break point [1,2,3,4,5,6,7 etc..],</span>\n<span class=\"sd\">pos2: Its the chromosome loaction for second break point [1-based],</span>\n<span class=\"sd\">str2: Its the read direction for the second break point [0=top/plus/reference, 1=bottom/minus/complement],</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\">vcf</span>\n<span class=\"kn\">import</span> <span class=\"nn\">checkparameters</span> <span class=\"k\">as</span> <span class=\"nn\">cp</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.dellyVcf2Tab&#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=\"vcf2tab\"><a class=\"viewcode-back\" href=\"../../../iCallSV/#iCallSV.dellyVcf2Tab.vcf2tab\">[docs]</a><span class=\"k\">def</span> <span class=\"nf\">vcf2tab</span><span class=\"p\">(</span><span class=\"n\">vcfFile</span><span class=\"p\">,</span> <span class=\"n\">outputDir</span><span class=\"p\">,</span> <span class=\"n\">verbose</span><span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;This ``converts`` the Delly Vcf file having tumor normal, to tab-delimited format for input to iAnnotateSV</span>\n\n\n<span class=\"sd\">    :param str vcfFile: str of vcf file to be converted</span>\n<span class=\"sd\">    :param str outputDir: str for the output directory</span>\n<span class=\"sd\">    :param bool verbose: a boolean</span>\n<span class=\"sd\">    :return: A str name of tab-delimited file</span>\n<span class=\"sd\">    :rtype: str</span>\n\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">cp</span><span class=\"o\">.</span><span class=\"n\">checkFile</span><span class=\"p\">(</span><span class=\"n\">vcfFile</span><span class=\"p\">)</span>\n    <span class=\"n\">cp</span><span class=\"o\">.</span><span class=\"n\">checkDir</span><span class=\"p\">(</span><span class=\"n\">outputDir</span><span class=\"p\">)</span>\n    <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"n\">verbose</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;dellyVcf2Tab: All Input Parameters look good. Lets convert to tab-delimited file&quot;</span><span class=\"p\">)</span>\n    <span class=\"n\">vcf_reader</span> <span class=\"o\">=</span> <span class=\"n\">vcf</span><span class=\"o\">.</span><span class=\"n\">Reader</span><span class=\"p\">(</span><span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">vcfFile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;r&#39;</span><span class=\"p\">))</span>\n    <span class=\"n\">outputFileName</span> <span class=\"o\">=</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">splitext</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\">basename</span><span class=\"p\">(</span><span class=\"n\">vcfFile</span><span class=\"p\">)))[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"s2\">&quot;.tab&quot;</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\">outputFileName</span>\n    <span class=\"n\">outputHandle</span> <span class=\"o\">=</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">outputFile</span><span class=\"p\">,</span> <span class=\"s2\">&quot;w&quot;</span><span class=\"p\">)</span>\n    <span class=\"n\">outputHandle</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"s2\">&quot;chr1</span><span class=\"se\">\\t</span><span class=\"s2\">pos1</span><span class=\"se\">\\t</span><span class=\"s2\">str1</span><span class=\"se\">\\t</span><span class=\"s2\">chr2</span><span class=\"se\">\\t</span><span class=\"s2\">pos2</span><span class=\"se\">\\t</span><span class=\"s2\">str2</span><span class=\"se\">\\n</span><span class=\"s2\">&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">for</span> <span class=\"n\">record</span> <span class=\"ow\">in</span> <span class=\"n\">vcf_reader</span><span class=\"p\">:</span>\n        <span class=\"p\">(</span><span class=\"n\">chrom1</span><span class=\"p\">,</span>\n         <span class=\"n\">start1</span><span class=\"p\">,</span>\n         <span class=\"n\">start2</span><span class=\"p\">,</span>\n         <span class=\"n\">chrom2</span><span class=\"p\">,</span>\n         <span class=\"n\">contype</span><span class=\"p\">,</span>\n         <span class=\"n\">str1</span><span class=\"p\">,</span>\n         <span class=\"n\">str2</span><span class=\"p\">)</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"kc\">None</span> <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">))</span>\n        <span class=\"n\">chrom1</span> <span class=\"o\">=</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">CHROM</span>\n        <span class=\"n\">start1</span> <span class=\"o\">=</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">POS</span>\n        <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"s2\">&quot;END&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">):</span>\n            <span class=\"n\">start2</span> <span class=\"o\">=</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">[</span><span class=\"s1\">&#39;END&#39;</span><span class=\"p\">]</span>\n        <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"s2\">&quot;CHR2&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">):</span>\n            <span class=\"n\">chrom2</span> <span class=\"o\">=</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">[</span><span class=\"s1\">&#39;CHR2&#39;</span><span class=\"p\">]</span>\n        <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"s2\">&quot;CT&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">):</span>\n            <span class=\"n\">contype</span> <span class=\"o\">=</span> <span class=\"n\">record</span><span class=\"o\">.</span><span class=\"n\">INFO</span><span class=\"p\">[</span><span class=\"s1\">&#39;CT&#39;</span><span class=\"p\">]</span>\n        <span class=\"p\">(</span><span class=\"n\">startCT</span><span class=\"p\">,</span> <span class=\"n\">endCT</span><span class=\"p\">)</span> <span class=\"o\">=</span> <span class=\"n\">contype</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s2\">&quot;to&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span><span class=\"p\">((</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">startCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">3</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"p\">(</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">endCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">3</span><span class=\"p\">)):</span>\n            <span class=\"n\">str1</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n            <span class=\"n\">str2</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n        <span class=\"k\">elif</span><span class=\"p\">((</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">startCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">3</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"p\">(</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">endCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">5</span><span class=\"p\">)):</span>\n            <span class=\"n\">str1</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n            <span class=\"n\">str2</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span><span class=\"p\">((</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">startCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">5</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"p\">(</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">endCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">3</span><span class=\"p\">)):</span>\n            <span class=\"n\">str1</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n            <span class=\"n\">str2</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n        <span class=\"k\">elif</span><span class=\"p\">((</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">startCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">5</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"p\">(</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">endCT</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">5</span><span class=\"p\">)):</span>\n            <span class=\"n\">str1</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n            <span class=\"n\">str2</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"n\">verbose</span><span class=\"p\">):</span>\n                <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"n\">info</span><span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;dellyVcf2Tab: The connection type (CT) given in the vcf file is incorrect.CT: </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span>\n                    <span class=\"n\">contype</span><span class=\"p\">)</span>\n        <span class=\"n\">outputHandle</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">chrom1</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\t</span><span class=\"s2\">&quot;</span> <span class=\"o\">+</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">start1</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\t</span><span class=\"s2\">&quot;</span> <span class=\"o\">+</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">str1</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\t</span><span class=\"s2\">&quot;</span> <span class=\"o\">+</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">chrom2</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\t</span><span class=\"s2\">&quot;</span> <span class=\"o\">+</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">start2</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\t</span><span class=\"s2\">&quot;</span> <span class=\"o\">+</span>\n            <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">str2</span><span class=\"p\">)</span> <span class=\"o\">+</span>\n            <span class=\"s2\">&quot;</span><span class=\"se\">\\n</span><span class=\"s2\">&quot;</span><span class=\"p\">)</span>\n    <span class=\"n\">outputHandle</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n    <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"n\">verbose</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;dellyVcf2Tab: Finished conversion of Vcf file to tab-delimited file&quot;</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;dellyVcf2Tab: Output can be found: </span><span class=\"si\">%s</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"n\">outputFile</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.dellyVcf2Tab", "customsidebar": null, "current_page_name": "_modules/iCallSV/dellyVcf2Tab", "sidebars": null, "parents": [{"link": "../../", "title": "Module code"}], "alabaster_version": "0.7.9"}