このコードは前の投稿に作成されました。私はそれを私たちのデータに合わせて適応しようとしています。しかし、それはここで...動作しません。私たちのファイルの例です:Pythonは次の行に移動し、内容を保存/編集します
read:1424:2165 TGACCA/1:2165 TGACCA/2
1..100 +chr1:3033296..3033395 #just this line
1..100 -chr1:3127494..3127395
1..100 +chr1:3740372..3740471
1 concordant read:1483:2172 TGACCA/1:2172 TGACCA/2
1..100 -chr7:94887644..94887545 #and just this line
このコードは、次の操作を行う必要があります。各回線を介して
- 検索
- は「読み取り:」の文字列を認識
- 次の行に移動し、 '+ chr:number..number'のように見える部分を抽出してください。 !
したがって、「読み取り:」の後に「-chr:no..no」が複数ある場合は、最初のものだけが必要になります。
残念ながら、私はこの作業を取得する方法を見つけ出すことはできません...
import re
infile='myfile.txt'
outfile='outfile.txt'
pat1 = re.compile(r'read:')
pat2 = re.compile(r'([+-])chr([^:]+):(\d+)\.\.(\d+)')
with open(infile, mode='r') as in_f, open(outfile, mode='w') as out_f:
for line in in_f.readlines():
if '\t' not in line.rstrip():
continue
a = pat1.search(line)
if a:
m = pat2.search(line)
out_f.write(' '.join(m.groups()) + '\n')
if not a:
continue
出力は、次のようになります。
1 3033293 3033395
7 94887644 94887545
誰かが私にしてください
骨を投げます下から回答を更新しました
さて、私は、私が使用しているTim McNamaraから若干修正されたバージョンをアップロードしています。それはうまく動作しますが、出力は「CHR」の後に2桁で番号を認識しないと、ここで最後の数
with open(infile, mode='r') as in_f, open(outfile, mode='w') as out_f:
lines = [line for line in in_f.readlines()]
for i, line in enumerate(lines):
if 'read' in line:
data = lines[i+1].replace(':', '..').split('..')
try:
out_f.write('{} {} {}\n'.format(data[1][-1], data[2], data[3])) #Here I tried to remove data[3] to avoid to have "start" in the output file.. didn't work ..
except IndexError:
continue
は、このコードで得られた出力された後、それは文字列を出力します。
6 140302505 140302604 start # 'start' is a string in our data after this number
5 46605561 46605462 start # I don't understand why it grabs it thou...
5 46605423 46605522 start # I tried to modify the code to avoid this, but ... didn't work out
6 29908310 29908409 start
6 29908462 29908363 start
4 12712132 12712231 start
これら2つのエラーを修正するにはどうすればよいですか?
なぜ '\ t'がline.rstrip()にないのか正確にチェックしていますか? –