File: AlignmentFileFetchTestUtils.py

package info (click to toggle)
python-pysam 0.15.4+ds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 27,992 kB
  • sloc: ansic: 140,738; python: 7,881; sh: 265; makefile: 223; perl: 41
file content (86 lines) | stat: -rw-r--r-- 2,973 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import subprocess
import pysam

from TestUtils import BAM_DATADIR, force_str

def build_fetch_with_samtoolsshell(fn):
    retval = os.popen("samtools view {} 2> /dev/null | wc -l".format(fn)).read()
    return int(retval.strip())


def build_fetch_with_samtoolspipe(fn):
    FNULL = open(os.devnull, 'w')
    with subprocess.Popen(["samtools", "view", fn],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=FNULL) as proc:
        return len(proc.stdout.readlines())

    
def build_fetch_with_pysam(*args, **kwargs):
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        return len(list(inf.fetch()))


def build_query_sequences_with_samtoolsshell(fn):
    retval = os.popen("samtools view {} 2> /dev/null | cut -f 11".format(fn)).read()
    return force_str(retval).splitlines()


def build_query_sequences_with_samtoolspipe(fn):
    FNULL = open(os.devnull, 'w')
    with subprocess.Popen(["samtools", "view", fn],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=FNULL) as proc:
        data = [force_str(x).split()[10] for x in proc.stdout.readlines()]
    return data

    
def build_query_sequences_with_pysam(*args, **kwargs):
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        data = [x.query_sequence for x in inf]
    return data


def build_query_qualities_with_pysam(*args, **kwargs):
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        data = [x.query_qualities for x in inf]
    return data


def build_query_sequences_flagfilter_with_samtoolsshell(fn):
    retval = os.popen("samtools view -f 2 {} 2> /dev/null | cut -f 11".format(fn)).read()
    return force_str(retval).splitlines()


def build_query_sequences_flagfilter_with_samtoolspipe(fn):
    FNULL = open(os.devnull, 'w')
    with subprocess.Popen(["samtools", "view", "-f", "2", fn],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=FNULL) as proc:
        data = [force_str(x).split()[10] for x in proc.stdout.readlines()]
    return data

def build_query_sequences_flagfilter_with_pysam(*args, **kwargs):
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        data = [x.query_sequence for x in inf if x.is_proper_pair]
    return data


def build_query_sequences_directflagfilter_with_pysam(*args, **kwargs):
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        data = [x.query_sequence for x in inf if x.flag & 2]
    return data


def build_aligned_pairs_with_pysam(*args, **kwargs):
    matches_only = kwargs.pop("matches_only", False)
    with_seq = kwargs.pop("with_seq", False)
    with pysam.AlignmentFile(*args, **kwargs) as inf:
        data = [x.get_aligned_pairs(matches_only=matches_only, with_seq=with_seq)
                for x in inf if not x.is_unmapped]
    return data