python 中实现统计基因缺失率

 

001、

[b20223040323@admin1 test2]$ ls
dat.txt  test.py
[b20223040323@admin1 test2]$ cat dat.txt    ## 测试数据
gene1   gene2   gene3
0       1       0
0       1       1
1       0       0
0       1       1
[b20223040323@admin1 test2]$ cat test.py     ## python程序
## calculate the absent percent.

in_file = open("dat.txt", "r")
out_file = open("result.txt", "w")
lines = in_file.readlines()
temp = lines[0].strip().split("\t")
list1 = [0] * len(temp)
lines = lines[1::]

for i in lines:
        i = i.strip().split("\t")
        for j in range(len(temp)):
                if int(i[j]) == 0:
                        list1[j] = list1[j] + 1

for i in range(len(list1)):
        list1[i] = 1.0*list1[i]/len(temp)
        out_file.write(str(1.0*list1[i]/len(temp)) + " ")

out_file.write("\n")

in_file.close()
out_file.close()
[b20223040323@admin1 test2]$ python test.py    ## 执行程序
[b20223040323@admin1 test2]$ ls
dat.txt  result.txt  test.py
[b20223040323@admin1 test2]$ cat result.txt     ## 运行结果
0.333333333333 0.111111111111 0.222222222222

 

posted @ 2023-02-26 21:10  小鲨鱼2018  阅读(39)  评论(0编辑  收藏  举报