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