forked from jaxcs/Seqnature
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjust_individualized_annotations.py
More file actions
executable file
·588 lines (494 loc) · 21.5 KB
/
adjust_individualized_annotations.py
File metadata and controls
executable file
·588 lines (494 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#! /usr/bin/env python
# adjust_DO_annotations.py:
# Author: Al Simons
# Copyright (C) 2012 The Jackson Laboratory
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Given a set of DO animal-specific adjustment (offset) files, such as
# produced by two runs (A and B) of the companion script
# build_DO_from_sanger_vcfs.py, create an animal specific gtf file as follows:
# 1. Chromosomes will designated <name>{L,R}.
# 2. Genes will have the code letters of the strains present in the overall
# range of the gene, appended to the gene name after an underscore.
# 3. Transcripts will have the code letters of the strains contributing to
# the transcripts exons, appended to the transcript name.
# To do this we also read in the animal's strain extents (probabilities) file.
# Since neither genes nor transcripts are explicitly recorded with ranges in
# the gtf file, we walk the gtf file twice. Once to determine the strains
# contributing to each gene/transcript, and once to write the file with
# updated names and positions. We do it in this order because the strain
# extent are recorded in reference coordinates.
import sys
import os
from optparse import OptionParser
import bisect
# En/Dis-able some logging to stdout / stderr:
DEBUG=False
def debug(msg):
global DEBUG
if DEBUG:
print >> sys.stderr, msg
def parseOptions():
usage = 'USAGE: %prog [options] annotation-file DO-identifier'
parser = OptionParser(usage=usage, version="0.1")
parser.add_option('-c', '--chr-column', dest='chrCol',
help='Column with chromosome info. (Required, one-based.)')
parser.add_option('-C', '--comments-file', dest='comments',
help='File to write adjustment comments to. (Default: no comments file.)')
parser.add_option('-d', '--dir', dest='dir', default='.',
help='path to directory containing offset files (default: current directory)')
parser.add_option('-e', '--end-column', dest='endCol',
help='The column containing the position of the end of the feature. (Required, one-based.)')
parser.add_option('-n', '--near', dest='near',
help='Report if a feature position is within near bases of an indel. (Optional, default is no report.)')
parser.add_option('-o', '--output-file', dest='ofn',
help='Output filename (Optional, default: stdout)')
parser.add_option('-p', '--position-columns', dest='posCols',
help='Comma-separated list of other columns with positions to be adjusted. (Optional, one-based.)')
parser.add_option('-s', '--start-column', dest='startCol',
help='The column containing the position of the end of the feature. (Required, one-based.)')
parser.add_option('-t', '--type-column', dest='typeCol',
help='Column with feature type info. (Optional, one-based.)')
parser.add_option('-x', '--extents', dest='extents',
help='File containing the strain extents.')
(opts, args) = parser.parse_args()
errors = ''
if len(args) != 3:
errors += 'You must specify the input annotation file, DO id and A/B designator!'
# Sanity check our options and arguments.
# Mandatory options are reference, indels and snps
if not opts.chrCol:
errors += '\n You must specify a chromosome column.'
if not opts.startCol:
errors += '\n You must specify the start position column.'
if not opts.endCol:
errors += '\n You must specify the end position column.'
if not opts.extents:
errors += '\n You must specify the file with strain extents.'
if errors:
parser.error(errors)
return (opts, args)
def readStrainBounds(fN):
chrs = []
for n in range(1,20):
chrs.append(str(n))
chrs.append('X')
chrStrains = {}
chrKeys = {}
headerSkipped = False
for line in open(fN):
if not headerSkipped:
headerSkipped = True
continue
(chr, offset, strain) = line.split(',')
chr = chr.strip(' \t')
offset = int(offset.strip(' \t'))
strain = strain.strip(' \t\r\n')
if chr not in chrStrains:
chrStrains[chr] = {}
chrKeys[chr] = []
chrStrains[chr][offset] = strain
chrKeys[chr].append(offset)
for chr in chrs:
chrKeys[chr] = sorted(chrKeys[chr])
return chrStrains, chrKeys
def readOffsets(dir, strain):
chrs = []
for n in range(1,20):
chrs.append(str(n))
chrs.append('X')
chrOffs = {}
chrKeys = {}
for chr in chrs:
fn = '%s_offsets_chr%s.txt' % (strain, chr)
fp = os.path.join(dir, fn)
try:
f = open(fp)
f.close()
except IOError:
debug('Could not open' + str(fp) + 'assuming all B6.')
continue
offsets = {}
keys = []
for line in open (fp):
line = line.rstrip()
parts = line.split('\t')
if len(parts) != 2:
print >> sys.stderr, 'ERROR: file', fn, 'has bad line', line
continue
offsets[int(parts[0])] = int(parts[1])
keys.append(int(parts[0]))
chrOffs[chr] = offsets
chrKeys[chr] = sorted(keys)
return chrOffs, chrKeys
def getRangeStrains(start, end, strains, keys):
# Return the codes for all the strains that contribute to this range.
# For exons, this will almost always be a single strain; for genes,
# it may well be two or even more.
startIdx = bisect.bisect(keys, start)
endIdx = bisect.bisect(keys, end)
if startIdx == 0 or endIdx == 0:
# There's been a problem. Should be no entry before the first.
print >> sys.stderr, "ERROR looking up strain info at positions", start, end
sys.exit(1)
retStrains = []
for n in range(startIdx-1, endIdx):
transitionPos = keys[n]
retStrains.append(strains[transitionPos])
return retStrains
def adjustPosition(pos, offsets, keys):
pos = int(pos)
offsetIdx = bisect.bisect(keys, pos)
if offsetIdx == 0:
# The pos is before the first offset entry, so the offset is 0.
eventPos = 0
offset = 0
else:
eventPos = keys[offsetIdx - 1]
offset = offsets[eventPos]
if offsetIdx < 2:
offsetDelta = offset
else:
# Our offsetDelta is the difference between the current
# offset and the previous one. If negative, this is a deletion.
offsetDelta = offset - offsets[keys[offsetIdx - 2]]
# Do some "quality" (exceptional events) checks.
# Is the position in a deletion?
inDeletion = offsetDelta < 0 and pos < eventPos - offsetDelta
# The new pos is pos + offset; the original (reference) pos is 'pos'.
return pos + offset, pos, eventPos, inDeletion
def checkNear(chr, startRef, endRef, refPos, adjPos, eventPos, featureType, near, outcf):
if near and abs(refPos - eventPos) <= near:
if type(adjPos) == type(''):
print >> sys.stderr, 'ERROR Bad adjPos:', adjPos
sys.exit(1)
if outcf:
if featureType:
print >> outcf, featureType,
print >> outcf, '%s\t%d\t%d\t%d\t%d\tPosition near event (%d bases)' % (chr, startRef, endRef, refPos, adjPos, near)
alreadyCommented = set()
def doComment(feature, chr, start, end, ref, adj, format, comment, outcf):
global alreadyCommented
if not outcf:
return
if feature:
key = feature
key = key + chr + str(start) + str(end) + comment
if key in alreadyCommented:
return
alreadyCommented.add(key)
if feature:
print >> outcf, feature,
# Chr \t RefStart \t RefEnd \t RefLoc \t AdjLoc \t Description
print >> outcf, format % (chr, start, end, ref, adj, comment)
female = False
def processLine(line, chrCol, startCol, endCol, posCols, chrOffs, chrKeys, typeCol, near, outf, outcf, geneNames, transcriptNames, proteinNames, lr):
global female
line = line.rstrip()
parts = line.split('\t')
chr = parts[chrCol]
if chr[:3] == 'chr':
chr == chr[3:]
if female and 'Y' in chr:
return
try:
offsets = chrOffs[chr]
except KeyError:
# If a chr is entirely B6 it will not have an offsets file,
# and won't appear in the chrOffs list.
# The correct thing to do is simply pass all the lines for that
# chr through unmodified except for properly munging the chr name,
# and gene and transcript ids.
# The wrinkle (another??) is that NT_, MT and Y also don't have
# an offsets file and should be passed through completely unchanged.
if chr[0] == 'Y' or chr[0] == 'N' or chr[0] == 'M':
print >> outf, line
return
parts[chrCol] = chr + lr
notes = parts[8].split(';')
for n in range(len(notes)):
for id in ["gene_id", "transcript_id"]:
if id in notes[n]:
# The strain identifier for B6 is "B", hard coded in this line...
notes[n] = ' %s "%sB%s"' % (id, notes[n].split(' ')[2][1:-1], lr)
parts[8] = ';'.join(notes)
print >> outf, '\t'.join(parts)
return
offset = None
changeInFeature = False
featureConsumed = False
inDeletion = []
nearHit = []
keys = chrKeys[chr]
# Process the start and end positions, then any extra positions.
# for each perform some checks.
startNewPos, startRefPos, startEventPos, startInDeletion = adjustPosition(parts[startCol], offsets, keys)
endNewPos, endRefPos, endEventPos, endInDeletion = adjustPosition(parts[endCol], offsets, keys)
if typeCol:
featureType = parts[typeCol] + '\t'
else:
featureType = None
if startInDeletion and endInDeletion and startEventPos == endEventPos:
# The entire feature was deleted.
# Set the start and end positions to zero.
startNewPos = endNewPos = 0
# and log it.
doComment(featureType, chr, startRefPos, endRefPos, '', '', '%s\t%d\t%d\t%s\t%s\t%s', 'Feature Deleted', outcf)
else:
# At least part of the feature is in the strain. Check for both the
# start and end. And note that these are NOT mutually exclusive.
# The start could be in one del and the end could be in another.
# (Heaven help us if that really happens in biology...)
if startInDeletion:
# Adjust it to be the first non-deleted base. Add this segment's
# offset to the event pos.
startNewPos = startEventPos + (startNewPos - startRefPos)
# and log it.
comment = 'Start in deletion, adjusted to first non-deleted base'
doComment(featureType, chr, startRefPos, endRefPos, startRefPos, startNewPos,
'%s\t%d\t%d\t%d\t%d\t%s', comment, outcf)
if endInDeletion:
# Adjust it to be the last base before the deletion.
# Have to find out the offset between the strain and the reference just before this
# event.
testRefPos = endEventPos - 1;
testNewPos, testRefPos, testEventPos, testInDeletion = adjustPosition(testRefPos, offsets, keys)
# A quick sanity test. Did we get a different offset?
if (testNewPos - testRefPos) == (endNewPos - endRefPos):
print >> sys.stderr, 'ERROR: We came up with the same offset!', chr, startRefPos, endRefPos
endNewPos = testNewPos
# and log it.
comment = 'End in deletion, adjusted to last base before deletion'
doComment(featureType, chr, startRefPos, endRefPos, endRefPos, endNewPos,
'%s\t%d\t%d\t%d\t%d\t%s', comment, outcf)
if startEventPos != endEventPos:
comment = 'Feature contains indel'
doComment(featureType, chr, startRefPos, endRefPos, startRefPos, startNewPos,
'%s\t%d\t%d\t%d\t%d\t%s', comment, outcf)
if endNewPos < startNewPos:
print >> sys.stderr, 'ERROR: end before start', chr + ':' + str(endNewPos), 'in feature at', str(startNewPos) + '-' + str(endNewPos), '(reference coords:', str(startRefPos) + '-' + str(endRefPos) + ')'
# Check start and end for "nearness"
if near:
checkNear(chr, startRefPos, endRefPos, startRefPos, startNewPos, startEventPos, featureType, near, outcf)
checkNear(chr, startRefPos, endRefPos, endRefPos, endNewPos, endEventPos, featureType, near, outcf)
for col in posCols:
newPos, refPos, eventPos, inDeletion = adjustPosition(parts[col], offsets, keys)
if inDeletion:
comment = 'Position in deletion, adjusted to base after deletion'
doComment(featureType, chr, startRefPos, endRefPos, refPos, newPos,
'%s\t%d\t%d\t%d\t%d\t%s', comment, outcf)
newPos = eventPos + (newPos - refPos)
parts[col] = str(newPos)
if newPos < startNewPos:
print >> sys.stderr, 'ERROR position before start', chr + ':' + str(newPos), 'in feature at', str(startNewPos) + '-' + str(endNewPos), '(reference coords:', str(startRefPos) + '-' + str(endRefPos) + ')'
if newPos > endNewPos:
print >> sys.stderr, 'ERROR position after end', chr + ':' + str(newPos), 'in feature at', str(startNewPos) + '-' + str(endNewPos), '(reference coords:', str(startRefPos) + '-' + str(endRefPos) + ')'
# Is this position within (the user specified) 'near' bases of an
# indel?
if near:
checkNear(chr, startRefPos, endRefPos, refPos, newPos, eventPos, featureType, near, outcf)
# Fix up the "notes" column (assumed to be last). The gene_id is the
# first part, and the transcript_id is the second. Both are updated.
notes = parts[-1].split(';')
if len(notes) < 2:
print >> sys.stderr, 'ERROR: notes are too short!', parts
sys.exit(1)
if notes[0][:8] != ' gene_id':
print >> sys.stderr, 'ERROR: gene_id not first in notes...', notes
sys.exit(1)
if notes[1][:14] != ' transcript_id':
print >> sys.stderr, 'ERROR: transcript_id not second in notes...', notes
sys.exit(1)
g = notes[0].split('"')[1]
t = notes[1].split('"')[1]
g = geneNames[g]
t = transcriptNames[t]
notes[0] = ' gene_id "%s"' % (g)
notes[1] = 'transcript_id "%s"' % (t)
p = notes[-1]
if p[:11] == ' protein_id':
p = p.split('"')[1]
notes[-1] = 'protein_id "%s"' % (proteinNames[p])
parts[-1] = '; '.join(notes)
# Update the file
parts[chrCol] = chr + lr
parts[startCol] = str(startNewPos)
parts[endCol] = str(endNewPos)
print >> outf, '\t'.join(parts)
class NameAnnotator:
def __init__(self, name, chr, lr):
self.chr = chr
self.name = name
self.start = 999999999999 # Only used for genes
self.end = 0 # Only used for genes
self.strains = [lr]
def addStrains(self, strains):
for strain in strains:
if strain not in self.strains:
self.strains.append(strain)
def expandRange(self, start, end):
if start < self.start:
self.start = start
if end > self.end:
self.end = end
def __str__(self):
return self.name + ''.join(sorted(self.strains))
def annotateGenesTranscriptsProteins(anns, chrCol, typeCol, startCol, endCol, chrStrains, chrStrainKeys, lr):
genes = {}
transcripts = {}
proteins = {}
notesCol = -1
for line in open(anns):
line = line.rstrip()
parts = line.split('\t')
# Only exons contribute to the transcripts and define the extent
# of the genes...
if parts[typeCol] != 'exon':
continue
chr = parts[chrCol]
start = int(parts[startCol])
end = int(parts[endCol])
notes = parts[notesCol].split(';')
gene_id = notes[0].split('"')[1]
transcript_id = notes[1].split('"')[1]
protein_id = notes[-1]
if protein_id[:11] == ' protein_id':
protein_id = protein_id.split('"')[1]
else:
protein_id = None
if chr in chrStrains:
strains = getRangeStrains(start, end, chrStrains[chr], chrStrainKeys[chr])
else:
strains = ''
# Annotate the gene names,
if gene_id not in genes:
genes[gene_id] = NameAnnotator(gene_id, chr, lr)
gene = genes[gene_id]
gene.expandRange(start, end)
# Since we want to annorate a gene with all the strains it spans,
# even those not in its exons, we'll add the strains later.
# Annotate transcripts
if transcript_id not in transcripts:
transcripts[transcript_id] = NameAnnotator(transcript_id, chr, lr)
transcript = transcripts[transcript_id]
transcript.addStrains(strains)
# And proteins
if protein_id:
if protein_id not in proteins:
proteins[protein_id] = NameAnnotator(protein_id, chr, lr)
protein = proteins[protein_id]
protein.addStrains(strains)
# We have now processed all the exons in the gtf. We can now
# 1) Update the strains in all the genes, and
# 2) Update the genes and transcripts hashes with the annotated names,
# throwing away the annotators in the process.
geneKeys = genes.keys()
for gene in geneKeys:
g = genes[gene]
c = g.chr
if c in chrStrains:
geneStrains = getRangeStrains(g.start, g.end, chrStrains[c], chrStrainKeys[c])
g.addStrains(geneStrains)
genes[gene] = str(g)
# Clean up the transcripts
transcriptKeys = transcripts.keys()
for transcript in transcriptKeys:
transcripts[transcript] = str(transcripts[transcript])
# and the proteins
proteinKeys = proteins.keys()
for protein in proteinKeys:
proteins[protein] = str(proteins[protein])
return genes, transcripts, proteins
def updateHeader(strain, outf):
print >> outf, '# The coordinates in this fle have been adjusted for DO animal genotype identified as', strain
print >> outf, "# based on Sanger's short Indel vcf files for the DO founder strains."
print >> outf, '# This file is based on:\n#'
def emitCommentsHeader(typeCol, outcf):
if not outcf:
return
header = 'Chr\tRefStart\tRefEnd\tRefLoc\tAdjLoc\tDescription'
if typeCol:
header = 'FeatureType\t' + header
print >> outcf, header
def main():
global female
(opts, args) = parseOptions()
anns = args[0]
lr = args[2]
animalId = args[1] + '_' + lr
if animalId[0] == 'F':
female = True
if opts.near:
near = int(opts.near)
else:
near = None
chrOffs, chrKeys = readOffsets(opts.dir, animalId)
chrStrains, chrStrainKeys = readStrainBounds(opts.extents)
# Capture the interesting columns and make them 0-based. (Input
# as 1-based.)
chrCol = int(opts.chrCol) - 1
typeCol = int(opts.typeCol) - 1
startCol = int(opts.startCol) - 1
endCol = int(opts.endCol) - 1
if chrCol == -1:
print >> sys.stderr, 'Columns are counted from 1, not zero.'
sys.exit(1)
if opts.posCols:
posCols = opts.posCols.split(',')
else:
posCols = []
for n in range(len(posCols)):
posCols[n] = int(posCols[n]) - 1
try:
f = open(anns)
f.close()
except IOError:
print >> sys.stderr, 'Could not open', anns, '\nExiting...'
sys.exit(1)
geneNames, transcriptNames, proteinNames = annotateGenesTranscriptsProteins(anns, chrCol, typeCol, startCol, endCol, chrStrains, chrStrainKeys, lr)
outf = sys.stdout # Main annotations output
outcf = None # Comments about the annotation translation process.
if opts.ofn:
try:
outf = open(opts.ofn, 'w')
except IOError:
print >> sys.stderr, 'Could not open', opts.ofn, 'for output.\nExiting...'
if opts.comments:
try:
#Try to open the comments file.
outcf = open(opts.comments, 'w')
except IOError:
print >> sys.stderr, 'Could not open comments file', opts.comments, 'for output. Continuing without comments.'
outcf = None
headerProcessed = False
headerUpdated = False
if outcf:
emitCommentsHeader(opts.typeCol, outcf)
for line in open(anns):
if not headerProcessed:
if line[0] == '#':
print >> outf, line,
if not headerUpdated:
updateHeader(strain, outf)
headerUpdated = True
continue
else:
headerProcessed = True
processLine(line, chrCol, startCol, endCol, posCols, chrOffs, chrKeys, typeCol, near, outf, outcf, geneNames, transcriptNames, proteinNames, lr)
if outf != sys.stdout:
outf.close()
if outcf:
outcf.close()
# Do it!
main()