|
| 1 | +#!/usr/bin/env python |
| 2 | +from collections import namedtuple |
| 3 | +from operator import itemgetter |
| 4 | + |
| 5 | +Annotation = namedtuple('Annotation', ['name', 'start', 'end', 'strand']) |
| 6 | +Event = namedtuple('Event', ['location', 'annotation', 'prediction']) |
| 7 | + |
| 8 | +def parse_readlengths(filename): |
| 9 | + with open(filename) as f: |
| 10 | + for line in f: |
| 11 | + name, length = line.strip().split(',') |
| 12 | + yield name, int(length) |
| 13 | + |
| 14 | +def parse_csv(filename): |
| 15 | + with open(filename) as f: |
| 16 | + for line in f: |
| 17 | + name, start, end, strand = line.strip().split(',') |
| 18 | + yield Annotation(name, int(start), int(end), 1 if strand == '+' else -1) |
| 19 | + |
| 20 | +def events(read_lengths, annotations, predictions): |
| 21 | + names = dict() |
| 22 | + total = 0 |
| 23 | + for name, length in parse_readlengths(read_lengths): |
| 24 | + names[name] = total |
| 25 | + total += length |
| 26 | + |
| 27 | + for name, start, end, strand in parse_csv(annotations): |
| 28 | + yield Event(names[name] + start, strand, None) |
| 29 | + yield Event(names[name] + end, 0, None) |
| 30 | + |
| 31 | + for name, start, end, strand in parse_csv(predictions): |
| 32 | + yield Event(names[name] + start, None, strand) |
| 33 | + yield Event(names[name] + end, None, 0) |
| 34 | + |
| 35 | +def rates(read_lengths, annotations, predictions): |
| 36 | + rates = dict(tp=0, fp=0, tn=0, fn=0) |
| 37 | + cl = 0 # current location |
| 38 | + ca = 0 # current annotation |
| 39 | + cp = 0 # current prediction |
| 40 | + for l, a, p in sorted(events(read_lengths, annotations, predictions), key=itemgetter(0)): |
| 41 | + if ca == 0 and cp == 0: |
| 42 | + rates['tn'] += l - cl |
| 43 | + elif ca == cp: |
| 44 | + rates['tp'] += l - cl |
| 45 | + elif ca == 0 and cp != 0: |
| 46 | + rates['fp'] += l - cl |
| 47 | + elif ca != 0 and cp == 0: |
| 48 | + rates['fn'] += l - cl |
| 49 | + else: # different strands |
| 50 | + rates['fp'] += l - cl |
| 51 | + |
| 52 | + if a is not None: ca = a |
| 53 | + if p is not None: cp = p |
| 54 | + cl = l |
| 55 | + return rates |
| 56 | + |
| 57 | +body = '{:<10}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}{:>8.2%}' |
| 58 | +head = '{:<10}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}{:>8.4s}' |
| 59 | +print(head.format('tool', 'TP', 'FP', 'TN', 'FN', 'precision', 'sensitivity', 'specificity', 'NPV', 'MCC')) |
| 60 | +for tool in ['FGS', 'FGS+', 'prodigal', 'FGSrs']: |
| 61 | + r = rates('readlengths.csv', 'annotations.csv', f'{tool}.csv') |
| 62 | + tp, fp, tn, fn = r['tp'], r['fp'], r['tn'], r['fn'] |
| 63 | + t = tp + fp + tn + fn |
| 64 | + print(body.format( |
| 65 | + tool, |
| 66 | + tp / t, |
| 67 | + fp / t, |
| 68 | + tn / t, |
| 69 | + fn / t, |
| 70 | + tp / (tp + fp), |
| 71 | + tp / (tp + fn), |
| 72 | + tn / (tn + fp), |
| 73 | + tn / (tn + fn), |
| 74 | + (tp * tn - fp * fn) / ((tp + fp)*(tp + fn)*(tn + fp)*(tn + fn))**0.5 |
| 75 | + )) |
0 commit comments