Python类变量的增量高于迭代次数
我在这一点上有点困惑,我确信这是由于我缺乏Python OOP的经验和类的工作方式,或者新的字符串格式是如何工作的。张贴在这里,以便其他人可以参考,如果他们有类似的问题 问题是:我使用下面的类作为循环中的生成器来读取文件并逐行解析它。每个感兴趣的行将Python类变量的增量高于迭代次数,python,Python,我在这一点上有点困惑,我确信这是由于我缺乏Python OOP的经验和类的工作方式,或者新的字符串格式是如何工作的。张贴在这里,以便其他人可以参考,如果他们有类似的问题 问题是:我使用下面的类作为循环中的生成器来读取文件并逐行解析它。每个感兴趣的行将self.reads\u total变量增加1,如果该行满足某些条件,则将self.reads\u mapping变量增加1。最后(就在调用next()方法的StopIteration()之前),我将这两个值输出到stdout Reads proce
self.reads\u total
变量增加1,如果该行满足某些条件,则将self.reads\u mapping
变量增加1。最后(就在调用next()
方法的StopIteration()
之前),我将这两个值输出到stdout
Reads processed:5220000029016,在52263262个总读取量中映射出两个配偶
此输出是在迭代结束前由以下代码行生成的:
print(“{0}和两个配偶从{1}个总读取数中映射出来\n”。格式(self.reads\u映射,self.reads\u-total))
self.reads_total
正在输出正确的迭代次数,但是self.reads_映射
没有
在SamParser(infle)循环中为x调用的类对象:
class SamParser:
"""This object takes as input a SAM file path and constructs an iterable that outputs
sequence information. Only one line will be held in memory at a time using this method.
"""
def __init__(self, filepath):
"""
constructor
@param filepath: filepath to the input raw SAM file.
"""
if os.path.exists(filepath): # if file is a file, read from the file
self.sam_file = str(filepath)
self.stdin = False
elif not sys.stdin.isatty(): # else read from standard in
self.stdin = True
else:
raise ValueError("Parameter filepath must be a SAM file")
self.current_line = None
self.reads_mapping = 0
self.reads_total = 0
# Allowable bitflags for SAM file -> reads with both mates mapping, regardless of other flags
self.true_flags = (99, 147, 83, 163, 67, 131, 115, 179, 81, 161, 97, 145, 65, 129, 113, 177)
def __iter__(self):
return self
def _iterate(self):
# Skip all leading whitespace
while True:
if self.stdin:
sam_line = sys.stdin.readline() # read from stdin
else:
sam_line = self.sam_file.readline() # read from file
if not sam_line:
return # End of file
if sam_line.startswith("@SQ"): # these lines contain refseq length information
temp = sam_line.split()
return temp[1][3:], temp[2][3:]
elif sam_line[0] != "@": # these lines are the actual reads
self.reads_total += 1
if self.reads_total % 100000 == 0: # update the counter on stdout every 100000 reads
sys.stdout.write("\rReads processed: {}".format(self.reads_total))
sys.stdout.flush()
temp = sam_line.split()
if int(temp[1]) in self.true_flags and temp[2] is not "*" and int(temp[3]) is not 0:
self.reads_mapping += 1
return temp[1], temp[2], temp[3], temp[9]
self.sam_file.close() # catch all in case this line is reached
assert False, "Should not reach this line"
def next(self):
if not self.stdin and type(self.sam_file) is str: # only open file here if sam_file is a str and not file
self.sam_file = open(self.sam_file, "r")
value = self._iterate()
if not value: # close file on EOF
if not self.stdin:
self.sam_file.close()
print("{0} with both mates mapped out of {1} total reads\n".format(self.reads_mapping, self.reads_total))
raise StopIteration()
else:
return value
如果您需要更多上下文,可以在此处找到完整的脚本:简单的字符串格式错误:该脚本旨在更新stdout上的行数,并且我没有在下一行的开头包含换行符,因此它覆盖了最后一行输出,其中包括前一行的大量读取计数ue输出应为:
Reads processed: 52200000
29016 with both mates mapped out of 52263262 total reads
Plots generated: 310
在任何情况下,如果将来有人偶然发现这个问题,该脚本对于SAM解析都是有用的。不要犯同样的错误。我建议您尝试使用较小的文件生成一个可复制的示例。
194在206个总读取数中映射出两个副本。
该脚本在206行的小测试文件上正确执行,这意味着它有问题我忽略了一些逻辑/文件格式的警告。谢谢。我找到后会报告的。