bghomraw.py 8.82 KB
Newer Older
jhoogenboom's avatar
jhoogenboom committed
1
#!/usr/bin/env python
2 3

#
4
# Copyright (C) 2017 Jerry Hoogenboom
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# This file is part of FDSTools, data analysis tools for Next
# Generation Sequencing of forensic DNA markers.
#
# FDSTools is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# FDSTools is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FDSTools.  If not, see <http://www.gnu.org/licenses/>.
#

jhoogenboom's avatar
jhoogenboom committed
23 24 25 26
"""
Compute noise ratios for all noise detected in homozygous reference
samples.

jhoogenboom's avatar
jhoogenboom committed
27 28 29 30
With this tool, separate data points are produced for each sample, which
can be visualised using "fdstools vis bgraw".  Use bghomstats or
bgestimate to compute aggregate statistics on noise instead.
"""
31
from errno import EPIPE
jhoogenboom's avatar
jhoogenboom committed
32
from ..lib import pos_int_arg, add_input_output_args, get_input_output_files,\
33
                  add_allele_detection_args, parse_allelelist,\
jhoogenboom's avatar
jhoogenboom committed
34
                  get_sample_data, add_sequence_format_args
jhoogenboom's avatar
jhoogenboom committed
35

36
__version__ = "1.0.1"
jhoogenboom's avatar
jhoogenboom committed
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


# Default values for parameters are specified below.

# Default minimum amount of background to consider, as a percentage of
# the highest allele.
# This value can be overridden by the -m command line option.
_DEF_THRESHOLD_PCT = 0.5

# Default minimum number of reads to consider.
# This value can be overridden by the -n command line option.
_DEF_THRESHOLD_ABS = 5

# Default minimum number of samples for each true allele.
# This value can be overridden by the -s command line option.
_DEF_MIN_SAMPLES = 2

# Default minimum number of samples required for each background product
# to be included in the analysis, as a percentage of the number of
# samples with a certain true allele.
# This value can be overridden by the -S command line option.
_DEF_MIN_SAMPLE_PCT = 80.


def add_sample_data(data, sample_data, sample_alleles, min_pct, min_abs, tag):
    # Check presence of all alleles.
    for marker in sample_alleles:
        allele = sample_alleles[marker]
        if (marker, allele) not in sample_data:
            raise ValueError(
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
67 68
                "Missing allele %s of marker %s in sample %s!" %
                        (allele, marker, tag))
jhoogenboom's avatar
jhoogenboom committed
69 70
        elif 0 in sample_data[marker, allele]:
            raise ValueError(
71 72
                "Allele %s of marker %s has 0 reads on one strand in "
                    "sample %s!" % (allele, marker, tag))
jhoogenboom's avatar
jhoogenboom committed
73 74 75

    # Enter the read counts into data and check the thresholds.
    for marker, sequence in sample_data:
76
        if marker not in sample_alleles:
jhoogenboom's avatar
jhoogenboom committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            # Sample does not participate in this marker.
            continue
        allele = sample_alleles[marker]
        factors = [100./x for x in sample_data[marker, allele]]
        factors.append(100./sum(sample_data[marker, allele]))
        if (marker, allele) not in data:
            data[marker, allele] = {}
        if sequence not in data[marker, allele]:
            data[marker, allele][sequence] = {
                "tag": [],
                "forward": [],
                "reverse": [],
                "fnoise": [],
                "rnoise": [],
                "tnoise": [],
                "passed_filter": 0}
        data[marker, allele][sequence]["tag"].append(tag)
        data[marker, allele][sequence]["forward"].append(
            sample_data[marker, sequence][0])
        data[marker, allele][sequence]["reverse"].append(
            sample_data[marker, sequence][1])
        data[marker, allele][sequence]["fnoise"].append(
            sample_data[marker, sequence][0] * factors[0])
        data[marker, allele][sequence]["rnoise"].append(
            sample_data[marker, sequence][1] * factors[1])
        data[marker, allele][sequence]["tnoise"].append(
            sum(sample_data[marker, sequence]) * factors[2])
        if sum(count >= min_abs and count*factor >= min_pct
               for count, factor in
               zip(sample_data[marker, sequence], factors[:2])):
            data[marker, allele][sequence]["passed_filter"] += 1
#add_sample_data


def filter_data(data, min_samples, min_sample_pct):
    """
    Remove all alleles from data that have less than min_samples samples
    and remove all data of sequences that don't pass the detection
    thresholds in at least min_sample_pct per cent of the samples with a
    particular allele.
    """
    for marker, allele in data.keys():
        if data[marker, allele][allele]["passed_filter"] < min_samples:
            del data[marker, allele]
            continue
        factor = 100./data[marker, allele][allele]["passed_filter"]
        for sequence in data[marker, allele].keys():
            if (data[marker, allele][sequence]["passed_filter"] * factor <
                    min_sample_pct):
                del data[marker, allele][sequence]
                continue
