<p>Edited with explanation below. I don't really understand the objective, because I initially thought you were editing the content of fastaq file, but you seem to be editing a file which contains fastaq filenames. Is that correct? I know the syntax but I don't know really what the goal is.</p>
<p><strong>Note</strong> I'm using the pathlib which has a glob function, do consider <code>rglob</code> under pathlib.</p>
<pre><code>import re
import pathlib as Path

# pathlib is super, good to habit to use it
mypath = Path('/Users/fewbugs/workingd')
allfilenames = mypath.glob(&quot;*.fastaq.gz&quot;)
r1, r2 = [], []
x = re.compile('_')
for filename in allfilenames:
    # the 'r' is super important 
    with open(filename, 'r') as file:
    # there is no need for a readlines() 
        for line in file:
    # split via re, it is better to compile (faster) 
        parts=x.split(line)
    # you must use '=='. REMEMBER lists start at 0 not 1
           if parts[6]==&quot;R1&quot;:
    # if you want to append to a list, declare the list first 
            r1.append(line)
    # there is no such thing as 'else if' REMEMBER lists start at 0 not 1
            elif parts[6]==&quot;R2&quot;:
                 r2.append(line)
    # when testing good idea to trap the stuff you're missing 
            else:
                 print (&quot;This one escaped %s&quot; % (line))
</code></pre>
<p>Please be CAREFUL with my spacing, everything needs to be multiples of 4. I've not checked the code.</p>
<hr />
<p>Forget this bit I thought you were processing the content of the fastaq file.</p>
<pre><code>outfile1 = Path(mypath.parent, 'outfile1')
outfile2 = Path(mypath.parent, 'outfile2')

with open (outfile1, 'w') as f:
    for item1 in r1:
        f.write(&quot;%s\n&quot; % item1)
with open (outfile2, 'w') as f2:
    for item2 in r2:
        f2.write(&quot;%s\n&quot; % item2)
 
</code></pre>
<hr />
<h2>A simpler way?</h2>
<p>... it looks like you're just processing filenames...</p>
<p>Might I suggest just globing no need for all this stuff.</p>
<pre><code>mypath = Path('/Users/fewbugs/workingd')
r1filenames = mypath.glob(&quot;*_R1_*.fastaq.gz&quot;)
r2filenames = mypath.glob(&quot;*_R2_*.fastaq.gz&quot;)
</code></pre>
<p>If that is the aim, thats it! Just be careful about the <code>glob</code> versus <code>rglob</code>.</p>
<p><strong>Idea</strong> If you want two paired lists this might be better done in a nested comprehension, e.g. a counter could be inserted at the central glob (*).</p>
<p>That may very well be what you're after. i.e.</p>
<pre><code>R1 = ['stuff_R1_001.fastaq.gz', 'stuff_R1_002.fastaq.gz' 'stuff_R1_003.fastaq.gz']
R2 = ['stuff_R2_001.fastaq.gz', 'stuff_R2_002.fastaq.gz' 'stuff_R2_003.fastaq.gz']
</code></pre>

CC BY-SA 4.0
2022-03-30T08:13:53.527Z
2022-03-31T19:53:43.530Z
2022-03-31T19:53:43.530Z
M__
<p>I am currently doing an analysis requiring a multiple-input script. I have a directory filled with 900+ samples.</p>
<p>Each sample is comprised of two-files in a fastq format. This is an example of a sample:</p>
<pre><code>MIP_REV_BC_01_01A_S1_R1_001.fastq.gz 
MIP_REV_BC_01_01A_S1_R2_001.fastq.gz
</code></pre>
<p>The script has a Read1 &amp; Read2 variable, and I need to make it so the R1 file is inputted for Read1 and the R2 file is inputted for Read2 for every sample. To be clear, the script is supposed to be executed on every sample separately.</p>
<p>This was an attempt:</p>
<pre><code>with open(filename) as file:
    lines=file.readlines()
    for line in lines:
        parts[]=line.split(&quot;_&quot;)
        if parts[7]=&quot;R1&quot;:
            r1[].append(line)
        else if parts[7]=&quot;R2&quot;:
             r2[].append(line)
