fi中不同元素的Python索引

2024-05-23 16:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有样品输入文件.txt地址:

chr15   74436458    74466677    pi-1700016M24Rik.1  -
chr17   79734018    79754230    pi-Cdc42ep3.1   -
chr3    124103907   124128909   pi-1700006A11Rik.1  -
chr5    102261978   102280532   pi-Wdfy3.1  -
chr6    85061409    85076088    pi-Gm5878.1 -
chr9    51573456    51661164    pi-Arhgap20.1   +
chr10   127114107   127132221   pi-Tmem194.1    +
chr11   103286577   103315010   11-qE1-9443.1   +
chr11   107855325   107859037   11-qE1-3997.1   +
chr11   108278889   108286739   11-qE1-252.1    -
chr12   99620581    99658258    12-qE-23911.1   -
chr12   99658453    99692927    12-qE-7089.1    +
chr13   21595489    21598393    13-qA3.1-213.1  -
chr13   24997468    25026901    13-qA3.1-355.1  +

第1列=染色体数目

Column2=开始

第3列=结束

Column4=基因名称

第5列=方向(或+或-)

1.)我需要提取具有相同染色体数目的品系,它们的起始位点有200个方向相反的差异(一个是正/负)。你知道吗

我不知道如何接近它。到目前为止,我已经列出了两个起始站点列表(一个是正方向的,一个是负方向的)。但我不知道如何用python实现这一点。任何建议都将不胜感激!你知道吗

#!/usr/bin/env python
import sys
import csv

f=open('Samplefileinput.txt', 'r')

list1=[] #start sites with + direction
list2=[] #start sites with - direction
for line in f:
  fields=line.strip().split()
  chromosome =fields[0]
  start=int(fields[1])
  end=int(fields[2])
  gene=fields[3]
  direction = fields[4] #plus/minus
  if '+' in direction:
     list1.append(start)
  if '-' in direction:
     list2.append(start)

但这对我一点帮助都没有。不知该如何继续。非常感谢。你知道吗

Ideal Output:

  ChrX   start_number1            gene_name1
  ChrX   start_number1+/-200      gene_name2
  ChrY   start_number2            gene_name3
  ChrZ   start_number2+/-200      gene_name4

前两个读数的方向不同。 最后两个也是同样的情况。你知道吗

我不确定会有多少行输出。非常感谢!你知道吗


Tags: intxtfieldspi方向startgene染色体
3条回答

不确定你是否想用多个其他基因复制符合要求的基因。itertools模块对于这些类型的分组和置换操作来说真是太棒了:

import itertools as it

def getrecords(f):
    for line in open(f):
        yield line.strip().split()

key = lambda x: x[0]
for i,rec in it.groupby(sorted(getrecords('inputfile.txt'), key=key), key=key):
    for c0,c1 in it.combinations(rec, 2): 
        if (c0[4] != c1[4]) and (abs(int(c0[1])-int(c1[1])) > 200):
            print '%s\t%s\t%s' % (c0[0], c0[1], c0[3])
            print '%s\t%s\t%s' % (c1[0], c1[1], c1[3])

输出:

chr11   103286577   11-qE1-9443.1
chr11   108278889   11-qE1-252.1
chr11   107855325   11-qE1-3997.1
chr11   108278889   11-qE1-252.1
chr12   99620581    12-qE-23911.1
chr12   99658453    12-qE-7089.1
chr13   21595489    13-qA3.1-213.1
chr13   24997468    13-qA3.1-355.1

从第一列的每个项目中删除Chr (你可以穿过每一条线

  [3:]

每行中,除了前3个字符以外的所有字符。 然后按第一列对行进行排序,就差不多完成了?你知道吗

您应该能够遍历每个列表并提取匹配项,但我认为您希望将其他属性提取到列表中,以便能够匹配它们。你知道吗

if '+' in direction:
   list1.append(fields)
if '-' in direction:
   list2.append(fields)

从这里你可以检查是否匹配。你知道吗

matchlist=[]
for pos in list1:
  for neg in list2:
    if pos[0]==neg[0] and abs(pos[1]-neg[1])==200:
      matchlist.append([pos[3], neg[3]])

这应该可以为您创建一个匹配项列表。你知道吗

相关问题 更多 >