comparison TEisotools-1.0/commons/core/coord/Map.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
comparison
equal deleted inserted replaced
5:4093a2fb58be 6:20ec0d14798e
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 __slots__ = ("name")
40
41 ## Constructor
42 #
43 # @param name the name of the region
44 # @param seqname the name of the sequence
45 # @param start the start coordinate
46 # @param end the end coordinate
47 #
48 def __init__(self, name="", seqname="", start=-1, end=-1):
49 self.name = name
50 Range.__init__( self, seqname, start, end )
51
52 ## Equal operator
53 #
54 # @param o a Map instance
55 #
56 def __eq__(self, o):
57 if type(o) is type(self):
58 if self.name == o.name:
59 return Range.__eq__(self, o)
60 return False
61
62 ## Not equal operator
63 #
64 def __ne__(self, o):
65 return not self.__eq__(o)
66
67 ## Return name
68 #
69 def getName( self ):
70 return self.name
71
72 ## Set attributes from tuple
73 #
74 # @param tuple: a tuple with (name,seqname,start,end)
75 #
76 def setFromTuple(self, tuple):
77 self.name = tuple[0]
78 Range.setFromTuple(self, tuple[1:])
79
80 ## Set attributes from string
81 #
82 # @param string a string formatted like name<sep>seqname<sep>start<sep>end
83 # @param sep field separator
84 #
85 def setFromString(self, string, sep="\t"):
86 string.strip()
87 self.setFromTuple(tuple(string.split(sep)))
88
89 ## Reset
90 #
91 def reset(self):
92 self.setFromTuple(("", "", -1, -1))
93
94 ## Read attributes from a Map file
95 #
96 # @param fileHandler: file handler of the file being read
97 # @return: 1 on success, 0 at the end of the file
98 #
99 def read(self, fileHandler):
100 self.reset()
101 line = fileHandler.readline()
102 if line == "":
103 return 0
104 tokens = line.split("\t")
105 if len(tokens) < 4:
106 return 0
107 self.setFromTuple(tuple(tokens))
108 return 1
109
110 ## Return the attributes as a formatted string
111 #
112 def toString(self):
113 string = "%s" % (self.name)
114 string += "\t%s" % (Range.toString(self))
115 return string
116
117 ## Write attributes into a Map file
118 #
119 # @param fileHandler: file handler of the file being filled
120 #
121 def write(self, fileHandler):
122 fileHandler.write("%s\n" % (self.toString()))
123
124 ## Save attributes into a Map file
125 #
126 # @param file: name of the file being filled
127 #
128 def save(self, file):
129 fileHandler = open( file, "a" )
130 self.write( fileHandler )
131 fileHandler.close()
132
133 ## Return a Range instance with the attributes
134 #
135 def getRange(self):
136 return Range( self.seqname, self.start, self.end)
137
138 ## Remove in the instance the region overlapping with another Map instance
139 #
140 # @param o a Map instance
141 #
142 def diff(self, o):
143 iRange = Range.diff(self, o.getRange())
144 new = Map()
145 if not iRange.isEmpty():
146 new.name = self.name
147 new.seqname = self.seqname
148 new.start = iRange.start
149 new.end = iRange.end
150 return new
151
152 ## Write attributes in a Path file, the name being the subject and the rest the Range query
153 #
154 # @param fileHandler: file handler of a Path file
155 #
156 def writeAsQueryOfPath(self, fileHandler):
157 string = "0"
158 string += "\t%s" % ( self.seqname )
159 string += "\t%i" % ( self.getMin() )
160 string += "\t%i" % ( self.getMax() )
161 string += "\t%s" % ( self.name )
162 string += "\t0"
163 string += "\t0"
164 string += "\t0.0"
165 string += "\t0"
166 string += "\t0"
167 fileHandler.write( "%s\n" % ( string ) )
168