Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • silver_steve
    Junior Member
    • Jan 2012
    • 3

    Script for extracting random sub-set of sequences

    I have sequence data that was created using several different primers. I found that the samples have large variation in sequences, depending on the primer used. I therefore wanted to create a file that randomly selects 1000 sequences, and then put these in a new FASTA file.

    My strategy is to put the sequences in a hash, split by the '>'. Then, assign each a number, and then randomly select the numbers that are assigned to the sequences, and then put this in a new FASTA file.
    I haven't gotten this to work yet, but does anyone know a simpler method for doing this?
  • maasha
    Senior Member
    • Apr 2009
    • 153

    #2
    With Biopieces you can do:

    Code:
    read_fasta -i input.fna | random_records -n 1000 | write_fasta -o random.fna -x

    Comment

    • Richard Finney
      Senior Member
      • Feb 2009
      • 701

      #3
      cat data | shuf | head -NUMBEROFLINES

      If the input is fasta, you'll have to join every other line with the next line and undo it. Example:

      cat test.fa| awk '{if ((NR%2)==0)print prev"XXXXXX"$0;prev=$0;}' | shuf | head -1000 | sed 's/XXXXXX/\n/'

      The shuf may not hang out in your /usr/sbin/ , if not, try

      sort -R file.txt | head -NUMBER OF LINES

      yep, "sort by random" !!!

      Comment

      • ETHANol
        Senior Member
        • Feb 2010
        • 308

        #4
        Discussion of next-gen sequencing related bioinformatics: resources, algorithms, open source efforts, etc
        --------------
        Ethan

        Comment

        • dawe
          Senior Member
          • Apr 2009
          • 258

          #5
          Originally posted by Richard Finney View Post
          cat data | shuf | head -NUMBEROFLINES

          If the input is fasta, you'll have to join every other line with the next line and undo it. Example:

          cat test.fa| awk '{if ((NR%2)==0)print prev"XXXXXX"$0;prev=$0;}' | shuf | head -1000 | sed 's/XXXXXX/\n/'

          The shuf may not hang out in your /usr/sbin/ , if not, try

          sort -R file.txt | head -NUMBER OF LINES

          yep, "sort by random" !!!
          Beware that shuf and sort -R are GNU. If you have a BSD system (OS X, FreeBSD...) those won't work.

          Comment

          • Richard Finney
            Senior Member
            • Feb 2009
            • 701

            #6
            I think this technique would work on BSD systems (and GNU systems) ...

            cat file.txt | awk '{print rand()" "$0}' | sort -n | head -1000 | cut -f2-9999 -d" "

            Comment

            • lh3
              Senior Member
              • Feb 2008
              • 686

              #7
              Suppose we want to sample n elements from a pool of N. The space complexity of the Biopieces solution is O(N) as it loads all sequences into memory. I guess shuf is no better. The optimal algorithm is to use reservoir sampling. The space complexity is O(n) instead of O(N). Of course, if N is not so large, it does not matter.

              The following is an awk snippet that randomly samples k=10 lines from a text file. Note that this program maximally keeps k=10 lines in memory.

              Code:
              cat file.txt|awk -v k=10 '{y=x++<k?x-1:int(rand()*x);if(y<k)a[y]=$0}END{for(z in a)print a[z]}'
              With bioawk, you can process fasta files this way:

              Code:
              awk -c fastx -v k=10 '{y=x++<k?x-1:int(rand()*x);if(y<k)a[y]=">"$name"\n"$seq}END{for(z in a)print a[z]}' seq.fa.gz
              Last edited by lh3; 01-14-2012, 08:47 PM.

              Comment

              • kga1978
                Senior Member
                • Nov 2010
                • 100

                #8
                Re: ETHANol's post: Here's a link to a script we made that will subsample any fastq or fasta file:



                Type subsampler.py -h for instructions - can do SE and PE reads.

                Comment

                • silver_steve
                  Junior Member
                  • Jan 2012
                  • 3

                  #9
                  Originally posted by maasha View Post
                  With Biopieces you can do:

                  Code:
                  read_fasta -i input.fna | random_records -n 1000 | write_fasta -o random.fna -x
                  Thanks Maasha. I've had some difficulty installing Biopieces. Particularly, I haven't been able to install the required perl modules. I get errors like this:
                  ERROR: Can't create '/Library/Perl/Updates/5.12.3/Module'
                  mkdir /Library/Perl/Updates/5.12.3/Module: Permission denied at /System/Library/Perl/5.12/ExtUtils/Install.pm line 494

                  Do you know how to get around this?

                  Comment

                  • maasha
                    Senior Member
                    • Apr 2009
                    • 153

                    #10
                    You need the correct permissions. Try using "sudo". Also, you should probably not contaminate this thread with Perl support request. Try stack-exchange or the Biopieces google group.


                    Cheers


                    Martin

                    Comment

                    Latest Articles

                    Collapse

                    • SEQadmin2
                      Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                      by SEQadmin2


                      Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                      The systematic characterization of the human proteome has
                      ...
                      07-20-2026, 11:48 AM
                    • SEQadmin2
                      Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                      by SEQadmin2



                      Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                      ...
                      07-09-2026, 11:10 AM
                    • SEQadmin2
                      Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                      by SEQadmin2



                      Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                      There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                      07-08-2026, 05:17 AM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by SEQadmin2, 07-24-2026, 12:17 PM
                    0 responses
                    26 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 07-23-2026, 11:41 AM
                    0 responses
                    20 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 07-20-2026, 11:10 AM
                    0 responses
                    28 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 07-13-2026, 10:26 AM
                    0 responses
                    38 views
                    0 reactions
                    Last Post SEQadmin2  
                    Working...