Skip to content

Commit

Permalink
CRAM support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmsk committed Nov 30, 2020
1 parent d022f2e commit 549adb8
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/_build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 5ac4abc510482ee3445e5e5476fdb18f
config: 29180893c7eadda9a872007d338d9a4c
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file modified docs/_build/html/.doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/_build/html/.doctrees/index.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/_build/html/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ In a command prompt or terminal window, run the following commands to install an
$ bamsnap -bam test.bam -pos chr1:7364529 -out test.bam.png
More examples and commands are available in `gallery <gallary.html>`_. Use ``-h`` to list the options available for ``bamsnap``.
More examples and commands are available in `gallery <gallery.html>`_. Use ``-h`` to list the options available for ``bamsnap``.


.. code:: console
Expand Down
5 changes: 3 additions & 2 deletions docs/_build/html/_static/basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ pre {
overflow-y: hidden; /* fixes display issues on Chrome browsers */
}

pre, div[class|="highlight"] {
pre, div[class*="highlight-"] {
clear: both;
}

Expand All @@ -704,7 +704,7 @@ span.pre {
hyphens: none;
}

div[class^="highlight-"] {
div[class*="highlight-"] {
margin: 1em 0;
}

Expand Down Expand Up @@ -764,6 +764,7 @@ div.code-block-caption code {
}

table.highlighttable td.linenos,
span.linenos,
div.doctest > div.highlight span.gp { /* gp: Generic.Prompt */
user-select: none;
}
Expand Down
5 changes: 3 additions & 2 deletions docs/_build/html/_static/doctools.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ var Documentation = {
initOnKeyListeners: function() {
$(document).keydown(function(event) {
var activeElementType = document.activeElement.tagName;
// don't navigate when in search box or textarea
// don't navigate when in search box, textarea, dropdown or button
if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
&& !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
&& activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey
&& !event.shiftKey) {
switch (event.keyCode) {
case 37: // left
var prevHref = $('link[rel="prev"]').prop('href');
Expand Down
7 changes: 6 additions & 1 deletion docs/_build/html/_static/pygments.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding-left: 5px; padding-right: 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding-left: 5px; padding-right: 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #f8f8f8; }
.highlight { background: #f8f8f8; }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
Expand Down
11 changes: 5 additions & 6 deletions docs/_build/html/_static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ var Search = {
_pulse_status : -1,

htmlToText : function(htmlString) {
var htmlElement = document.createElement('span');
htmlElement.innerHTML = htmlString;
$(htmlElement).find('.headerlink').remove();
docContent = $(htmlElement).find('[role=main]')[0];
var virtualDocument = document.implementation.createHTMLDocument('virtual');
var htmlElement = $(htmlString, virtualDocument);
htmlElement.find('.headerlink').remove();
docContent = htmlElement.find('[role=main]')[0];
if(docContent === undefined) {
console.warn("Content block not found. Sphinx search tries to obtain it " +
"via '[role=main]'. Could you check your theme or template.");
Expand Down Expand Up @@ -166,8 +166,7 @@ var Search = {
objectterms.push(tmp[i].toLowerCase());
}

if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
tmp[i] === "") {
if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i] === "") {
// skip this "word"
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/_build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ <h2>Setting up BamSnap<a class="headerlink" href="#setting-up-bamsnap" title="Pe
<span class="gp">$</span> bamsnap -bam test.bam -pos chr1:7364529 -out test.bam.png
</pre></div>
</div>
<p>More examples and commands are available in <a class="reference external" href="gallary.html">gallery</a>. Use <code class="docutils literal notranslate"><span class="pre">-h</span></code> to list the options available for <code class="docutils literal notranslate"><span class="pre">bamsnap</span></code>.</p>
<p>More examples and commands are available in <a class="reference external" href="gallery.html">gallery</a>. Use <code class="docutils literal notranslate"><span class="pre">-h</span></code> to list the options available for <code class="docutils literal notranslate"><span class="pre">bamsnap</span></code>.</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> bamsnap -h
</pre></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/_build/html/searchindex.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions docs/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ Alignment file
Input files (``-bam``)
^^^^^^^^^^^^^^^^^^^

Input files to be used can be specified using the ``-bam`` argument. It is possible to specify a single file or list multiple files.
Input files to be used can be specified using the ``-bam`` argument. It is possible to specify a single file or list multiple files. Also, a cram file can be assigned with ``-bam`` argument.

.. code:: console
$ bamsnap -bam ./data/NA12878.bam
$ bamsnap -bam ./data/NA12878.bam ./data/NA12877.bam ./data/NA12879.bam
$ bamsnap -bam ./data/NA12878.cram
$ bamsnap -bam ./data/NA12878.cram ./data/NA12877.bam ./data/NA12879.bam
Title of bam file(s) (``-title``)
Expand Down Expand Up @@ -65,20 +67,21 @@ BAM list file (``-bamlist``)
$ bamsnap -bamlist ./data/NATRIO_bamlist.txt
It is possible to provide a single file listing all the input bam files to be used. The expected format is a tabular (tab-separated) file. The first column is mandatory and must contain the paths to files, the second column is optional and allows to associate labels to files.
It also supports `.bam` and `.cram` file.

.. code:: bash
# example of bamlist file with lables
./data/NA12878.bam NA12878 (F)
./data/NA12877.bam NA12877 (M)
./data/NA12877.cram NA12877 (M)
./data/NA12879.bam NA12879 (D)
.. code:: bash
# example of bamlist file
./data/NA12878.bam
./data/NA12877.bam
./data/NA12877.cram
./data/NA12879.bam
Genomic position
Expand Down
2 changes: 1 addition & 1 deletion pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pip uninstall -y bamsnap
rm -rf build
rm -rf ./dist/*
python3 setup.py sdist bdist_wheel
pip install ./dist/bamsnap-0.2.14-py3-none-any.whl
pip install ./dist/bamsnap-0.2.15-py3-none-any.whl
# twine upload dist/*
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

setup(name='bamsnap',
version='0.2.14',
version='0.2.15',
url='https://github.com/danielmsk/bamsnap',
license='MIT',
author='Daniel Minseok Kwon',
Expand Down
2 changes: 1 addition & 1 deletion src/bamsnap.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: bamsnap
Version: 0.2.14
Version: 0.2.15
Summary: A converter from .bam to .png for specific genomic region.
Home-page: https://github.com/danielmsk/bamsnap
Author: Daniel Minseok Kwon
Expand Down
2 changes: 1 addition & 1 deletion src/bamsnap/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_options():
confjson = util.load_json(util.getDataPath('conf.json'))

parser = argparse.ArgumentParser(usage='%(prog)s <sub-command> [options]',
description='%(prog)s ver' + confjson['VERSION'] + " (" + confjson['VERSION_DATE'] + ")" + ': convert bam to image')
description='%(prog)s ver' + confjson['VERSION'] + " (" + confjson['VERSION_DATE'] + ")" + ': convert bam (or cram) to image')
parser.add_argument('-v', '--version', action='version',
version='%(prog)s ver' + confjson['VERSION'] + " (" + confjson['VERSION_DATE'] + ")")

Expand Down
15 changes: 15 additions & 0 deletions src/bamsnap/bam.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ def __init__(self, filename, title=''):
self.title = title
else:
self.title = self.filename.split('/')[-1]
self.ref = None

def __str__(self):
return self.title

def getSamfileFlags(self):
if self.filename.endswith('.bam'):
return 'rb'
elif self.filename.endswith('.cram'):
return 'rc'
else:
return 'r'

def setReference(self, ref):
self.ref = ref

def getReference(self):
return self.ref
17 changes: 13 additions & 4 deletions src/bamsnap/bamsnap.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,25 @@ def load_bamlist(self):
if len(line) > 0 and line[0] != '#':
arr = line.split('\t')
if len(arr) == 2:
self.bamlist.append(BAM(arr[0], arr[1]))
bamObj = BAM(arr[0], arr[1])
if 'ref' in self.opt:
bamObj.setReference(self.opt['ref'])
self.bamlist.append(bamObj)
else:
self.bamlist.append(BAM(arr[0]))
bamObj = BAM(arr[0])
if 'ref' in self.opt:
bamObj.setReference(self.opt['ref'])
self.bamlist.append(bamObj)
elif 'bam' in ks and len(self.opt['bam']) > 0:
for idx, bamfile in enumerate(self.opt['bam']):
try:
title = self.opt['title'][idx]
except IndexError:
title = ""
self.bamlist.append(BAM(bamfile, title))
bamObj = BAM(bamfile, title)
if 'ref' in self.opt:
bamObj.setReference(self.opt['ref'])
self.bamlist.append(bamObj)

def start_process_drawplot(self, image_w, bamlist):
for tno in range(self.opt['process']):
Expand Down Expand Up @@ -97,7 +106,7 @@ def run(self):
self.generate_zipfile()

self.opt['log'].debug('Total running time for getting reference sequence (set_refseq): ' +
str(round(timemap['set_refseq'], 3))+' sec')
str(round(timemap['set_refseq'], 3))+' sec')
self.opt['log'].info('Total running time: ' + str(round(t2-t0, 1))+' sec')


Expand Down
10 changes: 5 additions & 5 deletions src/bamsnap/data/conf.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"TITLE":"BAMSNAP",
"VERSION":"0.2.13",
"VERSION_DATE":"2020-09-15",
"VERSION":"0.2.14",
"VERSION_DATE":"2020-11-30",
"PROG":"bamsnap",
"options":[
{"param":"bam", "default":[], "nargs":"*", "action": null, "choices":null, "type":null, "help":"bam file(s)"},
{"param":"bamlist", "default":null, "nargs":null, "action":null, "choices":null, "type":null, "help":"list file with bam file paths"},
{"param":"title", "default":[], "nargs":"*", "action":null, "choices":null, "type":null, "help":"title (name) of bam file(s)"},
{"param":"bam", "default":[], "nargs":"*", "action": null, "choices":null, "type":null, "help":"bam or cram file(s)"},
{"param":"bamlist", "default":null, "nargs":null, "action":null, "choices":null, "type":null, "help":"list file with bam (or cram) file paths"},
{"param":"title", "default":[], "nargs":"*", "action":null, "choices":null, "type":null, "help":"title (name) of bam (or cram) file(s)"},
{"param":"no_title", "default":false, "nargs":null, "action":"store_true", "choices":null, "type":null, "help":"do not draw label."},
{"param":"title_fontsize", "default":18, "nargs":null, "action":null, "choices":null, "type":"int", "help":"font size of title"},
{"param":"pos", "default":[], "nargs":"*", "action":null, "choices":null, "type":null, "help":"genomic position (ex. 1:816687-818057, 12:7462545)"},
Expand Down
6 changes: 5 additions & 1 deletion src/bamsnap/drawreadset.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ class DrawReadSet():
STRAND_GROUP_LIST = ['pos_strand', 'neg_strand']

def __init__(self, bam, chrom, g_spos, g_epos, xscale, refseq="", coverage_vaf=10):
self.samAlign = pysam.AlignmentFile(bam.filename, "rb")
self.samAlign = pysam.AlignmentFile(bam.filename, bam.getSamfileFlags())
if bam.filename.endswith('.cram') and bam.getReference() is not None:
self.samAlign = pysam.AlignmentFile(bam.filename, bam.getSamfileFlags(), reference_filename=bam.getReference())
else:
self.samAlign = pysam.AlignmentFile(bam.filename, bam.getSamfileFlags())
self.chrom = chrom
self.refseq = refseq
self.g_spos = g_spos
Expand Down
Binary file added tests/data/test_miss_ref_chr1_944300_2.cram
Binary file not shown.
Binary file added tests/data/test_miss_ref_chr1_944300_2.cram.crai
Binary file not shown.
Binary file added tests/out/test_miss_ref_chr1_944300_2_cram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 2 additions & 5 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

cmdlist = []


cmdlist.append("""
-bam ./data/test_SV1_softclipped_1.bam \
-title "Clipped read" \
Expand Down Expand Up @@ -342,18 +341,16 @@
""")

cmdlist.append("""
-bam ./data/test_miss_ref_chr1_944300_2.bam \
-bam ./data/test_miss_ref_chr1_944300_2.cram \
-pos chr1:944315 \
-title "test" \
-out ./out/test_miss_ref_chr1_944300_2.png \
-out ./out/test_miss_ref_chr1_944300_2_cram.png \
-margin 50 \
-save_image_only
""")





def test_run():
for cmd in cmdlist:
# cmd = cmdlist[-1]
Expand Down

0 comments on commit 549adb8

Please sign in to comment.