在pycharm中运行py文件没有输出结果只显示Process finished with exit code 0
1.程序没报错,运行程序中也有print,解释器和终端方面已验证没问题。
2.我将网上例子的代码输入能正常输出结果(https://blog.csdn.net/weixin_42554337/article/details/103626958?ops_request_misc=&request_id=&biz_id=102&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0 "")
。
我的代码如下:
class DeterministicMotifFinding:
def __init__(self, size=6, seqs=None):
self.botif_size = size
if seqs is not None:
self.seqs = seqs
self.alphabet = seqs[0].alphabet()
else:
self.seqs = []
def __len__(self):
return len(self.seqs)
def __getitem__(self, n):
return self.seqs[n]
def seq_size(self, i):
return len(self.seqs[i])
def read_file(self, fic, t):
for s in open(fic, "r"):
self.seqs.append(MySeq(s.strip().upper(), t))
self.alphabet = self.seqs[0].alphabet()
def create_motif_from_indexes(self, indexes):
global subseq
pseqs = []
res = [[0] * self.motif_size for i in range(len(self.alphabet))] # type: List[Union[List[int], Any]]
for i, ind in enumerate(indexs): # type: [int, object]
subseq = self.seqs[i][ind:(ind + self.motif_size)] # type: object
for i in range(self.motif_size):
for k in range(len(self.alphabet)):
if subseq[i] == self.alphabet[k]:
res[k][i] = res[k][i] + 1
return res
def score(self, s):
score = 0
mat = self.create_motif_from_indexes(s)
for j in range(len(mat[0])):
maxcol = mat[0][j] # type: Union[int, Any]
for i in range(1, len(mat)):
if mat[i][j] > maxcol:
maxcol = mat[i][j]
score += maxcol
return score
def score_multiplicative(self, s):
score = 1.0 # type: float
mat = self.create_motif_from_indexes(s)
for j in range(len(mat[0])):
maxcol = mat[0][j]
for i in range(1, len(mat)):
if mat[i][j] > maxcol:
maxcol = mat[i][j]
score *= maxcol
return score
def next_solution(self):
next_sol = [0] * len(s) # type: List[int]
pos = len(s) - 1 # type: int
while pos >= 0 and s[pos] == self.seq_size(pos) - self.motif_size:
pos -= 1
if pos < 0:
next_sol = None
else:
for i in range(pos):
next_sol[i] = s[i]
next_sol[pos] = s[pos] + 1
for i in range(pos + 1, len(s)):
next_sol[i] = 0
return next_sol
def exhaustive_search(self):
best_score = -1 # type: int
res = []
s = [0] * len(self.seqs)
while s is not None:
sc = self.score(s)
if sc > best_score:
best_score = sc
res = s
s = self.next_solution
return res
def next_vertex(self, s):
res = []
if len(s) < len(self.seqs):
for i in range(len(s)):
res.append(s[i])
res.append(0)
else:
pos = len(s) - 1
while pos >= 0 and s[pos] == self.seq_size(pos) - self.motif_size:
pos -= 1
if pos < 0:
res = None
else:
for i in range(pos):
res.append(s[i])
res.append(s[pos] + 1)
return res
def bypass(self, s):
res = []
pos = len(s) - 1
while pos >= 0 and s[pos] == self.seq_size(pos) - self.motif_size:
pos -= 1
if pos < 0:
res = None
else:
for i in range(pos):
res.append(s[i])
res.append(s[pos] + 1)
return res
def branch_and_bound(self):
best_score = -1
best_motif = None
size = len(self.seqs)
s = [0] * size
while s is not None:
if len(s) < size:
optimum_score = self.score(s) + (size - len(s)) * self.motif_size
if optimum_score < best_score:
s = self.bypass(s)
else:
s = self.next_vertex(s)
else:
sc = self.score(s)
if sc > best_score:
best_score = sc
best_motif = s
s = self.next_vertex(s)
return best_motif
def heuristic_consensus(self):
res = [0] * len(self.seqs) # type: List[int]
max_score = -1
partial = [0, 0]
for i in range(self.seq_size(0) - self.motif_size):
for j in range(self.seq_size(1) - self.motif_size):
partial[0] = i
partial[1] = j
sc = self.score(partial) # type: int
if sc > max_score:
max_score = sc
res[0] = i
res[1] = j
for k in range(2, len(self.seqs)):
partial = [0] * (k + 1)
for j in range(k):
partial[j] = res[j]
max_score = -1
for i in range(self.seq_size(k) - self.motif_size):
partial[k] = i
sc = self.score(partial)
if sc > max_score:
max_score = sc
res[k] = i
return res
def test():
seq1 = MySeq("ATAGAGCTGA", "dna")
seq2 = MySeq("ACGTAGATGA", "dna")
seq3 = MySeq("AAGATAGGGG", "dna")
mf = DeterministicMotifFindigg(3, [seq1, seq2, seq3])
print ("Exhaustive:")
sol = mf.exhaustive_search
print ("Solution: ", sol)
print ("Score: ", mf.score(sol))
print ("\nBranch and Bound:")
sol2 = mf.branch_and_bound
print ("Solution: ", sol2)
print ("Score:", mf.score(sol2))
print ("\nHeuristic consensus: ")
sol3 = mf.heuristic_consensus # type: list1 = List
print ("Solution: ", sol3)
print ("Score:", mf.score(sol3))
def test2():
mf = DeterministicMotifFinding()
mf.read_file("fic.txt", "dna")
print ("Branch and Bound:")
sol = mf.branch_and_bound
print ("Solution: ", sol)
print ("Score:", mf.score(sol))
print ("\nHeuristic consensus: ")
sol2 = mf.heuristic_consensus
print ("Solution: ", sol2)
print ("Score:", mf.score(sol2))
请各位解答!!!