Repairing the headers of phylip bioinformatics files to accurately reflect the updated number of samples in the file(s)Select file based on number of lines and manipulate the resultcreate a new column based on existing columns using if else statement in awkPrint sets of lines that do not have a corresponding pairUsing numbers in file A to get unique ID from file B based on the order specified by file AUsing Uniq -c with a regular expression or counting the number of lines removedDetermine how long tabs 't' are on a lineextract fasta entries from list using while readscript to parse file for two consecutive lines of unequal lengthConcatenate multiple zipped files, skipping header lines in all but the first filedelete rows with duplications in first column in bash

Is there an evolutionary advantage to having two heads?

What was this black-and-white film set in the Arctic or Antarctic where the monster/alien gets fried in the end?

How can I grammatically understand "Wir über uns"?

Get LaTeX form from step by step solution

Are UK pensions taxed twice?

Infinitely many hats

Can an old DSLR be upgraded to match modern smartphone image quality

60s (or earlier) short story where each colony has one person who doesn't connect well with others who is there for being able to absorb knowledge

When a current flow in an inductor is interrupted, what limits the voltage rise?

SPI on stm32 won't work without pullup resistors and even then performs poorly

Adding strings in lists together

Different PCB color ( is it different material? )

How to properly maintain eye contact with people that have distinctive facial features?

Creating Fictional Slavic Place Names

8a-methyl-1,2,3,4,4a,8a-hexahydronaphthalen-4a-ylium carbocation rearrangement

Uncommanded roll at high speed

Can a non-EU citizen travel within the Schengen area without identity documents?

What is/are this/these giant NASA box(es)?

Does `declare -a A` create an empty array `A` in Bash?

how do I test for a unique string with multiple possibilities?

How to prevent bad sectors?

What does it mean when you think without speaking?

Fastest way to perform complex search on pandas dataframe

What are the problems in teaching guitar via Skype?



Repairing the headers of phylip bioinformatics files to accurately reflect the updated number of samples in the file(s)


Select file based on number of lines and manipulate the resultcreate a new column based on existing columns using if else statement in awkPrint sets of lines that do not have a corresponding pairUsing numbers in file A to get unique ID from file B based on the order specified by file AUsing Uniq -c with a regular expression or counting the number of lines removedDetermine how long tabs 't' are on a lineextract fasta entries from list using while readscript to parse file for two consecutive lines of unequal lengthConcatenate multiple zipped files, skipping header lines in all but the first filedelete rows with duplications in first column in bash






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I have a dataset that I am working with made up of phylip files that I have been editing. Phylip format is a bioinformatics format that contains as a header the number of samples and the sequence length, followed by each sample and its sequence. for example:



5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatcgca
sample_4 caatatccga
sample_5 gaataagcga


My issue is that in trimming these datasets, the sample number in the header no longer is accurate (e.g. in above example might say five, but I've since trimmed to have only three samples). What I need to do is to replace that sample count with the new, accurate sample count but I cannot figure out how to do so without losing the sequence length number (e.g. the 10).



I have 550 files so simply doing this by hand is not an option. I can for-loop the wc but again I need to retain that sequence length information and somehow combine it with a new, accurate wc.










share|improve this question



















  • 3





    Please don't post pictures of text but post the actual text in a code block.

    – Jesse_b
    May 15 at 17:49











  • Also, it's unclear how the number should be changed. Always to 3?

    – choroba
    May 15 at 17:55











  • ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

    – erikusrex
    May 15 at 18:18

















3















I have a dataset that I am working with made up of phylip files that I have been editing. Phylip format is a bioinformatics format that contains as a header the number of samples and the sequence length, followed by each sample and its sequence. for example:



5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatcgca
sample_4 caatatccga
sample_5 gaataagcga


My issue is that in trimming these datasets, the sample number in the header no longer is accurate (e.g. in above example might say five, but I've since trimmed to have only three samples). What I need to do is to replace that sample count with the new, accurate sample count but I cannot figure out how to do so without losing the sequence length number (e.g. the 10).



I have 550 files so simply doing this by hand is not an option. I can for-loop the wc but again I need to retain that sequence length information and somehow combine it with a new, accurate wc.










share|improve this question



















  • 3





    Please don't post pictures of text but post the actual text in a code block.

    – Jesse_b
    May 15 at 17:49











  • Also, it's unclear how the number should be changed. Always to 3?

    – choroba
    May 15 at 17:55











  • ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

    – erikusrex
    May 15 at 18:18













3












3








3








I have a dataset that I am working with made up of phylip files that I have been editing. Phylip format is a bioinformatics format that contains as a header the number of samples and the sequence length, followed by each sample and its sequence. for example:



5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatcgca
sample_4 caatatccga
sample_5 gaataagcga


My issue is that in trimming these datasets, the sample number in the header no longer is accurate (e.g. in above example might say five, but I've since trimmed to have only three samples). What I need to do is to replace that sample count with the new, accurate sample count but I cannot figure out how to do so without losing the sequence length number (e.g. the 10).



I have 550 files so simply doing this by hand is not an option. I can for-loop the wc but again I need to retain that sequence length information and somehow combine it with a new, accurate wc.










share|improve this question
















I have a dataset that I am working with made up of phylip files that I have been editing. Phylip format is a bioinformatics format that contains as a header the number of samples and the sequence length, followed by each sample and its sequence. for example:



5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatcgca
sample_4 caatatccga
sample_5 gaataagcga


My issue is that in trimming these datasets, the sample number in the header no longer is accurate (e.g. in above example might say five, but I've since trimmed to have only three samples). What I need to do is to replace that sample count with the new, accurate sample count but I cannot figure out how to do so without losing the sequence length number (e.g. the 10).



I have 550 files so simply doing this by hand is not an option. I can for-loop the wc but again I need to retain that sequence length information and somehow combine it with a new, accurate wc.







text-processing bioinformatics wc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 16 at 1:22









Jeff Schaller

45.9k1165150




45.9k1165150










asked May 15 at 17:49









erikusrexerikusrex

182




182







  • 3





    Please don't post pictures of text but post the actual text in a code block.

    – Jesse_b
    May 15 at 17:49











  • Also, it's unclear how the number should be changed. Always to 3?

    – choroba
    May 15 at 17:55











  • ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

    – erikusrex
    May 15 at 18:18












  • 3





    Please don't post pictures of text but post the actual text in a code block.

    – Jesse_b
    May 15 at 17:49











  • Also, it's unclear how the number should be changed. Always to 3?

    – choroba
    May 15 at 17:55











  • ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

    – erikusrex
    May 15 at 18:18







3




3





Please don't post pictures of text but post the actual text in a code block.

– Jesse_b
May 15 at 17:49





Please don't post pictures of text but post the actual text in a code block.

– Jesse_b
May 15 at 17:49













Also, it's unclear how the number should be changed. Always to 3?

– choroba
May 15 at 17:55





Also, it's unclear how the number should be changed. Always to 3?

– choroba
May 15 at 17:55













ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

– erikusrex
May 15 at 18:18





ok thank you, will do in the future. Not always to three as the sample number is different across files but most of the files have been edited and so the new number of samples is often fewer than what is currently stated in the header

– erikusrex
May 15 at 18:18










3 Answers
3






active

oldest

votes


















3














If I understand your requirement correctly you can use the following awk command:



awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input


samples will be set to the number of lines in the input file minus one (since you aren't counting the header line).



awk will then change the first column of the first line to the new sample number and print everything.




$ cat input
5 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatccga
$ awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input
3 10
sample_1 gaatatccga
sample_2 gaatatccga
sample_3 gaatatccga



With GNU awk you can use the -i flag to modify the files in place but I would prefer to make a second set of modified files to ensure the correct changes have been made.



Something like:



for file in *.phy; do
awk -v samples="$(($(grep -c . "$file")-1))" 'NR == 1 $1=samples 1' "$file" > "$file.new"
done





share|improve this answer
































    3














    Another option would be to use ed (of course!):



    for f in input*
    do
    printf '1s/[[:digit:]][[:digit:]]*/%dnwnq' $(( $(wc -l < "$f") - 1 )) | ed -s "$f"
    done


    This loops over the files (named, for example input-something) and sends a simple ed-script to ed:



    • on line 1, search and replace (s//) one or more digits at the beginning of the line with another number -- that replacement number being the result of computing the line length of the input minus one

    • after that, w write the file out and

    • then q quit ed





    share|improve this answer






























      1














      In Vim, run:



      :execute '1s/^[0-9]+/' . (line('$')-1) . '/'


      (Thanks also to this answer for pointing me in the right direction.)



      You can also do this in a loop, e.g. using :bufdo or just a shell for loop.






      share|improve this answer























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519129%2frepairing-the-headers-of-phylip-bioinformatics-files-to-accurately-reflect-the-u%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        If I understand your requirement correctly you can use the following awk command:



        awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input


        samples will be set to the number of lines in the input file minus one (since you aren't counting the header line).



        awk will then change the first column of the first line to the new sample number and print everything.




        $ cat input
        5 10
        sample_1 gaatatccga
        sample_2 gaatatccga
        sample_3 gaatatccga
        $ awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input
        3 10
        sample_1 gaatatccga
        sample_2 gaatatccga
        sample_3 gaatatccga



        With GNU awk you can use the -i flag to modify the files in place but I would prefer to make a second set of modified files to ensure the correct changes have been made.



        Something like:



        for file in *.phy; do
        awk -v samples="$(($(grep -c . "$file")-1))" 'NR == 1 $1=samples 1' "$file" > "$file.new"
        done





        share|improve this answer





























          3














          If I understand your requirement correctly you can use the following awk command:



          awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input


          samples will be set to the number of lines in the input file minus one (since you aren't counting the header line).



          awk will then change the first column of the first line to the new sample number and print everything.




          $ cat input
          5 10
          sample_1 gaatatccga
          sample_2 gaatatccga
          sample_3 gaatatccga
          $ awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input
          3 10
          sample_1 gaatatccga
          sample_2 gaatatccga
          sample_3 gaatatccga



          With GNU awk you can use the -i flag to modify the files in place but I would prefer to make a second set of modified files to ensure the correct changes have been made.



          Something like:



          for file in *.phy; do
          awk -v samples="$(($(grep -c . "$file")-1))" 'NR == 1 $1=samples 1' "$file" > "$file.new"
          done





          share|improve this answer



























            3












            3








            3







            If I understand your requirement correctly you can use the following awk command:



            awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input


            samples will be set to the number of lines in the input file minus one (since you aren't counting the header line).



            awk will then change the first column of the first line to the new sample number and print everything.




            $ cat input
            5 10
            sample_1 gaatatccga
            sample_2 gaatatccga
            sample_3 gaatatccga
            $ awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input
            3 10
            sample_1 gaatatccga
            sample_2 gaatatccga
            sample_3 gaatatccga



            With GNU awk you can use the -i flag to modify the files in place but I would prefer to make a second set of modified files to ensure the correct changes have been made.



            Something like:



            for file in *.phy; do
            awk -v samples="$(($(grep -c . "$file")-1))" 'NR == 1 $1=samples 1' "$file" > "$file.new"
            done





            share|improve this answer















            If I understand your requirement correctly you can use the following awk command:



            awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input


            samples will be set to the number of lines in the input file minus one (since you aren't counting the header line).



            awk will then change the first column of the first line to the new sample number and print everything.




            $ cat input
            5 10
            sample_1 gaatatccga
            sample_2 gaatatccga
            sample_3 gaatatccga
            $ awk -v samples="$(($(grep -c . input)-1))" 'NR == 1 $1=samples 1' input
            3 10
            sample_1 gaatatccga
            sample_2 gaatatccga
            sample_3 gaatatccga



            With GNU awk you can use the -i flag to modify the files in place but I would prefer to make a second set of modified files to ensure the correct changes have been made.



            Something like:



            for file in *.phy; do
            awk -v samples="$(($(grep -c . "$file")-1))" 'NR == 1 $1=samples 1' "$file" > "$file.new"
            done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 15 at 18:05

























            answered May 15 at 17:58









            Jesse_bJesse_b

            15.6k33877




            15.6k33877























                3














                Another option would be to use ed (of course!):



                for f in input*
                do
                printf '1s/[[:digit:]][[:digit:]]*/%dnwnq' $(( $(wc -l < "$f") - 1 )) | ed -s "$f"
                done


                This loops over the files (named, for example input-something) and sends a simple ed-script to ed:



                • on line 1, search and replace (s//) one or more digits at the beginning of the line with another number -- that replacement number being the result of computing the line length of the input minus one

                • after that, w write the file out and

                • then q quit ed





                share|improve this answer



























                  3














                  Another option would be to use ed (of course!):



                  for f in input*
                  do
                  printf '1s/[[:digit:]][[:digit:]]*/%dnwnq' $(( $(wc -l < "$f") - 1 )) | ed -s "$f"
                  done


                  This loops over the files (named, for example input-something) and sends a simple ed-script to ed:



                  • on line 1, search and replace (s//) one or more digits at the beginning of the line with another number -- that replacement number being the result of computing the line length of the input minus one

                  • after that, w write the file out and

                  • then q quit ed





                  share|improve this answer

























                    3












                    3








                    3







                    Another option would be to use ed (of course!):



                    for f in input*
                    do
                    printf '1s/[[:digit:]][[:digit:]]*/%dnwnq' $(( $(wc -l < "$f") - 1 )) | ed -s "$f"
                    done


                    This loops over the files (named, for example input-something) and sends a simple ed-script to ed:



                    • on line 1, search and replace (s//) one or more digits at the beginning of the line with another number -- that replacement number being the result of computing the line length of the input minus one

                    • after that, w write the file out and

                    • then q quit ed





                    share|improve this answer













                    Another option would be to use ed (of course!):



                    for f in input*
                    do
                    printf '1s/[[:digit:]][[:digit:]]*/%dnwnq' $(( $(wc -l < "$f") - 1 )) | ed -s "$f"
                    done


                    This loops over the files (named, for example input-something) and sends a simple ed-script to ed:



                    • on line 1, search and replace (s//) one or more digits at the beginning of the line with another number -- that replacement number being the result of computing the line length of the input minus one

                    • after that, w write the file out and

                    • then q quit ed






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 15 at 18:30









                    Jeff SchallerJeff Schaller

                    45.9k1165150




                    45.9k1165150





















                        1














                        In Vim, run:



                        :execute '1s/^[0-9]+/' . (line('$')-1) . '/'


                        (Thanks also to this answer for pointing me in the right direction.)



                        You can also do this in a loop, e.g. using :bufdo or just a shell for loop.






                        share|improve this answer



























                          1














                          In Vim, run:



                          :execute '1s/^[0-9]+/' . (line('$')-1) . '/'


                          (Thanks also to this answer for pointing me in the right direction.)



                          You can also do this in a loop, e.g. using :bufdo or just a shell for loop.






                          share|improve this answer

























                            1












                            1








                            1







                            In Vim, run:



                            :execute '1s/^[0-9]+/' . (line('$')-1) . '/'


                            (Thanks also to this answer for pointing me in the right direction.)



                            You can also do this in a loop, e.g. using :bufdo or just a shell for loop.






                            share|improve this answer













                            In Vim, run:



                            :execute '1s/^[0-9]+/' . (line('$')-1) . '/'


                            (Thanks also to this answer for pointing me in the right direction.)



                            You can also do this in a loop, e.g. using :bufdo or just a shell for loop.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 16 at 19:53









                            WildcardWildcard

                            23.5k1068175




                            23.5k1068175



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519129%2frepairing-the-headers-of-phylip-bioinformatics-files-to-accurately-reflect-the-u%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

                                What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

                                Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?