1

これは私の最初の python スクリプトです。私のデータは次のようになります。

Position ind1 ind2 ind3 ind4 ind5 ind5 ind7 ind8
 0        C    A     C   A    A    A    A     A
 1        C    A     C   C    C    A    A     A

ただし、列数が異なる場合があり、何千もの行があります。

私が必要とすることを行う私のスクリプトは、このファイルを 1 行ずつ読み取り、すべての位置 (POS) における個人 (以下、母集団) の組み合わせについて A と C の頻度を計算します。たとえば、母集団 1 (ind1、ind2、ind3、ind4) の位置 0 の A の頻度。母集団 2 (ind5、ind6、ind7、ind8) の位置 0 での A の頻度、次に POS 1、2、3 についても同じ ....

これを行うには、次のコードでスクリプトの列 (母集団) の組み合わせを定義します。

alleles1 = alleles[1:5]
alleles2 = alleles[5:]

ただし、列が 9 つ以上あり、列の組み合わせが異なる場合は、対立遺伝子* と残りのスクリプトを後で変更する必要があります。

ユーザーが人口の数を定義し、どの列がどの人口に対応するかを指定するように、プログラムをよりインタラクティブにしたいと考えています。

私がこれまでに持っているコード:

#ask for the number of populations
try:
    num_pop = int(raw_input("How many populations do you have? > "))
except ValueError:
    print "In is not an integer! \nThe program exits...\n "
#ask for individuals in population
ind_pop = {}
for i in range(num_pop):
    i += 1
    ind_input = str(raw_input("Type column numbers of population %i > " % i))
    ind_pop[i] = re.findall(r'[^,;\s]+', ind_input)

列 3、5、6 が母集団 1 で、列 2、5 が母集団 2 である 2 つの母集団がある場合、次のように動作します。

> How many populations do you have? > 2
> Type column numbers of population 1 > 3, 5, 6  
> Type column numbers of population 2 > 2, 4 

入力はディクショナリに格納されます。

{1: ['3', '5', '6'], 2: ['2', '4']}

問題は、この入力から対立遺伝子の定義に進む方法です。出力は次のようになります。

allele1 =  [allele[3], allele[5], allele[6]]
allele2 =  [allele[2], allele[4]]

必要に応じて、残りのコードの主要部分を以下に示します。

with open('test_file.txt') as datafile:
  next(datafile)
  for line in datafile:
    words = line.split() #splits string into the list of words 
    chr_pos = words[0:2] #select column chromosome and position
    alleles = words[2:] # this and next separates alleles for populations

    alleles1 = alleles[0:4]
    alleles2 = alleles[4:8]
    alleles3 = alleles[8:12]
    alleles4 = alleles[12:16]

    counter1=collections.Counter(alleles1)
    counter2=collections.Counter(alleles1)
    counter3=collections.Counter(alleles1)
    counter4=collections.Counter(alleles1)
#### the rest of the code and some filters within the part above were spiked
4

3 に答える 3

0

提案していただきありがとうございます。それらのいくつかは役に立ちました。方向転換が必要だと感じています。リストのリストを引き続き使用します。

pop_alleles = []
for key in ind_pop.keys():
  pop_alleles.append([alleles[el] for el in ind_pop[key]])
于 2013-04-25T22:21:24.120 に答える