0

クラスのイベントをグループ化するために、ファイルのネストされた辞書を作成しました。必要なクラスの数と最終的な値の数をキー番号で数えたかったのです。これは私がこれまでに持っているコードです:

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 で切り捨てて、辞書でクラスを作成することで機能します。最初に丸められた開始点でグループ化され、次に丸められた長さ変数でグループ化されるため、ネストされています。

コードの最後で、クラスの数と、値の合計数を数えようとします。ただし、これにより誤った出力が得られます。入力ファイルの行よりもクラスが多くなります。これを解決する方法はありますか?

4

1 に答える 1

0

どの合計数が必要かはわかりませんが、次のいずれかが探している可能性があります。

rounded_start_count = 0
rounded_length_count = 0
rounded_length_value_count = 0

for k1, v1 in split_region.items():
    print k1 + ": " + str(len(v1))
    rounded_start_count += len(v1)
    for k2, v2 in v1.items():
        rounded_length_count += len(v2)
        rounded_length_value_count += len(v2.values())

print ""

print "chrm count:                 ", len(split_region.keys())
print "Rounded start count:        ", rounded_start_count
print "Rounded length count:       ", rounded_length_count
print "Rounded length value count: ", rounded_length_count

これは for ループの後と外側に配置されます。これにより、サンプル データの次の出力が出力されます。

chr6: 2
chr2: 1
chr1: 2
chr9: 1
chrY: 1
chr20: 1
chrM: 1
chr18: 1

chrm count:                  8
Rounded start count:         10
Rounded length count:        10
Rounded length value count:  10
于 2013-09-23T00:19:43.930 に答える