디렉토리의 파일 구문 분석

디렉토리의 파일 구문 분석

내 디렉토리에 여러 파일이 있습니다: file1.txt file2.txt file3.txt

디렉토리의 각 파일에 대해 고유한 열만 인쇄하고 싶습니다. 열 1은 열 3 또는 4와 일치합니다. 유일한 열을 인쇄하여 f_parsed.txt로 저장하고 싶습니다.

파일 1.txt:

gene1   description1    gene1   gene88 
gene56  description2    gene67  gene56
gene6   description3    gene95  gene6

file1_parsed.txt:

gene1   description1    gene88  
gene56  description2    gene67  
gene6   description3    gene95  

이것은 지금까지 내 코드입니다.

for f in *.txt ; do while IFS= read -r line; do awk  -F "," '{if ($3 = $1) {print $1, $2, $3} else {print $1, $2, $4}}' > $f_parsed.txt;
    done

그런 다음 구문 분석된 각 파일에 대해 f_parsed.txt의 열 3에 있는 유전자를 grep하고 file_B.txt에서 찾아 일치하는 줄과 다음 줄을 반환하려고 합니다. 일치 항목이 포함된 모든 줄은 match1.txt로 저장됩니다(다음 파일은 match2.txt가 됩니다).

file_B.fasta는 다음과 같습니다:

>gene88 | shahid | ahifehhuh
TAGTCTTTCAAAAGA...
>gene6 | shahid | ahifehhuh
TAGTCTTTCAAAAGA...
>gene4 | jeiai | dhdhd
GTCAGTTTTTA...
>gene67 | vdiic | behej
GTCAGTTTTTA...
>gene95 | siis | ahifehhniniuh
TAGTCTTTCAAAAGA...
...
cat f_parsed.txt | while IFS= read -r line; do grep "$3" file_B.fasta  |awk '{x=NR+1}(NR<=x){print}' > match1.txt ; done

제가 시작한 예제 파일의 최종 출력은 match1.txt라고 해야 하며 다음과 같습니다.

>gene88 | shahid | ahifehhuh
TAGTCTTTCAAAAGA...
>gene67 | vdiic | behej
GTCAGTTTTTA...
>gene95 | siis | ahifehhniniuh
TAGTCTTTCAAAAGA...

미리 감사드립니다! 코드가 거친 건 알지만 초보자입니다.

답변1

이를 수행하는 한 가지 방법은 다음과 같습니다. 먼저 fasta 파일을 읽고 유전자 이름으로 구성된 배열을 만듭니다. 이 키에 해당하는 값은 줄 바꿈으로 구분된 현재 다음 n 줄입니다.

출력은 match*.txt 파일에 저장됩니다.

awk -F '|' '
  # @the beginning of file, get its type
  FNR==1 {  inCsv = !(inFasta = FS == "|") }

  # get gene name n record next line number
  inFasta && /^>/ {
    t=$0; gene=$1
    gsub(/^.|[[:space:]]*$/, "", gene)
    nxtln=NR+1
  }
  # fill up the value for the current gene
  inFasta && NR==nxtln { a[gene] = t ORS $0 }

  # we are in CSV file
  # close previously open filehandle
  # open fresh file handle (match*.txt)
  # write to filehandle based on equality
  # of field1 and field3
  inCsv && NF>3 {
    if (FNR == 1) {
      close(outf)
      outf = "match" ++k ".txt"
    }
    print a[$($1==$3?4:3)] > outf
  }

' file_B.fasta FS=, file*.txt
$ cat match1.txt
>gene88 | shahid | ahifehhuh
TAGTCTTTCAAAAGA...
>gene67 | vdiic | behej
GTCAGTTTTTA...
>gene95 | siis | ahifehhniniuh
TAGTCTTTCAAAAGA..

답변2

awk '{if($1 == $3) {print $1,$2,$NF}else{if($1 == $NF){print $1,$2,$3}}}' filename

산출

gene1 description1 gene88
gene56 description2 gene67
gene6 description3 gene95

관련 정보