Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Shorash
    Member
    • Sep 2012
    • 18

    Extract fasta script

    Hi all, I'm trying to extract nucleotide sequences from a fasta file using the following script which allows multiple extractions:

    perl -ne 'if(/^>(\S+)/){$c=grep{/^$1$/}qw()}print if $c' file.fasta

    My fasta file "file.fasta" contains contigs with two different headers from two different assemblers which I've merged. It doesn't seem to extract sequences from the second assembly headers but works fine for the first. Here's an example of each header:

    Header 1 (working) - comp89447_c0_seq4
    Header 2 (won't extract) - BN2_l1_1_(paired)_merged_contig_14067

    I can manually search and find the "BN2_l1_1_(paired)_merged_contig_14067" but I cannot extract it using the script. Any help would be greatly appreciated.
  • hugorody
    Junior Member
    • May 2013
    • 9

    #2
    just use grep

    why don't use only grep?

    cat filein.fasta | grep '>' --after-context=1 > fileout.fast a

    Comment

    • Shorash
      Member
      • Sep 2012
      • 18

      #3
      Originally posted by hugorody View Post
      why don't use only grep?

      cat filein.fasta | grep '>' --after-context=1 > fileout.fast a
      Sorry what exactly can I use this for?

      Comment

      • Shorash
        Member
        • Sep 2012
        • 18

        #4
        Originally posted by Shorash View Post
        Sorry what exactly can I use this for?
        I don't think I could use this for multiple extractions (up to 200-300)? It also doesn't seem to extract the full sequence following the contig name.

        Comment

        • hugorody
          Junior Member
          • May 2013
          • 9

          #5
          Originally posted by Shorash View Post
          Sorry what exactly can I use this for?
          could you explore a bit more what exactly you need to do?

          based on your post I think you just want to capture fasta sequences from a file. is that right?

          Comment

          • hugorody
            Junior Member
            • May 2013
            • 9

            #6
            grep will catch all lines that contains the symbol >, and also one line after context ">".
            you can use this for hundreds... thousands

            Comment

            • Shorash
              Member
              • Sep 2012
              • 18

              #7
              Originally posted by hugorody View Post
              could you explore a bit more what exactly you need to do?

              based on your post I think you just want to capture fasta sequences from a file. is that right?
              Ok, I have a fasta file containing nucleotide sequences with two different headings, eg:

              Heading 1:
              >comp35107_c0_seq1
              GTGGCAGGGACCAGAGCAAGCAGTTCTTCACAGACTTGTGGGAGTTCAGCCTGAAGGACC
              TTGAGTGGAAGGACAAGAGTCAACTCATCATTAGTGATGTGGCAGGCATGGTGCCCAGTG
              GCCGAAGTGGCGCCTCCACATGGGTCGGAAAGGATCAAGCACTCTACATGTTTGGTGGAA
              ACACTGTGGTCCGCACAGACTCAGGTCTCCGCAGCGGCATTGGATATGGAGCTGATCTCT
              GGAGGATGTCCACAAACAACCACAGCTGGCAGCTTTTGTCAGGCACTACAAAACCTGGGA
              CTCCAGCCAAGTTTGGTCGCCTTGGGGAGTACACTATAATGAGTCAGCCTGGCAGTCGGT
              GTGGGGCCATCACCTGGGTGGACACAGCCGGCAACCTGTGGATGTTTGGCGGTGATGGCA
              CAGACACAAGTCTTCCTTCTCCCTACCACGCATCACTGCTGCTCTCTGACCT

              Heading 2:
              >BN2_l1_1_(paired)_merged_contig_20016
              TCTCTCTCTCTCTCTCTGTGCCTATTCACATATCTCTTTTTTGTGCGTCTTCTCCTCTAA
              ACCACTGCAATAAAACTGTCCGAGTGCAGTCTCTGTCGGGACGCTGATGAAGGGAGGCTG
              GGGAGATGGAGAGAGGAGATGACACCCCCAGGTCCTGATTAAGCTGAGAGCTATTGCCGT
              AATGGACTAAAAGCACACGGGCGCCGTATTTCCGCTCNNCCGCTCAGACTCCATCCGCTT
              TATTCGGGACTTCGATGAGATGAAAGTCCTCGTTGCATTACGCCAATTTGATTACGGCAC
              TGATTTGACCCTGCAAACGAACCCCTGCAACTTCAGGAGTGCTCGCCCAATTGGGGTTGG
              CACGCTGTGGAACGCTCGAGGCACCGTGGGCAGCCGCCAGACCTTCGGTCTCCAATCTGC
              AACGCCGTGGCAGGTGGAATTACAAGGAAATGGACACTCGAACCTCTTTGTGTCAGGAGC
              AGATTGCTTGCGGCTGTGGGATTTATTGTAGG

              I require a script to extract multiple of these sequences using the headers I have in bold above. So basically I will copy a large number of the headers above from Excel and using a script I want to extract all the corresponding sequences. My current script which I've outlined in the opening post, for some reason, only extracts the sequences from header 1.

              Hope that clarifies a little.

              Comment

              • hugorody
                Junior Member
                • May 2013
                • 9

                #8
                OK. are u using Linux right?

                So, create a list.txt file with all headers you need separated by enter:

                header1
                header2
                ...

                then type on shell:

                $cat name_your_fasta_file.fasta | grep --file=list.txt --after-context=1 > my_sequences.fasta

                and that's it.
                you don't need a script.
                Last edited by hugorody; 08-03-2014, 10:31 PM.

                Comment

                • rhinoceros
                  Senior Member
                  • Apr 2013
                  • 372

                  #9
                  Originally posted by hugorody View Post
                  OK. are u using Linux right?

                  So, create a list.txt file with all headers you need separated by enter:

                  header1
                  header2
                  ...

                  then type on shell:

                  $cat name_your_fasta_file.fasta | grep --file=list.txt --after-context=1 > my_sequences.fasta

                  and that's it.
                  you don't need a script.
                  It doesn't work if there are linebreaks in the seqs like OP posted. You first have to deal with them..
                  savetherhino.org

                  Comment

                  • SylvainL
                    Senior Member
                    • Feb 2012
                    • 180

                    #10
                    Hi, just use R and the seqinr package. You will find the read.fasta function...

                    Comment

                    • hugorody
                      Junior Member
                      • May 2013
                      • 9

                      #11
                      Originally posted by rhinoceros View Post
                      It doesn't work if there are linebreaks in the seqs like OP posted. You first have to deal with them..
                      To remove the linebreaks:

                      $cat your_file_with_linebreaks.fasta | sed 's/ //g' | sed 's/\(>.*\)/\1 /g' | sed ':a;N;s/\n//g;ta' | sed 's/>/\n>/g' | sed 's/ /\n/g' > file_without_linebreaks.fasta

                      Comment

                      • Shorash
                        Member
                        • Sep 2012
                        • 18

                        #12
                        Originally posted by hugorody View Post
                        To remove the linebreaks:

                        $cat your_file_with_linebreaks.fasta | sed 's/ //g' | sed 's/\(>.*\)/\1 /g' | sed ':a;N;s/\n//g;ta' | sed 's/>/\n>/g' | sed 's/ /\n/g' > file_without_linebreaks.fasta
                        Hi there,

                        I receive the following error when attempting to remove the linebreaks:

                        :~/Extract_fasta/Orthologs> $cat BN_clc.fasta | sed 's/ //g' | sed 's/\(>.*\)/\1 /g' | sed ':a;N;s/\n//g;ta' | sed 's/>/\n>/g' | sed 's/ /\n/g' > BN_Fixed.fasta
                        ./BN_clc.fasta: line 1: syntax error near unexpected token `('
                        '/BN_clc.fasta: line 1: `>BN2_l1_1_(paired)_merged_contig_4

                        Comment

                        • GenoMax
                          Senior Member
                          • Feb 2008
                          • 7142

                          #13
                          faSomeRecords from Kent utilities would be the simplest/fast solution (http://hgdownload.soe.ucsc.edu/admin/exe/linux.x86_64/)

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




                          Last edited by GenoMax; 08-04-2014, 05:10 PM.

                          Comment

                          • hugorody
                            Junior Member
                            • May 2013
                            • 9

                            #14
                            Originally posted by Shorash View Post
                            Hi there,

                            I receive the following error when attempting to remove the linebreaks:

                            :~/Extract_fasta/Orthologs> $cat BN_clc.fasta | sed 's/ //g' | sed 's/\(>.*\)/\1 /g' | sed ':a;N;s/\n//g;ta' | sed 's/>/\n>/g' | sed 's/ /\n/g' > BN_Fixed.fasta
                            ./BN_clc.fasta: line 1: syntax error near unexpected token `('
                            '/BN_clc.fasta: line 1: `>BN2_l1_1_(paired)_merged_contig_4


                            which linux distribution do you use?
                            you should try use double quotes ( " ) instead single quotes ( ' ).

                            Comment

                            • Shorash
                              Member
                              • Sep 2012
                              • 18

                              #15
                              Originally posted by hugorody View Post
                              which linux distribution do you use?
                              you should try use double quotes ( " ) instead single quotes ( ' ).
                              I'm using a portable batch system (PBS).

                              Comment

                              Latest Articles

                              Collapse

                              • 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-17-2026, 06:09 AM
                              0 responses
                              38 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-09-2026, 11:58 AM
                              0 responses
                              100 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-05-2026, 10:09 AM
                              0 responses
                              121 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-04-2026, 08:59 AM
                              0 responses
                              114 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...