0

I would appreciate if someone could give me some tips on how to progress here.

The layout: Writing an Ubuntu bash script.

I have 3 input files into a folder called "reads":

$HOME/reads/
-Al-26-c45.fastq.trim.gz
-Br-45-e12.fastq.trim.gz
-Ct-1-d3.fastq.trim.gz

then I have 3 target files into a folder called "genomes":

$HOME/genomes/
-GCA_000066785.1_JCVI-PMFA1-2.0_genomic.fna.gz
-GCA_000003455.1_CPGHF-drosophila_genomic.fna.gz
-GCA_000034485.1_Sirimis_Turnuata.0_genomic.fna.gz

My expected output is:

$HOME/reads/
-Al-26-c45.fastq.aligned.gz
-Al-26-c45.fastq.trim.gz
-Br-45-e12.fastq.aligned.gz
-Br-45-e12.fastq.trim.gz
-Ct-1-d3.fastq.aligned.gz
-Ct-1-d3.fastq.trim.gz

I am trying to write a script using a nested For-Do loop to iterate the program "Myprogram" on each read against all the target genomes, and output a new file that contains the same read name except the "trim" word is replaced with "aligned" word.

This is what I wrote so far, but it only works for the first read file:

#!/bin/bash
dir="$HOME/reads/"

  for input in "$dir"/*.fastq.gz; do
  orifile=$input

   for file in $HOME/genomes/*.fna.gz; do

    Myprogram in=$input ref=$file out=unmapped.fastq

    old="trim"
    new="aligned"
    ls -1 "$dir"/*.fastq.trim.gz |\

     while read file ; do
        mv ./unmapped.fastq "${input/${old}/${new}}"
     done

    done

   done

The script fails to rename the output file for reprocessing on the next iteration.

Any guidance on the parts that need work, and where I can learn, are truly appreciated. Thanks.

Gogol
  • 11
  • 2
  • I am a bit confused here. You want to copy the files from `genomes` to `read`? And how to determine which file should be renamed to which `something.new.gz` ? – PesaThe Jul 27 '18 at 17:08
  • [ShellCheck](https://shellcheck.net) says: "[Tilde does not expand in quotes](https://github.com/koalaman/shellcheck/wiki/SC2088). Use $HOME." – that other guy Jul 27 '18 at 17:51
  • If your original code used `~` instead of `$HOME`, that's not just a "clarifying" change (as described in the edit history), it's a functional change -- you'll now get a different error message than you did before. Be sure you update the question to show that change in errors. Running `bash -x yourscript` to trace execution, by the way, is probably a good place to start towards being able to isolate a more specific and actionable question. – Charles Duffy Jul 30 '18 at 22:12
  • That said, what's with the `ls -1 | while read file`? `for file in "$1"/*` -- as you're already using in other code -- is **much** less error-prone. – Charles Duffy Jul 30 '18 at 22:13
  • One obvious issue, by the way -- if you're running `mv unmapped.fastq ...anything...` in a loop, obviously only the first iteration can possibly work. – Charles Duffy Jul 30 '18 at 22:15
  • @PesaThe. Not really. What I am trying to do is take the first read, input it in the Myprogram script which will use the first file into "genome", then use this newly created output file, rename it as the same previous input file, and use it as the new input file to be run again through the Myprogram using the second "genome" file, and so forth. I hope this clarifies what I am trying to do. – Gogol Jul 30 '18 at 22:16
  • Anyhow, if you want a question that can be usefully answered here, a good place to start is building a [mcve] -- the shortest possible program someone else can run to see the problem themselves. For it to be something someone else can run, it can't depend on pre-created files -- ie. it should `mkdir` and `touch` as appropriate to create files it needs to have present to demonstrate your problem. Moreover, in keeping with the "minimal" part of the guideline, any components not absolutely essential to demonstrating an issue should be removed. – Charles Duffy Jul 30 '18 at 22:16
  • Thank you Charles Duffy. I appreciate your responses and I will work on the points you are making while improving the question and the format. – Gogol Jul 30 '18 at 22:18

0 Answers0