[英]How do obtain results from two loops within the same function?
I have a function that calculates an "either or" type of list for my data. 我有一个函数可以为我的数据计算列表的“或”类型。
keyword = sys.argv[1] # a name from the Name column
def exon_coords():
exon_start_plus = [] # Plus strand coordinates
exon_start_minus = [] # Minus strand coordinates
for line in csv.reader(sys.stdin, csv.excel_tab):
if len(line) >= 1:
if re.search(keyword, str(line)): # If arg keyword exists in file
if line[3] == "-": # If the DNA strand is a minus strand
chrompos = line[0] + ";" # Get the chromosome position
exon_start_minus.append(chrompos+line[1]) # Full exon position
else: # all other lines are plus strands
chrompos = line[0] + ";"
exon_start_plus.append(chrompos+line[1])
return exon_start_minus, exon_start_plus #Return lists
Goal is to then write an output text file with the coordinates. 目的是编写一个带有坐标的输出文本文件。
with open(keyword+"_plus.txt", "w") as thefile:
for item in exon_start_plus:
thefile.write("{}, ".format(item))
OR if the keyword resulted in MINUS strands: 或者,如果关键字产生了MINUS股:
with open(keyword+"_minus.txt", "w") as thefile:
for item in exon_start_minus:
thefile.write("{}, ".format(item))
I tried putting these write files within the code but then the return functions just would not give me the full list, and I end up only writing one coordinate every time. 我尝试将这些写文件放入代码中,但是返回函数将无法提供完整列表,而我每次只能写一个坐标。 I put them at the end, but this results in empty files and empty strings - I would like to keep this as one function and have it determine if the keyword (ie a gene ID) has coordinates given for plus/minus strand (I have a gigantic data file that contains this data and the point is to not manually scan the IDs and see if they are plus/minus DNA strands).
我将它们放在最后,但是这导致空文件和空字符串-我想将其保留为一个函数,并让它确定关键字(即基因ID)是否具有给定正负链的坐标(我有包含此数据的巨大数据文件,目的是不要手动扫描ID,看看它们是否是正负DNA链)。
Thank you! 谢谢!
EDIT (sample data, had to remove some columns so I edited the code as well): 编辑(示例数据,必须删除一些列,所以我也编辑了代码):
Position Start End Strand Overhang Name
1 3798630 3798861 + . ENSPFOG0000001
1 3799259 3799404 + . ENSPFOG0000001
1 3809992 3810195 + . ENSPFOG0000001
1 3810582 3810729 + . ENSPFOG0000001
2 4084800 4084866 - . ENSPFOG0000002
2 4084466 4084566 - . ENSPFOG0000002
2 4084089 4084179 - . ENSPFOG0000002
So if I use ENSPFOG0000001 as my keyword, then the script should run and determine that the strands are plus, collect the start coordinates in a list and then output a file that just has the coordinates. 因此,如果我将ENSPFOG0000001用作关键字,则脚本应运行并确定线束为正,将起始坐标收集在列表中,然后输出仅包含坐标的文件。 The file would have keyword+"_plus.txt" appended.
该文件将附加关键字+“ _ plus.txt”。 If it was ENSPFOG0000002, then it would collect the minus strand coordinates, and create a file where keyword+"_minus.txt" is created.
如果是ENSPFOG0000002,则它将收集负链坐标,并创建一个文件,其中创建了关键字+“ _ minus.txt”。
An empty list evaluates to False: 一个空列表的结果为False:
>>> exon_start_minus, exon_start_plus = [], []
>>> bool(exon_start_minus), bool(exon_start_plus)
(False, False)
>>> exon_start_minus, exon_start_plus = [1], []
>>> bool(exon_start_minus), bool(exon_start_plus)
(True, False)
>>> exon_start_minus, exon_start_plus = [1], [1]
>>> bool(exon_start_minus), bool(exon_start_plus)
(True, True)
>>> exon_start_minus, exon_start_plus = [], [1]
>>> bool(exon_start_minus), bool(exon_start_plus)
(False, True)
So you can test for an empty list and take action as appropriate 因此,您可以测试一个空列表并采取适当的措施
>>> if exon_start_plus:
print('!!!')
!!!
>>> if exon_start_minus:
print('!!!')
>>>
To retrieve both lists from the function: 要从函数中检索两个列表:
exon_start_minus, exon_start_plus = exon_coords()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.