def testAddColumn(self): tsv = TSVTable(self.getInputFile("mrna1.tsv"), uniqKeyCols="qName") tsv.addColumn("joke") i = 0 for row in tsv: row.joke = i i += 1 fh = open(self.getOutputFile(".tsv"), "w") tsv.write(fh) fh.close() self.diffExpected(".tsv")
def testColTypeDefault(self): # default to int type typeMap = { "strand": str, "qName": str, "tName": str, "blockSizes": intArrayType, "qStarts": intArrayType, "tStarts": intArrayType } tsv = TSVTable(self.getInputFile("mrna1.tsv"), uniqKeyCols="qName", typeMap=typeMap, defaultColType=int) r = tsv.idx.qName["AK095183"] self.failUnlessEqual(r.tStart, 4222) self.failUnlessEqual(r.tEnd, 19206) self.failUnlessEqual(r.tName, "chr1") self.failUnlessEqual(r.tStart, 4222) tStarts = (4222, 4832, 5658, 5766, 6469, 6719, 7095, 7355, 7777, 8130, 14600, 19183) self.failUnlessEqual(len(r.tStarts), len(tStarts)) for i in xrange(len(tStarts)): self.failUnlessEqual(r.tStarts[i], tStarts[i])
def testMissingIdxCol(self): err = None try: tsv = TSVTable(self.getInputFile("mrna1.tsv"), multiKeyCols=("noCol", )) except TSVError, e: err = e
def testLoad(self): tsv = TSVTable(self.getInputFile("mrna1.tsv")) self.failUnlessEqual(len(tsv), 10) for r in tsv: self.failUnlessEqual(len(r), 22) r = tsv[0] self.failUnlessEqual(r["qName"], "BC032353") self.failUnlessEqual(r[10], "BC032353") self.failUnlessEqual(r.qName, "BC032353")
def testMultiIdx(self): tsv = TSVTable(self.getInputFile("mrna1.tsv"), multiKeyCols=("tName", "tStart")) rows = tsv.idx.tName["chr1"] self.failUnlessEqual(len(rows), 10) self.failUnlessEqual(rows[1].qName, "AK095183") rows = tsv.idx.tStart["4268"] self.failUnlessEqual(len(rows), 5) self.failUnlessEqual(rows[0].qName, "BC015400")
def doTestColType(self, inFile): typeMap = { "intCol": int, "floatCol": float, "onOffCol": (self.onOffParse, self.onOffFmt) } tsv = TSVTable(self.getInputFile(inFile), typeMap=typeMap) r = tsv[0] self.failUnlessEqual(r.strCol, "name1") self.failUnlessEqual(r.intCol, 10) self.failUnlessEqual(r.floatCol, 10.01) self.failUnlessEqual(str(r), "name1\t10\t10.01\ton") r = tsv[2] self.failUnlessEqual(r.strCol, "name3") self.failUnlessEqual(r.intCol, 30) self.failUnlessEqual(r.floatCol, 30.555) self.failUnlessEqual(str(r), "name3\t30\t30.555\toff")
def testColNameMap(self): typeMap = { "int_col": int, "float_col": float, "onOff_col": (self.onOffParse, self.onOffFmt) } tsv = TSVTable(self.getInputFile('typesColNameMap.tsv'), typeMap=typeMap, columnNameMapper=lambda s: s.replace(' ', '_')) r = tsv[0] self.failUnlessEqual(r.str_col, "name1") self.failUnlessEqual(r.int_col, 10) self.failUnlessEqual(r.float_col, 10.01) self.failUnlessEqual(str(r), "name1\t10\t10.01\ton") r = tsv[2] self.failUnlessEqual(r.str_col, "name3") self.failUnlessEqual(r.int_col, 30) self.failUnlessEqual(r.float_col, 30.555) self.failUnlessEqual(str(r), "name3\t30\t30.555\toff")
def testAllowEmptyTbl(self): tbl = TSVTable("/dev/null", allowEmpty=True) self.failUnlessEqual(len(tbl), 0)
def testDupColumn(self): err = None try: tsv = TSVTable(self.getInputFile("dupCol.tsv")) except TSVError, e: err = e
def testWrite(self): tsv = TSVTable(self.getInputFile("mrna1.tsv"), uniqKeyCols="qName") fh = open(self.getOutputFile(".tsv"), "w") tsv.write(fh) fh.close() self.diffExpected(".tsv")