-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroarray.py
116 lines (87 loc) · 2.94 KB
/
microarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'''
LICENSE:
Copyright (C) 2011, Pankaj Kumar Garg
This program is distributed under GNU General Public License
'''
__author__ = "Pankaj Kumar Garg"
__email__ = "pankajn17@gmail.com"
__copyright__ = "Copyright (c) 2011, Pankaj Kumar Garg"
__license__ = "GPLv3"
import os
os.chdir(os.path.abspath(os.path.dirname(__file__)))
import math, csv, array, operator, json, os
import mds
class MicroArray:
def __init__(self, fileObj, **kwargs):
'fileObj - a filename or file object referencing to the microarray csv file'
if isinstance(fileObj, str):
self.fileObj = open(fileObj, "rb")
elif isinstance(fileObj, file):
self.fileObj = fileObj
else:
raise Exception('input to Microarray is not of type string or file')
self.genes = self.fileReader(self.fileObj, **kwargs)
self.numGenes = len(self.genes)
uidCount = 1
for geneId in self.genes.iterkeys():
self.genes[geneId].uid = uidCount
uidCount += 1
def gene(self, geneId):
if geneId in self.genes:
return self.genes[geneId]
else:
return None
def fileReader(self, fileObj, idCol = 1, ignoreRows = [1], ignoreCols = [2]):
'''
values of idCol, ignoreRows, and ignoreCols begin from 1 (instead of 0).... so 1 is the first column
'''
if isinstance(ignoreRows, int):
ignoreRows = [ignoreRows]
if isinstance(ignoreCols, int):
ignoreCols = [ignoreCols]
ignoreCols = set(ignoreCols)
ignoreRows = set(ignoreRows)
#dialect = csv.Sniffer().sniff(fileObj.read(1024))
#fileObj.seek(0)
#Shall only work with comma separated file
reader = csv.reader(fileObj)
genes = {}
for i,row in enumerate(reader):
lineNum = i+1
if lineNum in ignoreRows:
continue
if row[idCol - 1] == "":
continue
values = []
tempId = ''
for j, cell in enumerate(row):
colNum = j + 1
if colNum in ignoreCols:
continue
if colNum == idCol:
tempId = cell.strip().upper()
else:
if cell == '':
cell = '0'
values.append(float(cell))
#Checking if the variance of the gene expression profile is zero, if it is so, then it is left out
if len(values) == 0 or self.variance(values) == 0:
continue
genes[tempId] = Gene(tempId, values)
fileObj.close()
return genes
def variance(self, values):
mean = sum(values)/float(len(values))
return sum( pow((mean-i), 2) for i in values)/float(len(values))
class Gene:
def __init__(self, id, profile):
'profile refers to the row of values related to the gene in the microarray '
self.id = id
self.profile = array.array('f', profile)
if not isinstance(profile, list):
print profile
raise Exception(" gene profile is not a list")
self.numCols = len(self.profile)
self.mean = sum(self.profile)/float(len(self.profile))
self.meanDiff = array.array('f', [self.profile[k] - self.mean for k in xrange(self.numCols)])
self.varianceLike = pow( sum( pow((self.profile[k] - self.mean), 2) for k in xrange(self.numCols)), 0.5)