annotate annotate.py @ 0:b001b50f2009 draft default tip

Imported from capsule None
author devteam
date Mon, 27 Jan 2014 09:28:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/python
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
2
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
3 import os.path
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
4 import sys
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
5 import optparse
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
6
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
7 import vcfClass
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
8 from vcfClass import *
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
9
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
10 import tools
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
11 from tools import *
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
12
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
13 if __name__ == "__main__":
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
14 main()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
15
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
16 # Check that the reference and alternate in the dbsnp vcf file match those
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
17 # from the input vcf file.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
18 def checkRefAlt(vcfRef, vcfAlt, dbsnpRef, dbsnpAlt, ref, position, annotation):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
19 text = "WARNING: ref and alt alleles differ between vcf and " + annotation + " " + ref + ":" + str(position) + " vcf: " + \
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
20 vcfRef + "/" + vcfAlt + ", dbsnp: " + dbsnpRef + "/" + dbsnpAlt
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
21
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
22 allelesAgree = True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
23 if vcfRef.lower() != dbsnpRef.lower():
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
24 if vcfRef.lower() != dbsnpAlt.lower():
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
25 #print >> sys.stderr, text
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
26 allelesAgree = False
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
27 else:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
28 if vcfAlt.lower() != dbsnpAlt.lower():
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
29 #print >> sys.stderr, text
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
30 allelesAgree = False
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
31
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
32 return allelesAgree
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
33
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
34 # Intersect two vcf files. It is assumed that the two files are
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
35 # sorted by genomic coordinates and the reference sequences are
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
36 # in the same order.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
37 def annotateVcf(v, d, outputFile, annotation):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
38 success1 = v.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
39 success2 = d.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
40 currentReferenceSequence = v.referenceSequence
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
41
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
42 # Finish when the end of the first file has been reached.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
43 while success1:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
44
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
45 # If the end of the dbsnp vcf file is reached, write out the
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
46 # remaining records from the vcf file.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
47 if not success2:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
48 outputFile.write(v.record)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
49 success1 = v.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
50
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
51 if v.referenceSequence == d.referenceSequence and v.referenceSequence == currentReferenceSequence:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
52 if v.position == d.position:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
53 allelesAgree = checkRefAlt(v.ref, v.alt, d.ref, d.alt, v.referenceSequence, v.position, annotation)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
54 if annotation == "dbsnp": v.rsid = d.getDbsnpInfo()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
55 elif annotation == "hapmap":
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
56 if allelesAgree: v.info += ";HM3"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
57 else: v.info += ";HM3A"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
58 record = v.buildRecord(False)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
59 outputFile.write(record)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
60
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
61 success1 = v.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
62 success2 = d.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
63 elif d.position > v.position: success1 = v.parseVcf(d.referenceSequence, d.position, True, outputFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
64 elif v.position > d.position: success2 = d.parseVcf(v.referenceSequence, v.position, False, None)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
65 else:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
66 if v.referenceSequence == currentReferenceSequence: success1 = v.parseVcf(d.referenceSequence, d.position, True, outputFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
67 elif d.referenceSequence == currentReferenceSequence: success2 = d.parseVcf(v.referenceSequence, v.position, False, None)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
68
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
69 # If the last record for a reference sequence is the same for both vcf
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
70 # files, they will both have referenceSequences different from the
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
71 # current reference sequence. Change the reference sequence to reflect
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
72 # this and proceed.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
73 else:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
74 if v.referenceSequence != d.referenceSequence:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
75 print >> sys.stderr, "ERROR: Reference sequences for both files are unexpectedly different."
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
76 print >> sys.stderr, "Check that both files contain records for the following reference sequences:"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
77 print >> sys.stderr, "\t", v.referenceSequence, " and ", d.referenceSequence
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
78 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
79 currentReferenceSequence = v.referenceSequence
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
80
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
81 def main():
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
82
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
83 # Parse the command line options
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
84 usage = "Usage: vcfPytools.py annotate [options]"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
85 parser = optparse.OptionParser(usage = usage)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
86 parser.add_option("-i", "--in",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
87 action="store", type="string",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
88 dest="vcfFile", help="input vcf files")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
89 parser.add_option("-d", "--dbsnp",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
90 action="store", type="string",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
91 dest="dbsnpFile", help="input dbsnp vcf file")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
92 parser.add_option("-m", "--hapmap",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
93 action="store", type="string",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
94 dest="hapmapFile", help="input hapmap vcf file")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
95 parser.add_option("-o", "--out",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
96 action="store", type="string",
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
97 dest="output", help="output vcf file")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
98
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
99 (options, args) = parser.parse_args()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
100
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
101 # Check that a single vcf file is given.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
102 if options.vcfFile == None:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
103 parser.print_help()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
104 print >> sys.stderr, "\nInput vcf file (--in, -i) is required for dbsnp annotation."
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
105 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
106
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
107 # Check that either a hapmap or a dbsnp vcf file is included.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
108 if options.dbsnpFile == None and options.hapmapFile == None:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
109 parser.print_help()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
110 print >> sys.stderr, "\ndbSNP or hapmap vcf file is required (--dbsnp, -d, --hapmap, -h)."
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
111 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
112 elif options.dbsnpFile != None and options.hapmapFile != None:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
113 parser.print_help()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
114 print >> sys.stderr, "\ndbSNP or hapmap vcf file is required, not both (--dbsnp, -d, --hapmap, -h)."
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
115 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
116
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
117 # Set the output file to stdout if no output file was specified.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
118 outputFile, writeOut = setOutput(options.output) # tools.py
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
119
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
120 v = vcf() # Define vcf object.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
121 d = vcf() # Define dbsnp/hapmap vcf object.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
122 if options.dbsnpFile:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
123 d.dbsnpVcf = True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
124 annotationFile = options.dbsnpFile
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
125 annotation = "dbsnp"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
126 elif options.hapmapFile:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
127 d.hapmapVcf = True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
128 annotationFile = options.hapmapFile
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
129 annotation = "hapmap"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
130
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
131 # Open the vcf files.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
132 v.openVcf(options.vcfFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
133 d.openVcf(annotationFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
134
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
135 # Read in the header information.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
136 v.parseHeader(options.vcfFile, writeOut)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
137 d.parseHeader(annotationFile, writeOut)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
138
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
139 # Add an extra line to the vcf header to indicate the file used for
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
140 # performing dbsnp annotation.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
141 taskDescriptor = "##vcfPytools=annotated vcf file with "
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
142 if options.dbsnpFile: taskDescriptor += "dbSNP file " + options.dbsnpFile
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
143 elif options.hapmapFile:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
144 taskDescriptor += "hapmap file " + options.hapmapFile
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
145 v.infoHeaderString["HM3"] = "##INFO=<ID=HM3,Number=0,Type=Flag,Description=\"Hapmap3.2 membership determined from file " + \
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
146 options.hapmapFile + "\">"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
147 v.infoHeaderString["HM3A"] = "##INFO=<ID=HM3A,Number=0,Type=Flag,Description=\"Hapmap3.2 membership (with different alleles)" + \
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
148 ", determined from file " + options.hapmapFile + "\">"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
149 writeHeader(outputFile, v, False, taskDescriptor) # tools.py
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
150
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
151 # Annotate the vcf file.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
152 annotateVcf(v, d, outputFile, annotation)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
153
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
154 # Check that the input files had the same list of reference sequences.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
155 # If not, it is possible that there were some problems.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
156 checkReferenceSequenceLists(v.referenceSequenceList, d.referenceSequenceList) # tools.py
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
157
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
158 # Close the vcf files.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
159 v.closeVcf(options.vcfFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
160 d.closeVcf(annotationFile)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
161
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
162 # End the program.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
163 return 0