-1

I have fasta.qz sequence files with identical names, but in two different folders that I need to concatenate (concatenate always those two with identical names only!). The directory structure looks like this:

sequences

**|--folderA**

|  \-- \--MOSA_F_TAC01n.1.fq.gz       
 |  \-- --MOSA_F_TAC08n.rem.1.fq.gz       
 |  \-- --WAIN_N_15m.rem.2.fq.gz       
 |  \-- --MOSA_F_TAC01n.2.fq.gz       
 |  \-- --MOSA_F_TAC08n.rem.2.fq.gz       
 |  \-- --WAIN_N_16m.1.fq.gz       
 |  \-- --MOSA_F_TAC01n.rem.1.fq.gz       
 |  \-- --PAUA_F_16v.1.fq.gz       
 |  \-- --WAIN_N_16m.2.fq.gz       
 |  \-- --MOSA_F_TAC01n.rem.2.fq.gz       
 |  \-- --PAUA_F_16v.2.fq.gz     


**| \--folderB**       
 |  \-- --MOSA_F_TAC01n.1.fq.gz       
 |  \-- --MOSA_F_TAC08n.rem.1.fq.gz       
 |  \-- --WAIN_N_15m.rem.2.fq.gz       
 |  \-- --MOSA_F_TAC01n.2.fq.gz       
 |  \-- --MOSA_F_TAC08n.rem.2.fq.gz       
 |  \-- --WAIN_N_16m.1.fq.gz       
 |  \-- --MOSA_F_TAC01n.rem.1.fq.gz       
 |  \-- --PAUA_F_16v.1.fq.gz       
 |  \-- --WAIN_N_16m.2.fq.gz       
 |  \-- --MOSA_F_TAC01n.rem.2.fq.gz       
 |  \-- --PAUA_F_16v.2.fq.gz   

As you see, the file names are identical (their content is not, i.e., it is complementary thus need to cat). I would like to keep the files' names and concatenate the identical files into one single file with said name (in e.g. a new folder named folderC).

  • 1
    You could probably loop over the files in one directory and check if a file of each name exists in the other directory. If so, cat the two of them into a new file in the third directory. – Christian Gibbons Aug 28 '18 at 18:53
  • 1
    Are all files in dirA guaranteed to have a pair in dirB ? If not, what would you want to do with the unpairable ones: skip them or copy them to dirC ? – don_crissti Aug 28 '18 at 19:32
  • all those names actually begin with -- or is that filler for your example material? if so, what named them? globbing that on eval set --* would drop three chars (the same count in ../ incidentally) for each name, and most standard utility command lines would effect similar drops... – mikeserv Aug 28 '18 at 22:51
0

Try something like this:

for FILE in dir1/* ; do
    FILE2=dir2/${FILE#*/}
    if [ -f $FILE2 ] ; then
        cat $FILE $FILE2 > dir3/${FILE#*/}
    fi
done

Where dir1 is one of the source directories, dir2 is the other source directory, and dir3 is the output directory.

0

I'm afraid its not that easy. The files' "extensions" indicate those are gzipped data that need to be gunzipped before they can be concatenated. Stealing from Christian Gibbons' answer, try

for FILE in dir1/* ; do
    FILE2=dir2/${FILE#*/}
    if [ -f $FILE2 ] ; then
        gunzip -c $FILE $FILE2 | gzip > dir3/${FILE#*/}
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.