Tuple to table from counting DNA sequences





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to count the number of bases in a DNA sequence, return the counts of each type of base in the sequence, and also print out a two column table where the first column is the base and the second column is the associated base count. I can get the function to return the base count but I am not sure how to print the table. I would like to do this analysis with base python functions although I assume it would be easier to do with some python module.



Code:



 def base_counter(DNA):
A = 0
T = 0
G = 0
C = 0
for base in DNA:
if base == "A":
A = A + 1
elif base == "T":
T = T + 1
elif base == "G":
G = G + 1
elif base == "C":
C = C + 1
else:
pass
return A,T,G,C


Parameter input:



dna="AAGCTACGTGGGTGACTTT"


Function call:



counts=base_counter(dna)
print(counts)


Output:



(4, 6, 6, 3)


Desired output:



print(counts)
A 4
T 6
G 6
C 3


and



counts
(4, 6, 6, 3)









share|improve this question































    0















    I want to count the number of bases in a DNA sequence, return the counts of each type of base in the sequence, and also print out a two column table where the first column is the base and the second column is the associated base count. I can get the function to return the base count but I am not sure how to print the table. I would like to do this analysis with base python functions although I assume it would be easier to do with some python module.



    Code:



     def base_counter(DNA):
    A = 0
    T = 0
    G = 0
    C = 0
    for base in DNA:
    if base == "A":
    A = A + 1
    elif base == "T":
    T = T + 1
    elif base == "G":
    G = G + 1
    elif base == "C":
    C = C + 1
    else:
    pass
    return A,T,G,C


    Parameter input:



    dna="AAGCTACGTGGGTGACTTT"


    Function call:



    counts=base_counter(dna)
    print(counts)


    Output:



    (4, 6, 6, 3)


    Desired output:



    print(counts)
    A 4
    T 6
    G 6
    C 3


    and



    counts
    (4, 6, 6, 3)









    share|improve this question



























      0












      0








      0








      I want to count the number of bases in a DNA sequence, return the counts of each type of base in the sequence, and also print out a two column table where the first column is the base and the second column is the associated base count. I can get the function to return the base count but I am not sure how to print the table. I would like to do this analysis with base python functions although I assume it would be easier to do with some python module.



      Code:



       def base_counter(DNA):
      A = 0
      T = 0
      G = 0
      C = 0
      for base in DNA:
      if base == "A":
      A = A + 1
      elif base == "T":
      T = T + 1
      elif base == "G":
      G = G + 1
      elif base == "C":
      C = C + 1
      else:
      pass
      return A,T,G,C


      Parameter input:



      dna="AAGCTACGTGGGTGACTTT"


      Function call:



      counts=base_counter(dna)
      print(counts)


      Output:



      (4, 6, 6, 3)


      Desired output:



      print(counts)
      A 4
      T 6
      G 6
      C 3


      and



      counts
      (4, 6, 6, 3)









      share|improve this question
















      I want to count the number of bases in a DNA sequence, return the counts of each type of base in the sequence, and also print out a two column table where the first column is the base and the second column is the associated base count. I can get the function to return the base count but I am not sure how to print the table. I would like to do this analysis with base python functions although I assume it would be easier to do with some python module.



      Code:



       def base_counter(DNA):
      A = 0
      T = 0
      G = 0
      C = 0
      for base in DNA:
      if base == "A":
      A = A + 1
      elif base == "T":
      T = T + 1
      elif base == "G":
      G = G + 1
      elif base == "C":
      C = C + 1
      else:
      pass
      return A,T,G,C


      Parameter input:



      dna="AAGCTACGTGGGTGACTTT"


      Function call:



      counts=base_counter(dna)
      print(counts)


      Output:



      (4, 6, 6, 3)


      Desired output:



      print(counts)
      A 4
      T 6
      G 6
      C 3


      and



      counts
      (4, 6, 6, 3)






      python dna-sequence






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 18:20

























      asked Nov 24 '18 at 14:51







      user4400727































          4 Answers
          4






          active

          oldest

          votes


















          0














          from within the function:



          out_str="A    "+str(A)+"n"+
          "T "+str(T)+"n"+
          "G "+str(G)+"n"+
          "C "+str(C)
          return out_str


          now you can call and print it and it will be printed in your desired format:



          result=base_counter(DNA)
          print(result)


          for OP's request full code:



          def base_counter(DNA):
          A = 0
          T = 0
          G = 0
          C = 0
          for base in DNA:
          if base == "A":
          A = A + 1
          elif base == "T":
          T = T + 1
          elif base == "G":
          G = G + 1
          elif base == "C":
          C = C + 1
          out_str = "A " + str(A) + "n"+
          "T " + str(T) + "n"+
          "G " + str(G) + "n"+
          "C " + str(C)
          return out_str

          base=base_counter("AAGCTACGTGGGTGACTTT")
          print(base)


          output:



          A    4
          T 6
          G 6
          C 3





          share|improve this answer


























          • I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

            – user4400727
            Nov 24 '18 at 15:39











          • @user3683803 I've left you another answer making a function that prints the results

            – prophet-five
            Nov 24 '18 at 16:22











          • Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

            – user4400727
            Nov 24 '18 at 22:26





















          2














          You can use collections.Counter to count the bases and pandas to set the data in a column-wise manner. Here is an example



          from collections import Counter
          import pandas as pd

          # Count the bases
          dna="AAGCTACGTGGGTGACTTT"
          count = Counter(dna)
          tup = ()
          for _, value in sorted(count.items()):
          tup += (value,)
          print(tup # Outputs (4, 3, 6, 6)

          # Set it in a pandas dataframe
          df = pd.DataFrame(list(dict(count).items()), columns=['Base', 'Count'])
          print(df.to_string(index=False))
          # Output
          # Base Count
          # A 4
          # G 6
          # C 3
          # T 6





          share|improve this answer































            1














            1) you have a bug - your return is indented one extra tab to the right



            2) use a dict:



            def base_counter(DNA):
            dna_dict = {
            "A": 0,
            "T": 0,
            "G": 0,
            "C": 0,
            }
            for base in DNA:
            if base == "A":
            dna_dict["A"] += 1
            elif base == "T":
            dna_dict["T"] += 1
            elif base == "G":
            dna_dict["G"] += 1
            elif base == "C":
            dna_dict["C"] += 1
            return dna_dict


            dna = "AAGCTACGTGGGTGACTTT"

            counts = base_counter(dna)
            for base, count in counts.items():
            print(base, count)


            but if you have to keep the function as is:



            def base_counter(DNA):
            A = 0
            T = 0
            G = 0
            C = 0
            for base in DNA:
            if base == "A":
            A = A + 1
            elif base == "T":
            T = T + 1
            elif base == "G":
            G = G + 1
            elif base == "C":
            C = C + 1
            return A,T,G,C


            dna = "AAGCTACGTGGGTGACTTT"

            counts = base_counter(dna)
            for base, count in zip("ATGC", counts):
            print(base, count)





            share|improve this answer


























            • The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

              – user4400727
              Nov 24 '18 at 15:25











            • so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

              – motyzk
              Nov 24 '18 at 15:37













            • can you please update your answer with this solution?

              – user4400727
              Nov 24 '18 at 15:41











            • OK, just did...

              – motyzk
              Nov 24 '18 at 15:45











            • You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

              – usr2564301
              Nov 24 '18 at 16:25



















            0














            you can make another function for printing the results:



            def print_bases(bases):
            print("A "+str(bases[0])+"n"
            "T "+str(bases[1])+"n"
            "G "+str(bases[2])+"n"
            "C "+str(bases[3]))
            print_bases(counts)





            share|improve this answer
























            • Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

              – user4400727
              Nov 24 '18 at 16:44













            • from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

              – prophet-five
              Nov 24 '18 at 16:51













            • should i ask a new question? hard to understand without an example of what you just mentioned.

              – user4400727
              Nov 24 '18 at 16:57











            • no, just edit this one, I'll edit my answer

              – prophet-five
              Nov 24 '18 at 17:01











            • @user3683803 edited my first answer

              – prophet-five
              Nov 24 '18 at 17:08












            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53459358%2ftuple-to-table-from-counting-dna-sequences%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            from within the function:



            out_str="A    "+str(A)+"n"+
            "T "+str(T)+"n"+
            "G "+str(G)+"n"+
            "C "+str(C)
            return out_str


            now you can call and print it and it will be printed in your desired format:



            result=base_counter(DNA)
            print(result)


            for OP's request full code:



            def base_counter(DNA):
            A = 0
            T = 0
            G = 0
            C = 0
            for base in DNA:
            if base == "A":
            A = A + 1
            elif base == "T":
            T = T + 1
            elif base == "G":
            G = G + 1
            elif base == "C":
            C = C + 1
            out_str = "A " + str(A) + "n"+
            "T " + str(T) + "n"+
            "G " + str(G) + "n"+
            "C " + str(C)
            return out_str

            base=base_counter("AAGCTACGTGGGTGACTTT")
            print(base)


            output:



            A    4
            T 6
            G 6
            C 3





            share|improve this answer


























            • I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

              – user4400727
              Nov 24 '18 at 15:39











            • @user3683803 I've left you another answer making a function that prints the results

              – prophet-five
              Nov 24 '18 at 16:22











            • Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

              – user4400727
              Nov 24 '18 at 22:26


















            0














            from within the function:



            out_str="A    "+str(A)+"n"+
            "T "+str(T)+"n"+
            "G "+str(G)+"n"+
            "C "+str(C)
            return out_str


            now you can call and print it and it will be printed in your desired format:



            result=base_counter(DNA)
            print(result)


            for OP's request full code:



            def base_counter(DNA):
            A = 0
            T = 0
            G = 0
            C = 0
            for base in DNA:
            if base == "A":
            A = A + 1
            elif base == "T":
            T = T + 1
            elif base == "G":
            G = G + 1
            elif base == "C":
            C = C + 1
            out_str = "A " + str(A) + "n"+
            "T " + str(T) + "n"+
            "G " + str(G) + "n"+
            "C " + str(C)
            return out_str

            base=base_counter("AAGCTACGTGGGTGACTTT")
            print(base)


            output:



            A    4
            T 6
            G 6
            C 3





            share|improve this answer


























            • I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

              – user4400727
              Nov 24 '18 at 15:39











            • @user3683803 I've left you another answer making a function that prints the results

              – prophet-five
              Nov 24 '18 at 16:22











            • Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

              – user4400727
              Nov 24 '18 at 22:26
















            0












            0








            0







            from within the function:



            out_str="A    "+str(A)+"n"+
            "T "+str(T)+"n"+
            "G "+str(G)+"n"+
            "C "+str(C)
            return out_str


            now you can call and print it and it will be printed in your desired format:



            result=base_counter(DNA)
            print(result)


            for OP's request full code:



            def base_counter(DNA):
            A = 0
            T = 0
            G = 0
            C = 0
            for base in DNA:
            if base == "A":
            A = A + 1
            elif base == "T":
            T = T + 1
            elif base == "G":
            G = G + 1
            elif base == "C":
            C = C + 1
            out_str = "A " + str(A) + "n"+
            "T " + str(T) + "n"+
            "G " + str(G) + "n"+
            "C " + str(C)
            return out_str

            base=base_counter("AAGCTACGTGGGTGACTTT")
            print(base)


            output:



            A    4
            T 6
            G 6
            C 3





            share|improve this answer















            from within the function:



            out_str="A    "+str(A)+"n"+
            "T "+str(T)+"n"+
            "G "+str(G)+"n"+
            "C "+str(C)
            return out_str


            now you can call and print it and it will be printed in your desired format:



            result=base_counter(DNA)
            print(result)


            for OP's request full code:



            def base_counter(DNA):
            A = 0
            T = 0
            G = 0
            C = 0
            for base in DNA:
            if base == "A":
            A = A + 1
            elif base == "T":
            T = T + 1
            elif base == "G":
            G = G + 1
            elif base == "C":
            C = C + 1
            out_str = "A " + str(A) + "n"+
            "T " + str(T) + "n"+
            "G " + str(G) + "n"+
            "C " + str(C)
            return out_str

            base=base_counter("AAGCTACGTGGGTGACTTT")
            print(base)


            output:



            A    4
            T 6
            G 6
            C 3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 22:40

























            answered Nov 24 '18 at 15:12









            prophet-fiveprophet-five

            1089




            1089













            • I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

              – user4400727
              Nov 24 '18 at 15:39











            • @user3683803 I've left you another answer making a function that prints the results

              – prophet-five
              Nov 24 '18 at 16:22











            • Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

              – user4400727
              Nov 24 '18 at 22:26





















            • I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

              – user4400727
              Nov 24 '18 at 15:39











            • @user3683803 I've left you another answer making a function that prints the results

              – prophet-five
              Nov 24 '18 at 16:22











            • Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

              – user4400727
              Nov 24 '18 at 22:26



















            I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

            – user4400727
            Nov 24 '18 at 15:39





            I want to be able to do this: counts=base_counter(dna) print (counts) and not have it print within the function

            – user4400727
            Nov 24 '18 at 15:39













            @user3683803 I've left you another answer making a function that prints the results

            – prophet-five
            Nov 24 '18 at 16:22





            @user3683803 I've left you another answer making a function that prints the results

            – prophet-five
            Nov 24 '18 at 16:22













            Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

            – user4400727
            Nov 24 '18 at 22:26







            Can you please update your answer including my code? I'm having a difficult time reproducing your solution. Thanks. :)

            – user4400727
            Nov 24 '18 at 22:26















            2














            You can use collections.Counter to count the bases and pandas to set the data in a column-wise manner. Here is an example



            from collections import Counter
            import pandas as pd

            # Count the bases
            dna="AAGCTACGTGGGTGACTTT"
            count = Counter(dna)
            tup = ()
            for _, value in sorted(count.items()):
            tup += (value,)
            print(tup # Outputs (4, 3, 6, 6)

            # Set it in a pandas dataframe
            df = pd.DataFrame(list(dict(count).items()), columns=['Base', 'Count'])
            print(df.to_string(index=False))
            # Output
            # Base Count
            # A 4
            # G 6
            # C 3
            # T 6





            share|improve this answer




























              2














              You can use collections.Counter to count the bases and pandas to set the data in a column-wise manner. Here is an example



              from collections import Counter
              import pandas as pd

              # Count the bases
              dna="AAGCTACGTGGGTGACTTT"
              count = Counter(dna)
              tup = ()
              for _, value in sorted(count.items()):
              tup += (value,)
              print(tup # Outputs (4, 3, 6, 6)

              # Set it in a pandas dataframe
              df = pd.DataFrame(list(dict(count).items()), columns=['Base', 'Count'])
              print(df.to_string(index=False))
              # Output
              # Base Count
              # A 4
              # G 6
              # C 3
              # T 6





              share|improve this answer


























                2












                2








                2







                You can use collections.Counter to count the bases and pandas to set the data in a column-wise manner. Here is an example



                from collections import Counter
                import pandas as pd

                # Count the bases
                dna="AAGCTACGTGGGTGACTTT"
                count = Counter(dna)
                tup = ()
                for _, value in sorted(count.items()):
                tup += (value,)
                print(tup # Outputs (4, 3, 6, 6)

                # Set it in a pandas dataframe
                df = pd.DataFrame(list(dict(count).items()), columns=['Base', 'Count'])
                print(df.to_string(index=False))
                # Output
                # Base Count
                # A 4
                # G 6
                # C 3
                # T 6





                share|improve this answer













                You can use collections.Counter to count the bases and pandas to set the data in a column-wise manner. Here is an example



                from collections import Counter
                import pandas as pd

                # Count the bases
                dna="AAGCTACGTGGGTGACTTT"
                count = Counter(dna)
                tup = ()
                for _, value in sorted(count.items()):
                tup += (value,)
                print(tup # Outputs (4, 3, 6, 6)

                # Set it in a pandas dataframe
                df = pd.DataFrame(list(dict(count).items()), columns=['Base', 'Count'])
                print(df.to_string(index=False))
                # Output
                # Base Count
                # A 4
                # G 6
                # C 3
                # T 6






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 15:37









                b-fgb-fg

                2,09811724




                2,09811724























                    1














                    1) you have a bug - your return is indented one extra tab to the right



                    2) use a dict:



                    def base_counter(DNA):
                    dna_dict = {
                    "A": 0,
                    "T": 0,
                    "G": 0,
                    "C": 0,
                    }
                    for base in DNA:
                    if base == "A":
                    dna_dict["A"] += 1
                    elif base == "T":
                    dna_dict["T"] += 1
                    elif base == "G":
                    dna_dict["G"] += 1
                    elif base == "C":
                    dna_dict["C"] += 1
                    return dna_dict


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in counts.items():
                    print(base, count)


                    but if you have to keep the function as is:



                    def base_counter(DNA):
                    A = 0
                    T = 0
                    G = 0
                    C = 0
                    for base in DNA:
                    if base == "A":
                    A = A + 1
                    elif base == "T":
                    T = T + 1
                    elif base == "G":
                    G = G + 1
                    elif base == "C":
                    C = C + 1
                    return A,T,G,C


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in zip("ATGC", counts):
                    print(base, count)





                    share|improve this answer


























                    • The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                      – user4400727
                      Nov 24 '18 at 15:25











                    • so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                      – motyzk
                      Nov 24 '18 at 15:37













                    • can you please update your answer with this solution?

                      – user4400727
                      Nov 24 '18 at 15:41











                    • OK, just did...

                      – motyzk
                      Nov 24 '18 at 15:45











                    • You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                      – usr2564301
                      Nov 24 '18 at 16:25
















                    1














                    1) you have a bug - your return is indented one extra tab to the right



                    2) use a dict:



                    def base_counter(DNA):
                    dna_dict = {
                    "A": 0,
                    "T": 0,
                    "G": 0,
                    "C": 0,
                    }
                    for base in DNA:
                    if base == "A":
                    dna_dict["A"] += 1
                    elif base == "T":
                    dna_dict["T"] += 1
                    elif base == "G":
                    dna_dict["G"] += 1
                    elif base == "C":
                    dna_dict["C"] += 1
                    return dna_dict


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in counts.items():
                    print(base, count)


                    but if you have to keep the function as is:



                    def base_counter(DNA):
                    A = 0
                    T = 0
                    G = 0
                    C = 0
                    for base in DNA:
                    if base == "A":
                    A = A + 1
                    elif base == "T":
                    T = T + 1
                    elif base == "G":
                    G = G + 1
                    elif base == "C":
                    C = C + 1
                    return A,T,G,C


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in zip("ATGC", counts):
                    print(base, count)





                    share|improve this answer


























                    • The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                      – user4400727
                      Nov 24 '18 at 15:25











                    • so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                      – motyzk
                      Nov 24 '18 at 15:37













                    • can you please update your answer with this solution?

                      – user4400727
                      Nov 24 '18 at 15:41











                    • OK, just did...

                      – motyzk
                      Nov 24 '18 at 15:45











                    • You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                      – usr2564301
                      Nov 24 '18 at 16:25














                    1












                    1








                    1







                    1) you have a bug - your return is indented one extra tab to the right



                    2) use a dict:



                    def base_counter(DNA):
                    dna_dict = {
                    "A": 0,
                    "T": 0,
                    "G": 0,
                    "C": 0,
                    }
                    for base in DNA:
                    if base == "A":
                    dna_dict["A"] += 1
                    elif base == "T":
                    dna_dict["T"] += 1
                    elif base == "G":
                    dna_dict["G"] += 1
                    elif base == "C":
                    dna_dict["C"] += 1
                    return dna_dict


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in counts.items():
                    print(base, count)


                    but if you have to keep the function as is:



                    def base_counter(DNA):
                    A = 0
                    T = 0
                    G = 0
                    C = 0
                    for base in DNA:
                    if base == "A":
                    A = A + 1
                    elif base == "T":
                    T = T + 1
                    elif base == "G":
                    G = G + 1
                    elif base == "C":
                    C = C + 1
                    return A,T,G,C


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in zip("ATGC", counts):
                    print(base, count)





                    share|improve this answer















                    1) you have a bug - your return is indented one extra tab to the right



                    2) use a dict:



                    def base_counter(DNA):
                    dna_dict = {
                    "A": 0,
                    "T": 0,
                    "G": 0,
                    "C": 0,
                    }
                    for base in DNA:
                    if base == "A":
                    dna_dict["A"] += 1
                    elif base == "T":
                    dna_dict["T"] += 1
                    elif base == "G":
                    dna_dict["G"] += 1
                    elif base == "C":
                    dna_dict["C"] += 1
                    return dna_dict


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in counts.items():
                    print(base, count)


                    but if you have to keep the function as is:



                    def base_counter(DNA):
                    A = 0
                    T = 0
                    G = 0
                    C = 0
                    for base in DNA:
                    if base == "A":
                    A = A + 1
                    elif base == "T":
                    T = T + 1
                    elif base == "G":
                    G = G + 1
                    elif base == "C":
                    C = C + 1
                    return A,T,G,C


                    dna = "AAGCTACGTGGGTGACTTT"

                    counts = base_counter(dna)
                    for base, count in zip("ATGC", counts):
                    print(base, count)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 24 '18 at 15:44

























                    answered Nov 24 '18 at 15:03









                    motyzkmotyzk

                    1846




                    1846













                    • The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                      – user4400727
                      Nov 24 '18 at 15:25











                    • so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                      – motyzk
                      Nov 24 '18 at 15:37













                    • can you please update your answer with this solution?

                      – user4400727
                      Nov 24 '18 at 15:41











                    • OK, just did...

                      – motyzk
                      Nov 24 '18 at 15:45











                    • You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                      – usr2564301
                      Nov 24 '18 at 16:25



















                    • The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                      – user4400727
                      Nov 24 '18 at 15:25











                    • so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                      – motyzk
                      Nov 24 '18 at 15:37













                    • can you please update your answer with this solution?

                      – user4400727
                      Nov 24 '18 at 15:41











                    • OK, just did...

                      – motyzk
                      Nov 24 '18 at 15:45











                    • You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                      – usr2564301
                      Nov 24 '18 at 16:25

















                    The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                    – user4400727
                    Nov 24 '18 at 15:25





                    The output has to be as shown in my post (i.e. (4, 6, 6, 3) and not {'A': 4, 'T': 6, 'G': 6, 'C': 3})

                    – user4400727
                    Nov 24 '18 at 15:25













                    so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                    – motyzk
                    Nov 24 '18 at 15:37







                    so, keep your function as is (just don't forget to unindent the return, and replace the function call code snippet with this: counts = base_counter(dna); for base, count in zip("ATGC", counts): print(base, count)

                    – motyzk
                    Nov 24 '18 at 15:37















                    can you please update your answer with this solution?

                    – user4400727
                    Nov 24 '18 at 15:41





                    can you please update your answer with this solution?

                    – user4400727
                    Nov 24 '18 at 15:41













                    OK, just did...

                    – motyzk
                    Nov 24 '18 at 15:45





                    OK, just did...

                    – motyzk
                    Nov 24 '18 at 15:45













                    You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                    – usr2564301
                    Nov 24 '18 at 16:25





                    You can omit the ifs in your dictionary approach (which I would prefer) by using the simpler for base in DNA: dna_dict[base] += 1. It's at the cost of not being able to handle invalid characters, but it would work for the OP. (Although adding an error handler should be easy, too.)

                    – usr2564301
                    Nov 24 '18 at 16:25











                    0














                    you can make another function for printing the results:



                    def print_bases(bases):
                    print("A "+str(bases[0])+"n"
                    "T "+str(bases[1])+"n"
                    "G "+str(bases[2])+"n"
                    "C "+str(bases[3]))
                    print_bases(counts)





                    share|improve this answer
























                    • Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                      – user4400727
                      Nov 24 '18 at 16:44













                    • from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                      – prophet-five
                      Nov 24 '18 at 16:51













                    • should i ask a new question? hard to understand without an example of what you just mentioned.

                      – user4400727
                      Nov 24 '18 at 16:57











                    • no, just edit this one, I'll edit my answer

                      – prophet-five
                      Nov 24 '18 at 17:01











                    • @user3683803 edited my first answer

                      – prophet-five
                      Nov 24 '18 at 17:08
















                    0














                    you can make another function for printing the results:



                    def print_bases(bases):
                    print("A "+str(bases[0])+"n"
                    "T "+str(bases[1])+"n"
                    "G "+str(bases[2])+"n"
                    "C "+str(bases[3]))
                    print_bases(counts)





                    share|improve this answer
























                    • Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                      – user4400727
                      Nov 24 '18 at 16:44













                    • from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                      – prophet-five
                      Nov 24 '18 at 16:51













                    • should i ask a new question? hard to understand without an example of what you just mentioned.

                      – user4400727
                      Nov 24 '18 at 16:57











                    • no, just edit this one, I'll edit my answer

                      – prophet-five
                      Nov 24 '18 at 17:01











                    • @user3683803 edited my first answer

                      – prophet-five
                      Nov 24 '18 at 17:08














                    0












                    0








                    0







                    you can make another function for printing the results:



                    def print_bases(bases):
                    print("A "+str(bases[0])+"n"
                    "T "+str(bases[1])+"n"
                    "G "+str(bases[2])+"n"
                    "C "+str(bases[3]))
                    print_bases(counts)





                    share|improve this answer













                    you can make another function for printing the results:



                    def print_bases(bases):
                    print("A "+str(bases[0])+"n"
                    "T "+str(bases[1])+"n"
                    "G "+str(bases[2])+"n"
                    "C "+str(bases[3]))
                    print_bases(counts)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 16:19









                    prophet-fiveprophet-five

                    1089




                    1089













                    • Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                      – user4400727
                      Nov 24 '18 at 16:44













                    • from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                      – prophet-five
                      Nov 24 '18 at 16:51













                    • should i ask a new question? hard to understand without an example of what you just mentioned.

                      – user4400727
                      Nov 24 '18 at 16:57











                    • no, just edit this one, I'll edit my answer

                      – prophet-five
                      Nov 24 '18 at 17:01











                    • @user3683803 edited my first answer

                      – prophet-five
                      Nov 24 '18 at 17:08



















                    • Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                      – user4400727
                      Nov 24 '18 at 16:44













                    • from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                      – prophet-five
                      Nov 24 '18 at 16:51













                    • should i ask a new question? hard to understand without an example of what you just mentioned.

                      – user4400727
                      Nov 24 '18 at 16:57











                    • no, just edit this one, I'll edit my answer

                      – prophet-five
                      Nov 24 '18 at 17:01











                    • @user3683803 edited my first answer

                      – prophet-five
                      Nov 24 '18 at 17:08

















                    Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                    – user4400727
                    Nov 24 '18 at 16:44







                    Is there a way to do it in the same function such that the function returns the tuple ((4, 6, 6, 3)) after running base_counter(dna) and then is able to print the table by print(counts))? So basically the opposite output of your first solution. Your first solution returns the table and prints the tuple.

                    – user4400727
                    Nov 24 '18 at 16:44















                    from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                    – prophet-five
                    Nov 24 '18 at 16:51







                    from the first solution take you can return a string (exactly whats written in the print function) and then when you call the function it will return a string, which you will be able to print with print() function

                    – prophet-five
                    Nov 24 '18 at 16:51















                    should i ask a new question? hard to understand without an example of what you just mentioned.

                    – user4400727
                    Nov 24 '18 at 16:57





                    should i ask a new question? hard to understand without an example of what you just mentioned.

                    – user4400727
                    Nov 24 '18 at 16:57













                    no, just edit this one, I'll edit my answer

                    – prophet-five
                    Nov 24 '18 at 17:01





                    no, just edit this one, I'll edit my answer

                    – prophet-five
                    Nov 24 '18 at 17:01













                    @user3683803 edited my first answer

                    – prophet-five
                    Nov 24 '18 at 17:08





                    @user3683803 edited my first answer

                    – prophet-five
                    Nov 24 '18 at 17:08


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53459358%2ftuple-to-table-from-counting-dna-sequences%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()