Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python counting bases fasta file

    New to python but trying to learn, here I wrote a program to count the nucleotides in a fasta file. The approach is that when the line does not contain a ">" symbol then the program counts nucleotides one by one and adds it to the variable sum. The program does not work and I don't know why. Please help!

    #!/usr/bin/env python
    import sys

    # Takes fasta file and counts total bases

    infile = open(sys.argv[0], "rU")
    sum = 0
    line = infile.readlines()
    while ">" not in lline:
    for i in line:
    sum += 1
    print (sum)

    usage: $ python countbases.py infile.fasta

  • #2
    Perhaps this

    It would be helpful if you gave more information about how the program fails. However, one issue I think is this line:

    infile = open(sys.argv[0], "rU")

    Should be:

    infile = open(sys.argv[1], "rU")

    In this case sys.argv[0] = your script name, whereas sys.argv[1] = infile.fasta. See here for more information:
    Quoting from docs.python.org: "sys.argv The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname o...

    Comment


    • #3
      Originally posted by Cogitare View Post
      It would be helpful if you gave more information about how the program fails. However, one issue I think is this line:

      infile = open(sys.argv[0], "rU")

      Should be:

      infile = open(sys.argv[1], "rU")

      In this case sys.argv[0] = your script name, whereas sys.argv[1] = infile.fasta. See here for more information:
      http://stackoverflow.com/questions/5...-documentation
      Thank you. I also tried with sys.argv[1]
      The program does not return any error, it just does not return anything. If I run it in Idle, it crashes and if I run it in terminal, it jumps to the next line as if it was running but it stays there forever. I am trying with very small test files and if it worked it should go fairly fast instead of staying hanged for a long time.

      Comment


      • #4
        Originally posted by illinu View Post
        New to python but trying to learn, here I wrote a program to count the nucleotides in a fasta file. The approach is that when the line does not contain a ">" symbol then the program counts nucleotides one by one and adds it to the variable sum. The program does not work and I don't know why. Please help!

        #!/usr/bin/env python
        import sys

        # Takes fasta file and counts total bases

        infile = open(sys.argv[0], "rU")
        sum = 0
        line = infile.readlines()
        while ">" not in lline:
        for i in line:
        sum += 1
        print (sum)

        usage: $ python countbases.py infile.fasta
        Aside from the argv[0], issue, you're also looking for things in "lline" rather than "line"...

        Comment


        • #5
          Originally posted by dpryan View Post
          Aside from the argv[0], issue, you're also looking for things in "lline" rather than "line"...
          That's a typo here , in the program it's ok. So you're saying that apart from that the program seems ok and it should work?

          Comment


          • #6
            Well, you're also trying to iterate through a list in an odd way that I'm not sure would work. You might take a rather more efficient approach:
            Code:
            #!/usr/bin/env python
            import sys
            
            f = open(sys.argv[1], "rU")
            total=0
            for line in f :
                if(line[0] == ">") :
                    continue
            
                total += len(line)-1
            print("%s has %i nucleotides" % (sys.argv[1], total))
            With readlines(), you end up reading the whole file into a list, which is really memory inefficient if you're dealing with a whole genome.
            Last edited by dpryan; 08-20-2013, 06:49 AM. Reason: typo

            Comment


            • #7
              Impressed! it works like a charm

              Thanks a million

              Comment


              • #8
                No problem. In a real version, you'd probably want to use argparse (so you can easily give help and usage information) and check to see that the fasta file exists before running further. You also might want to add the number of bases per chromosome, likely only printed if the user specifies.

                Comment


                • #9
                  import sys
                  print sum(len(l.rstrip()) for l in open(sys.argv[1]) if l[0] != ">")

                  Comment


                  • #10
                    Yet another way (and more general) is to use the BioPython module. You will want to get familiar with handling sequences like this anyway, it is much more convenient to have a data structure. It would be this:

                    Code:
                    import sys
                    from Bio import SeqIO
                    
                    for seq_record in SeqIO.parse(sys.argv[1], "fasta"):
                        print seq_record.id
                        print len(seq_record)
                    And that would handle multifasta files (with multiple sequences in them) which you will no doubt encounter eventually.

                    Comment

                    Latest Articles

                    Collapse

                    • seqadmin
                      Current Approaches to Protein Sequencing
                      by seqadmin


                      Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
                      04-04-2024, 04:25 PM
                    • seqadmin
                      Strategies for Sequencing Challenging Samples
                      by seqadmin


                      Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
                      03-22-2024, 06:39 AM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by seqadmin, 04-11-2024, 12:08 PM
                    0 responses
                    22 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 04-10-2024, 10:19 PM
                    0 responses
                    24 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 04-10-2024, 09:21 AM
                    0 responses
                    19 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 04-04-2024, 09:00 AM
                    0 responses
                    52 views
                    0 likes
                    Last Post seqadmin  
                    Working...
                    X