Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #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


    • #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

      • seqadmin
        Current Approaches to Protein Sequencing
        by seqadmin


        Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
        04-04-2024, 04:25 PM
      • seqadmin
        Strategies for Sequencing Challenging Samples
        by seqadmin


        Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
        03-22-2024, 06:39 AM

      ad_right_rmr

      Collapse

      News

      Collapse

      Topics Statistics Last Post
      Started by seqadmin, 04-11-2024, 12:08 PM
      0 responses
      30 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-10-2024, 10:19 PM
      0 responses
      32 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-10-2024, 09:21 AM
      0 responses
      28 views
      0 likes
      Last Post seqadmin  
      Started by seqadmin, 04-04-2024, 09:00 AM
      0 responses
      52 views
      0 likes
      Last Post seqadmin  
      Working...
      X