annotate genome_diversity.py @ 36:51cd0307fb70

Phylip's extra ouputs are now stored in the job working directory
author Richard Burhans <burhans@bx.psu.edu>
date Wed, 20 Nov 2013 16:32:01 -0500
parents 2c498d40ecde
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
1 #!/usr/bin/env python
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
2
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
3 import sys
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
4 import cdblib
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
5
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
6 def _openfile( filename=None, mode='r' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
7 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
8 fh = open( filename, mode )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
9 except IOError, err:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
10 raise RuntimeError( "can't open file: %s\n" % str( err ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
11 return fh
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
12
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
13 def get_filename_from_loc( species=None, filename=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
14 fh = _openfile( filename )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
15 for line in fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
16 if line and not line.startswith( '#' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
17 line = line.rstrip( '\r\n' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
18 if line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
19 elems = line.split( '\t' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
20 if len( elems ) >= 2 and elems[0] == species:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
21 return elems[1]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
22
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
23 raise RuntimeError( "can't find '%s' in location file: %s\n" % ( species, filename ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
24
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26 class SnpFile( object ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 def __init__( self, filename=None, seq_col=1, pos_col=2, ref_seq_col=7, ref_pos_col=8 ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 self.filename = filename
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 self.fh = _openfile( filename )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 self.seq_col = seq_col
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 self.pos_col = pos_col
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 self.ref_seq_col = ref_seq_col
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
33 self.ref_pos_col = ref_pos_col
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 self.elems = None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 self.line = None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 self.comments = []
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 def next( self ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 while self.fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41 self.line = self.fh.next()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 except StopIteration:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43 self.line = None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
44 self.elems = None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 return None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 if self.line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 self.line = self.line.rstrip( '\r\n' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48 if self.line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49 if self.line.startswith( '#' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50 self.comments.append( self.line )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 self.elems = self.line.split( '\t' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 return 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 def get_seq_pos( self ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 if self.elems:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 return self.elems[ self.seq_col - 1 ], self.elems[ self.pos_col - 1 ]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 return None, None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 def get_ref_seq_pos( self ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 if self.elems:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 return self.elems[ self.ref_seq_seq - 1 ], self.elems[ self.ref_pos_col - 1 ]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 return None, None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 class IndexedFile( object ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 def __init__( self, data_file=None, index_file=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71 self.data_file = data_file
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 self.index_file = index_file
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 self.data_fh = _openfile( data_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74 self.index_fh = _openfile( index_file )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 self._reader = cdblib.Reader( self.index_fh.read(), hash )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 def get_indexed_line( self, key=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78 line = None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79 if key in self._reader:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 offset = self._reader.getint( key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 self.data_fh.seek( offset )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 line = self.data_fh.next()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84 except StopIteration:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85 raise RuntimeError( 'index file out of sync for %s' % key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86 return line
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 class PrimersFile( IndexedFile ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89 def get_primer_header( self, sequence=None, position=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
90 key = "%s %s" % ( str( sequence ), str( position ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
91 header = self.get_indexed_line( key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
92 if header:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
93 if header.startswith( '>' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
94 elems = header.split()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
95 if len( elems ) < 3:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
96 raise RuntimeError( 'short primers header for %s' % key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
97 if sequence != elems[1] or str( position ) != elems[2]:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
98 raise RuntimeError( 'primers index for %s finds %s %s' % ( key, elems[1], elems[2] ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
99 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
100 raise RuntimeError( 'primers index out of sync for %s' % key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
101 return header
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
102
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
103 def get_entry( self, sequence=None, position=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
104 entry = self.get_primer_header( sequence, position )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
105 if entry:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
106 while self.data_fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
107 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
108 line = self.data_fh.next()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
109 except StopIteration:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
110 break
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
111 if line.startswith( '>' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
112 break
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
113 entry += line
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
114 return entry
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
115
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
116 def get_enzymes( self, sequence=None, position=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
117 entry = self.get_primer_header( sequence, position )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
118 enzyme_list = []
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
119 if entry:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
120 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
121 line = self.data_fh.next()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
122 except StopIteration:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
123 raise RuntimeError( 'primers entry for %s %s is truncated' % ( str( sequence ), str( position ) ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
124 if line.startswith( '>' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
125 raise RuntimeError( 'primers entry for %s %s is truncated' % ( str( sequence ), str( position ) ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
126 line.rstrip( '\r\n' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
127 if line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
128 enzymes = line.split( ',' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
129 for enzyme in enzymes:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
130 enzyme = enzyme.strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
131 if enzyme:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
132 enzyme_list.append( enzyme )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
133 return enzyme_list
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
134
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
135 class SnpcallsFile( IndexedFile ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
136 def get_snp_seq( self, sequence=None, position=None ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
137 key = "%s %s" % ( str( sequence ), str( position ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
138 line = self.get_indexed_line( key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
139 if line:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
140 elems = line.split( '\t' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
141 if len (elems) < 3:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
142 raise RuntimeError( 'short snpcalls line for %s' % key )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
143 if sequence != elems[0] or str( position ) != elems[1]:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
144 raise RuntimeError( 'snpcalls index for %s finds %s %s' % ( key, elems[0], elems[1] ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
145 return elems[2]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
146 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
147 return None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
148
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
149 def get_flanking_dna( self, sequence=None, position=None, format='fasta' ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
150 if format != 'fasta' and format != 'primer3':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
151 raise RuntimeError( 'invalid format for flanking dna: %s' % str( format ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
152 seq = self.get_snp_seq( sequence, position )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
153 if seq:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
154 p = seq.find('[')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
155 if p == -1:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
156 raise RuntimeError( 'snpcalls entry for %s %s missing left bracket: %s' % ( str( sequence ), str( position ), seq ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
157 q = seq.find(']', p + 1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
158 if q == -1:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
159 raise RuntimeError( 'snpcalls entry for %s %s missing right bracket: %s' % ( str( sequence ), str( position ), seq ) )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
160 q += 1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
161
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
162 if format == 'fasta':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
163 flanking_seq = '> '
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
164 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
165 flanking_seq = 'SEQUENCE_ID='
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
166
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
167 flanking_seq += "%s %s %s %s\n" % ( str( sequence ), str( position ), seq[p+1], seq[p+3] )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
168
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
169 if format == 'primer3':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
170 flanking_seq += 'SEQUENCE_TEMPLATE='
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
171
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
172 flanking_seq += "%sn%s\n" % ( seq[0:p], seq[q:] )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
173
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
174 if format == 'primer3':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
175 flanking_seq += "SEQUENCE_TARGET=%d,11\n=\n" % ( p - 5 )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
176
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
177 return flanking_seq
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
178 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
179 return None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
180
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
181
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
182
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
183 class LocationFile( object ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
184 def __init__(self, filename):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
185 self.build_map(filename)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
186
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
187 def build_map(self, filename):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
188 self.map = {}
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
189 self.open_file(filename)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
190 for line in self.read_lines():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
191 elems = line.split('\t', 1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
192 if len(elems) == 2:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
193 self.map[ elems[0].strip() ] = elems[1].strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
194 self.close_file()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
195
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
196 def read_lines(self):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
197 for line in self.fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
198 if not line.startswith('#'):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
199 line = line.rstrip('\r\n')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
200 yield line
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
201
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
202 def open_file(self, filename):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
203 self.filename = filename
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
204 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
205 self.fh = open(filename, 'r')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
206 except IOError, err:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
207 print >> sys.stderr, "Error opening location file '%s': %s" % (filename, str(err))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
208 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
209
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
210 def close_file(self):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
211 self.fh.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
212
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
213 def loc_file( self, key ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
214 if key in self.map:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
215 return self.map[key]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
216 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
217 print >> sys.stderr, "'%s' does not appear in location file '%s'" % (key, self.filename)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
218 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
219
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
220 class ChrLens( object ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
221 def __init__( self, chrlen_filename ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
222 self.chrlen_filename = chrlen_filename
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
223 self.build_map()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
224
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
225 def build_map(self):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
226 self.map = {}
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
227 self.open_file(self.chrlen_filename)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
228 for line in self.read_lines():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
229 elems = line.split('\t', 1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
230 if len(elems) == 2:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
231 chrom = elems[0].strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
232 chrom_len_text = elems[1].strip()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
233 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
234 chrom_len = int( chrom_len_text )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
235 except ValueError:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
236 print >> sys.stderr, "Bad length '%s' for chromosome '%s' in '%s'" % (chrom_len_text, chrom, self.chrlen_filename)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
237 self.map[ chrom ] = chrom_len
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
238 self.close_file()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
239
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
240 def read_lines(self):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
241 for line in self.fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
242 if not line.startswith('#'):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
243 line = line.rstrip('\r\n')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
244 yield line
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
245
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
246 def open_file(self, filename):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
247 self.filename = filename
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
248 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
249 self.fh = open(filename, 'r')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
250 except IOError, err:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
251 print >> sys.stderr, "Error opening chromosome length file '%s': %s" % (filename, str(err))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
252 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
253
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
254 def close_file(self):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
255 self.fh.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
256
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
257 def length( self, key ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
258 if key in self.map:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
259 return self.map[key]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
260 else:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
261 return None
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
262
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
263 def __iter__( self ):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
264 for chrom in self.map:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
265 yield chrom
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
266