File size: 5,146 Bytes
0936134 | 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 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #!/usr/bin/env python
import sys
import math
import os.path
import Script
### Program object
def usage():
progname = os.path.split(sys.argv[0])[1]
sys.stderr.write("""{} - Analyze intron retention.
Usage: {} [options] intronsdb
Options:
-i1 FILE | Name of BED file for introns in sample 1 (required)
-i2 FILE | Name of BED file for introns in sample 2 (required)
-j1 FILE | Name of BED file for junctions in sample 1
-j2 FILE | Name of BED file for junctions in sample 2
-o FILE | Set output file to FILE (default: stdout)
-fc F | Set fold change threshold to F (default: {})
-t T | Set coverage threshold to T (default: {})
""".format(progname, progname, Params.fc, Params.thr))
P = Script.Script("compareIntrons", version="1.0", usage=usage)
def readBEDfile(bedfile):
dict = {}
sys.stderr.write("Reading `{}'... ".format(bedfile))
with open(bedfile, "r") as f:
for line in f:
parsed = line.rstrip("\r\n").split("\t")
dict[parsed[3]] = float(parsed[7])
sys.stderr.write("done, {} entries.\n".format(len(dict)))
return dict
def dget(key, dict):
if key in dict:
return dict[key]
else:
return 0.0
class Params():
bedfile = None
introns1file = None
juncs1file = None
introns2file = None
juncs2file = None
outfile = None
fc = 1
thr = 0.00001
intr1 = {}
junc1 = {}
intr2 = {}
junc2 = {}
def __init__(self):
self.intr1 = {}
self.junc1 = {}
self.intr2 = {}
self.junc2 = {}
def parseArgs(self, args):
P.standardOpts(args)
next = ""
for a in args:
if next == "-i1":
self.introns1file = P.isFile(a)
next = ""
elif next == "-j1":
self.juncs1file = P.isFile(a)
next = ""
elif next == "-i2":
self.introns2file = P.isFile(a)
next = ""
elif next == "-j2":
self.juncs2file = P.isFile(a)
next = ""
elif next == "-o":
self.outfile = a
next = ""
elif next == "-fc":
self.fc = P.toFloat(a)
next = ""
elif next == "-t":
self.thr = P.toFloat(a)
next = ""
elif a in ["-i1", "-j1", "-i2", "-j2", "-fc", "-o", "-t"]:
next = a
else:
self.bedfile = a
if self.bedfile == None or self.introns1file == None or self.introns2file == None:
P.errmsg(P.NOFILE)
def readFiles(self):
self.intr1 = readBEDfile(self.introns1file)
if self.juncs1file != None:
self.juncs1 = readBEDfile(self.juncs1file)
self.intr2 = readBEDfile(self.introns2file)
if self.juncs2file != None:
self.juncs2 = readBEDfile(self.juncs2file)
def compare(self, out):
nin = 0
nup = 0
ndown = 0
maxup = 0
maxdn = 0
with open(self.bedfile, "r") as f:
for line in f:
nin += 1
parsed = line.rstrip("\r\n").split("\t")
intron = parsed[3]
gene = parsed[4]
sp = intron.split("_")
tx = sp[0]
intid = sp[1]
iv1 = dget(intron, self.intr1)
iv2 = dget(intron, self.intr2)
if self.juncs1file:
iv1 = (iv1 + dget(intron + "_a", self.juncs1) + dget(intron + "_b", self.juncs1)) / 3.0
if self.juncs2file:
iv2 = (iv2 + dget(intron + "_a", self.juncs2) + dget(intron + "_b", self.juncs2)) / 3.0
if iv1 == 0 and iv2 == 0:
pass
elif iv1 == 0:
if iv2 >= self.thr:
out.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(gene, tx, intid, iv1, iv2, "+inf"))
elif iv2 == 0:
if iv1 >= self.thr:
out.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(gene, tx, intid, iv1, iv2, "-inf"))
else:
l2fc = math.log(iv2/iv1, 2)
if abs(l2fc) > self.fc:
out.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(gene, tx, intid, iv1, iv2, l2fc))
if l2fc > 0:
nup += 1
if l2fc > maxup:
maxup = l2fc
else:
ndown += 1
if l2fc < maxdn:
maxdn = l2fc
return (nin, nup, ndown, maxup, maxdn)
if __name__ == "__main__":
PA = Params()
PA.parseArgs(sys.argv[1:])
PA.readFiles()
if PA.outfile:
with open(PA.outfile, "w") as out:
(nin, nup, ndown, maxup, maxdn) = PA.compare(out)
else:
(nin, nup, ndown, maxup, maxdn) = PA.compare(sys.stdout)
sys.stderr.write("{}\t{}\t{}\t{}\t{}\n".format(nin, nup, ndown, maxup, maxdn))
|