Debian Codegraph @

tests/AlignmentFileFetchTestUtils.py

raw symbols SCIP
1 import os
2 import subprocess
3 import pysam
4
5 from TestUtils import force_str
6
7
8 def build_fetch_with_samtoolsshell(fn):
9 retval = os.popen("samtools view {} 2> /dev/null | wc -l".format(fn)).read()
10 return int(retval.strip())
11
12
13 def build_fetch_with_samtoolspipe(fn):
14 FNULL = open(os.devnull, 'w')
15 with subprocess.Popen(["samtools", "view", fn],
16 stdin=subprocess.PIPE,
17 stdout=subprocess.PIPE,
18 stderr=FNULL) as proc:
19 return len(proc.stdout.readlines())
20
21
22 def build_fetch_with_pysam(*args, **kwargs):
23 with pysam.AlignmentFile(*args, **kwargs) as inf:
24 return len(list(inf.fetch()))
25
26
27 def build_query_sequences_with_samtoolsshell(fn):
28 retval = os.popen("samtools view {} 2> /dev/null | cut -f 11".format(fn)).read()
29 return force_str(retval).splitlines()
30
31
32 def build_query_sequences_with_samtoolspipe(fn):
33 FNULL = open(os.devnull, 'w')
34 with subprocess.Popen(["samtools", "view", fn],
35 stdin=subprocess.PIPE,
36 stdout=subprocess.PIPE,
37 stderr=FNULL) as proc:
38 data = [force_str(x).split()[10] for x in proc.stdout.readlines()]
39 return data
40
41
42 def build_query_sequences_with_pysam(*args, **kwargs):
43 with pysam.AlignmentFile(*args, **kwargs) as inf:
44 data = [x.query_sequence for x in inf]
45 return data
46
47
48 def build_query_qualities_with_pysam(*args, **kwargs):
49 with pysam.AlignmentFile(*args, **kwargs) as inf:
50 data = [x.query_qualities for x in inf]
51 return data
52
53
54 def build_query_sequences_flagfilter_with_samtoolsshell(fn):
55 retval = os.popen("samtools view -f 2 {} 2> /dev/null | cut -f 11".format(fn)).read()
56 return force_str(retval).splitlines()
57
58
59 def build_query_sequences_flagfilter_with_samtoolspipe(fn):
60 FNULL = open(os.devnull, 'w')
61 with subprocess.Popen(["samtools", "view", "-f", "2", fn],
62 stdin=subprocess.PIPE,
63 stdout=subprocess.PIPE,
64 stderr=FNULL) as proc:
65 data = [force_str(x).split()[10] for x in proc.stdout.readlines()]
66 return data
67
68 def build_query_sequences_flagfilter_with_pysam(*args, **kwargs):
69 with pysam.AlignmentFile(*args, **kwargs) as inf:
70 data = [x.query_sequence for x in inf if x.is_proper_pair]
71 return data
72
73
74 def build_query_sequences_directflagfilter_with_pysam(*args, **kwargs):
75 with pysam.AlignmentFile(*args, **kwargs) as inf:
76 data = [x.query_sequence for x in inf if x.flag & 2]
77 return data
78
79
80 def build_aligned_pairs_with_pysam(*args, **kwargs):
81 matches_only = kwargs.pop("matches_only", False)
82 with_seq = kwargs.pop("with_seq", False)
83 with pysam.AlignmentFile(*args, **kwargs) as inf:
84 data = [x.get_aligned_pairs(matches_only=matches_only, with_seq=with_seq)
85 for x in inf if x.is_mapped]
86 return data
87