#filter_data


def compute_ratios(samples_in, outfile, allelefile, annotation_column, min_pct,
                   min_abs, min_samples, min_sample_pct, seqformat, library,
                   marker):

135
    # Parse allele list.
jhoogenboom's avatar
jhoogenboom committed
136 137 138 139 140 141 142 143 144 145 146
    allelelist = {} if allelefile is None \
                    else parse_allelelist(allelefile, seqformat, library)

    # Read sample data.
    data = {}
    get_sample_data(
        samples_in,
        lambda tag, sample_data: add_sample_data(
            data, sample_data,
            {m: allelelist[tag][m].pop() for m in allelelist[tag]},
            min_pct, min_abs, tag),
147
        allelelist, annotation_column, seqformat, library, marker, True,
148
        drop_special_seq=True)
jhoogenboom's avatar
jhoogenboom committed
149 150 151 152 153 154 155 156 157 158 159

    # Ensure minimum number of samples per allele and filter
    # insignificant background products.
    filter_data(data, min_samples, min_sample_pct)

    outfile.write("\t".join(["sample", "marker", "allele", "sequence",
        "forward", "reverse", "total", "fnoise", "rnoise", "tnoise"]) + "\n")
    for marker, allele in data:
        for sequence in data[marker, allele]:
            for i in range(len(data[marker, allele][sequence]["tag"])):
                outfile.write("\t".join([
jhoogenboom's avatar
jhoogenboom committed
160
                    data[marker, allele][sequence]["tag"][i], marker, allele,
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
161
                    sequence] + map(str, (
jhoogenboom's avatar
jhoogenboom committed
162 163 164
                        data[marker, allele][sequence]["forward"][i],
                        data[marker, allele][sequence]["reverse"][i],
                        data[marker, allele][sequence]["forward"][i] +
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
165
                        data[marker, allele][sequence]["reverse"][i])) + [
166
                    "%.3g" % x if abs(x) > 0.0000000001 else "0" for x in (
jhoogenboom's avatar
jhoogenboom committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                        data[marker, allele][sequence]["fnoise"][i],
                        data[marker, allele][sequence]["rnoise"][i],
                        data[marker, allele][sequence]["tnoise"][i])]) + "\n")
#compute_ratios


def add_arguments(parser):
    add_input_output_args(parser)
    add_allele_detection_args(parser)
    filtergroup = parser.add_argument_group("filtering options")
    filtergroup.add_argument('-m', '--min-pct', metavar="PCT", type=float,
        default=_DEF_THRESHOLD_PCT,
        help="minimum amount of background to consider, as a percentage "
             "of the highest allele (default: %4.2f)" % _DEF_THRESHOLD_PCT)
    filtergroup.add_argument('-n', '--min-abs', metavar="N", type=pos_int_arg,
        default=_DEF_THRESHOLD_ABS,
        help="minimum amount of background to consider, as an absolute "
             "number of reads (default: %(default)s)")
    filtergroup.add_argument('-s', '--min-samples', metavar="N",
        type=pos_int_arg,
        default=_DEF_MIN_SAMPLES,
        help="require this minimum number of samples for each true allele "
             "(default: %(default)s)")
    filtergroup.add_argument('-S', '--min-sample-pct', metavar="PCT",
        type=float,
        default=_DEF_MIN_SAMPLE_PCT,
        help="require this minimum number of samples for each background "
             "product, as a percentage of the number of samples with a "
             "particular true allele (default: %(default)s)")
    filtergroup.add_argument('-M', '--marker', metavar="MARKER",
        help="work only on MARKER")
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
198
    add_sequence_format_args(parser, "raw")
jhoogenboom's avatar
jhoogenboom committed
199 200 201 202 203 204 205 206
#add_arguments


def run(args):
    files = get_input_output_files(args)
    if not files:
        raise ValueError("please specify an input file, or pipe in the output "
                         "of another program")
207 208 209 210 211 212 213 214 215
    try:
        compute_ratios(files[0], files[1], args.allelelist,
                       args.annotation_column, args.min_pct, args.min_abs,
                       args.min_samples, args.min_sample_pct,
                       args.sequence_format, args.library, args.marker)
    except IOError as e:
        if e.errno == EPIPE:
            return
        raise
Hoogenboom, Jerry's avatar
Hoogenboom, Jerry committed
216
#run