comparison smart_toolShed/commons/core/coord/Map.py @ 0:e0f8dcca02ed

Uploaded S-MART tool. A toolbox manages RNA-Seq and ChIP-Seq data.
author yufei-luo
date Thu, 17 Jan 2013 10:52:14 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e0f8dcca02ed
1 # Copyright INRA (Institut National de la Recherche Agronomique)
2 # http://www.inra.fr
3 # http://urgi.versailles.inra.fr
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30
31
32 from commons.core.coord.Range import Range
33
34
35 ## Record a named region on a given sequence
36 #
37 class Map( Range ):
38
39 ## Constructor
40 #
41 # @param name the name of the region
42 # @param seqname the name of the sequence
43 # @param start the start coordinate
44 # @param end the end coordinate
45 #
46 def __init__(self, name="", seqname="", start=-1, end=-1):
47 self.name = name
48 Range.__init__( self, seqname, start, end )
49
50 ## Equal operator
51 #
52 # @param o a Map instance
53 #
54 def __eq__(self, o):
55 if self.name == o.name:
56 return Range.__eq__(self, o)
57 return False
58
59 ## Return name
60 #
61 def getName( self ):
62 return self.name
63
64 ## Set attributes from tuple
65 #
66 # @param tuple: a tuple with (name,seqname,start,end)
67 #
68 def setFromTuple(self, tuple):
69 self.name = tuple[0]
70 Range.setFromTuple(self, tuple[1:])
71
72 ## Set attributes from string
73 #
74 # @param string a string formatted like name<sep>seqname<sep>start<sep>end
75 # @param sep field separator
76 #
77 def setFromString(self, string, sep="\t"):
78 if string[-1] == "\n":
79 string = string[:-1]
80 self.setFromTuple( string.split(sep) )
81
82 ## Reset
83 #
84 def reset(self):
85 self.setFromTuple( [ "", "", -1, -1 ] )
86
87 ## Read attributes from a Map file
88 #
89 # @param fileHandler: file handler of the file being read
90 # @return: 1 on success, 0 at the end of the file
91 #
92 def read(self, fileHandler):
93 self.reset()
94 line = fileHandler.readline()
95 if line == "":
96 return 0
97 tokens = line.split("\t")
98 if len(tokens) < len(self.__dict__.keys()):
99 return 0
100 self.setFromTuple(tokens)
101 return 1
102
103 ## Return the attributes as a formatted string
104 #
105 def toString(self):
106 string = "%s" % (self.name)
107 string += "\t%s" % (Range.toString(self))
108 return string
109
110 ## Write attributes into a Map file
111 #
112 # @param fileHandler: file handler of the file being filled
113 #
114 def write(self, fileHandler):
115 fileHandler.write("%s\n" % (self.toString()))
116
117 ## Save attributes into a Map file
118 #
119 # @param file: name of the file being filled
120 #
121 def save(self, file):
122 fileHandler = open( file, "a" )
123 self.write( fileHandler )
124 fileHandler.close()
125
126 ## Return a Range instance with the attributes
127 #
128 def getRange(self):
129 return Range( self.seqname, self.start, self.end)
130
131 ## Remove in the instance the region overlapping with another Map instance
132 #
133 # @param o a Map instance
134 #
135 def diff(self, o):
136 iRange = Range.diff(self, o.getRange())
137 new = Map()
138 if not iRange.isEmpty():
139 new.name = self.name
140 new.seqname = self.seqname
141 new.start = iRange.start
142 new.end = iRange.end
143 return new
144
145 ## Write attributes in a Path file, the name being the subject and the rest the Range query
146 #
147 # @param fileHandler: file handler of a Path file
148 #
149 def writeAsQueryOfPath(self, fileHandler):
150 string = "0"
151 string += "\t%s" % ( self.seqname )
152 string += "\t%i" % ( self.getMin() )
153 string += "\t%i" % ( self.getMax() )
154 string += "\t%s" % ( self.name )
155 string += "\t0"
156 string += "\t0"
157 string += "\t0.0"
158 string += "\t0"
159 string += "\t0"
160 fileHandler.write( "%s\n" % ( string ) )
161