Commit ac6c5d6b authored by Mikael Boden's avatar Mikael Boden

python3_5

parent 934c2bff
...@@ -95,8 +95,8 @@ def betacf(a, b, x): ...@@ -95,8 +95,8 @@ def betacf(a, b, x):
h *= delta h *= delta
if (abs(delta-1.0) < EPS): break if (abs(delta-1.0) < EPS): break
if (m > MAXIT): print >> sys.stderr, ("a or b too big or MAXIT too small " if (m > MAXIT): print(("a or b too big or MAXIT too small "
"in betacf") "in betacf"), file=sys.stderr)
return h return h
...@@ -118,5 +118,5 @@ def gammaln(x): ...@@ -118,5 +118,5 @@ def gammaln(x):
def die(string): def die(string):
print >> sys.stderr, string print(string, file=sys.stderr)
...@@ -105,7 +105,7 @@ class GeneExpression: ...@@ -105,7 +105,7 @@ class GeneExpression:
{'G2': array([ 4.1, -0.9]), 'G3': array([ 2.1, -2.1])} {'G2': array([ 4.1, -0.9]), 'G3': array([ 2.1, -2.1])}
""" """
if names == None: if names == None:
return self.genes.keys() return list(self.genes.keys())
elif isinstance(names, str): elif isinstance(names, str):
return self.matrix[self.genes[names],:] return self.matrix[self.genes[names],:]
else: else:
...@@ -148,7 +148,7 @@ class GeneExpression: ...@@ -148,7 +148,7 @@ class GeneExpression:
except: except:
index = samples index = samples
mygenes = {} mygenes = {}
for (name, ndx) in self.genes.items(): for (name, ndx) in list(self.genes.items()):
mygenes[name] = self.matrix[ndx, index] mygenes[name] = self.matrix[ndx, index]
return mygenes return mygenes
...@@ -165,7 +165,7 @@ class GeneExpression: ...@@ -165,7 +165,7 @@ class GeneExpression:
sort_ndx = np.nan_to_num(self.matrix[:,index]).argsort() sort_ndx = np.nan_to_num(self.matrix[:,index]).argsort()
except: except:
sort_ndx = np.nan_to_num(self.matrix[:,sample]).argsort() sort_ndx = np.nan_to_num(self.matrix[:,sample]).argsort()
name_tuples = sorted(self.genes.items(), key=lambda v: v[1]) # put all gene names in order of the matrix of profiles name_tuples = sorted(list(self.genes.items()), key=lambda v: v[1]) # put all gene names in order of the matrix of profiles
names = [] names = []
if descending: if descending:
for (name, index) in [name_tuples[index] for index in sort_ndx[::-1]]: # reverse the order for (name, index) in [name_tuples[index] for index in sort_ndx[::-1]]: # reverse the order
...@@ -199,7 +199,7 @@ class GeneExpression: ...@@ -199,7 +199,7 @@ class GeneExpression:
Creates and returns a gene dictionary with the corresponding ratios. """ Creates and returns a gene dictionary with the corresponding ratios. """
mygenes = {} mygenes = {}
mdiv = self.matrix[:, index1] / self.matrix[:, index2] mdiv = self.matrix[:, index1] / self.matrix[:, index2]
for (name, ndx) in self.genes.items(): for (name, ndx) in list(self.genes.items()):
mygenes[name] = mdiv[ndx] mygenes[name] = mdiv[ndx]
return mygenes return mygenes
...@@ -208,7 +208,7 @@ class GeneExpression: ...@@ -208,7 +208,7 @@ class GeneExpression:
Creates and returns a gene dictionary with the corresponding log-ratios. """ Creates and returns a gene dictionary with the corresponding log-ratios. """
mygenes = {} mygenes = {}
mlr = np.log2(self.matrix[:, index1] / self.matrix[:, index2]) mlr = np.log2(self.matrix[:, index1] / self.matrix[:, index2])
for (name, ndx) in self.genes.items(): for (name, ndx) in list(self.genes.items()):
mygenes[name] = mlr[ndx] mygenes[name] = mlr[ndx]
return mygenes return mygenes
...@@ -218,7 +218,7 @@ class GeneExpression: ...@@ -218,7 +218,7 @@ class GeneExpression:
index = self.genes[probeID] index = self.genes[probeID]
profile = self.matrix[index, :] profile = self.matrix[index, :]
mygenes = {} mygenes = {}
for (name, ndx) in self.genes.items(): for (name, ndx) in list(self.genes.items()):
other = self.matrix[ndx, :] other = self.matrix[ndx, :]
mygenes[name] = pearson(profile, other) mygenes[name] = pearson(profile, other)
return mygenes return mygenes
...@@ -252,7 +252,7 @@ class GeneExpression: ...@@ -252,7 +252,7 @@ class GeneExpression:
# Calculate Z-score for the given column for each gene # Calculate Z-score for the given column for each gene
zscore = (self.matrix[:, index] - mu) / sd zscore = (self.matrix[:, index] - mu) / sd
mygenes = {} mygenes = {}
for (name, ndx) in self.genes.items(): for (name, ndx) in list(self.genes.items()):
try: try:
mygenes[name] = zscore[ndx, :] mygenes[name] = zscore[ndx, :]
except IndexError: except IndexError:
...@@ -331,9 +331,9 @@ def readGEOFile(filename, id_column=0): ...@@ -331,9 +331,9 @@ def readGEOFile(filename, id_column=0):
genes[name] = values genes[name] = values
if len(genes) == 0: if len(genes) == 0:
raise RuntimeError('No data in file') raise RuntimeError('No data in file')
print 'Data set %s contains %d entries' % (dataset, len(genes)) print('Data set %s contains %d genes' % (dataset, len(genes)))
if cnt_null > 0: if cnt_null > 0:
print 'Data set has %d null-values' % (cnt_null) print('Data set has %d null-values' % (cnt_null))
return GeneExpression(dataset, headers[2:], genes) return GeneExpression(dataset, headers[2:], genes)
...@@ -357,40 +357,29 @@ def pearson(X, Y): ...@@ -357,40 +357,29 @@ def pearson(X, Y):
return 0 return 0
return (sum - n * (Xmu * Ymu)) / (n * math.sqrt(Xvar) * math.sqrt(Yvar)) return (sum - n * (Xmu * Ymu)) / (n * math.sqrt(Xvar) * math.sqrt(Yvar))
# ------------------- Example --------------------- # ------------------- Example (basically exercise 7 in prac 9)---------------------
ge3716 = readGEOFile('/Users/mikael/workspace/COSC2000/GDS3716.soft') if __name__=='__main__':
ratio = GeneExpression('GDS3716_ratio') g = readGEOFile('GDS3198.soft', id_column = 1)
ratio.addSamples('S1_ER+/Healthy', ge3716.getRatio( 33, 0)) meanfold = {}
ratio.addSamples('S2_ER+/Healthy', ge3716.getRatio( 34, 1)) for gene in g.genes:
ratio.addSamples('S3_ER+/Healthy', ge3716.getRatio( 35, 2)) profile = g.getGenes(gene)
ratio.addSamples('S4_ER+/Healthy', ge3716.getRatio( 36, 3)) meanfold[gene] = (np.log2(profile[0] / profile[3]) + np.log2(profile[1] / profile[4]) + np.log2(profile[2] / profile[5])) / 3
ratio.addSamples('S5_ER+/Healthy', ge3716.getRatio( 37, 4))
ratio.addSamples('S6_ER+/Healthy', ge3716.getRatio( 38, 5)) import matplotlib.pyplot as plt
ratio.addSamples('S7_ER+/Healthy', ge3716.getRatio( 39, 6)) scores = [y for y in list(meanfold.values()) if not np.isnan(y)]
ratio.addSamples('S8_ER+/Healthy', ge3716.getRatio( 40, 7)) hist, bins = np.histogram(scores, bins=50)
ratio.addSamples('S9_ER+/Healthy', ge3716.getRatio( 41, 8)) width = 0.7 * (bins[1] - bins[0])
ratio.addSamples('S1_ER-/Healthy', ge3716.getRatio( 24, 9)) center = (bins[:-1] + bins[1:]) / 2
ratio.addSamples('S2_ER-/Healthy', ge3716.getRatio( 25, 10)) plt.bar(center, hist, align='center', width=width)
ratio.addSamples('S3_ER-/Healthy', ge3716.getRatio( 26, 11)) plt.show()
ratio.addSamples('S4_ER-/Healthy', ge3716.getRatio( 27, 12))
ratio.addSamples('S5_ER-/Healthy', ge3716.getRatio( 28, 13))
ratio.addSamples('S6_ER-/Healthy', ge3716.getRatio( 29, 14))
ratio.addSamples('S7_ER-/Healthy', ge3716.getRatio( 30, 15))
ratio.addSamples('S8_ER-/Healthy', ge3716.getRatio( 31, 16))
ratio.addSamples('S9_ER-/Healthy', ge3716.getRatio( 32, 17))
ratio.writeGEOFile('/Users/mikael/workspace/COSC2000/GDS3716_ratios.soft')
print ge3716.getHeaders()
z = ratio.getZScore(0) # NOT recommended! Ratios are NOT normally distributed! Use log-ratios instead.
ge38 = readGEOFile('/Users/mikael/workspace/COSC2000/GDS38.soft', id_column = 1)
cln2_profile = ge38.getGenes('CLN2')
pcorr = ge38.getPearson('CLN2')
gp = GeneExpression('Ex3', 'PC_CLN2', pcorr)
sorted = gp.sort('PC_CLN2', True)
print sorted[0], ge38.getGenes(sorted[0])
print sorted[1], ge38.getGenes(sorted[1])
result = sorted(list(meanfold.items()), key=lambda v: v[1])
print('========== Wildtype may down-regulate ==========')
for r in result[0:100]:
print(r[0], r[1])
print('========== Wildtype may up-regulate ==========')
for r in result[-1:-100:-1]:
print(r[0], r[1])
...@@ -138,7 +138,7 @@ class GibbsMotif(): ...@@ -138,7 +138,7 @@ class GibbsMotif():
LL += math.log(Qk / Pk) LL += math.log(Qk / Pk)
except ZeroDivisionError: except ZeroDivisionError:
pass pass
print "LL @ %5d=\t%5.2f" % (round, LL) print("LL @ %5d=\t%5.2f" % (round, LL))
# end main for-loop # end main for-loop
self.q = q self.q = q
...@@ -312,7 +312,7 @@ class GibbsAlign(): ...@@ -312,7 +312,7 @@ class GibbsAlign():
LL += math.log(Qk / Pk) LL += math.log(Qk / Pk)
except ZeroDivisionError: except ZeroDivisionError:
pass pass
print "LL @ %5d=\t%5.2f" % (round, LL) print("LL @ %5d=\t%5.2f" % (round, LL))
# end main for-loop # end main for-loop
self.q = q self.q = q
......
This diff is collapsed.
This diff is collapsed.
...@@ -21,7 +21,7 @@ class NN(): ...@@ -21,7 +21,7 @@ class NN():
self.b_hid = numpy.random.randn(nHidden) # biases hidden layer self.b_hid = numpy.random.randn(nHidden) # biases hidden layer
self.w_out = numpy.random.randn(nOutput, nHidden) # weights hid -> out self.w_out = numpy.random.randn(nOutput, nHidden) # weights hid -> out
self.b_out = numpy.random.randn(nOutput) # biases output layer self.b_out = numpy.random.randn(nOutput) # biases output layer
print "Constructed NN with %d inputs, %d hidden and %d output nodes." % (self.ninput, len(self.hidden), len(self.output)) print("Constructed NN with %d inputs, %d hidden and %d output nodes." % (self.ninput, len(self.hidden), len(self.output)))
def writeFile(self, filename): def writeFile(self, filename):
""" Save NN to a file. """ """ Save NN to a file. """
...@@ -110,7 +110,7 @@ class NN(): ...@@ -110,7 +110,7 @@ class NN():
multi_targ = [ target ] multi_targ = [ target ]
for i in range(niter): for i in range(niter):
mse = 0.0 mse = 0.0
entries = range(len(multi_input)) entries = list(range(len(multi_input)))
if shuffle: if shuffle:
random.shuffle(entries) random.shuffle(entries)
for p in entries: for p in entries:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Module with methods and classes for phylogeny. Module with methods and classes for phylogeny.
@author: mikael @author: mikael
''' '''
##import sequence import sequence
class PhyloTree: class PhyloTree:
""" Rooted, binary (bifurcating) tree for representing phylogenetic relationships. """ Rooted, binary (bifurcating) tree for representing phylogenetic relationships.
...@@ -140,7 +140,19 @@ class PhyloNode: ...@@ -140,7 +140,19 @@ class PhyloNode:
return left+',' return left+','
elif self.left and self.right: elif self.left and self.right:
return '(' + left + ',' + right + ')' + dist return '(' + left + ',' + right + ')' + dist
def __le__(self, other):
""" Returns indication of less than other node. """
return other and self.__hash__() <= other.__hash__()
def __eq__(self, other):
""" Returns indication of equivalence to other node. """
return other and self.__hash__() == other.__hash__()
def __hash__(self):
""" Returns hash of object. """
return hash((self.label, self.dist, self.sequence))
def _printSequences(self, start, end): def _printSequences(self, start, end):
""" Returns string with node (incl descendants) in a Newick style. """ """ Returns string with node (incl descendants) in a Newick style. """
left = right = label = dist = '' left = right = label = dist = ''
...@@ -352,12 +364,12 @@ def runUPGMA(aln, measure, absoluteDistances = False): ...@@ -352,12 +364,12 @@ def runUPGMA(aln, measure, absoluteDistances = False):
find the *closest* pair of clusters, and find the *closest* pair of clusters, and
merge that pair into a new cluster (to replace the two that merged). merge that pair into a new cluster (to replace the two that merged).
In each case, the new cluster is represented by the (phylo)node that is formed. """ In each case, the new cluster is represented by the (phylo)node that is formed. """
while len(N) > 1: # N will contain all "live" clusters, to be reduced to a signle below while len(N) > 1: # N will contain all "live" clusters, to be reduced to a single below
closest_pair = (None, None) # The two nodes that are closest to one another according to supplied metric closest_pair = (None, None) # The two nodes that are closest to one another according to supplied metric
closest_dist = None # The distance between them closest_dist = None # The distance between them
for pair in D: # check all pairs which should be merged for pair in D: # check all pairs which should be merged
dist = D[pair] dist = D[pair]
if dist < closest_dist or closest_dist == None: if closest_dist == None or dist < closest_dist:
closest_dist = dist closest_dist = dist
closest_pair = pair closest_pair = pair
# So we know the closest, now we need to merge... # So we know the closest, now we need to merge...
...@@ -365,8 +377,10 @@ def runUPGMA(aln, measure, absoluteDistances = False): ...@@ -365,8 +377,10 @@ def runUPGMA(aln, measure, absoluteDistances = False):
y = closest_pair[1] y = closest_pair[1]
z = PhyloNode() # create a new node for the cluster z z = PhyloNode() # create a new node for the cluster z
z.dist = D.pop(_getkey(x, y)) / 2.0 # assign the absolute distance, travelled so far, note: this will change to relative distance later z.dist = D.pop(_getkey(x, y)) / 2.0 # assign the absolute distance, travelled so far, note: this will change to relative distance later
Nx = N.pop(x) # find number of sequences in x, remove the cluster from list N Nx = N.pop(x, None) # find number of sequences in x, remove the cluster from list N
Ny = N.pop(y) # find number of sequences in y, remove the cluster from list N Ny = N.pop(y, None) # find number of sequences in y, remove the cluster from list N
if Nx == None or Ny == None:
continue
dz = {} # new distances to cluster z dz = {} # new distances to cluster z
for w in N: # for each node w ... for w in N: # for each node w ...
# we will merge x and y into a new cluster z, so need to consider w (which is not x or y) # we will merge x and y into a new cluster z, so need to consider w (which is not x or y)
......
...@@ -277,7 +277,7 @@ def _readDistrib(linelist): ...@@ -277,7 +277,7 @@ def _readDistrib(linelist):
if len(d) == 0: if len(d) == 0:
return None return None
alpha = Alphabet(symstr) alpha = Alphabet(symstr)
if '*' in d.keys(): # tot provided if '*' in list(d.keys()): # tot provided
for sym in d: for sym in d:
if sym != '*': if sym != '*':
d[sym] = d[sym] * d['*'] d[sym] = d[sym] * d['*']
...@@ -338,7 +338,7 @@ def _readMultiCount(linelist, format = 'JASPAR'): ...@@ -338,7 +338,7 @@ def _readMultiCount(linelist, format = 'JASPAR'):
ncol = len(counts) ncol = len(counts)
if len(name) == 1: # proper symbol if len(name) == 1: # proper symbol
symcount[name] = counts symcount[name] = counts
alpha = Alphabet(''.join(symcount.keys())) alpha = Alphabet(''.join(list(symcount.keys())))
distribs = [] distribs = []
for col in range(ncol): for col in range(ncol):
d = dict([(sym, symcount[sym][col]) for sym in symcount]) d = dict([(sym, symcount[sym][col]) for sym in symcount])
...@@ -412,7 +412,7 @@ def readMultiCount(filename, format = 'JASPAR'): ...@@ -412,7 +412,7 @@ def readMultiCount(filename, format = 'JASPAR'):
""" """
d = readMultiCounts(filename, format=format) d = readMultiCounts(filename, format=format)
if len(d) > 0: if len(d) > 0:
return d.values()[0] return list(d.values())[0]
################################################################################################# #################################################################################################
# Joint class # Joint class
...@@ -628,12 +628,12 @@ class IndepJoint(Joint): ...@@ -628,12 +628,12 @@ class IndepJoint(Joint):
def displayMatrix(self, count = False): def displayMatrix(self, count = False):
""" Pretty-print matrix """ """ Pretty-print matrix """
print " \t%s" % (''.join("\t%5d" % (i + 1) for i in range(len(self.alphas)))) print((" \t%s" % (''.join("\t%5d" % (i + 1) for i in range(len(self.alphas))))))
for a in self.alphas[0]: for a in self.alphas[0]:
if count: if count:
print "%s\t%s" % (a, ''.join("\t%5d" % (y) for y in self.getRow(a, True))) print(("%s\t%s" % (a, ''.join("\t%5d" % (y) for y in self.getRow(a, True)))))
else: else:
print "%s\t%s" % (a, ''.join("\t%5.3f" % (y) for y in self.getRow(a))) print(("%s\t%s" % (a, ''.join("\t%5.3f" % (y) for y in self.getRow(a)))))
def __str__(self): def __str__(self):
""" Text representation of the table. Note that size is an issue so big tables """ Text representation of the table. Note that size is an issue so big tables
...@@ -718,5 +718,3 @@ class NaiveBayes(): ...@@ -718,5 +718,3 @@ class NaiveBayes():
prob *= condprob[i][key[i]] or 0.0 prob *= condprob[i][key[i]] or 0.0
out.observe(outsym, prob) out.observe(outsym, prob)
return out return out
This diff is collapsed.
This diff is collapsed.
...@@ -55,10 +55,11 @@ class Sequence(object): ...@@ -55,10 +55,11 @@ class Sequence(object):
['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'V', 'W', 'Y'] """ 'R', 'S', 'T', 'V', 'W', 'Y'] """
try: # convert sequence data into a compact array representation #try: # convert sequence data into a compact array representation
self.sequence = array.array('c', ''.join([s.upper() for s in sequence])) # self.sequence = sequence.encode("utf-8") #array.array('b', ''.join([s.upper() for s in sequence]))
except TypeError: #except TypeError:
raise RuntimeError('Sequence data is not specified correctly: must be iterable') # raise RuntimeError('S"""""""""""""""""""""""""""""""equence data is not specified correctly: must be iterable')
self.sequence = sequence
# Assign an alphabet # Assign an alphabet
self.alphabet = None self.alphabet = None
...@@ -133,15 +134,15 @@ class Sequence(object): ...@@ -133,15 +134,15 @@ class Sequence(object):
Calling self.__getitem__(3) is equivalent to self[3] Calling self.__getitem__(3) is equivalent to self[3]
""" """
if type(ndx) is slice: if type(ndx) is slice:
return self.sequence[ndx].tostring() return ''.join(self.sequence[ndx])
else: else:
return self.sequence[ndx] return self.sequence[ndx]
def writeFasta(self): def writeFasta(self):
""" Write one sequence in FASTA format to a string and return it. """ """ Write one sequence in FASTA format to a string and return it. """
fasta = '>' + self.name + ' ' + self.info + '\n' fasta = '>' + self.name + ' ' + self.info + '\n'
data = self.sequence.tostring() data = ''.join(self.sequence)
nlines = (len(self.sequence) - 1) / 60 + 1 nlines = int(math.ceil((len(self.sequence) - 1) / 60 + 1))
for i in range(nlines): for i in range(nlines):
lineofseq = ''.join(data[i*60 : (i+1)*60]) + '\n' lineofseq = ''.join(data[i*60 : (i+1)*60]) + '\n'
fasta += lineofseq fasta += lineofseq
...@@ -164,7 +165,7 @@ class Sequence(object): ...@@ -164,7 +165,7 @@ class Sequence(object):
def find(self, findme): def find(self, findme):
""" Find the position of the specified symbol or sub-sequence """ """ Find the position of the specified symbol or sub-sequence """
return self.sequence.tostring().find(findme) return ''.join(self.sequence).find(findme)
""" """
Below are some useful methods for loading data from strings and files. Below are some useful methods for loading data from strings and files.
...@@ -438,8 +439,8 @@ class Alignment(): ...@@ -438,8 +439,8 @@ class Alignment():
column index, entropy, number of gaps, and symbols in order of decreasing probability. column index, entropy, number of gaps, and symbols in order of decreasing probability.
theta1 is the threshold for displaying symbols in upper case, theta1 is the threshold for displaying symbols in upper case,
theta2 is the threshold for showing symbols at all, and in lower case. """ theta2 is the threshold for showing symbols at all, and in lower case. """
print "Alignment of %d sequences, with %d columns" % (len(self.seqs), self.alignlen) print(("Alignment of %d sequences, with %d columns" % (len(self.seqs), self.alignlen)))
print "Column\tEntropy\tGaps\tProb\tConserv\tSymbols (Up>=%.2f;Low>=%.2f)\n" % (theta1, theta2) print(("Column\tEntropy\tGaps\tProb\tConserv\tSymbols (Up>=%.2f;Low>=%.2f)\n" % (theta1, theta2)))
for col in range(self.alignlen): for col in range(self.alignlen):
d = Distrib(self.alphabet) d = Distrib(self.alphabet)
gaps = 0 gaps = 0
...@@ -448,21 +449,21 @@ class Alignment(): ...@@ -448,21 +449,21 @@ class Alignment():
d.observe(seq[col]) d.observe(seq[col])
else: else:
gaps += 1 gaps += 1
print (col + 1), "\t%5.3f" % d.entropy(), "\t%4d\t" % gaps, print(((col + 1), "\t%5.3f" % d.entropy(), "\t%4d\t" % gaps,))
symprobs = d.getProbsort() symprobs = d.getProbsort()
(_, maxprob) = symprobs[0] (_, maxprob) = symprobs[0]
if maxprob >= theta1: if maxprob >= theta1:
print "%d\tTRUE\t" % int(maxprob * 100), print(("%d\tTRUE\t" % int(maxprob * 100),))
else: else:
print "%d\t\t" % int(maxprob * 100), print(("%d\t\t" % int(maxprob * 100),))
for (sym, prob) in symprobs: for (sym, prob) in symprobs:
if prob >= theta1: if prob >= theta1:
print sym, "%d%%" % int(prob * 100), print((sym, "%d%%" % int(prob * 100),))
elif prob >= theta2 and lowercase: elif prob >= theta2 and lowercase:
print sym.lower(), "%d%%" % int(prob * 100), print((sym.lower(), "%d%%" % int(prob * 100),))
elif prob >= theta2: elif prob >= theta2:
print sym, "%d%%" % int(prob * 100), print((sym, "%d%%" % int(prob * 100),))
print print()
def saveConsensus(self, myseq, filename, theta1 = 0.2, theta2 = 0.05, lowercase = True, compact = False): def saveConsensus(self, myseq, filename, theta1 = 0.2, theta2 = 0.05, lowercase = True, compact = False):
""" Display a table with rows for each alignment column, showing """ Display a table with rows for each alignment column, showing
...@@ -644,7 +645,7 @@ class Alignment(): ...@@ -644,7 +645,7 @@ class Alignment():
return distmat return distmat
def writeHTML(self, filename=None): def writeHTML(self, filename=None):
""" Generate HTML that displays the alignment in color. """ Generate HTML that displays the alignment in color.
Requires that the alphabet is annotated with the label 'html-color' (see Sequence.annotateSym) Requires that the alphabet is annotated with the label 'html-color' (see Sequence.annotateSym)
and that each symbol maps to a text string naming the color, e.g. 'blue' and that each symbol maps to a text string naming the color, e.g. 'blue'
""" """
...@@ -681,10 +682,9 @@ class Alignment(): ...@@ -681,10 +682,9 @@ class Alignment():
htmlstr += html htmlstr += html
htmlstr += '<pre>' htmlstr += '<pre>'
if filename: if filename:
fh = open(filename, 'w') with open(filename, 'w+') as fh:
fh.write(htmlstr) fh.write(htmlstr)
fh.write('</body></html>\n') fh.write('</body></html>\n')
fh.close()
else: else:
return htmlstr return htmlstr
...@@ -985,12 +985,12 @@ def readClustal(string, alphabet): ...@@ -985,12 +985,12 @@ def readClustal(string, alphabet):
index = name.find('/') index = name.find('/')
if index >= 0: if index >= 0:
name = name[0:index] name = name[0:index]
if seqs.has_key(name): if name in seqs:
seqs[name] += seqstr seqs[name] += seqstr
else: else:
seqs[name] = seqstr seqs[name] = seqstr
sequences = [] sequences = []
for name, seqstr in seqs.items(): for name, seqstr in list(seqs.items()):
sequences.append(Sequence(seqstr, alphabet, name, gappy = True)) sequences.append(Sequence(seqstr, alphabet, name, gappy = True))
return Alignment(sequences) return Alignment(sequences)
...@@ -1180,12 +1180,12 @@ class PWM(object): ...@@ -1180,12 +1180,12 @@ class PWM(object):
def display(self, format = 'COLUMN'): def display(self, format = 'COLUMN'):
if format == 'COLUMN': if format == 'COLUMN':
print " \t%s" % (' '.join(" %5d" % (i + 1) for i in range(self.length))) print((" \t%s" % (' '.join(" %5d" % (i + 1) for i in range(self.length)))))
for j in range(len(self.alphabet)): for j in range(len(self.alphabet)):
print "%s\t%s" % (self.alphabet[j], ' '.join("%+6.2f" % (y) for y in self.m[j])) print(("%s\t%s" % (self.alphabet[j], ' '.join("%+6.2f" % (y) for y in self.m[j]))))
elif format == 'JASPAR': elif format == 'JASPAR':
for j in range(len(self.alphabet)): for j in range(len(self.alphabet)):
print "%s\t[%s]" % (self.alphabet[j], ' '.join("%+6.2f" % (y) for y in self.m[j])) print(("%s\t[%s]" % (self.alphabet[j], ' '.join("%+6.2f" % (y) for y in self.m[j]))))
def search(self, sequence, lowerBound=0): def search(self, sequence, lowerBound=0):
""" Find matches to the motif in a specified sequence. Returns a list """ Find matches to the motif in a specified sequence. Returns a list
...@@ -1229,7 +1229,7 @@ def getSequence(id, database = 'uniprotkb', start=None, end=None): ...@@ -1229,7 +1229,7 @@ def getSequence(id, database = 'uniprotkb', start=None, end=None):
""" Get the sequence identified by the given ID from the given database """ Get the sequence identified by the given ID from the given database
(e.g. 'uniprotkb', 'refseqn' or 'refseqp'), and return it as a Sequence (e.g. 'uniprotkb', 'refseqn' or 'refseqp'), and return it as a Sequence
object. An error is caused if the sequence ID is not found. If start and object. An error is caused if the sequence ID is not found. If start and
end are given, then only that section of the sequence is returned. end are given, then only that section of the sequence is returned.
Note: more flexible search options are supported by using webservice.fetch Note: more flexible search options are supported by using webservice.fetch
directly.""" directly."""
...@@ -1237,12 +1237,12 @@ def getSequence(id, database = 'uniprotkb', start=None, end=None): ...@@ -1237,12 +1237,12 @@ def getSequence(id, database = 'uniprotkb', start=None, end=None):
for i in range(MAX_TRY): for i in range(MAX_TRY):
try: try:
fastaData = fetch(id, database) fastaData = fetch(id, database).decode("utf-8")
seq = readFasta(fastaData)[0] seq = readFasta(fastaData)[0]
break break
except: except:
from time import sleep from time import sleep
print 'Failed on {i}th try for id {id}'.format(i=i, id=id) print(('Failed on {i}th try for id {id}'.format(i=i, id=id)))
sleep(0.1) sleep(0.1)
try: try:
return Sequence(seq[start:end], seq.alphabet, seq.name, seq.info) return Sequence(seq[start:end], seq.alphabet, seq.name, seq.info)
...@@ -1319,5 +1319,4 @@ def runBLAST(sequence, program='blastp', database='uniprotkb', exp='1e-1'): ...@@ -1319,5 +1319,4 @@ def runBLAST(sequence, program='blastp', database='uniprotkb', exp='1e-1'):
if __name__ == '__main__': if __name__ == '__main__':
seqs = readFastaFile('/Users/mikael/ASR/CYP11/CYP11_aln_full.fa', Protein_wX, gappy=True) seqs = readFastaFile('/Users/mikael/ASR/CYP11/CYP11_aln_full.fa', Protein_wX, gappy=True)
print 'Read', len(seqs), 'sequences' print(('Read', len(seqs), 'sequences'))
...@@ -71,7 +71,7 @@ class SeqNN(): ...@@ -71,7 +71,7 @@ class SeqNN():
im[row, _onehotIndex(alpha, subseqs[k])] = 1 im[row, _onehotIndex(alpha, subseqs[k])] = 1
if targets: om[row, self.outp_alpha.index(subtarg[k])] = 1 if targets: om[row, self.outp_alpha.index(subtarg[k])] = 1
row += 1 row += 1
print "There are", row, "entries in data set" print("There are", row, "entries in data set")
if targets: if targets:
return im, om return im, om
else: else:
...@@ -85,7 +85,7 @@ class SeqNN(): ...@@ -85,7 +85,7 @@ class SeqNN():
im, om = self._encodeseq(seqs, targets) im, om = self._encodeseq(seqs, targets)
for i in range(niter): # train first NN for i in range(niter): # train first NN
rmse = self.nn1.train(im, om, eta = eta, niter = 1) rmse = self.nn1.train(im, om, eta = eta, niter = 1)
print i, ":", rmse print(i, ":", rmse)
if not self.cascade: # if there's no cascaded NN, finish here if not self.cascade: # if there's no cascaded NN, finish here
return rmse return rmse
nn1seqs = [] # a list of new SS sequences ... nn1seqs = [] # a list of new SS sequences ...
...@@ -95,7 +95,7 @@ class SeqNN(): ...@@ -95,7 +95,7 @@ class SeqNN():
im, om = self._encodeseq(nn1seqs, targets) # construct input/output patterns from SS sequences im, om = self._encodeseq(nn1seqs, targets) # construct input/output patterns from SS sequences
for i in range(niter): # train cascaded NN for i in range(niter): # train cascaded NN
rmse = self.nn2.train(im, om, eta = eta, niter = 1) rmse = self.nn2.train(im, om, eta = eta, niter = 1)
print i, ":", rmse print(i, ":", rmse)
return rmse return rmse
def testAll(self, seqs, targets): def testAll(self, seqs, targets):
......
...@@ -85,7 +85,7 @@ def extendDownstream(scores, calls, width = 4): ...@@ -85,7 +85,7 @@ def extendDownstream(scores, calls, width = 4):
specified width average of 100. specified width average of 100.
""" """
sum = 0.0 sum = 0.0
order = range(0, len(calls) - 1, +1) # we are extending calls downstream order = list(range(0, len(calls) - 1, +1)) # we are extending calls downstream
cnt = 0 cnt = 0
for i in order: # extend to the right for i in order: # extend to the right
if calls[i]: # to extend a call is required in the first place if calls[i]: # to extend a call is required in the first place
...@@ -105,7 +105,7 @@ def extendUpstream(scores, calls, width = 4): ...@@ -105,7 +105,7 @@ def extendUpstream(scores, calls, width = 4):
AND extend this list upstream containing a specified width average of 100. AND extend this list upstream containing a specified width average of 100.
""" """
sum = 0.0 sum = 0.0
order = range(len(calls) - 1, 0, -1) # we are extending calls upstream/to-the-left order = list(range(len(calls) - 1, 0, -1)) # we are extending calls upstream/to-the-left
cnt = 0 cnt = 0
for i in order: # extend to the right for i in order: # extend to the right
if calls[i]: # a requirement to extend is to have a call in the first place if calls[i]: # a requirement to extend is to have a call in the first place
......
...@@ -291,7 +291,7 @@ class TupleEntries(object): ...@@ -291,7 +291,7 @@ class TupleEntries(object):
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
""" Step through sequence of entries, either """ Step through sequence of entries, either
(if not sparse) with a step-size based on alphabet-sizes and what symbols are specified or (if not sparse) with a step-size based on alphabet-sizes and what symbols are specified or
(if sparse) with calls to tuple store based on all possible symbol combinations.""" (if sparse) with calls to tuple store based on all possible symbol combinations."""
......
This diff is collapsed.
...@@ -45,7 +45,7 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100): ...@@ -45,7 +45,7 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100):
neg[word] = 1 neg[word] = 1
logratio = RCDict() # DNA dictionary for storing the log-ration between pos and neg logratio = RCDict() # DNA dictionary for storing the log-ration between pos and neg
for (word, cnt_pos) in pos.items(): for (word, cnt_pos) in list(pos.items()):
cnt_neg = 0.0001 cnt_neg = 0.0001
try: try:
cnt_neg = neg[word] cnt_neg = neg[word]
...@@ -53,10 +53,10 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100): ...@@ -53,10 +53,10 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100):
pass pass
logratio[word] = math.log(float(cnt_pos) / float(cnt_neg)) logratio[word] = math.log(float(cnt_pos) / float(cnt_neg))
allpos = logratio.items() # extract all pairs of words:log-ratio allpos = list(logratio.items()) # extract all pairs of words:log-ratio
sortpos = sorted(allpos, key=lambda v: v[1], reverse=True) # sort them sortpos = sorted(allpos, key=lambda v: v[1], reverse=True) # sort them
print "Enriched words (sorted by ln pos/neg)" print("Enriched words (sorted by ln pos/neg)")
print "Word \tln pos/neg\tE-value" print("Word \tln pos/neg\tE-value")
for (word, lgr) in sortpos[0:100]: # Look at the top-entries according to log-ratio, compute e-values for (word, lgr) in sortpos[0:100]: # Look at the top-entries according to log-ratio, compute e-values
cnt_pos = int(pos[word]) cnt_pos = int(pos[word])
try: cnt_neg = int(neg[word]) try: cnt_neg = int(neg[word])
...@@ -65,7 +65,7 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100): ...@@ -65,7 +65,7 @@ def countWordsReport(seqs, WordWidth = 8, PeakWidth = 100, PeakMargin = 100):
pval = stats.getFETpval(cnt_pos, cnt_neg, len(seqs) * (PeakWidth - WordWidth + 1) - cnt_pos, len(seqs) * (len(seq) - (PeakMargin * 2 + PeakWidth) - (WordWidth - 1) * 2) - cnt_neg, False) pval = stats.getFETpval(cnt_pos, cnt_neg, len(seqs) * (PeakWidth - WordWidth + 1) - cnt_pos, len(seqs) * (len(seq) - (PeakMargin * 2 + PeakWidth) - (WordWidth - 1) * 2) - cnt_neg, False)
# Correct for multiple testing (very conservatively) # Correct for multiple testing (very conservatively)
eval = pval * len(allpos) eval = pval * len(allpos)
print "%s\t%6.3f \t%e" % (word, lgr, eval) print("%s\t%6.3f \t%e" % (word, lgr, eval))
def getReverse(distribs): def getReverse(distribs):
""" Construct a new list of probability distributions of DNA, by """ Construct a new list of probability distributions of DNA, by
...@@ -94,10 +94,10 @@ def scanMotifReport(seqs, motif, threshold=0, jaspar = 'JASPAR_matrices.txt'): ...@@ -94,10 +94,10 @@ def scanMotifReport(seqs, motif, threshold=0, jaspar = 'JASPAR_matrices.txt'):
except KeyError: except KeyError:
usage(sys.argv[0], "Unknown motif %s" % motif) usage(sys.argv[0], "Unknown motif %s" % motif)
return return
print "Motif %s:" % motif print("Motif %s:" % motif)
pwm1 = sequence.PWM(fg1, bg) pwm1 = sequence.PWM(fg1, bg)
pwm1.display(format='JASPAR') pwm1.display(format='JASPAR')
print "Motif %s (reverse complement):" % motif print("Motif %s (reverse complement):" % motif)
pwm2 = sequence.PWM(fg2, bg) pwm2 = sequence.PWM(fg2, bg)
pwm2.display(format='JASPAR') pwm2.display(format='JASPAR')
...@@ -141,7 +141,7 @@ def scanMotifReport(seqs, motif, threshold=0, jaspar = 'JASPAR_matrices.txt'): ...@@ -141,7 +141,7 @@ def scanMotifReport(seqs, motif, threshold=0, jaspar = 'JASPAR_matrices.txt'):
# plot the average score curve # plot the average score curve
# print >> sys.stderr, "" # print >> sys.stderr, ""
x = range(-(seq_len/2), (seq_len/2)) # call center of sequence X=0 x = list(range(-(seq_len/2), (seq_len/2))) # call center of sequence X=0
lbl = "%s" % (motif) lbl = "%s" % (motif)
plt.plot(x, avg_motif_score, label=lbl) plt.plot(x, avg_motif_score, label=lbl)
#plt.plot(x, smoothed_avg_motif_score, label=lbl) #plt.plot(x, smoothed_avg_motif_score, label=lbl)
...@@ -187,10 +187,10 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices ...@@ -187,10 +187,10 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices
except KeyError: except KeyError:
usage(sys.argv[0], "Unknown motif %s" % motif) usage(sys.argv[0], "Unknown motif %s" % motif)
return return
print "Motif %s:" % motif print("Motif %s:" % motif)
pwm1 = sequence.PWM(fg1, bg) pwm1 = sequence.PWM(fg1, bg)
pwm1.display(format='JASPAR') pwm1.display(format='JASPAR')
print "Motif %s (reverse complement):" % motif print("Motif %s (reverse complement):" % motif)
pwm2 = sequence.PWM(fg2, bg) pwm2 = sequence.PWM(fg2, bg)
pwm2.display(format='JASPAR') pwm2.display(format='JASPAR')
...@@ -222,7 +222,7 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices ...@@ -222,7 +222,7 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices
# divide number of sequences with hit by total number of hits # divide number of sequences with hit by total number of hits
site_probability = [ (cnt/n_seqs_with_hits) for cnt in hit_count ] site_probability = [ (cnt/n_seqs_with_hits) for cnt in hit_count ]
print >> sys.stderr, "Number of sequences with hit (score >= %f): %d" % (threshold, n_seqs_with_hits) print("Number of sequences with hit (score >= %f): %d" % (threshold, n_seqs_with_hits), file=sys.stderr)
# STATISTICS # STATISTICS
# Get the cumulative hit counts in concentric windows # Get the cumulative hit counts in concentric windows
...@@ -250,7 +250,7 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices ...@@ -250,7 +250,7 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices
for i in range(hw, seq_len-motif_width+1-hw): for i in range(hw, seq_len-motif_width+1-hw):
smoothed_site_probability[i]=sum(site_probability[i-hw:i+hw+1])/(2*hw+1) smoothed_site_probability[i]=sum(site_probability[i-hw:i+hw+1])/(2*hw+1)
x = range(-(seq_len/2), (seq_len/2)) # call center of sequence X=0 x = list(range(-(seq_len/2), (seq_len/2))) # call center of sequence X=0
lbl = "%s, t=%.2f" % (motif, threshold) lbl = "%s, t=%.2f" % (motif, threshold)
#lbl = "%s, t=%.2f, w=%d, p=%.2e" % (motif, threshold, best_r, math.exp(best_log_pvalue)) #lbl = "%s, t=%.2f, w=%d, p=%.2e" % (motif, threshold, best_r, math.exp(best_log_pvalue))
plt.plot(x, smoothed_site_probability, label=lbl) plt.plot(x, smoothed_site_probability, label=lbl)
...@@ -263,20 +263,20 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices ...@@ -263,20 +263,20 @@ def scanMotifReport_new(seqs, motif, threshold=3.4567, jaspar = 'JASPAR_matrices
def usage(name, errmsg = None): def usage(name, errmsg = None):
if errmsg != None: if errmsg != None:
print "Error: %s" % errmsg print("Error: %s" % errmsg)
print """Usage: %s [options] print("""Usage: %s [options]
-f <fasta-filename> (required) -f <fasta-filename> (required)
-d discover enriched words -d discover enriched words
-w <word width, default 8> -w <word width, default 8>
-p <peak width, default 100> -p <peak width, default 100>
-m <peak margin, default 100> -m <peak margin, default 100>
-s <JASPAR-ID> scan for JASPAR motif -s <JASPAR-ID> scan for JASPAR motif
-h print this help""" % name -h print this help""" % name)
if __name__ == '__main__': if __name__ == '__main__':
try: try:
optlst, args = getopt.getopt(sys.argv[1:], 'f:hds:j:w:p:m:') optlst, args = getopt.getopt(sys.argv[1:], 'f:hds:j:w:p:m:')
except getopt.GetoptError, err: except getopt.GetoptError as err:
usage(sys.argv[0], str(err)) usage(sys.argv[0], str(err))
sys.exit(2) sys.exit(2)
FILENAME = None FILENAME = None
...@@ -301,7 +301,7 @@ if __name__ == '__main__': ...@@ -301,7 +301,7 @@ if __name__ == '__main__':
sys.exit(3) sys.exit(3)
seqs = sequence.readFastaFile(FILENAME, sym.DNA_Alphabet_wN) seqs = sequence.readFastaFile(FILENAME, sym.DNA_Alphabet_wN)
if DISCOVER_MODE: if DISCOVER_MODE:
print "Discover (f=%s; w=%d; p=%d; m=%d)" % (FILENAME, WORD_WIDTH, PEAK_WIDTH, PEAK_MARGIN) print("Discover (f=%s; w=%d; p=%d; m=%d)" % (FILENAME, WORD_WIDTH, PEAK_WIDTH, PEAK_MARGIN))
countWordsReport(seqs, WORD_WIDTH, PEAK_WIDTH, PEAK_MARGIN) countWordsReport(seqs, WORD_WIDTH, PEAK_WIDTH, PEAK_MARGIN)
elif SCAN_MODE: elif SCAN_MODE:
scanMotifReport(seqs, MOTIF_ID) scanMotifReport(seqs, MOTIF_ID)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment