31
|
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__":
|
54
|
36 if len(sys.argv[:])!= 6:
|
|
37 print "python segToMatrixGalaxy.py inputSegmentFile refGeneFile outputMatrix outputProbeMap NORMAL_CNV\n"
|
31
|
38 sys.exit()
|
|
39 seg = CGData.GenomicSegment.GenomicSegment()
|
|
40 seg.load(sys.argv[1])
|
|
41
|
|
42 refgene = CGData.RefGene.RefGene()
|
54
|
43 refgene.load(os.path.dirname(sys.argv[0])+"/"+os.path.basename(sys.argv[2]))
|
|
44
|
|
45 NORMAL_CNV = sys.argv[5]
|
53
|
46
|
31
|
47 handle = open(sys.argv[3], "w")
|
|
48 m = matrix_write(handle)
|
54
|
49 CGData.SegToMatrix.seg_to_matrix(seg, m, NORMAL_CNV)
|
31
|
50 handle.close()
|
|
51
|
|
52 handle = open(sys.argv[4], "w")
|
|
53 probeMapper = CGData.GeneMap.ProbeMapper('b')
|
54
|
54 handle.write("%s\t%s\t%s\t%s\t%s\t%s\n" % ("id", "gene","chrom","chromStart","chromEnd","strand"))
|
31
|
55 for probe in m.probes:
|
|
56 hits = []
|
|
57 for hit in probeMapper.find_overlap( probe, refgene ):
|
|
58 if hit.name not in hits:
|
|
59 hits.append(hit.name)
|
|
60 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))
|
|
61 handle.close()
|
|
62
|
|
63
|