annotate SMART/Java/Python/getDistribution.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 """Get the repartition of some elements in a chromosomes"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.misc.RPlotter import RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from math import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def divideKeyDict(dictionary, ratio):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 return dict([(key / ratio, dictionary[key]) for key in dictionary])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 def setTranscript(chromosome, direction, start, end, name, value):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 transcript.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 transcript.setDirection(direction)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 transcript.setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 transcript.setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 transcript.setName(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 transcript.setTagValue("nbElements", value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 return transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 magnifyingFactor = 1000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 description = "Get Distribution v1.0.1: Get the distribution of the genomic coordinates on a genome. [Category: Visualization]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the input file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 parser.add_option("-r", "--reference", dest="referenceFileName", action="store", default=None, type="string", help="file containing the genome [compulsory] [format: file in FASTA format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 parser.add_option("-n", "--nbBins", dest="nbBins", action="store", default=1000, type="int", help="number of bins [default: 1000] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 parser.add_option("-2", "--bothStrands", dest="bothStrands", action="store_true", default=False, help="plot one curve per strand [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 parser.add_option("-w", "--raw", dest="raw", action="store_true", default=False, help="plot raw number of occurrences instead of density [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 parser.add_option("-x", "--csv", dest="csv", action="store_true", default=False, help="write a .csv file [format: bool]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 parser.add_option("-c", "--chromosome", dest="chromosome", action="store", default=None, type="string", help="plot only a chromosome [format: string]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 parser.add_option("-s", "--start", dest="start", action="store", default=None, type="int", help="start from a given region [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 parser.add_option("-e", "--end", dest="end", action="store", default=None, type="int", help="end from a given region [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 parser.add_option("-y", "--yMin", dest="yMin", action="store", default=None, type="int", help="minimum value on the y-axis to plot [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 parser.add_option("-Y", "--yMax", dest="yMax", action="store", default=None, type="int", help="maximum value on the y-axis to plot [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 parser.add_option("-g", "--gff", dest="gff", action="store_true", default=False, help="also write GFF3 file [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 parser.add_option("-H", "--height", dest="height", action="store", default=None, type="int", help="height of the graphics [format: int] [default: 300]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 parser.add_option("-W", "--width", dest="width", action="store", default=None, type="int", help="width of the graphics [format: int] [default: 1000]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 parser.add_option("-l", "--log", dest="log", action="store_true", default=False, help="write a log file [format: bool]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 parser.add_option("-D", "--directory", dest="working_Dir", action="store", default=os.getcwd(), type="string", help="the directory to store the results [format: directory]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 sizes = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if options.referenceFileName != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 # get the sizes of the chromosomes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 referenceHandle = open(options.referenceFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 name = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 maxSize = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 for line in referenceHandle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 if line == "": continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if name != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 if options.verbosity > 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 print name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 sizes[name] = size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 maxSize = max(maxSize, size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 name = line[1:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 size += len(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 sizes[name] = size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 maxSize = max(maxSize, size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 if options.verbosity > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 print "done"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 start = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 end = maxSize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 if options.chromosome == None or options.start == None or options.end == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 raise Exception("Missing chromosome or start and end positions, or reference file")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 maxSize = options.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 sizes[options.chromosome] = options.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 start = options.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 end = options.end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 tmp1 = int(maxSize / float(options.nbBins))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 tmp2 = 10 ** (len("%d" % (tmp1))-2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 sliceSize = int((tmp1 / tmp2) * tmp2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 bins = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 binsPlus = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 binsMinus = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 for chromosome in sizes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 bins[chromosome] = dict([(i * sliceSize + 1, 0) for i in range(start / sliceSize, sizes[chromosome] / sliceSize + 1)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 binsPlus[chromosome] = dict([(i * sliceSize + 1, 0) for i in range(start / sliceSize, sizes[chromosome] / sliceSize + 1)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 binsMinus[chromosome] = dict([(i * sliceSize + 1, 0) for i in range(start / sliceSize, sizes[chromosome] / sliceSize + 1)])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 parser = TranscriptContainer(options.inputFileName, options.format, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 progress = Progress(parser.getNbTranscripts(), "Parsing %s" % (options.inputFileName), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 maxSlice = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 # count the number of reads
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if options.chromosome == None or (transcript.getChromosome() == options.chromosome and transcript.getStart() >= start and transcript.getStart() <= end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 if transcript.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 binsPlus[transcript.getChromosome()][(transcript.getStart() / sliceSize) * sliceSize + 1] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 binsMinus[transcript.getChromosome()][(transcript.getStart() / sliceSize) * sliceSize + 1] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 bins[transcript.getChromosome()][(transcript.getStart() / sliceSize) * sliceSize + 1] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 maxSlice = max(maxSlice, transcript.getStart() / sliceSize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 # compute densities
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 densityPlus = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 for chromosome in bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 densityPlus[chromosome] = dict([(bin, 0) for bin in binsPlus[chromosome]])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 for bin in binsPlus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 densityPlus[chromosome][bin] = float(binsPlus[chromosome][bin]) / sliceSize * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 # correct densities for first and last bins
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 if start % sliceSize != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 densityPlus[chromosome][(start / sliceSize) * sliceSize + 1] = float(binsPlus[chromosome][(start / sliceSize) * sliceSize + 1]) / (sliceSize - (start % sliceSize)) * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 if sizes[chromosome] % sliceSize != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 densityPlus[chromosome][(sizes[chromosome] / sliceSize) * sliceSize + 1] = float(binsPlus[chromosome][(sizes[chromosome] / sliceSize) * sliceSize + 1]) / (sizes[chromosome] % sliceSize) * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 densityMinus = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 for chromosome in binsMinus:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 densityMinus[chromosome] = dict([(bin, 0) for bin in binsMinus[chromosome]])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 for bin in binsMinus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 densityMinus[chromosome][bin] = float(binsMinus[chromosome][bin]) / sliceSize * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 # correct densities for first and last bins
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 if start % sliceSize != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 densityMinus[chromosome][(start / sliceSize) * sliceSize + 1] = float(binsMinus[chromosome][(start / sliceSize) * sliceSize + 1]) / (sliceSize - (start % sliceSize)) * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 if sizes[chromosome] % sliceSize != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 densityMinus[chromosome][(sizes[chromosome] / sliceSize) * sliceSize + 1] = float(binsMinus[chromosome][(sizes[chromosome] / sliceSize) * sliceSize + 1]) / (sizes[chromosome] % sliceSize) * magnifyingFactor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 density = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 for chromosome in bins:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 density[chromosome] = dict([(bin, 0) for bin in bins[chromosome]])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 for bin in bins[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 density[chromosome][bin] = densityPlus[chromosome][bin] + densityMinus[chromosome][bin]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 for chromosome in densityMinus:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 for bin in densityMinus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 densityMinus[chromosome][bin] *= -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 for bin in binsMinus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 binsMinus[chromosome][bin] *= -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 for chromosome in density:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 maxX = max(bins[chromosome].keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 if maxX <= 1000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 unit = "nt."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 ratio = 1.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 elif maxX <= 1000000:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 unit = "kb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 ratio = 1000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 unit = "Mb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 ratio = 1000000.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 outputFileName = "%s_%s" % (options.outputFileName, chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 if options.start != None and options.end != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 outputFileName += ":%d-%d" % (options.start, options.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 outputFileName += ".png"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 plotter = RPlotter(outputFileName, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 plotter.setXLabel("Position on %s (in %s)" % (chromosome.replace("_", " "), unit))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 plotter.setYLabel("# reads")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 if options.bothStrands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 plotter.setImageSize(1000, 300)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 plotter.setImageSize(1000, 200)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 if options.height != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 plotter.setHeight(options.height)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 if options.width != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 plotter.setWidth(options.width)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 if options.yMax != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 plotter.setMinimumY(options.yMin)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 if options.yMax != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 plotter.setMaximumY(options.yMax)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 if options.bothStrands :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 if options.raw:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 plotter.addLine(divideKeyDict(binsPlus[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 plotter.addLine(divideKeyDict(densityPlus[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 if options.raw:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 plotter.addLine(divideKeyDict(binsMinus[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 plotter.addLine(divideKeyDict(densityMinus[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 if options.raw:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 plotter.addLine(divideKeyDict(bins[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 plotter.addLine(divideKeyDict(density[chromosome], ratio))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 plotter.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 if options.csv:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 outputFileName = "%s" % (options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 if options.chromosome != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 outputFileName += "_%s" % (options.chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 if options.start != None and options.end != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 outputFileName += ":%d-%d" % (options.start, options.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 outputFileName += ".csv"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 csvHandle = open(outputFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 for slice in range(start / sliceSize, maxSlice + 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 csvHandle.write(";%d-%d" % (slice * sliceSize + 1, (slice+1) * sliceSize))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 if options.bothStrands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 for chromosome in densityPlus:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 if len(densityPlus[chromosome]) > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 csvHandle.write("%s [+]" % (chromosome))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 for slice in sorted(densityPlus[chromosome].keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 csvHandle.write(";%.2f" % (densityPlus[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 if len(densityMinus[chromosome]) > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 csvHandle.write("%s [-]" % (chromosome))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 for slice in sorted(densityPlus[chromosome].keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 csvHandle.write(";%.2f" % (-densityMinus[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 for chromosome in density:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 if len(density[chromosome]) > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 csvHandle.write(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 for slice in sorted(density[chromosome].keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 csvHandle.write(";%.2f" % (density[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 csvHandle.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 csvHandle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 if options.gff:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 chromosome = "" if options.chromosome == None else options.chromosome.capitalize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 start = "" if options.start == None else "%d" % (options.start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 end = "" if options.end == None else "%d" % (options.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 link1 = "" if options.start == None and options.end == None else ":"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 link2 = "" if options.start == None and options.end == None else "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 writer = Gff3Writer("%s%s%s%s%s.gff3" % (options.outputFileName, link1, start, link2, end), options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 cpt = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 if options.raw:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 valuesPlus = binsPlus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 valuesMinus = binsMinus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 values = bins
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 valuesPlus = densityPlus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 valuesMinus = densityMinus
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 values = density
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 if options.bothStrands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 for chromosome in values:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 for slice in valuesPlus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 writer.addTranscript(setTranscript(chromosome, 1, slice, slice + sliceSize, "region%d" % (cpt), valuesPlus[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 for slice in valuesMinus[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 writer.addTranscript(setTranscript(chromosome, -1, slice, slice + sliceSize, "region%d" % (cpt), - valuesMinus[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 for chromosome in values:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 for slice in values[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 writer.addTranscript(setTranscript(chromosome, 1, slice, slice + sliceSize, "region%d" % (cpt), values[chromosome][slice]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 writer.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291