I have some paired-end sequencing data in a directory:
sam1_R1.fastq.gz
sam1_R2.fastq.gz
sam2_R1.fastq.gz
sam2_R2.fastq.gz
I want to generate txt files as follow (for each sample (sam) separately:
$less sam1.txt
(dir)/sam1_R1.fastq.gz
(dir)/sam1_R2.fastq.gz
$less sam2.txt
(dir)/sam2_R1.fastq.gz
(dir)/sam2_R2.fastq.gz
2 answers
I found the solution myself:
for sample in samples;
do ls (dir)/*.fastq.gz | grep $sample > (dir)/$sample.txt
done
Here you go .....
ls *.gz | cut -f 1 -d "_" | sort | uniq | while read line
do
touch "$line".txt
echo "/dir/$line"_R1.fastq.gz > "$line".txt
echo "/dir/$line"_R2.fastq.gz >> "$line".txt
done
- Why the
touch? - Why not a simple
echo -e "/dir/${line}_R1.fastq.gz\n/dir/${line}_R2.fastq.gz" > "${line}.txt"? That would be one line instead of 3. - Why
sort | uniqand notsort -u?
Thank you for guiding, I am a novice and I have started shell scripting, so learning, but happy for your advice next time will try to implement it.
That's great! You will enjoy learning the various ways to get a job done on the shell. If like me, you were taught to create files using the touch command, be aware that it's not necessary. >, 2>, etc - redirection operators as well as programs that write to files, such as tee and editors like emacs, nano, vim, vi can directly create files.
echo has an option which enables us to use -extended expressions, such as \t\ and\n.sort -uachieves the same assort | uniq, so it's better unless we need someuniqspecific features, such asuniq -c`.
Thank you, Yeah, I am a hardcore biologist, but I love this, so exploring my own so suggestions are always welcome. Happy New Year...
Log in to answer this question.
what's the difference between the two dirs ? may be you wanted
sam2_...Sorry! it was a typo!
try this:
assuming that dir in OP is parent directory and files share the same pattern as in OP.
What does this do, cpad? It seems to be just a
cd .. && ls *R1.fastq.gz ** cd dir. OP seems to want each a list of sample's fastq files in separate text files.it would print: <parent_directory>/filename (for eg. test/sam1_R1.fastq.gz, test/sam1_R2.fastq.gz ....so on, assuming that test being parent directory). @ RamRS
output would look like this (on dummy .fq files in a directory named "test"):
OK, but this is not what OP wants at all. It actually leads them down a slightly wrong path.
is this OP wants? :
or
try this with awk:
I think that's close to what they want. I also think we should not be investing so much effort into a pure shell question that the OP has already solved.