複数ファイルのデータを使用したtsvファイルの作成

複数ファイルのデータを使用したtsvファイルの作成

複数のファイルからtsvファイルを作成するには?

複数の異なるファイルからtsvファイルを作成できるようにしたいです。サンプルは、データを含む5つの「ゾーン」ファイルに関連付けられています。たとえば、サンプル1のファイルは、1_region1.cov、1_region2.cov、1_region3.cov、1_region4.cov、1_region5.covです。ここで、1_regionはX.covはTSVです。ヘッダー。私は「平均深さ」というタイトルの下のデータに興味があります。 1_region1の値を取得し、Region1ヘッダーの下の私のtsvファイルに追加したいと思います。 13のサンプルがあり、それぞれ5つのゾーンファイルがあるため、合計65の.covファイルがあります。

私の出力の例は次のとおりです。

サンプル エリア1 エリア2 エリア3 エリア4 エリア5
1 45 32 33 28 15
2 30 25 22 60 105
サム 44 50 22 55 77
... ... ... ... ...
13 2 サム 50 45 66

この場合、数字は構成されたばかりです。

これは私の現在の試みです:

## Sample array
samples=()
for i in {1..13};do samples+=($i); done
## Regions array
regions=(region1 region2 region3 region4 region5)

## I make some variables to store data
arr=()
CountData=()
CountIndex=0
SampleIndex=0
x=''
delim=':'

## I loop through my samples array to collect CountData from the .cov files. I know the naming convention of these files and follow it.
for ((i=0; i<${#samples[@]}; i++)); do
  for j in ${regions[@]};do CountData+=($(awk '{ for(k=1;k<=NF;k++){if($k == "meandepth"){getline; print $k} } }' ${samples[$i]}_${j}.cov)); done
done

## I loop through my CountData array to collect the tuples and store them into an array
for n in $(seq 0 $((${#CountData[@]} - 1))); do 
  count=$((CountIndex + 1))
  samplename=${samples[$SampleIndex]}
  if [ $((count % 6)) -eq 0 ];then
    arr+=($samplename$x) && CountIndex=$((CountIndex + 1)) && x='' && \ 
    SampleIndex=$((SampleIndex + 1))
  else
    x=$x$delim${CountData[$CountIndex]}
    CountIndex=$((CountIndex + 1))
  fi 
done

# I loop through my array and output the tuples as a tsv
for i in ${arr[@]}; do echo $i | sed 's/:/\t/g' >> output.tsv; done

# I add the header in after
sed -i "1iSample\tRegion1\tRegion2\tRegion3\tRegion4\tRegion5

私の試みは、2つのインデックスを使用して配列を繰り返します。これは、サンプル1に関連付けられているすべてのファイルを同じ行にインポートしようとしますが、最初のサンプル以降、数字はファイル内のファイルと一致しません。つまり、Sample2 Region1は30ではなく15を報告します。実際、このスクリプトは最初の11サンプルしか繰り返すことができません。これはおそらく条件でモジュロ6を使用しているからです。

Sample1に関連付けられている5つのファイルのそれぞれがSample1と同じ行にあるように、複数のファイルからtsvファイルを作成するにはどうすればよいですか?

ありがとうございます。

ベストアンサー1

おすすめ記事