| 1 | """ |
|---|
| 2 | Support for masking potential CpG sites in *pairwise* alignments. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from _cpg import * |
|---|
| 6 | from bx.align.sitemask import Masker |
|---|
| 7 | from bx.filter import * |
|---|
| 8 | import string |
|---|
| 9 | |
|---|
| 10 | # Restricted. Only mask out sites that are defitely CpG |
|---|
| 11 | class Restricted( Masker ): |
|---|
| 12 | def __init__( self, mask = '?' ): |
|---|
| 13 | self.mask = mask |
|---|
| 14 | self.masked = 0 |
|---|
| 15 | self.total = 0 |
|---|
| 16 | |
|---|
| 17 | def __call__( self, block ): |
|---|
| 18 | if not block: return block |
|---|
| 19 | if len(block.components) < 2: |
|---|
| 20 | return |
|---|
| 21 | cpglist = list_cpg_restricted( \ |
|---|
| 22 | string.upper(block.components[0].text), \ |
|---|
| 23 | string.upper(block.components[1].text) ) |
|---|
| 24 | |
|---|
| 25 | # now we have a fast list of CpG columns, iterate/mask |
|---|
| 26 | self.masked += len(cpglist) |
|---|
| 27 | self.total += len(block.components[0].text) |
|---|
| 28 | for component in block.components: |
|---|
| 29 | component.text = mask_columns( cpglist, component.text, self.mask ) |
|---|
| 30 | |
|---|
| 31 | return block |
|---|
| 32 | |
|---|
| 33 | # Inclusive. Mask out all sites that are not non-CpG sites. |
|---|
| 34 | class Inclusive( Masker ): |
|---|
| 35 | def __init__( self, mask = '?' ): |
|---|
| 36 | self.mask = mask |
|---|
| 37 | self.masked = 0 |
|---|
| 38 | self.total = 0 |
|---|
| 39 | |
|---|
| 40 | def __call__( self, block ): |
|---|
| 41 | if not block: return block |
|---|
| 42 | if len(block.components) < 2: |
|---|
| 43 | return |
|---|
| 44 | cpglist = list_cpg( \ |
|---|
| 45 | string.upper(block.components[0].text), \ |
|---|
| 46 | string.upper(block.components[1].text) ) |
|---|
| 47 | |
|---|
| 48 | self.masked += len( cpglist ) |
|---|
| 49 | self.total += len( block.components[0].text ) |
|---|
| 50 | for component in block.components: |
|---|
| 51 | component.text = mask_columns( cpglist, component.text, self.mask) |
|---|
| 52 | |
|---|
| 53 | return block |
|---|
| 54 | |
|---|
| 55 | #Mak nonCpG sites |
|---|
| 56 | class nonCpG( Masker ): |
|---|
| 57 | def __init__( self, mask = '?' ): |
|---|
| 58 | self.mask = mask |
|---|
| 59 | self.masked = 0 |
|---|
| 60 | self.total = 0 |
|---|
| 61 | |
|---|
| 62 | def __call__( self, block ): |
|---|
| 63 | if not block: return block |
|---|
| 64 | if len(block.components) < 2: |
|---|
| 65 | return |
|---|
| 66 | noncpglist = list_non_cpg( \ |
|---|
| 67 | string.upper(block.components[0].text), \ |
|---|
| 68 | string.upper(block.components[1].text) ) |
|---|
| 69 | |
|---|
| 70 | # now we have a fast list of non-CpG columns, iterate/mask |
|---|
| 71 | self.masked += len(noncpglist) |
|---|
| 72 | self.total += len(block.components[0].text) |
|---|
| 73 | for component in block.components: |
|---|
| 74 | component.text = mask_columns( noncpglist, component.text, self.mask ) |
|---|
| 75 | |
|---|
| 76 | return block |
|---|
| 77 | |
|---|
| 78 | def mask_columns( masklist, text, mask ): |
|---|
| 79 | templist = list() |
|---|
| 80 | for position in masklist: |
|---|
| 81 | if text[position] != "-": |
|---|
| 82 | templist.append(position) |
|---|
| 83 | templist.append(len(text)) # Add the end of the text |
|---|
| 84 | #cut string |
|---|
| 85 | newtext = list() |
|---|
| 86 | c = 0 |
|---|
| 87 | for position in templist: |
|---|
| 88 | newtext.append(text[c:position]) |
|---|
| 89 | c = position + 1 # Gaps have len = 1 |
|---|
| 90 | joinedtext = mask.join(newtext) |
|---|
| 91 | return joinedtext |
|---|