免费注册 查看新帖 |

Chinaunix

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

据说会python的人一般精通perl [复制链接]

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-16 21:51 |只看该作者 |倒序浏览

额,不好意思啊~我一学长给了我一个程序,可我一点都不懂python,只能大概明白它的意思,如果有大神愿意感兴趣的话,能不能试着把下面的这个python程序改成兼容性更好的perl呢,坐等大神出现!!!
p.s:也在perl区提出了,求关注~
下面是代码:
  1. #usage: python cds_abs.py input_file output_file

  2. # the input file contains 2 columns
  3. #eg;
  4. #NM_000497.0     c.595+16G>T
  5. #
  6. import re
  7. import sys
  8. def usage():
  9.         print '''
  10.         python cds_abs.py input_file output_file
  11.         '''
  12. if len(sys.argv)!=3:
  13.         sys.exit(usage())
  14. import gzip
  15. fin=sys.argv[1]
  16. fout=sys.argv[2]
  17. f=open(fin,'r')
  18. o=open(fout,'w')
  19. ######
  20. def position(nm,ps):
  21.         global ore
  22.         cds=gzip.open(‘hg19.dbref.gz')
  23. #       cds=open('hg19.dbref.sort')
  24.         ps = int(ps)
  25.         while 1:
  26.                 line=cds.readline()
  27.                 if not line:
  28.                         break
  29.                 nm1=re.search("NM_(\d+)",line.split('\t')[0])
  30.                 if not nm1:
  31.                         continue
  32.                 else:
  33.                         flag=line.split('\t')[2]
  34.                         global chr
  35.                         chr = line.split('\t')[1]
  36.                 nm1=nm1.group(1)
  37.                 if nm==nm1:
  38.                         while 1:
  39.                                 pos=cds.readline()
  40.                                 if not re.match("^C\d+",pos):
  41.                                         continue
  42.                                 else:
  43.                                         cdspos=pos.split('\t')[1:3]
  44.                                         if flag == '+':
  45.                                                 ore = '+'
  46.                                                 for num in range(int(cdspos[0]),int(cdspos[1])+1):
  47.                                                         ps-=1
  48.                                                         if ps==0:
  49.                                                                 return (num,flag)
  50.                                         else:
  51.                                                 ore = '-'
  52.                                                 for num in range(int(cdspos[1]),int(cdspos[0])-1,-1):
  53.                                                         ps-=1
  54.                                                         if ps==0:
  55.                                                                 return (num,flag)
  56.         cds.close()
  57. #########
  58. def result(nm,ps):
  59.         fu = re.search("^-(\d+)",ps)
  60.         if fu:
  61. #               print fu.group(1)
  62.                 (res,flag)=position(nm,'1')
  63.                 if ore == '+':
  64.                         return (res-int(fu.group(1)))
  65.                 else:
  66.                         return (res+int(fu.group(1)))
  67.         he=re.search("(\d+)\-(\d+)",ps)
  68.         if he:
  69.                 (res,flag)=position(nm,he.group(1))
  70.                 if flag == '+':
  71.                         return (res-int(he.group(2)))
  72.                 else:
  73.                         return (res+int(he.group(2)))
  74.         else:
  75.                 hehe=re.search("(\d+)\+(\d+)",ps)
  76.                 if hehe:
  77.                         (res,flag)=position(nm,hehe.group(1))
  78.                         if flag == '+':
  79.                                 return res+int(hehe.group(2)),
  80.                         else:
  81.                                 return res-int(hehe.group(2))
  82.                 else:
  83.                         (res,flag)=position(nm,ps)
  84.                         return res
  85. #res=position("000015",20)
  86. #print res
  87. ####
  88. #res=result("014020","205")
  89. #print res
  90. ###

  91. ########### for the finding of mutation in 3'UTR
  92. def utr3(nm, ps):
  93.         global ore
  94.         ps=int(ps)
  95.         cds=gzip.open('hg19.dbref.gz')
  96.         while 1:
  97.                 line=cds.readline()
  98.                 if not line:
  99.                         break
  100.                 nm1=re.search("NM_(\d+)",line.split('\t')[0])
  101.                 if not nm1:
  102.                         continue
  103.                 nm1 = nm1.group(1)
  104.                 if nm==nm1:
  105.                         global chr
  106.                         chr = line.split('\t')[1]
  107.                         flag = line.split('\t')[2]
  108.                         ut1 = line.split('\t')[5]
  109.                         ut2 = line.split('\t')[6]
  110.                         while 1:
  111.                                 pos = cds.readline()
  112.                                 if not re.match("^3U\d+",pos):
  113.                                         continue
  114.                                 utrpos = pos.split('\t')[1:3]
  115.                                 if flag == '+':
  116.                                         ore = '+'
  117.                                         for num in range(int(utrpos[0]),int(utrpos[1])+1):
  118.                                                 ps -= 1
  119.                                                 if ps == 0:
  120.                                                         return num
  121.                                 else:
  122.                                         ore = '-'
  123.                                         for num in range(int(utrpos[1]), int(utrpos[0])-1,-1):
  124.                                                 ps -= 1
  125.                                                 if ps == 0:
  126.                                                         return num


  127. for line in f.readlines():
  128.         word=line.split('\t')[0:2]
  129.         nm=re.search("NM_(\d+)",word[0])
  130.         if nm:
  131.                 nm=nm.group(1)
  132.         else:
  133.                 o.write("-\t-\t-\t-\t-\n")
  134.                 continue
  135.         mut=re.search("c\.([0-9|\-|\+]+)_([0-9|\-|\+]+)",word[1])
  136.         if mut:
  137.                 res=str(result(nm,mut.group(1)))
  138.                 res+='..'+str(result(nm,mut.group(2)))
  139.         else:
  140.                 mut=re.search("c\.([0-9|\-|\+]+)",word[1])
  141.                 if mut:
  142.                         res=result(nm,mut.group(1))
  143.                 else:
  144.                         mut=re.search("c\.(\d+)",word[1])
  145.                         if mut:
  146.                                 res=positon(nm,mut.group(1))
  147.                         else:
  148.                                 ##### c.*111A>G : the 3'UTR region
  149.                                 utr = re.search("c\.\*(\d+)",word[1])
  150.                                 if utr:
  151.                                         res = utr3(nm, utr.group(1))
  152.                                 else:
  153.                                         res="ERR"
  154.         o.write(chr+"\t"+str(res)+'\t'+ore+"\t"+line)
  155. #               delpos=re.search("(\d+)_(\d+)",word[1])
  156. #               delpos=delpos.group(1)
  157. #               a=delpos.group(1)
  158. #               b=delpos.group(2)
  159. #               res=position(nm,a)
  160. #               res+=position(nm,b)
  161. #       print res
复制代码
据说这个程序的逻辑是比较差的,遇到错误不能跳过,效率上也不怎么好。不过实现起来非常好用~
大神们,来吧来吧,嘻嘻,谢谢

论坛徽章:
0
2 [报告]
发表于 2014-01-16 22:05 |只看该作者
本帖最后由 remyzane 于 2014-01-16 22:41 编辑

> 据说会python的人一般精通perl

楼主这句式风骚啊!我也来一个:

据说会炒菜的人一般精通洗碗

论坛徽章:
4
金牛座
日期:2013-10-11 16:12:50卯兔
日期:2014-07-31 09:17:19辰龙
日期:2014-08-08 09:28:02狮子座
日期:2014-09-14 20:32:05
3 [报告]
发表于 2014-01-17 09:42 |只看该作者
楼主感情这是拿坛友当免费劳动力使呀。

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
4 [报告]
发表于 2014-01-17 10:08 |只看该作者
回复 3# ssfjhh


    对不起

论坛徽章:
0
5 [报告]
发表于 2014-01-17 17:41 |只看该作者
貼一個這麼長的東西出來,太傷神了……

1. 沒說明這東西是做什麼用的
2. 你已經找出一些間鍵的話大家也好幫忙

直接把全部貼出來真的是幫不上忙啊!

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
6 [报告]
发表于 2014-01-17 19:03 |只看该作者
回复 5# play9091


    其实我也不知道可以做什么的,据说是在一个大文件中匹配对应的位置得到坐标。对不起各位捧场的亲了,sorry

论坛徽章:
26
2015亚冠之胡齐斯坦钢铁
日期:2015-06-25 21:40:202015亚冠之柏斯波利斯
日期:2015-08-31 17:03:192015亚冠之柏斯波利斯
日期:2015-11-07 13:10:00程序设计版块每日发帖之星
日期:2015-11-10 06:20:00每日论坛发贴之星
日期:2015-11-10 06:20:00程序设计版块每日发帖之星
日期:2015-11-26 06:20:00程序设计版块每日发帖之星
日期:2015-12-02 06:20:00黄金圣斗士
日期:2015-12-07 17:57:4615-16赛季CBA联赛之天津
日期:2015-12-23 18:34:14程序设计版块每日发帖之星
日期:2016-01-02 06:20:00程序设计版块每日发帖之星
日期:2016-01-06 06:20:00每日论坛发贴之星
日期:2016-01-06 06:20:00
7 [报告]
发表于 2014-01-17 19:32 |只看该作者
人一般精通perl ,据说会python
人一般精通python ,据说也不知道会perl

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
8 [报告]
发表于 2014-01-17 19:57 |只看该作者
回复 7# substr函数


    噢?呵呵,是吗

论坛徽章:
1
CU大牛徽章
日期:2013-03-14 14:08:55
9 [报告]
发表于 2014-01-18 00:18 |只看该作者
好丑陋的代码..............顿时无趣,不想写了

论坛徽章:
8
技术图书徽章
日期:2013-08-22 11:21:28未羊
日期:2015-01-19 22:22:25巳蛇
日期:2014-08-11 16:53:08子鼠
日期:2014-05-29 09:04:44摩羯座
日期:2014-04-11 14:15:07丑牛
日期:2014-01-24 12:41:28金牛座
日期:2013-11-21 17:38:28射手座
日期:2015-01-21 08:50:32
10 [报告]
发表于 2014-01-18 00:30 |只看该作者
回复 9# jun413947139


   大神,我作为一个菜鸟,不应受到这样的打击的。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP