0

fasta_ids と frags_by_density の 2 つの配列があります。両方とも ≈1300 文字列の同じセットを含んでいます。fasta_ids は、['frag1', 'frag2', 'frag3'...] のように数値順に並べられます。frags_by_density には、同じ文字列が異なる順序で並べられています。

frag_by_density が順序付けられる方法は、質問とは無関係です (ただし、バイオインフォマティシャンにとって、「frags」は snp 密度によって順序付けられたコンティグです)。

私がやりたいことは、fasta_ids の各文字列を含む frag_by_density 配列のインデックスを見つけることです。これらの位置 (インデックス) の新しい配列で終わりたいと思います。これは、fasta_ids 配列と同じ順序になります。

たとえば、'frag' 文字列の順序が fasta_ids 配列と frags_by_density 配列の両方で同じである場合、出力配列は [0, 1, 2, 3...] になります。この例では、出力配列 (2) のインデックス 2 の値は、fasta_ids ('frag3') のインデックス 2 の値に対応します。したがって、これから、'frag3' 文字列がfrags_by_density のインデックス 2 にあると推測できます。 .

以下は私が思いついたコードです。現時点では、無限ループであると思われるものに引っかかっています。各部分が何をすべきかについて注釈を付けました。

x = 0 #the value of x will represent the position (index) in the density array
position_each_frag_id_in_d = [] #want to get positions of the values in frag_ids in frags_by_density
iteration = []
fasta_ids.each do |i|
    if frags_by_density[x] == i
        position_each_frag_id_in_d << x #if the value at position x matches the value at i, add it to the new array
        iteration << i
    else
        until frags_by_density[x] == i #otherwise increment x until they do match, and add the position
            x +=1
        end
        position_each_frag_id_in_d << x
        iteration << i
    end
    x = iteration.length # x should be incremented, however I cannot simply do: x += 1, as x may have been incremented by the until loop
end
puts position_each_frag_id_in_d

これは、言葉にするのが非常に難しい質問でした。もっと簡単な解決策があることを願っています。または、少なくとも誰かが私が始めたものを変更できることを願っています。

更新:コードにあるように、配列 fasta_ids の名前を変更しました (混乱があれば申し訳ありません) fasta_id = frag_id

4

1 に答える 1

0

最適化されていないバージョン。array.index(x)配列内の x のインデックスを返すか、見つからない場合は nil を返します。compact次に、配列から nil 要素を削除します。

position_of_frag_id_in_d = frag_ids.map{|x| frag_by_density.index(x)}.compact

于 2013-09-26T15:17:09.183 に答える