Package bamm :: Module bamLink
[hide private]
[frames] | no frames]

Source Code for Module bamm.bamLink

  1  #!/usr/bin/env python 
  2  ############################################################################### 
  3  #                                                                             # 
  4  #    bamLink.py                                                               # 
  5  #                                                                             # 
  6  #    Class for storing information about paired links between contigs         # 
  7  #                                                                             # 
  8  #    Copyright (C) Michael Imelfort                                           # 
  9  #                                                                             # 
 10  ############################################################################### 
 11  #                                                                             # 
 12  #    This library is free software; you can redistribute it and/or            # 
 13  #    modify it under the terms of the GNU Lesser General Public               # 
 14  #    License as published by the Free Software Foundation; either             # 
 15  #    version 3.0 of the License, or (at your option) any later version.       # 
 16  #                                                                             # 
 17  #    This library is distributed in the hope that it will be useful,          # 
 18  #    but WITHOUT ANY WARRANTY; without even the implied warranty of           # 
 19  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        # 
 20  #    Lesser General Public License for more details.                          # 
 21  #                                                                             # 
 22  #    You should have received a copy of the GNU Lesser General Public         # 
 23  #    License along with this library.                                         # 
 24  #                                                                             # 
 25  ############################################################################### 
 26   
 27  __author__ = "Michael Imelfort" 
 28  __copyright__ = "Copyright 2014" 
 29  __credits__ = ["Michael Imelfort"] 
 30  __license__ = "LGPLv3" 
 31  __maintainer__ = "Michael Imelfort" 
 32  __email__ = "mike@mikeimelfort.com" 
 33   
 34  ############################################################################### 
 35   
 36  # system imports 
 37  import ctypes as c 
 38   
 39  # local imports 
 40  from cWrapper import * 
 41   
 42  ############################################################################### 
 43  ############################################################################### 
 44  ############################################################################### 
 45  ############################################################################### 
 46   
 47  # links-associated structures "Python land" 
48 -class BM_linkInfo(object):
49 '''A single link joining two contigs''' 50
51 - def __init__(self, 52 r1, 53 r2, 54 p1, 55 p2, 56 bid = None):
57 '''Default constructor. 58 59 Initializes a BM_LinkInfo instance with the provided set of properties. 60 61 Inputs: 62 r1 - int, == 1 if the read maps to contig 1 in reverse orientation 63 r2 - int, == 1 if the read maps to contig 2 in reverse orientation 64 pos1 - int, leftmost position of read on contig 1 65 pos2 - int, leftmost position of read on contig 2 66 bid - unique identifier for the bam file describing this link 67 68 Outputs: 69 None 70 ''' 71 self.reversed1 = r1 72 self.reversed2 = r2 73 self.pos1 = p1 74 self.pos2 = p2 75 self.bamID = bid
76
77 - def printMore(self, 78 bamFileNames, 79 len1, 80 len2):
81 '''Advanced string function 82 83 used for creating output to links file 84 85 Inputs: 86 bamFileNames - { bamId : string }, storage for long bam file names 87 len1 - int, length of contig 1 88 len1 - int, length of contig 2 89 90 Outputs: 91 String describing the link 92 ''' 93 return "%d\t%d\t%d\t%d\t%d\t%d\t%s" % (len1, 94 self.pos1, 95 self.reversed1, 96 len2, 97 self.pos2, 98 self.reversed2, 99 bamFileNames[self.bamID])
100
101 - def __str__(self):
102 '''override basic string function''' 103 return "%d\t%d\t%d\t%d\t%s" % (self.pos1, 104 self.reversed1, 105 self.pos2, 106 self.reversed2, 107 self.bamID)
108 109 110 111 ############################################################################### 112 ############################################################################### 113 ############################################################################### 114 ############################################################################### 115
116 -class BM_linkPair(object):
117 '''Container class for storing all links joining two contigs''' 118
119 - def __init__(self, 120 cid1, 121 cid2):
122 '''Default constructor. 123 124 Initializes a BM_LinkPair instance with the provided set of properties. 125 126 Inputs: 127 cid1 - unique identifier for contig 1 128 cid2 - unique identifier for contig 2 129 130 Outputs: 131 None 132 ''' 133 self.cid1 = cid1 134 self.cid2 = cid2 135 self.numLinks = 0 136 self.links = []
137 163
164 - def makeKey(self):
165 '''Return a unique key for this link pair''' 166 return "%d,%d" % (self.cid1, self.cid2)
167
168 - def printMore(self, 169 contigNames, 170 contigLengths, 171 bamFileNames):
172 '''Advanced string function 173 174 used for creating output to links file 175 calls link.printmore() 176 177 Inputs: 178 contigNames - { cid : string }, storage for long contig names 179 contigLengths - { cid : int }, storage for contig lengths 180 bamFileNames - { bamId : string }, storage for long bam file names 181 182 Outputs: 183 Multi-line string descrtibing all the links between the two contigs 184 ''' 185 186 """Print function used to export a tabular format""" 187 return "\n".join(["%s\t%s\t%s"%(contigNames[self.cid1], 188 contigNames[self.cid2], 189 link.printMore(bamFileNames, 190 contigLengths[self.cid1], 191 contigLengths[self.cid2] 192 ) 193 ) 194 for link in self.links])
195
196 - def __str__(self):
197 '''override basic string function''' 198 return "\n".join(["%s\t%s\t%s" % (self.cid1, 199 self.cid2, 200 link) for link in self.links] 201 )
202 203 ############################################################################### 204 ############################################################################### 205 ############################################################################### 206 ############################################################################### 207