Answers.Body: <p>The most likely issue is that the <code>assembly.fasta</code> file is not found in the current directory. Taking @m's comments into consideration, a minimal rewriting of your script to fix this would be as follows:</p>
  <pre><code>for k in */Assembly/*/*.fastq; do minimap2 -ax map-ont <span class="math-container">$(dirname $</span>{k})/assembly.fasta <span class="math-container">$k &gt; $</span>(dirname <span class="math-container">${k})/mapping.sam; echo $</span>k; done
  </code></pre>
  <p>But I'd recommend at least adding additional quote and brace protection, just in case file names contained spaces:</p>
  <pre><code>for k in */Assembly/*/*.fastq;
    fDir = <span class="math-container">$(dirname $</span>{k});
    do minimap2 -ax map-ont &quot;<span class="math-container">${fDir}/assembly.fasta" "$</span>{k}&quot; &gt; &quot;<span class="math-container">${fDir}/mapping.sam";
    echo $</span>{k};
  done
  </code></pre>
  <p>edit: fixed up variable capitalisation</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-21T21:21:11.300Z
Answers.LastActivityDate: 2022-03-24T05:31:28.103Z
Answers.LastEditDate: 2022-03-24T05:31:28.103Z
Answers.UserDisplayName: gringer
Answers.Body: <p>The solution is quite simple you issue the one liner either from within <code>A4_assembly</code> directory or else <code>NC_D2</code> (see below). It will then find <code>assembly.fasta</code> and the error will disappear. That's because the current working directory is assigned to the directory in which the one liner is issued. Directory A4_assembly is where <code>assembly.fasta</code> is situated, thus is will work.</p>
  <p>If will fail to find the <code>fastq</code> file thus you will need to rewrite the loop, simple,</p>
  <pre><code>for k in *.fastq; do minimap2 -ax map-ont assembly.fasta <span class="math-container">$k &gt; mapping.sam; echo $</span>k; done
  </code></pre>
  <p>... will work, executed from within <code>Assembly_4</code></p>
  <p>The one liner suggests you are executing the command from the directory above the <code>Drain_4</code> directory.
  If this is how you intended the one liner to operate I don't think its the solution to the one liner, because there is a risk you will encounter logical errors (not bugs) ..</p>
  <pre><code>*/Assembly/*/*.fastq
  </code></pre>
  <p>If you did execute this commend from above Drain_4 it will looking all over the place here in every Drain and every assembly directory therein. If you executed this anywhere else it will fail to find *.fastq because the start of the wildcards is the current working directory in a bash for relative paths.</p>
  <p>However, when you dump the file it goes into one one place:</p>
  <pre><code> &gt; mapping.sam
  </code></pre>
  <p>So there is a risk are performing loads and loads of minimaps but they are simply overwriting each of other, i.e. they are replacing each other.</p>
  <p>The other issue is</p>
  <pre><code>NC_D2/
  </code></pre>
  <p>This directory is not represented in your directory file.</p>
  <p>Fundamentally the problem is that <code>assembly.fasta</code> is always in one location which appears to be in the directory above the directory structure you described, viz. <code>*/Assembly/*/*.fastq</code> ... according to the one liner, but in the directory structure its in <code>A4_assembly</code> it is also likely to be in <code>\D3_assembly</code> amongst loads of others and other Drains (master directories).</p>
  <p>Thus even if it 'finds' the reference genome (if that is what it is?), it will see that for everything ... and I mean everything.</p>
  <p>There are dynamic solutions to what I think the code using bash is looking to achieve because you can issue regex from within the do ... done, or simply do it as a script.</p>
  <hr />
  <p>I suggest to simply execute the first script I wrote in every Assembly directory then everything will simply work and there's no risk of logical errors.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-21T16:42:32.163Z
Answers.LastActivityDate: 2022-03-21T19:27:25.327Z
Answers.LastEditDate: 2022-03-21T19:27:25.327Z
Answers.UserDisplayName: M__
Body: <p>I am performing a bash loop one liner:.</p>
  <pre><code>for k in */Assembly/*/*.fastq; do minimap2 -ax map-ont assembly.fasta <span class="math-container">$k &gt; mapping.sam; echo $</span>k; done
  </code></pre>
  <p>The file hierarchy is as follows:</p>
  <pre><code>\Drain_4 
      \Assembly
          \A4_assembly
             assembly.fasta
             A4.fastq
          \D3_assembly
          \I8_assembly
          \U7_assembly
  \Drain_5
  \Drain_7
  </code></pre>
  <p>Where <code>\</code> denotes a directory. This is the error i am getting:</p>
  <pre><code>[ERROR] failed to open file '.fasta': No such file or directory A4.fastq
  </code></pre>
  <p>Both input files are in the same directory.</p>
  <p>The fastq is my current directory. I'm guessing i will need to provide the complete pathname for both my input files?</p>
  <p>I should mention that in all folders the assembly.fasta file is named the same but the fastq file is different (has a different prefix depending on which folder it is in.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-03-21T21:22:58.383Z
Comments.Text: your file hierarchy doesn't indicate any file called `NC_D2/NC_D2_trim_filt.fastq`; where is this file from?
Comments.UserDisplayName: gringer
Tags: <bash><linux><loop>
Title: Mapping to assemblies in subdirectories
ThreadText: # Mapping to assemblies in subdirectories
  
  (Asked by: username_0 on 2022-03-21)
  
  I am performing a bash loop one liner:.
  
  ```
  for k in */Assembly/*/*.fastq; do minimap2 -ax map-ont assembly.fasta $k > mapping.sam; echo $k; done
  
  ```
  
  The file hierarchy is as follows:
  
  ```
  \Drain_4 
      \Assembly
          \A4_assembly
             assembly.fasta
             A4.fastq
          \D3_assembly
          \I8_assembly
          \U7_assembly
  \Drain_5
  \Drain_7
  
  ```
  
  Where `\` denotes a directory. This is the error i am getting:
  
  ```
  [ERROR] failed to open file '.fasta': No such file or directory A4.fastq
  
  ```
  
  Both input files are in the same directory.
  
  The fastq is my current directory. I'm guessing i will need to provide the complete pathname for both my input files?
  
  I should mention that in all folders the assembly.fasta file is named the same but the fastq file is different (has a different prefix depending on which folder it is in.
  
  **Tags:** bash, linux, loop
  
  > **Comments:**
  > **username_1**: your file hierarchy doesn't indicate any file called `NC_D2/NC_D2_trim_filt.fastq`; where is this file from?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-03-21. Score: 2
  
  The most likely issue is that the `assembly.fasta` file is not found in the current directory. Taking @m's comments into consideration, a minimal rewriting of your script to fix this would be as follows:
  
  ```
  for k in */Assembly/*/*.fastq; do minimap2 -ax map-ont $(dirname ${k})/assembly.fasta $k > $(dirname ${k})/mapping.sam; echo $k; done
  
  ```
  
  But I'd recommend at least adding additional quote and brace protection, just in case file names contained spaces:
  
  ```
  for k in */Assembly/*/*.fastq;
    fDir = $(dirname ${k});
    do minimap2 -ax map-ont "${fDir}/assembly.fasta" "${k}" > "${fDir}/mapping.sam";
    echo ${k};
  done
  
  ```
  
  edit: fixed up variable capitalisation
  
  
  ---
  
  ### Answer by username_2 on 2022-03-21. Score: 0
  
  The solution is quite simple you issue the one liner either from within `A4_assembly` directory or else `NC_D2` (see below). It will then find `assembly.fasta` and the error will disappear. That's because the current working directory is assigned to the directory in which the one liner is issued. Directory A4\_assembly is where `assembly.fasta` is situated, thus is will work.
  
  If will fail to find the `fastq` file thus you will need to rewrite the loop, simple,
  
  ```
  for k in *.fastq; do minimap2 -ax map-ont assembly.fasta $k > mapping.sam; echo $k; done
  
  ```
  
  ... will work, executed from within `Assembly_4`
  
  The one liner suggests you are executing the command from the directory above the `Drain_4` directory.
  If this is how you intended the one liner to operate I don't think its the solution to the one liner, because there is a risk you will encounter logical errors (not bugs) ..
  
  ```
  */Assembly/*/*.fastq
  
  ```
  
  If you did execute this commend from above Drain\_4 it will looking all over the place here in every Drain and every assembly directory therein. If you executed this anywhere else it will fail to find \*.fastq because the start of the wildcards is the current working directory in a bash for relative paths.
  
  However, when you dump the file it goes into one one place:
  
  ```
   > mapping.sam
  
  ```
  
  So there is a risk are performing loads and loads of minimaps but they are simply overwriting each of other, i.e. they are replacing each other.
  
  The other issue is
  
  ```
  NC_D2/
  
  ```
  
  This directory is not represented in your directory file.
  
  Fundamentally the problem is that `assembly.fasta` is always in one location which appears to be in the directory above the directory structure you described, viz. `*/Assembly/*/*.fastq` ... according to the one liner, but in the directory structure its in `A4_assembly` it is also likely to be in `\D3_assembly` amongst loads of others and other Drains (master directories).
  
  Thus even if it 'finds' the reference genome (if that is what it is?), it will see that for everything ... and I mean everything.
  
  There are dynamic solutions to what I think the code using bash is looking to achieve because you can issue regex from within the do ... done, or simply do it as a script.
  
  ---
  
  I suggest to simply execute the first script I wrote in every Assembly directory then everything will simply work and there's no risk of logical errors.
  
  
  ---
  
  