| 1 | import os |
| 2 | import subprocess |
| 3 | import pysam |
| 4 | |
| 5 | from TestUtils import force_str |
| 6 | |
| 7 | |
| 8 | def build_pileup_with_samtoolsshell(fn): |
| 9 | os.system("samtools mpileup {} 2> /dev/null | wc -l > /dev/null".format(fn)) |
| 10 | return 2998 |
| 11 | |
| 12 | |
| 13 | def build_pileup_with_samtoolspipe(fn): |
| 14 | FNULL = open(os.devnull, 'w') |
| 15 | with subprocess.Popen(["samtools", "mpileup", 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_pileup_with_pysam(*args, **kwargs): |
| 23 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 24 | return len(list(inf.pileup(stepper="samtools"))) |
| 25 | |
| 26 | |
| 27 | def build_depth_with_samtoolsshell(fn): |
| 28 | os.system( |
| 29 | "samtools mpileup {} 2> /dev/null | awk '{{a += $4}} END {{print a}}' > /dev/null".format(fn)) |
| 30 | return 107241 |
| 31 | |
| 32 | |
| 33 | def build_depth_with_samtoolspipe(fn): |
| 34 | FNULL = open(os.devnull, 'w') |
| 35 | with subprocess.Popen(["samtools", "mpileup", fn], |
| 36 | stdin=subprocess.PIPE, |
| 37 | stdout=subprocess.PIPE, |
| 38 | stderr=FNULL) as proc: |
| 39 | data = [x.split() for x in proc.stdout.readlines()] |
| 40 | return [int(x[3]) for x in data] |
| 41 | |
| 42 | |
| 43 | def build_depth_with_filter_with_pysam(*args, **kwargs): |
| 44 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 45 | return [x.get_num_aligned() for x in inf.pileup(stepper="samtools")] |
| 46 | |
| 47 | |
| 48 | def build_depth_with_pysam(*args, **kwargs): |
| 49 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 50 | return [x.nsegments for x in inf.pileup(stepper="samtools")] |
| 51 | |
| 52 | |
| 53 | def build_query_bases_with_samtoolsshell(fn): |
| 54 | os.system("samtools mpileup {} 2> /dev/null | awk '{{a = a $5}} END {{print a}}' | wc -c > /dev/null".format(fn)) |
| 55 | return 116308 |
| 56 | |
| 57 | |
| 58 | def build_query_bases_with_samtoolspipe(fn, *args, **kwargs): |
| 59 | FNULL = open(os.devnull, 'w') |
| 60 | with subprocess.Popen(["samtools", "mpileup", fn] + list(args), |
| 61 | stdin=subprocess.PIPE, |
| 62 | stdout=subprocess.PIPE, |
| 63 | stderr=FNULL) as proc: |
| 64 | stdout = proc.stdout.read().decode() |
| 65 | return [x.split()[4] for x in stdout.splitlines()] |
| 66 | |
| 67 | |
| 68 | def build_query_bases_with_samtoolspysam(fn, *args): |
| 69 | return [x.split()[4] for x in pysam.samtools.mpileup(fn, *args).splitlines()] |
| 70 | |
| 71 | |
| 72 | def build_query_bases_with_pysam_pileups(*args, **kwargs): |
| 73 | total_pileup = [] |
| 74 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 75 | total_pileup = [ |
| 76 | [r.alignment.query_sequence[r.query_position_or_next] |
| 77 | for r in column.pileups if r.query_position_or_next is not None] |
| 78 | for column in inf.pileup(stepper="samtools")] |
| 79 | return total_pileup |
| 80 | |
| 81 | |
| 82 | def build_query_qualities_with_pysam_pileups(*args, **kwargs): |
| 83 | total_pileup = [] |
| 84 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 85 | total_pileup = [ |
| 86 | [r.alignment.query_qualities[r.query_position_or_next] |
| 87 | for r in column.pileups if r.query_position_or_next is not None] |
| 88 | for column in inf.pileup(stepper="samtools")] |
| 89 | return total_pileup |
| 90 | |
| 91 | |
| 92 | def build_query_bases_with_pysam(fn, *args, **kwargs): |
| 93 | total_pileup = [] |
| 94 | with pysam.AlignmentFile(fn) as inf: |
| 95 | total_pileup = [column.get_query_sequences( |
| 96 | mark_ends=True, add_indels=True, mark_matches=True) for column in |
| 97 | inf.pileup(*args, **kwargs)] |
| 98 | return total_pileup |
| 99 | |
| 100 | |
| 101 | def build_query_names_with_pysam(*args, **kwargs): |
| 102 | total_pileup = [] |
| 103 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 104 | total_pileup = [column.get_query_names() for column in |
| 105 | inf.pileup(stepper="samtools")] |
| 106 | return total_pileup |
| 107 | |
| 108 | |
| 109 | def build_query_qualities_with_samtoolspipe(fn): |
| 110 | FNULL = open(os.devnull, 'w') |
| 111 | with subprocess.Popen(["samtools", "mpileup", fn], |
| 112 | stdin=subprocess.PIPE, |
| 113 | stdout=subprocess.PIPE, |
| 114 | stderr=FNULL) as proc: |
| 115 | data = [force_str(x).split()[5] for x in proc.stdout.readlines()] |
| 116 | return data |
| 117 | |
| 118 | |
| 119 | def build_query_qualities_with_pysam(*args, **kwargs): |
| 120 | total_pileup = [] |
| 121 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 122 | total_pileup = [column.get_query_qualities() for column in |
| 123 | inf.pileup(stepper="samtools")] |
| 124 | return total_pileup |
| 125 | |
| 126 | |
| 127 | def build_mapping_qualities_with_samtoolspipe(fn): |
| 128 | FNULL = open(os.devnull, 'w') |
| 129 | with subprocess.Popen(["samtools", "mpileup", "-s", fn], |
| 130 | stdin=subprocess.PIPE, |
| 131 | stdout=subprocess.PIPE, |
| 132 | stderr=FNULL) as proc: |
| 133 | data = [force_str(x).split()[6] for x in proc.stdout.readlines()] |
| 134 | return data |
| 135 | |
| 136 | |
| 137 | def build_mapping_qualities_with_pysam(*args, **kwargs): |
| 138 | total_pileup = [] |
| 139 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 140 | total_pileup = [column.get_mapping_qualities() for column in |
| 141 | inf.pileup(stepper="samtools")] |
| 142 | return total_pileup |
| 143 | |
| 144 | |
| 145 | def build_query_positions_with_samtoolspipe(fn): |
| 146 | FNULL = open(os.devnull, 'w') |
| 147 | with subprocess.Popen(["samtools", "mpileup", "-O", fn], |
| 148 | stdin=subprocess.PIPE, |
| 149 | stdout=subprocess.PIPE, |
| 150 | stderr=FNULL) as proc: |
| 151 | data = [list(map(int, force_str(x).split()[6].split(","))) |
| 152 | for x in proc.stdout.readlines()] |
| 153 | return data |
| 154 | |
| 155 | |
| 156 | def build_query_positions_with_pysam(*args, **kwargs): |
| 157 | total_pileup = [] |
| 158 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 159 | total_pileup = [column.get_query_positions() for column in |
| 160 | inf.pileup(stepper="samtools")] |
| 161 | return total_pileup |