1 """Sequencing plate well convertor 384 <--> 96 by yong27, 2005-05-12
   2 """
   3 import unittest
   4 
   5 class WellConvertor:
   6     def getPlate96(self, x,y):
   7         if x%2 and y%2:
   8             return 1
   9         elif x%2 and not y%2:
  10             return 2
  11         elif not x%2 and y%2:
  12             return 3
  13         else:
  14             return 4
  15 
  16     def getWell96(self, well384):
  17         downScale = lambda n: n%2 and n/2+1 or n/2
  18         row=ord(well384[0])-64
  19         col=int(well384[1:])
  20         return (chr(downScale(row)+64)+str(downScale(col)).zfill(2), 
  21                 self.getPlate96(row,col))
  22 
  23     def getWell384(self, well96, plate96):
  24         row=2*(ord(well96[0])-64)
  25         col=2*int(well96[1:])
  26         if plate96 in (1, 2):
  27             row-=1
  28         if plate96 in (1, 3):
  29             col-=1
  30         return chr(row+64)+str(col).zfill(2)
  31 
  32 class DrJangsWellConvertor(WellConvertor):
  33     def convertTo96(self, aStr):
  34         words = aStr.split('_')
  35         plate = int(words[2])
  36         well384 = words[4].replace('.abi','')
  37         well96, plate96 = self.getWell96(well384)
  38         new_plate = plate96 + (plate-1)
  39         return '_'.join([words[0], words[1], str(new_plate).zfill(4), 
  40                 well96+'.abi'])
  41 
  42 
  43 class TestWellConvertor(unittest.TestCase):
  44     def setUp(self):
  45         self.wc = WellConvertor()
  46     def testGetWell96(self):
  47         self.assertEquals(('A01',1),self.wc.getWell96('A01'))
  48         self.assertEquals(('C05',2),self.wc.getWell96('E10'))
  49         self.assertEquals(('G12',1),self.wc.getWell96('M23'))
  50 
  51     def testGetWell384(self):
  52         self.assertEquals('A01',self.wc.getWell384('A01',1))
  53         self.assertEquals('E10',self.wc.getWell384('C05',2))
  54         self.assertEquals('N20',self.wc.getWell384('G10',4))
  55 
  56 class TestDrJangsWellConvertor(unittest.TestCase):
  57     def testJangsCvt(self):
  58         jwc = DrJangsWellConvertor()
  59         self.assertEquals('susfelck_BF_0001_A03.abi', 
  60                 jwc.convertTo96('susfelck_BF_0001_0004_A05.abi'))
  61         self.assertEquals('susfelck_BF_0008_C03.abi', 
  62                 jwc.convertTo96('susfelck_BF_0005_0008_F06.abi'))
  63         self.assertEquals('susfelck_BF_0005_A01.abi', 
  64                 jwc.convertTo96('susfelck_BF_0005_0008_A01.abi'))
  65 
  66 def main(iFile, oFile):
  67     jwc = DrJangsWellConvertor()
  68     for line in iFile:
  69         if line.strip():
  70             oFile.write(jwc.convertTo96(line.strip())+'\n')
  71 
  72 if __name__=='__main__':
  73     unittest.main()
  74     #main(file('input.txt'),file('output.txt','w'))
web biohackers.net