0

入力ファイルから一連の出力ファイルまで、特定の基準に一致するデータ行を解析するプログラムを Python で作成しようとしています。

このプログラムは、染色体上の遺伝子の開始位置と停止位置を含む入力ファイルを読み取ります。この入力ファイルの各行に対して、目的の染色体上の既知の SNP の位置を含む 2 番目の入力ファイルを 1 行ずつ開きます。SNP が反復される遺伝子の開始位置と停止位置の間にある場合、それは新しいファイルにコピーされます。

現状の私のプログラムの問題は、それが非効率的だということです。分析される遺伝子ごとに、プログラムは SNP データの入力ファイルの最初の行から読み取りを開始し、そのファイルよりも大きな染色体位置 (つまり、より高い位置番号を持つ) にある SNP に到達するまで停止しません。繰り返される遺伝子の停止位置。すべての遺伝子と SNP データは染色体の位置によって順序付けられているため、反復される各遺伝子について、SNP 位置データの入力ファイルの読み取りを開始するようにプログラムに「伝える」ことができれば、プログラムの速度と効率は大幅に向上します。最後の反復で読み取られた最後の行から。ファイルの最初の行からではなく。

このPythonを実行する方法はありますか? それとも、すべてのファイルを最初の行から読み取る必要がありますか?

これまでの私のコードは以下のとおりです。どんな提案でも大歓迎です。

import sys
import fileinput
import shlex
geneCoordinates = open("Gene Coordinates.txt",'r')
geneCoordinates = list(geneCoordinates)
n = (len(geneCoordinates))
nSNPsPerGene=open("C:/Users/gwilymh/Desktop/Python/SNPsPerGene/nSNPs per gene.txt", 'a')

i=0
for i in range(i,n):
    x=i
    L=shlex.shlex(geneCoordinates[x],posix=True)
    L.whitespace += ','
    L.whitespace_split = True
    L=list(L)
    output=open((("C:/Users/gwilymh/Desktop/Python/SNPsPerGene/%s.txt")%(str(L[2]))), 'a')
    geneStart=int(L[2])
    geneStop=int(L[3])
    for line in fileinput.input("SNPs.txt"):
        if not fileinput.isfirstline():
            nSNPs=0
            SNP=shlex.shlex(line,posix=True)
            SNP.whitespace += '\t'
            SNP.whitespace_split = True
            SNP=list(SNP)
            SNPlocation=int(SNP[0])
            if SNPlocation < geneStart:
                continue
            if SNPlocation >= geneStart:
                if SNPlocation <= geneStop:
                    nSNPs=nSNPs+1
                    output.write(str(SNP))
                    output.write("\n")
            else: break
    nSNPsPerGene.write(("%s\t%s")%s(str(L[2]),nSNPs))
4

1 に答える 1

1

2 番目のファイル内の位置を追跡するには、(ループの外側のスコープで) イテレータを使用するだけです。次のようになります。

import shlex
geneCoordinates = open("Gene Coordinates.txt",'r')
geneCoordinates = list(geneCoordinates)
n = (len(geneCoordinates))
nSNPsPerGene=open("C:/Users/gwilymh/Desktop/Python/SNPsPerGene/nSNPs per gene.txt", 'a')

i=0

#NEW CODE - 2 lines added.  By opening a file iterator outside of the loop, we can remember our position in it
SNP_file = open("SNPs.txt")
SNP_file.readline() #chomp up the first line, so we don't have to constantly check we're not at the beginning
#end new code.


for i in range(i,n):

   x=i
   L=shlex.shlex(geneCoordinates[x],posix=True)
   L.whitespace += ','
   L.whitespace_split = True
   L=list(L)
   output=open((("C:/Users/gwilymh/Desktop/Python/SNPsPerGene/%s.txt")%(str(L[2]))), 'a')
   geneStart=int(L[2])
   geneStop=int(L[3])

   #NEW CODE - deleted 2 lines, added 4
   #loop until break
   While 1:
      line = SNP_file.readLine()
      if not line: #exit loop if end of file reached
         break
      #end new code - the rest of your loop should behave normally

      nSNPs=0
      SNP=shlex.shlex(line,posix=True)
      SNP.whitespace += '\t'
      SNP.whitespace_split = True
      SNP=list(SNP)
      SNPlocation=int(SNP[0])
      if SNPlocation < geneStart:
          continue
      #NEW CODE - 1 line changed
      else: #if SNPlocation >= geneStart: 
      #logic dictates that if SNPLocation is not < geneStart, then it MUST be >= genestart. so ELSE is sufficient
          if SNPlocation <= geneStop:
              nSNPs=nSNPs+1
              output.write(str(SNP))
              output.write("\n")
              #NEW CODE 1 line added- need to exit this loop once we have found a match.
              #NOTE - your old code would return the LAST match. new code returns the FIRST match.
              #assuming there is only 1 match this won't matter... but I'm not sure if that assumption is true.
              break
      #NEW CODE - 1 line deleted
      #else: break else nolonger required. there are only two possible options.

      j = j+1
   nSNPsPerGene.write(("%s\t%s")%s(str(L[2]),nSNPs))
于 2013-02-13T02:10:52.960 に答える