#!/usr/local/bin/python #you should prepare .CSV input file with name "chromosome" #.CSV file should contains a list of human chromosomal regions for example "chr1p32.1" def set_cancer_types(inFile): global cancerTypes header = inFile.readline().strip() cancerTypes = header.split(',') def load_data_rows(inFile): global data global chrms line = inFile.readline().strip() while line != "": entries = line.split(',') line = inFile.readline().strip() ind = 0 for entry in entries: cancerType = cancerTypes[ind] ind += 1 entry = entry.strip() if len(entry) == 0: continue chrms.add(entry) key = (cancerType, entry) if key in data: data[key] += 1 else: data[key] = 1 def write_header(outFile): for type in cancerTypes: outFile.write("," + type) def write_result(outFile): write_header(outFile) for chrm in chrms: outFile.write(chrm + ",") for type in cancerTypes: key = (type, chrm) count = 0 if key in data: count = data[key] outFile.write(str(count) + ",") outFile.write("\n") cancerTypes = [] chrms = set() data = {} inFile = open("chromosome.csv") set_cancer_types(inFile) load_data_rows(inFile) inFile.close() outFile = open("result.csv","w") write_result(outFile) outFile.close() #print data #print chrmsPart1 #print chrmsPart2 #print cancerTypes