入力データサイズを拡張すると、ソート機能が機能しません。

入力データサイズを拡張すると、ソート機能が機能しません。

それで一週間前に私は質問しました。 > ここ 、問題はソートについてです。このコードを使用してファイルを並べ替えて生成する場合:

tail -n +2 File1.txt |
  split -l1 --filter='
    { 
      head -n 1 File2.txt &&
        cat <(tail -n +2 File2.txt) - |
        sort -n -r -k4 
   ; } > "$FILE"'

例で使用しているファイルでは機能しますが、より大きな物理ファイルで使用するとソートが機能しないようです。

LC_ALL = Cを使用する前にこの問題を解決しましたが、一度だけ動作しているようで、実際の問題が何であるかわかりません。その列を具体的に印刷して並べ替えると機能しますが、このコードでは機能しません。

一度に処理することが多すぎるからではないでしょうか?異なるデータに注釈付きの151列があり、43列と151列だけを並べ替えたいのですが、新しいソートファイルが必要です。助けてください。

ベストアンサー1

はい、列は位置、つまり行の先頭からの文字数によって定義されますので、前のデータ例の形式を使用します。残念ながら、見つかったように、これらの列のいずれかが空の場合、使用しようとしたツールはその列をまったく列としてカウントしません。

col 1   col 2       col 3       col 4   col 5 
chr3    31663820    31663820    0.713   3

col 1               col 2       col 3   col 4 
chr3                33093371    3.753   4

わかりやすく、Pythonでクイックスクリプトを書いた。コマンドラインに2つのファイルがある場合、行のハードコーディングされた部分に基づいてソートされますが、これは明らかに変更される可能性があります。現在は、入力した各フィールドのリストを一度に並べ替えます。しかしながら、比較のために、単一の浮動小数点の代わりに所望の順序で浮動小数点タプルを返すようにソート関数を更新することも同様に可能である。

#! /usr/bin/python

# own_sort.py
# ./own_sort.py 'unique values file' 'duplicate values file'

# allows access to command line arguments.
import sys


# this is just to get some example inputs
test_line = 'chr3    39597927    39597927    8.721   5'
phylop_col = (test_line.find('8.721'), test_line.find('8.721')+7)


# this will return a sorting function with the particular column start and end
# positions desired, so its easy to change
def return_sorting_func(col_start, col_end):
    # a sorting key for pythons built in sort. the key must take a single element, 
    # and return something for the sort function to compare.
    def sorting_func(line):
        # use the exact location, ie how many characters from the start of the line.
        field = line[phylop_col[0]: phylop_col[1]]
        try:
            # if this field has a float, return it
            return float(field)
        except ValueError:
            # else return a default
            return float('-inf')  # will give default of lowest rank
            # return 0.0  # default value of 0
    return sorting_func


if __name__ == '__main__':
    uniq_list = []
    dups_list = []

    # read both files into their own lists
    with open(sys.argv[1]) as uniqs, open(sys.argv[2]) as dups:
        uniq_list = list(uniqs.readlines())
        dups_list = list(dups.readlines())

    # and sort, using our key function from above, with relevant start and end positions
    # and reverse the resulting list.
    combined_list = sorted(uniq_list[1:] + dups_list[1:], 
            key=return_sorting_func(phylop_col[0], phylop_col[1]), 
            reverse=True)

    # to print out, cut off end of line (newline) and print header and footer around other 
    # results, which can then be piped from stdout.
    print(dups_list[0][:-1])
    for line in combined_list:
        print(line[:-1])
    print(dups_list[0][:-1])

したがって、他の質問で提供されたファイルを使用すると、次のようになります。

