Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Giorgio C
    Member
    • Oct 2010
    • 89

    Scripting Help - Common Elements

    Hi all,

    I have a question hoping someone can help me.

    I have a file with two columns A and B with a list of gene coordinates in each of them:

    I'd like to write in another file every element that is present in column A but not in B.
    (I mean I'd like to write in output the NON common elements of the two columns).


    Do you have any suggestions ( little script in perl or python) not exel cuz the list is huge.


    Thanks you in advance,
    Giorgio
  • dariober
    Senior Member
    • May 2010
    • 311

    #2
    Hi Giorgio,

    See if this helps:

    say you have test.txt like this (tab-separated):
    Code:
    chr1    chr2
    chr2    chr1
    chr3    chr4
    chr5    chr4
    You want elements in column B (2nd) not in A (1st). So the output would be "chr4" outputted twice

    This python script should do it. It assumes that all the unique elements in column A can fit in memory:

    Code:
    python -c "
    fin= open('test.txt')
    
    col_a= set()
    for line in fin:
        ## Unique elements in column A
        a= line.strip().split()[0]
        col_a.add(a)
    
    fin.seek(0)
    for line in fin:
        ## Print out elements in column B not in set A
        b= line.strip().split()[1]
        if b not in col_a:
            print(b)
    fin.close()
    "
    Output goes to stdin so use > to send it to a file

    Hope it helps and I haven't made any mistake!

    Dario

    Comment

    • Giorgio C
      Member
      • Oct 2010
      • 89

      #3
      Thank you so much for your answer I've tried but it give me an error:

      Traceback (most recent call last):
      File "script.py", line 9, in <module>
      a= line.strip().split()[0]
      IndexError: list index out of range

      Do you know what may be the problem?

      Comment

      • dariober
        Senior Member
        • May 2010
        • 311

        #4
        Originally posted by Giorgio C View Post
        Thank you so much for your answer I've tried but it give me an error:

        Traceback (most recent call last):
        File "script.py", line 9, in <module>
        a= line.strip().split()[0]
        IndexError: list index out of range

        Do you know what may be the problem?
        Can you post a sample of the first few lines from your input file? It could be that the first line(s) are emtpy hence the error above.

        In fact, this version will skip blank lines:

        Code:
        python -c "
        fin= open('test.txt')
        
        col_a= set()
        for line in fin:
            if line.strip() == '':
                continue
            ## Unique elements in column A
            a= line.strip().split()[0]
            col_a.add(a)
        
        fin.seek(0)
        for line in fin:
            if line.strip() == '':
                continue
            ## Print out elements in column B not in set A
            b= line.strip().split()[1]
            if b not in col_a:
                print(b)
        fin.close()
        "
        By the way, this script pulls out elements in B not in A, but it doesn't pull out elements in A not in B. Is this ok?

        Comment

        • Richard Finney
          Senior Member
          • Feb 2009
          • 701

          #5
          "comm" command is useful for this stuff ..

          Code:
          -bash-3.00$ cat junk
          chr1    chr2
          chr2    chr1
          chr3    chr4
          chr5    chr4
          -bash-3.00$ awk '{print $1}' < junk | sort | uniq > junkcol1
          -bash-3.00$ awk '{print $2}' < junk | sort | uniq > junkcol2
          -bash-3.00$ comm -3 junkcol1 junkcol2
          chr3
                  chr4
          chr5
          ----
          check out the "comm" command with "man comm" or via search engine.

          Comment

          • swbarnes2
            Senior Member
            • May 2008
            • 910

            #6
            Rather than write your own tool, BEDTools intersectBed can do exactly this, if you convert your lists to .bed format

            Comment

            • Giorgio C
              Member
              • Oct 2010
              • 89

              #7
              Thank you Dariober,

              as you said it was a problem of some empty lines in the middle of the list that I did not see at first. Now it works good and however the 'skip blank script' version works greatly.

              Richard Finney thank you for your other suggestion, it works good too.

              Comment

              • dariober
                Senior Member
                • May 2010
                • 311

                #8
                Originally posted by Giorgio C View Post
                Thank you Dariober,

                as you said it was a problem of some empty lines in the middle of the list that I did not see at first. Now it works good and however the 'skip blank script' version works greatly.

                Richard Finney thank you for your other suggestion, it works good too.
                Glad to hear it worked. By the way, I realized that if your input file is tab-separated you should replace in the script "split()" with "split('\t')". The script I posted splits lines into columns at every occurance of a blank space, so including, but not restricing to, tab characters.

                Good luck!
                Dario

                Comment

                • Giorgio C
                  Member
                  • Oct 2010
                  • 89

                  #9
                  Yes infact, that was one of the thing I've noticed, in the tab separeted file needs to add (\t) to the script.

                  By the way it works good !

                  Thank you again for your precious help.

                  Cheers,
                  Giorgio

                  Comment

                  • LudesMeyers
                    Junior Member
                    • Aug 2013
                    • 2

                    #10
                    This is a great discussion for a novice scriptor of Python and bash/C shells. Thank you Giorgio C for all of your questions. They provide food for my thoughts. And thanks to Richard Finney for pointing out the use of the "comm" and "awk" commands.

                    This code is a combo of my first thoughts on how to approach the problem with subsequent introduction of the "comm" command.

                    Code:
                    bash-3.2$ cat junk
                    chr1	chr2
                    chr2	chr1
                    chr3	chr4
                    chr5	chr4
                    bash-3.2$ cut -f1 junk | sort | uniq > junkcol1
                    bash-3.2$ cut -f2 junk | sort | uniq > junkcol2
                    bash-3.2$ comm -3 junkcol1 junkcol2 > commonjunk
                    bash-3.2$ cat commonjunk
                    chr3
                    	chr4
                    chr5
                    Thanks everyone,
                    John

                    Comment

                    • Giorgio C
                      Member
                      • Oct 2010
                      • 89

                      #11
                      Sure John,

                      this is a very useful forum

                      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-20-2026, 11:10 AM
                      0 responses
                      12 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-13-2026, 10:26 AM
                      0 responses
                      30 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-09-2026, 10:04 AM
                      0 responses
                      41 views
                      0 reactions
                      Last Post SEQadmin2  
                      Started by SEQadmin2, 07-08-2026, 10:08 AM
                      0 responses
                      26 views
                      0 reactions
                      Last Post SEQadmin2  
                      Working...