comparison seg2matrix/segToMatrixGalaxy.py @ 31:ab20c0d04f4a

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