Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssully
    Member
    • Aug 2010
    • 48

    #1

    change order of FASTA seqs, based on ID list

    Is there any tool for doing this -- change the order of sequences in a FASTA file according to the order of a list of sequence IDs in another file?

    Seaview will reorder sequences based on sequence order in a tree, but that is very specific case. I'd like something more general.

    I know I could make a db of the FASTA set and use something like a batch NCBI blastdbcmd to extract the sequences in the order I want, but I'm hoping something less time consuming exists.
  • JackieBadger
    Senior Member
    • Mar 2009
    • 385

    #2
    Without being a code monkey I would have to do it a slightly long-winded way:
    1. Use Galaxy web portal to change Fasta-Tabular
    2.Copy and Paste this list into excel
    3. Use the Match+Index function in excel to create the new list
    4. save to .txt, and use Galaxy to convert to FASTA

    Comment

    • dariober
      Senior Member
      • May 2010
      • 311

      #3
      Originally posted by ssully View Post
      Is there any tool for doing this -- change the order of sequences in a FASTA file according to the order of a list of sequence IDs in another file?
      Hi- The script below should do what you want. Save it as reorder_fasta.py (or whatever you want) and execute it as
      Code:
      reorder_fasta.py seq.fasta ref.txt
      . See the help in the script itself for more detail and example.

      Hope this helps!
      Dario

      Code for reorder_fasta.py
      Code:
      #!/usr/bin/env python
      
      import sys
      
      docstring= """DESCRIPTION
          Reorder the sequences in a FASTA file according to the order given in a reference
          file. The reference file has one sequence name per line.
      USAGE
          reorder_fasta.py <file.fasta> <file.reference>
      
      ----------- EXAMPLE -------------
      ## fasta file
      echo '>second_seq
      AAAAAAAAAAAA
      AAAAAAAAAAAA
      AAAA
      >first_seq
      TTTTTTTTTTTTTT
      TTTTTTTTTTTTTT
      TTTTTTTTT
      >third_seq
      CCCCCCCCCCCCCCCCCC' > seq.fasta
      
      ## reference file
      echo 'first_seq
      second_seq
      third_seq' > ref.txt
      
      ## Reorder fasta according to reference:
      reorder_fasta.py seq.fasta ref.txt
      >first_seq
      TTTTTTTTTTTTTT
      TTTTTTTTTTTTTT
      TTTTTTTTT
      >second_seq
      AAAAAAAAAAAA
      AAAAAAAAAAAA
      AAAA
      >third_seq
      CCCCCCCCCCCCCCCCCC
      """
      
      if len(sys.argv) != 3:
          sys.exit(docstring)
      
      fasta= open(sys.argv[1])
      ref= open(sys.argv[2])
      
      seq_dict= {}
      while True:
          line= fasta.readline()
          if line == '':
              break
          if line.strip().startswith('>'):
              seq_name= line.strip()[1:]
              seq_dict[seq_name]= []
          else:
              seq_dict[seq_name].append(line.strip())
      fasta.close()
      for seq_name in ref:
          seq_name= seq_name.strip()
          print('>' + seq_name)
          print('\n'.join(seq_dict[seq_name]))
      ref.close()
      sys.exit()

      Comment

      Latest Articles

      Collapse

      • SEQadmin2
        Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
        by SEQadmin2



        Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

        Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
        ...
        10-14-2026, 09:55 AM
      • SEQadmin2
        New Genomics Technologies Take Aim at Long-Standing Limits
        by SEQadmin2


        Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

        We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
        ...
        09-28-2026, 10:25 AM

      ad_right_rmr

      Collapse

      News

      Collapse

      Topics Statistics Last Post
      Started by SEQadmin2, 10-22-2026, 11:13 AM
      0 responses
      25 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-22-2026, 10:48 AM
      0 responses
      20 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-15-2026, 10:52 AM
      0 responses
      36 views
      0 reactions
      Last Post SEQadmin2  
      Started by SEQadmin2, 10-14-2026, 05:39 AM
      0 responses
      42 views
      0 reactions
      Last Post SEQadmin2  
      Working...