Answers.Body: <p>The general idea is:</p>
  <ol>
  <li>iterate over all files
  <ol>
  <li>read the first line of each file, extract the <code>segment</code> bit</li>
  <li>move the file based on that</li>
  </ol>
  </li>
  </ol>
  <p>In Python, this can be done in many ways. Here’s one way:</p>
  <pre class="lang-py prettyprint-override"><code>import os
  import re
  
  
  def find_segment(id):
      return re.search(r'segment (\d)', id).group(1)
  
  
  for segment in range(8):
      os.mkdir(f'segment-{segment + 1}')
  
  for entry in os.scandir():
      if not entry.is_file():
          continue
  
      with open(entry.path, 'r') as file:
          segment = find_segment(next(file))
      os.rename(entry.path, f'segment-{segment}/{entry.path}')
  </code></pre>
  <p>Note the absence of error handling — this code assumes that every operation succeeds; empty files, or files without “<code>segment X</code>” in their FASTA ID, will trigger an error, as will usual IO failures (missing permissions, etc.).</p>
  <hr />
  <p>But many people wouldn’t use Python to solve this but a shell scripting language. For example, here’s something equivalent in Bash (untested):</p>
  <pre class="lang-sh prettyprint-override"><code>mkdir segment-{1..8}
  
  for file in *.fa *.fasta; do
      segment=$(sed 's/.*segment \([[:digit:]]\).*/\1/; q' &quot;$file&quot;)
      if [[ &quot;$segment&quot; != '' ]]; then
          mv &quot;$file&quot; &quot;segment-$segment&quot;
      done
  done
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-29T10:08:58.387Z
Answers.LastActivityDate: 2020-10-29T12:08:45.777Z
Answers.UserDisplayName: Konrad Rudolph
Body: <p>So I have around 10,000 FASTA files of Influenza A virus. These files contains sequences of each of the 8 segments of the viral genome and I want to separate these files into different locations based on the content of these FASTA files.
  In each FASTA file, first line has the segment number.
  Eg.</p>
  <pre><code>&gt;KM368312.1 Influenza A virus (A/swine/Shandong/01/2009(H1N1)) segment 3 polymerase PA (PA) and PA-X protein (PA-X) genes, complete cds
  </code></pre>
  <p>Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T09:10:00.363Z
Comments.Text: Thank you, It's my first time using stackoverflow, so sorry about that.
Comments.UserDisplayName: Neeleshwar Pandey
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T09:13:11.693Z
Comments.Text: We can treat these fasta files as basic text files containing sequences. So i have 10,000 text files and i want to sort them into different folders based on the segment sequence these files have. Eg. One text file will have segment 1 sequence only, and so on.  In total there are 8 segments, so 8 folders.
  I hope i am clear now.
Comments.UserDisplayName: Neeleshwar Pandey
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T09:16:51.803Z
Comments.Text: This is accession number, but each text file will have a unique accession number.
  But i want to group together all the segment 1 files together and so on.
Comments.UserDisplayName: Neeleshwar Pandey
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T09:25:24.410Z
Comments.Text: Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.
Comments.UserDisplayName: Neeleshwar Pandey
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T10:17:23.480Z
Comments.Text: @Stephan There’s naturally overlap between the two sites but there are dedicated tool and libraries for working with FASTA files. But, furthermore, the current question is completely off topic on Stack Overflow because it is a “please write code for me” question. On bioinformatics.SE we’re generally more tolerant of such questions for specific bioinformatics-related tasks.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-29T10:18:37.220Z
Comments.Text: The close notice is confusing because each close voter found a different reason to close this - you are vaguely violating several of the guidelines for [What topics can I ask about here.](/help/on-topic) Probably review the [help] and in particular also [How to ask.](/help/how-to-ask) If you can [edit] this into a well-scoped question about a Python program you have written, with details about what you have already tried and how it failed, it can certainly be salvaged and reopened.
Comments.UserDisplayName: tripleee
Tags: <python><covid-19><virus>
Title: How to sort multiple FASTA files based on their content?
ThreadText: # How to sort multiple FASTA files based on their content?
  
  (Asked by: username_0 on 2020-10-29)
  
  So I have around 10,000 FASTA files of Influenza A virus. These files contains sequences of each of the 8 segments of the viral genome and I want to separate these files into different locations based on the content of these FASTA files.
  In each FASTA file, first line has the segment number.
  Eg.
  
  ```
  >KM368312.1 Influenza A virus (A/swine/Shandong/01/2009(H1N1)) segment 3 polymerase PA (PA) and PA-X protein (PA-X) genes, complete cds
  
  ```
  
  Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.
  
  **Tags:** python, covid-19, virus
  
  > **Comments:**
  > **username_0**: Thank you, It's my first time using stackoverflow, so sorry about that.  
  > **username_0**: We can treat these fasta files as basic text files containing sequences. So i have 10,000 text files and i want to sort them into different folders based on the segment sequence these files have. Eg. One text file will have segment 1 sequence only, and so on.  In total there are 8 segments, so 8 folders. I hope i am clear now.  
  > **username_0**: This is accession number, but each text file will have a unique accession number. But i want to group together all the segment 1 files together and so on.  
  > **username_0**: Okay imagine you have 10,000 text files in one folder and there are 8 types of these text files based on the content of them. Eg. file1.txt has segment 1 written in it, file2.txt has segment 2 written in it upto segment 8. So I want to group the files containing segment 1 together in one folder, segment 2 in other folder together and so on.  
  > **username_1**: @Stephan There’s naturally overlap between the two sites but there are dedicated tool and libraries for working with FASTA files. But, furthermore, the current question is completely off topic on Stack Overflow because it is a “please write code for me” question. On bioinformatics.SE we’re generally more tolerant of such questions for specific bioinformatics-related tasks.  
  > **username_2**: The close notice is confusing because each close voter found a different reason to close this - you are vaguely violating several of the guidelines for [What topics can I ask about here.](/help/on-topic) Probably review the [help] and in particular also [How to ask.](/help/how-to-ask) If you can [edit] this into a well-scoped question about a Python program you have written, with details about what you have already tried and how it failed, it can certainly be salvaged and reopened.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-29. Score: 1
  
  The general idea is:
  
  1. iterate over all files
     1. read the first line of each file, extract the `segment` bit
     2. move the file based on that
  
  In Python, this can be done in many ways. Here’s one way:
  
  ```py
  import os
  import re
  
  def find_segment(id):
      return re.search(r'segment (\d)', id).group(1)
  
  for segment in range(8):
      os.mkdir(f'segment-{segment + 1}')
  
  for entry in os.scandir():
      if not entry.is_file():
          continue
  
      with open(entry.path, 'r') as file:
          segment = find_segment(next(file))
      os.rename(entry.path, f'segment-{segment}/{entry.path}')
  
  ```
  
  Note the absence of error handling — this code assumes that every operation succeeds; empty files, or files without “`segment X`” in their FASTA ID, will trigger an error, as will usual IO failures (missing permissions, etc.).
  
  ---
  
  But many people wouldn’t use Python to solve this but a shell scripting language. For example, here’s something equivalent in Bash (untested):
  
  ```sh
  mkdir segment-{1..8}
  
  for file in *.fa *.fasta; do
      segment=$(sed 's/.*segment \([[:digit:]]\).*/\1/; q' "$file")
      if [[ "$segment" != '' ]]; then
          mv "$file" "segment-$segment"
      done
  done
  
  ```
  
  
  ---
  
  