我有一个函数,它为我的数据计算一个“非此即彼”类型的列表。你知道吗
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
目标是编写一个带有坐标的输出文本文件。你知道吗
with open(keyword+"_plus.txt", "w") as thefile:
for item in exon_start_plus:
thefile.write("{}, ".format(item))
或者如果关键字导致负股:
with open(keyword+"_minus.txt", "w") as thefile:
for item in exon_start_minus:
thefile.write("{}, ".format(item))
我试着把这些写文件放在代码中,但是返回函数不能给出完整的列表,结果每次只写一个坐标。我把它们放在最后,但这会导致空文件和空字符串-我想把它作为一个函数,并让它确定关键字(即一个基因ID)是否为正/负链给定了坐标(我有一个巨大的数据文件,其中包含此数据,重点是不要手动扫描ID,看看它们是否正/负DNA)股)。你知道吗
谢谢你!你知道吗
编辑(示例数据,必须删除一些列,因此我也编辑了代码):
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
因此,如果我使用ENSPFOG0000001作为关键字,那么脚本应该运行并确定链是正的,收集列表中的起始坐标,然后输出一个只有这些坐标的文件。文件将具有关键字+“_plus.txt文件”追加。如果它是ENSPFOG0000002,那么它将收集负串坐标,并创建一个文件,其中关键字+“_减.txt“已创建。你知道吗
空列表的计算结果为False:
因此,您可以测试空列表,并根据需要采取措施
要从函数中检索两个列表:
相关问题 更多 >
编程相关推荐