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
                      Advancing Precision Medicine for Rare Diseases in Children
                      by seqadmin




                      Many organizations study rare diseases, but few have a mission as impactful as Rady Children’s Institute for Genomic Medicine (RCIGM). “We are all about changing outcomes for children,” explained Dr. Stephen Kingsmore, President and CEO of the group. The institute’s initial goal was to provide rapid diagnoses for critically ill children and shorten their diagnostic odyssey, a term used to describe the long and arduous process it takes patients to obtain an accurate...
                      12-16-2024, 07:57 AM
                    • seqadmin
                      Recent Advances in Sequencing Technologies
                      by seqadmin



                      Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

                      Long-Read Sequencing
                      Long-read sequencing has seen remarkable advancements,...
                      12-02-2024, 01:49 PM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by seqadmin, 12-17-2024, 10:28 AM
                    0 responses
                    33 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 12-13-2024, 08:24 AM
                    0 responses
                    49 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 12-12-2024, 07:41 AM
                    0 responses
                    34 views
                    0 likes
                    Last Post seqadmin  
                    Started by seqadmin, 12-11-2024, 07:45 AM
                    0 responses
                    46 views
                    0 likes
                    Last Post seqadmin  
                    Working...
                    X