-
Notifications
You must be signed in to change notification settings - Fork 0
/
freqDNA.py
62 lines (53 loc) · 2.21 KB
/
freqDNA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
__author__="chiyuyen"
__date__ ="$Sep 23, 2014 10:57:04 AM$"
# Lab 2, Program 4
#
# Given a DNA sequence, find the percentages of nucleotides in the sequence
# that are A's, C's, T's, and G's.
#
# This program is based largely on Program 3. In order to find the
# percentages, we need to count the A's, C's, T's, and G's, then divide
# each by the total number of nucleotides in the sequence.
#
# There are two things going on here that are new to us:
#
# * Because we want to divide by the length of the DNA sequence, it would
# be handy if we could find its length. There is a function in Python
# called "len" (short for length), which figures out the length of a
# sequence of things that you pass to it. Since a string is a sequence
# of characters, len tells you the number of characters in the string.
#
# * The counts (like aCount) and the length of the DNA sequence are both
# integers. If we divide one by the other, according to the rules of
# Python, we get an integer as a result. So, we have to tell Python
# that we're interested in getting a decimal result. Decimal numbers
# in Python are called "floats" (short for "floating-point numbers,"
# which alludes to the way that computers store them in memory). If
# we want to divide two integers, i and j, but get a decimal result, we
# can do so by first converting one of them to a float; this tells
# Python that decimal digits are important to us. Here's how you can
# do it:
#
# float(i) / j
#
# It makes no difference which one you convert, so long as you convert
# at least one of them.
dnaSequence = "atcgatgagagctagcgata"
aCount = 0
cCount = 0
tCount = 0
gCount = 0
for c in dnaSequence:
if c == 'a':
aCount = aCount + 1
elif c == 'c':
cCount = cCount + 1
elif c == 't':
tCount = tCount + 1
elif c == 'g':
gCount = gCount + 1
sequenceLength = len(dnaSequence)
print (("Percentage of A's in sequence:"), (float(aCount) / sequenceLength) * 100)
print (("Percentage of C's in sequence:"), (float(cCount) / sequenceLength) * 100)
print (("Percentage of T's in sequence:"), (float(tCount) / sequenceLength) * 100)
print (("Percentage of G's in sequence:"), (float(gCount) / sequenceLength) * 100)