Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aeolus Huios
    Junior Member
    • Nov 2011
    • 9

    #1

    To get the no. of repeats and along with the repeated element

    Hi all,

    I want to compare first line with next line of a file containg single column. for example a file cotains:
    NM_1
    NM_1
    NM_1
    NM_2
    NM_2
    NM_3
    NM_4
    NM_5
    NM_5
    NM_5
    NM_5
    i want to get output as
    1 NM_1
    2 NM_1
    3 NM_1
    1 NM_2
    2 NM_2
    1 NM_3
    1 NM_4
    1 NM_5
    2 NM_5
    3 NM_5
    4 NM_5
    separated by tabs.

    Can anybody help me to write perl script to get the above output giving an input file. It will be well appreciated.

    But I have written the code as follows. I could not find where to correct my script:
    #!/usr/bin/perl

    $file=$ARGV[0];
    open(INFILE,"$file");
    $i = 1;

    while(<INFILE>){

    chomp;
    $currentline = <INFILE>;
    print "$i\t $currentline";
    $nextline = <INFILE>;

    if ($currentline == $nextline ){
    $i++;
    print "$i\t $nextline";

    }
    else{
    print "1\t $nextline";
    $i = 1;

    }

    }
    close(INFILE);


    With Regards,
    Aeolus
  • kmcarr
    Senior Member
    • May 2008
    • 1181

    #2
    Originally posted by Aeolus Huios View Post
    I want to compare first line with next line of a file containg single column. for example a file cotains:
    NM_1
    NM_1
    NM_1
    NM_2
    NM_2
    NM_3
    NM_4
    NM_5
    NM_5
    NM_5
    NM_5
    i want to get output as
    1 NM_1
    2 NM_1
    3 NM_1
    1 NM_2
    2 NM_2
    1 NM_3
    1 NM_4
    1 NM_5
    2 NM_5
    3 NM_5
    4 NM_5
    separated by tabs.
    First question, do you really need every occurrence of each identical element written to the output? It seems to me that it would be just as informative, easier to read/parse and more compact if your output only contained one line for each unique element with the count for that element. For example:

    Code:
    3 NM_1
    2 NM_2
    1 NM_3
    1 NM_4
    4 NM_5
    etc....
    The way I would do this would be with the unix command uniq, and since uniq requires that its input be sorted I always use sort first since it's never good to assume that your input is already sorted. By default uniq collapses all identical lines into a single line and adding the -c option will also output the count of the number of element in the original file.
    Code:
    # sort <inputFile> | uniq -c > <outputFile>
    I should note that uniq prints leading spaces before the count and the separator between the count and the element is a space not a tab. The output for the above example would look like:

    Code:
       3 NM_1
       2 NM_2
       1 NM_3
       1 NM_4
       4 NM_5
    You could clean these up by adding sed and tr to the command pipeline

    Code:
    # sort <inputFile> | uniq -c | sed -e 's/^ *//' | tr ' ' '\t' > <outputFile>
    
    Which will produce an output which looks like:
    
    3	NM_1
    2	NM_2
    1	NM_3
    1	NM_4
    4	NM_5
    Last edited by kmcarr; 02-08-2012, 10:27 AM.

    Comment

    • Richard Finney
      Senior Member
      • Feb 2009
      • 701

      #3
      cat file.txt| awk '{if ($1!=prev)k=1;else k++;print k"\t"$0;prev=$0}'

      Comment

      • adaptivegenome
        Super Moderator
        • Nov 2009
        • 436

        #4
        sort | uniq -c

        this is the most elegant solution

        Comment

        • Aeolus Huios
          Junior Member
          • Nov 2011
          • 9

          #5
          Hi kmcar, Gege

          Thanks alot for reply but i know using uniq linux function for getting the frequency of repeated data. But i want the output as what i said.
          :-) )))

          Hi Rechard,

          Let me try I will reply U back after a while.
          Thanks alot. :-) )))

          With reagrds,
          Aeolus

          Comment

          • adaptivegenome
            Super Moderator
            • Nov 2009
            • 436

            #6
            Originally posted by Aeolus Huios View Post
            Hi kmcar, Gege

            Thanks alot for reply but i know using uniq linux function for getting the frequency of repeated data. But i want the output as what i said.
            :-) )))

            Hi Rechard,

            Let me try I will reply U back after a while.
            Thanks alot. :-) )))

            With reagrds,
            Aeolus
            Certainly you can write a long script. But you could also do a "sort | uniq -c" then use "cut" to grab the columns individually and "paste" to reassemble however you want with whatever delimiter you want. So this makes it just a couple unix commands.

            Comment

            • Aeolus Huios
              Junior Member
              • Nov 2011
              • 9

              #7
              Hi Rechard ,

              Once again thanks alot. Its works very well. :-) ))))
              Can you tell me a good guide book or online articles for AWK commands.
              It will very grateful.

              With regards,
              Pawan

              Comment

              Latest Articles

              Collapse

              • SEQadmin2
                16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
                by SEQadmin2




                NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification, detection...
                Today, 06:46 AM
              • SEQadmin2
                Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                by SEQadmin2



                Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                ...
                10-14-2026, 09:55 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 10-28-2026, 09:57 AM
              0 responses
              22 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-28-2026, 09:51 AM
              0 responses
              14 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-22-2026, 11:13 AM
              0 responses
              68 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-22-2026, 10:48 AM
              0 responses
              48 views
              0 reactions
              Last Post SEQadmin2  
              Working...