Mercurial > repos > xuebing > sharplabtool
comparison tools/regVariation/maf_cpg_filter.py @ 0:9071e359b9a3
Uploaded
author | xuebing |
---|---|
date | Fri, 09 Mar 2012 19:37:19 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9071e359b9a3 |
---|---|
1 #!/usr/bin/env python | |
2 #Guruprasad Ananda | |
3 #Adapted from bx/scripts/maf_mask_cpg.py | |
4 """ | |
5 Mask out potential CpG sites from a maf. Restricted or inclusive definition | |
6 of CpG sites can be used. The total fraction masked is printed to stderr. | |
7 | |
8 usage: %prog < input > output restricted | |
9 -m, --mask=N: Character to use as mask ('?' is default) | |
10 """ | |
11 | |
12 from galaxy import eggs | |
13 import pkg_resources | |
14 pkg_resources.require( "bx-python" ) | |
15 try: | |
16 pkg_resources.require( "numpy" ) | |
17 except: | |
18 pass | |
19 import bx.align | |
20 import bx.align.maf | |
21 from bx.cookbook import doc_optparse | |
22 import sys | |
23 import bx.align.sitemask.cpg | |
24 | |
25 assert sys.version_info[:2] >= ( 2, 4 ) | |
26 | |
27 def main(): | |
28 options, args = doc_optparse.parse( __doc__ ) | |
29 try: | |
30 inp_file, out_file, sitetype, definition = args | |
31 if options.mask: | |
32 mask = int(options.mask) | |
33 else: | |
34 mask = 0 | |
35 except: | |
36 print >> sys.stderr, "Tool initialization error." | |
37 sys.exit() | |
38 | |
39 reader = bx.align.maf.Reader( open(inp_file, 'r') ) | |
40 writer = bx.align.maf.Writer( open(out_file,'w') ) | |
41 | |
42 mask_chr_dict = {0:'#', 1:'$', 2:'^', 3:'*', 4:'?', 5:'N'} | |
43 mask = mask_chr_dict[mask] | |
44 | |
45 if sitetype == "CpG": | |
46 if int(definition) == 1: | |
47 cpgfilter = bx.align.sitemask.cpg.Restricted( mask=mask ) | |
48 defn = "CpG-Restricted" | |
49 else: | |
50 cpgfilter = bx.align.sitemask.cpg.Inclusive( mask=mask ) | |
51 defn = "CpG-Inclusive" | |
52 else: | |
53 cpgfilter = bx.align.sitemask.cpg.nonCpG( mask=mask ) | |
54 defn = "non-CpG" | |
55 cpgfilter.run( reader, writer.write ) | |
56 | |
57 print "%2.2f percent bases masked; Mask character = %s, Definition = %s" %(float(cpgfilter.masked)/float(cpgfilter.total) * 100, mask, defn) | |
58 | |
59 if __name__ == "__main__": | |
60 main() |