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

                    • GATTACAT
                      Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                      by GATTACAT
                      Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                      Yesterday, 11:43 AM
                    • SEQadmin2
                      Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                      by SEQadmin2


                      I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

                      Here are nine questions we think about, in roughly the order they matter, before...
                      06-18-2026, 07:11 AM
                    • SEQadmin2
                      From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                      by SEQadmin2


                      Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                      The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                      ...
                      06-02-2026, 10:05 AM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by SEQadmin2, 06-30-2026, 05:37 AM
                    0 responses
                    9 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-26-2026, 11:10 AM
                    0 responses
                    18 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-17-2026, 06:09 AM
                    0 responses
                    52 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-09-2026, 11:58 AM
                    0 responses
                    110 views
                    0 reactions
                    Last Post SEQadmin2  
                    Working...