</code></pre>
<p>We were bogged down by syntax errors, but either way, this script doesn't seem to do what we wanted it to. However, it captures the idea of what we're trying to achieve.</p>
<p><strong>Question</strong> I want to input a directory into the function so it automatically runs the script on the samples in the directory.</p>
<p>Any idea what I should do?</p>

CC BY-SA 4.0
2022-03-31T04:47:48.353Z
is the idea that you want to process both files at the same time and not store them in memory?
Maximilian Press
<python><fastq>
Multiple file Python script
# Multiple file Python script

(Asked by: username_0 on 2022-03-30)

I am currently doing an analysis requiring a multiple-input script. I have a directory filled with 900+ samples.
Each sample is comprised of two-files in a fastq format. This is an example of a sample:
MIP_REV_BC_01_01A_S1_R1_001.fastq.gz 
MIP_REV_BC_01_01A_S1_R2_001.fastq.gz

The script has a Read1 & Read2 variable, and I need to make it so the R1 file is inputted for Read1 and the R2 file is inputted for Read2 for every sample. To be clear, the script is supposed to be executed on every sample separately.
This was an attempt:
with open(filename) as file:
    lines=file.readlines()
    for line in lines:
        parts[]=line.split("_")
        if parts[7]="R1":
            r1[].append(line)
        else if parts[7]="R2":
             r2[].append(line)

We were bogged down by syntax errors, but either way, this script doesn't seem to do what we wanted it to. However, it captures the idea of what we're trying to achieve.
Question I want to input a directory into the function so it automatically runs the script on the samples in the directory.
Any idea what I should do?

**Tags:** python, fastq
> **Comments:**
> **username_1**: is the idea that you want to process both files at the same time and not store them in memory?  

---

## 1 answers

### Answer by username_2 on 2022-03-30. Score: 2 | Accepted answer

Edited with explanation below. I don't really understand the objective, because I initially thought you were editing the content of fastaq file, but you seem to be editing a file which contains fastaq filenames. Is that correct? I know the syntax but I don't know really what the goal is.
Note I'm using the pathlib which has a glob function, do consider rglob under pathlib.
import re
import pathlib as Path

# pathlib is super, good to habit to use it
mypath = Path('/Users/fewbugs/workingd')
allfilenames = mypath.glob("*.fastaq.gz")
r1, r2 = [], []
x = re.compile('_')
for filename in allfilenames:
    # the 'r' is super important 
    with open(filename, 'r') as file:
    # there is no need for a readlines() 
        for line in file:
    # split via re, it is better to compile (faster) 
        parts=x.split(line)
    # you must use '=='. REMEMBER lists start at 0 not 1
           if parts[6]=="R1":
    # if you want to append to a list, declare the list first 
            r1.append(line)
    # there is no such thing as 'else if' REMEMBER lists start at 0 not 1
            elif parts[6]=="R2":
                 r2.append(line)
    # when testing good idea to trap the stuff you're missing 
            else:
                 print ("This one escaped %s" % (line))

Please be CAREFUL with my spacing, everything needs to be multiples of 4. I've not checked the code.

Forget this bit I thought you were processing the content of the fastaq file.
outfile1 = Path(mypath.parent, 'outfile1')
outfile2 = Path(mypath.parent, 'outfile2')

with open (outfile1, 'w') as f:
    for item1 in r1:
        f.write("%s\n" % item1)
with open (outfile2, 'w') as f2:
    for item2 in r2:
        f2.write("%s\n" % item2)
 


A simpler way?
... it looks like you're just processing filenames...
Might I suggest just globing no need for all this stuff.
mypath = Path('/Users/fewbugs/workingd')
r1filenames = mypath.glob("*_R1_*.fastaq.gz")
r2filenames = mypath.glob("*_R2_*.fastaq.gz")

If that is the aim, thats it! Just be careful about the glob versus rglob.
Idea If you want two paired lists this might be better done in a nested comprehension, e.g. a counter could be inserted at the central glob (*).
That may very well be what you're after. i.e.
R1 = ['stuff_R1_001.fastaq.gz', 'stuff_R1_002.fastaq.gz' 'stuff_R1_003.fastaq.gz']
R2 = ['stuff_R2_001.fastaq.gz', 'stuff_R2_002.fastaq.gz' 'stuff_R2_003.fastaq.gz']


---