~$>cat unique_data.txt 
chromosoom  start    end       phylop   GPS
chr1    28745756    28745756    7.905   5   
chr1    31227215    31227215    10.263  5
chr1    47562402    47562402    2.322   4
chr1    64859630    64859630    1.714   3
chr1    70805699    70805699    1.913   2
chr1    89760653    89760653    -0.1    0
chr1    95630169    95630169    -1.651  -1
~$>cat dups_data.txt 
chromosoom  start    end       phylop   GPS
chr3    15540407    15540407    -1.391  -1
chr3    30648039    30648039    2.214   3
chr3    31663820    31663820    0.713   3
chr3    33093371    33093371    3.753   4
chr3    37050398    37050398    1.650   2
chr3    38053456    38053456    1.1     1
chr3    39597927    39597927    8.721   5
~$>cat dups_data_with_gaps_1.txt 
chromosoom  start    end       phylop   GPS
chr3    15540407    15540407    -1.391  -1
chr3    30648039    30648039    2.214   3
chr3    31663820    31663820    0.713   3
chr3                33093371    3.753   4
chr3    37050398    37050398    1.650   2
chr3    38053456    38053456    1.1     1
chr3    39597927                8.721   5

どちらも同じ出力を提供します。

~$>./own_sort.py unique_data.txt dups_data_with_gaps_1.txt
chromosoom  start    end       phylop   GPS
chr1    31227215    31227215    10.263  5
chr3    39597927    39597927    8.721   5
chr1    28745756    28745756    7.905   5   
chr3    33093371    33093371    3.753   4
chr1    47562402    47562402    2.322   4
chr3    30648039    30648039    2.214   3
chr1    70805699    70805699    1.913   2
chr1    64859630    64859630    1.714   3
chr3    37050398    37050398    1.650   2
chr3    38053456    38053456    1.1     1
chr3    31663820    31663820    0.713   3
chr1    89760653    89760653    -0.1    0
chr3    15540407    15540407    -1.391  -1
chr1    95630169    95630169    -1.651  -1
chromosoom  start    end       phylop   GPS

ただし、ソート列に次の間隔がある場合、その要素は最後の行になります。

~$>cat dups_data_with_gaps_2.txt 
chromosoom  start    end       phylop   GPS
chr3    15540407    15540407    -1.391  -1
chr3    30648039    30648039            3
chr3    31663820    31663820    0.713   3
chr3    33093371    33093371    3.753   4
chr3    37050398    37050398    1.650   2
chr3    38053456    38053456    1.1     1
chr3    39597927    39597927    8.721   5
~$>./own_sort.py unique_data.txt dups_data_with_gaps_2.txt 
chromosoom  start    end       phylop   GPS
chr1    31227215    31227215    10.263  5
chr3    39597927    39597927    8.721   5
chr1    28745756    28745756    7.905   5   
chr3    33093371    33093371    3.753   4
chr1    47562402    47562402    2.322   4
chr1    70805699    70805699    1.913   2
chr1    64859630    64859630    1.714   3
chr3    37050398    37050398    1.650   2
chr3    38053456    38053456    1.1     1
chr3    31663820    31663820    0.713   3
chr1    89760653    89760653    -0.1    0
chr3    15540407    15540407    -1.391  -1
chr1    95630169    95630169    -1.651  -1
chr3    30648039    30648039            3
chromosoom  start    end       phylop   GPS

この出力に基づいて、パイプを介してリスト全体の「唯一の」ファイル内の対応する行の最終位置を一覧表示することもできます。

~$>./own_sort.py unique_data.txt dups_data.txt | head -n -1 | tail -n +2 | grep -Fn  -f unique_data.txt 
1:chr1    31227215    31227215    10.263  5
3:chr1    28745756    28745756    7.905   5   
5:chr1    47562402    47562402    2.322   4
7:chr1    70805699    70805699    1.913   2
8:chr1    64859630    64859630    1.714   3
12:chr1    89760653    89760653    -0.1    0
14:chr1    95630169    95630169    -1.651  -1

grepは文字列をソートし-F()、行番号を出力し()、-nファイルから検索する文字列を読み取ります(-f unique_data.txt)。

申し訳ありません。世界には多くの例があります。フィールドが多い場合にしなければならない厄介なことの1つは、フィールドの始まりと終わりを識別し、より大きなファイルに対してインポートするための安定した方法があることを確認することです。

おすすめ記事