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;
}
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
add a comment |
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
add a comment |
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
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
python dna-sequence
edited Nov 24 '18 at 18:20
asked Nov 24 '18 at 14:51
user4400727
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
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
add a comment |
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
add a comment |
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)
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 thereturn
, 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 theif
s in your dictionary approach (which I would prefer) by using the simplerfor 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
add a comment |
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)
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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 15:37
b-fgb-fg
2,09811724
2,09811724
add a comment |
add a comment |
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)
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 thereturn
, 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 theif
s in your dictionary approach (which I would prefer) by using the simplerfor 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
add a comment |
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)
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 thereturn
, 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 theif
s in your dictionary approach (which I would prefer) by using the simplerfor 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
add a comment |
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)
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)
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 thereturn
, 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 theif
s in your dictionary approach (which I would prefer) by using the simplerfor 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
add a comment |
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 thereturn
, 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 theif
s in your dictionary approach (which I would prefer) by using the simplerfor 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
if
s 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
if
s 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
add a comment |
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)
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
|
show 3 more comments
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)
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
|
show 3 more comments
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)
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)
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
|
show 3 more comments
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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