免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10970 | 回复: 11
打印 上一主题 下一主题

python for 循环问题求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-09 12:45 |只看该作者 |倒序浏览
我这里一共有3个文件
1是样本编号
2是每个位点信息
3是样本的位点突变信息

不是每个样本都有这个位点突变信息,写了个for循环,有该位点突变的,从3中提取突变信息,没有的从2中写入原来未突变位点信息,要求每个1中样本,要有2中每个位点的信息,即最后需要得到1中的行数x2中的列数的文件,但是我最后for循环只有一行。

文件图和代码如下:
  1. import xlwt

  2. fname=open("1.txt","r")
  3. flocation=open("2.txt","r")
  4. fsnp=open("3","r")

  5. book=xlwt.Workbook(encoding="utf-8",style_compression=0)
  6. sheet=book.add_sheet("snp",cell_overwrite_ok=True)

  7. n=0
  8. for names in fname.readlines():
  9.     row=0
  10.     name=names.strip("\n")
  11.     row=row+1
  12.     n=n+1
  13.     for locations in flocation.readlines():
  14.         col=5
  15.         location=locations.split("\t")
  16.         col=col+1
  17.         snp1=location[0]
  18.         position=str(location[1])
  19.         ref=location[2]+" "+location[2]
  20.         for snps in fsnp:
  21.             snps=snps.split("\t")
  22.             sample_name=snps[0]
  23.             sample_snp=snps[2]
  24.             sample_position=str(snps[3])
  25.             genotype=snps[5]
  26.             allele=genotype[0]+" "+genotype[1]
  27.             if name==sample_name and position in sample_position:
  28.                 sheet.write(row,col,allele)
  29.             else:
  30.                 #sheet.write(row,col,ref)
  31.          
  32. book.save("result.xls")

  33. print "done"
复制代码

1.png (1.67 KB, 下载次数: 183)

文件1

文件1

2.png (4.12 KB, 下载次数: 187)

文件2

文件2

3.png (8.13 KB, 下载次数: 179)

文件3

文件3

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
2 [报告]
发表于 2014-06-09 16:23 |只看该作者
回复 1# yusmile0618
能给个期望的结果的例子说明嘛?
怀疑是你的row/col的赋值放错地方了,另外,同时你好像没有在最后的if/else处做对应的rol/col的增加或者减少操作。

论坛徽章:
0
3 [报告]
发表于 2014-06-09 16:36 |只看该作者
你好,谢谢回答
我给一个期望得到结果的图吧

回复 2# icymirror


   

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
4 [报告]
发表于 2014-06-09 16:40 |只看该作者
太长, 没看懂, 放弃.

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
5 [报告]
发表于 2014-06-09 17:39 |只看该作者
回复 3# yusmile0618
大体理解了,(还不是非常确定)
至少有这几个地方需要修改:
1. 12行的row=0需要放到主循环外,比如和11行交换位置
2. 17行的col=5也是同样问题,需要和16行交换位置
3. 另外:在循环体16、23结束之后,需要加入:flocation.seek(0), fsnp.seek(0),来确定对于文件2、3中的数据每次都是从头开始遍历的,而不是一遍到底就只看文件结束符号了。
(注意缩进,要和循环体开头的for是一样的缩进,而不是和循环体内容一样的缩进。)
现在只是简单找出来这几个,先改下试试看。

论坛徽章:
0
6 [报告]
发表于 2014-06-09 21:28 |只看该作者
你好,已将row和col放到主循环外
然后flocation.seek(0)因为下面接了另一个for循环,将它与for ... in flocation 对齐会报错。
将fsnp.seek(0)添加并缩进对齐,程序就进入死循环了

如果不加两个seek语句,还是只有一行结果。

请你再帮我分析分析

