annotate commons/core/writer/CsvWriter.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -0500
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 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.writer.TranscriptListWriter import TranscriptListWriter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 class CsvWriter(TranscriptListWriter):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 A class that writes a transcript list into a file with CSV (Excel) format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 @ivar fileName: name of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 @type fileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 @ivar handle: handle to the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 @type handle: file handle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def __init__(self, fileName, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @param fileName: name of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 @type fileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 super(CsvWriter, self).__init__(fileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.header = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.title = "chromosome,start,end,strand,exons,tags\n"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.modified = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 Destructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 (Trick to write 1 tag per column)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 if self.handle != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.modifyCsv()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 super(CsvWriter, self).__del__()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def close(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 if self.handle != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.modifyCsv()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 super(CsvWriter, self).close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 def modifyCsv(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 Clean CSV file so that there is one column per tag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 if self.modified:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 # read all the tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self.handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.handle = open(self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 nbFirstFields = 5
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 tags = set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 if self.verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 print "Modifying CSV file..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 number = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 for number, line in enumerate(self.handle):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if number != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 theseTags = line.strip().split(",")[nbFirstFields:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 for tag in theseTags:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 if tag.find("=") != -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 (key, value) = tag.split("=", 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 if value != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 tags.add(key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 if self.verbosity >= 10:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 print " ...done"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 # re-write the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 tmpFileName = "tmpFile%d.csv" % (random.randint(0, 100000))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 tmpFile = open(tmpFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 self.handle.seek(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 progress = Progress(number + 1, "Re-writting CSV file", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 tmpFile.write(self.title.replace("tags", ",".join(sorted(tags))))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 for line in self.handle:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 tagValues = dict([(key, None) for key in tags])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 tmpFile.write(",".join(line.strip().split(",")[:nbFirstFields]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 for tag in line.strip().split(",")[nbFirstFields:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 if tag.find("=") != -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 key = tag.split("=", 1)[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 tagValues[key] = tag.split("=", 1)[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 tagValues[key] += ";%s" % (tag)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 for key in sorted(tagValues.keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 tmpFile.write(",%s" % (tagValues[key]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 tmpFile.write("\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 tmpFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 # replace former file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 import shutil
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 shutil.move(tmpFile.name, self.fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 self.modified = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 def getFileFormats():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 Get the format of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 return ["csv", "xls", "excel"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 def getExtension():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 Get the usual extension for the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 return "csv"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 def printTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 Export the given transcript with GFF2 format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 @param transcript: transcript to be printed
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 return transcript.printCsv()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153