annotate SMART/Java/Python/getLetterDistribution.py @ 21:91f8c4ce8c27

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:25:37 -0400
parents 769e306b7933
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 size distribution of a Fasta / BED file"""
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 commons.core.parsing.FastaParser import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.misc.Progress import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.misc.RPlotter import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 def writeCVSfile(outHandler):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 for pos in range(len(letters)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 posTrue = pos +1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 outHandler.write( "%s;" % (posTrue))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 for letter in lettersRate:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 if positionRate[letter].has_key(pos):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 outHandler.write("%s=%.2f%s;" %(letter, positionRate[letter][pos], "%"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 outHandler.write("%s=0%s;" % (letter, "%"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 outHandler.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 description = "Get Letter Distribution v1.0.1: Compute the distribution of nucleotides of a set of genomic coordinates. [Category: Visualization]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file to be analyzed [compulsory] [format: file in sequence format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of file [format: sequence file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in PNG format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 parser.add_option("-c", "--csv", dest="csv", action="store_true", default=False, help="write a .csv file [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 parser.add_option("-l", "--log", dest="log", action="store_true", default=False, help="write a log file [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 chooser = ParserChooser()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 chooser.findFormat(options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 parser = chooser.getParser(options.inputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 nbSequences = parser.getNbSequences()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 print "%i sequences read" % (nbSequences)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 # treat items
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 progress = Progress(nbSequences, "Analyzing sequences of " + options.inputFileName, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 nbLettersTotal = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 nbLetters = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 lettersRate = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 nbPositions = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 positionCount = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 positionRate = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 nbPositionRate = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 for sequence in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 letters = sequence.getSequence()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 thisNbLettersTotal = sequence.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 nbLettersTotal += thisNbLettersTotal
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 thisNbLetters = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 for pos in range(len(letters)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 letter = letters[pos]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 if letter not in thisNbLetters:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 thisNbLetters[letter] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 thisNbLetters[letter] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 if pos+1 not in nbPositions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 nbPositions[pos+1] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 nbPositions[pos+1] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if letter not in positionCount:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 positionCount[letter] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 if pos+1 not in positionCount[letter]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 positionCount[letter][pos+1] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 positionCount[letter][pos+1] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 for letter in thisNbLetters:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 if letter not in nbLetters:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 nbLetters[letter] = thisNbLetters[letter]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 nbLetters[letter] += thisNbLetters[letter]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 if letter not in lettersRate:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 lettersRate[letter] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 rate = int(float(thisNbLetters[letter]) / thisNbLettersTotal * 100)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 if rate not in lettersRate[letter]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 lettersRate[letter][rate] = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 lettersRate[letter][rate] += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 for letter in positionCount:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 positionRate[letter] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 for pos in positionCount[letter]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 positionRate[letter][pos] = positionCount[letter][pos] / float(nbPositions[pos]) * 100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 for pos in nbPositions:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 nbPositionRate[pos] = nbPositions[pos] / float(nbPositions[1]) * 100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 # plot content distributions
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 plotter = RPlotter("%s.png" % (options.outputFileName), options.verbosity, True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 plotter.setFill(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 plotter.setLegend(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 for letter in lettersRate:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 plotter.addLine(lettersRate[letter], letter)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 plotter.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 # plot distribution per position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 plotter = RPlotter("%sPerNt.png" % (options.outputFileName), options.verbosity, True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 plotter.setFill(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 plotter.setLegend(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 plotter.setXLabel("Position on the read")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 plotter.setYLabel("Percentage")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 for letter in positionRate:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 plotter.addLine(positionRate[letter], letter)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 plotter.addLine(nbPositionRate, "#")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 plotter.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if options.csv:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 outHandler = open("%s.csv" % (options.outputFileName), "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 writeCVSfile(outHandler)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 outHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 print "%d sequences" % (nbSequences)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 print "%d letters" % (nbLettersTotal)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 for letter in nbLetters:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 print "%s: %d (%.2f%%)" % (letter, nbLetters[letter], float(nbLetters[letter]) / nbLettersTotal * 100)