回复 5# icymirror


   

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
7 [报告]
发表于 2014-06-11 14:11 |只看该作者
回复 6# yusmile0618
不确定我们说的对齐是不是一致,把这边修改的给贴一下:
  1. import xlwt

  2. fname=open("1.txt","r")
  3. flocation=open("2.txt","r")
  4. fsnp=open("3.txt","r")

  5. book=xlwt.Workbook(encoding="utf-8",style_compression=0)
  6. sheet=book.add_sheet("snp",cell_overwrite_ok=True)

  7. n=0
  8. row=0
  9. for names in fname.readlines():
  10.     name=names.strip("\n")
  11.     row=row+1
  12.     n=n+1
  13.     col=5
  14.     for locations in flocation.readlines():
  15.         location=locations.split("\t")
  16.         col=col+1
  17.         snp1=location[0]
  18.         position=str(location[1])
  19.         ref=location[2]+" "+location[2]
  20.         for snps in fsnp:
  21.             snps=snps.split("\t")
  22.             sample_name=snps[0]
  23.             sample_snp=snps[2]
  24.             sample_position=str(snps[3])
  25.             genotype=snps[5]
  26.             allele=genotype[0]+" "+genotype[1]
  27.             if name==sample_name and position in sample_position:
  28.                 print "%d, %d, %s"%(row, col, allele)
  29.                 sheet.write(row,col,allele)
  30.             else:
  31.                 pass
  32.                 #sheet.write(row,col,ref)
  33.         fsnp.seek(0)
  34.     flocation.seek(0)

  35. book.save("result.xls")
复制代码

论坛徽章:
0
8 [报告]
发表于 2014-06-11 17:15 |只看该作者
你好,非常感谢你给我修改的代码,
这样可以打印出所以在3里面的那些内容,但是不能打印出不在3里面的,就是为什么这里的else,当没有在3当中时,不可以打印2中第三列呢?
回复 7# icymirror


   

论坛徽章:
0
9 [报告]
发表于 2014-06-11 20:00 |只看该作者
本帖最后由 Hadron74 于 2014-06-11 20:02 编辑

程序不是这么写的。

建议:
你用Excel存得下,数据量很小。建议把2,3文件存到内存中,用字典的方式,采用输入,输出分开的方式,更简洁,且容易调试。
这是我的代码,由于没有数据,没有调试。希望有帮助。
  1. import xlwt


  2. all_snps = {}
  3. for snp in open("3.txt"):
  4.     snps = snp.rstrip().split('\t')
  5.     sample_name = snps[0]
  6.     sample_position = snps[3]
  7.     allele = " ".join(snps[5])
  8.     if sample_name not in all_snps:
  9.         all_snps[sample_name] = []
  10.     all_snps[sample_name].append((sample_position,allele))     # store alleles in a dictionary


  11. locations = {}
  12. locationsKeys = []
  13. for location in open("2.txt"):
  14.     loc = location.rstrip().split('\t')
  15.     position = loc[1]
  16.     ref=" ".join(loc[2]*2)
  17.     locations[position] = ref                         # store default alleles in locations
  18.     locationsKeys.append(position)                    # record the locations in order

  19. book=xlwt.Workbook(encoding="utf-8",style_compression=0)
  20. sheet=book.add_sheet("snp",cell_overwrite_ok=True)

  21. row = 0
  22. for sample in open("3.txt"):
  23.     name = sample.strip()
  24.     row += 1
  25.     alleles = dict(locations)  # set a new dict with reference alleles for initial

  26.    # here you can add some filter for number of snp by eg. if len(all_snps[name]) < 2 : continue

  27.     for sample_postion,allele in all_snps[name]:
  28.         alleles[sample_postion] = allele   # update new alleles
  29.     # output to Excel

  30.     col = 5
  31.     for loc in locationsKeys:           # write alleles according to the order
  32.         col += 1
  33.         sheet.write(row,col,alleles[loc]) # get allele from alleles

  34. book.save("result.xls")
复制代码

论坛徽章:
0
10 [报告]
发表于 2014-06-11 20:32 |只看该作者

非常感谢您的指导,程序可以成功运行。
因为刚开始学python,还不太会用字典,所以还应该再努力好好学习下。
再次感谢!
回复 9# Hadron74


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP