クラスのイベントをグループ化するために、ファイルのネストされた辞書を作成しました。必要なクラスの数と最終的な値の数をキー番号で数えたかったのです。これは私がこれまでに持っているコードです:
infile = open('ALL','r')
def round_down(num):
return num - (num%100)
count = 0
a = []
split_region = {}
lengths = []
for region in infile:
#print region
(cov,chrm,pos,end,leng) = region.split()
start = int(pos)#-1#-int(leng) ## loosen conditions about break points
end = int(end)
lengths = int(leng)
coverage=int(cov)
rounded_start=round_down(start)
rounded_length=round_down(lengths)
if not (chrm in split_region):
split_region[chrm]={}
if not (rounded_start in split_region[chrm]):
split_region[chrm][rounded_start]={}
if not (rounded_length in split_region[chrm][rounded_start]):
split_region[chrm][rounded_start][rounded_length]= []
split_region[chrm][rounded_start][rounded_length].append({'start':start,'length':lengths,'cov':coverage})
for k,v in split_region[chrm][rounded_start].items():
print len(v),k,v
a.append(len(v))
count +=1
print count
print sum(a)
ファイルは次のようにフォーマットされています。
5732 chrM 1 16572 16571
804 chr6 58773612 58780166 6554
722 chr1 142535435 142538993 3558
448 chrY 13447747 13451695 3948
372 chr9 68422753 68423813 1060
327 chr2 133017433 133018716 1283
302 chr18 107858 109884 2026
256 chr20 29638813 29641416 2603
206 chr6 57423087 57429121 6034
204 chr1 142537237 142538991 1754
したがって、基本的には、数値を 100 で切り捨てて、辞書でクラスを作成することで機能します。最初に丸められた開始点でグループ化され、次に丸められた長さ変数でグループ化されるため、ネストされています。
コードの最後で、クラスの数と、値の合計数を数えようとします。ただし、これにより誤った出力が得られます。入力ファイルの行よりもクラスが多くなります。これを解決する方法はありますか?