annotate seg2matrix/segToMatrixGalaxy.py @ 47:23d98125d20c

parse snpEff output
author jingchunzhu
date Thu, 13 Aug 2015 23:26:33 -0700
parents ab20c0d04f4a
children 7de3db823f90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
1 #!/usr/bin/env python
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
2
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
3 import sys,os
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
4 import CGData.GenomicSegment
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
5 import CGData.SegToMatrix
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
6 import CGData.RefGene
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
7 import CGData.GeneMap
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
8
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
9 class matrix_write:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
10 def __init__(self, handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
11 self.buff = ""
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
12 self.handle = handle
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
13 self.probes = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 def write(self, s):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15 self.buff += s
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16 if s.endswith("\n"):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17 tmp = self.buff.split("\t")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 if tmp[0] != "probe":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 tmp2 = tmp[0].split("_")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 p = probeseg(tmp[0], tmp2[0], int(tmp2[1]), int(tmp2[2]))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 self.probes.append(p)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23 self.handle.write(self.buff)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24 self.buff = ""
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
25
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
26 class probeseg:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27 def __init__(self, name, chrom, chrom_start, chrom_end):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28 self.name = name
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29 self.chrom = chrom
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30 self.chrom_start = chrom_start
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
31 self.chrom_end = chrom_end
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
32 self.strand = "."
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
33
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
34
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
35 if __name__ == "__main__":
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
36 if len(sys.argv[:])!= 5:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
37 print "python segToMatrixGalaxy.py inputSegmentFile refGeneFile outputMatrix outputProbeMap\n"
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
38 sys.exit()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
39 seg = CGData.GenomicSegment.GenomicSegment()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
40 seg.load(sys.argv[1])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
41
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
42 refgene = CGData.RefGene.RefGene()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
43 refgene.load(os.path.dirname(sys.argv[0])+"/"+sys.argv[2])
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
44
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
45 handle = open(sys.argv[3], "w")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
46 m = matrix_write(handle)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
47 CGData.SegToMatrix.seg_to_matrix(seg, m)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
48 handle.close()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
49
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
50 handle = open(sys.argv[4], "w")
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
51 probeMapper = CGData.GeneMap.ProbeMapper('b')
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
52 handle.write("%s\t%s\t%s\t%s\t%s\t%s\n" % ("#id", "gene","chrom","chromStart","chromEnd","strand"))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
53 for probe in m.probes:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
54 hits = []
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
55 for hit in probeMapper.find_overlap( probe, refgene ):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
56 if hit.name not in hits:
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
57 hits.append(hit.name)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
58 handle.write("%s\t%s\t%s\t%s\t%s\t.\n" % (probe.name, ",".join(hits), probe.chrom, probe.chrom_start, probe.chrom_end))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
59 handle.close()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
60
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
61