In Python, using IDLE 3.4 (x64), I have created a two-dimensional list of 3 rows and 2 columns. I then had the list populate with random numbers.
Now I want to display the sum values in the rows and then display the sum of the columns, and I'm at a loss for how to do that.
Here is what I have so far:
import random
def main():
#Create nested loop
rows = 3
cols = 2
values = [[0,0],
[0,0],
[0,0]]
#Generate random integers in list
for r in range(rows):
for c in range(cols):
values[r][c] = random.randint(1, 100)
#Display results
print(values)
main()
Here is what I got:
import random
def main():
#Create nested loop
rows = 3
cols = 2
values = [[0,0],
[0,0],
[0,0]]
#Generate random integers in list
for r in range(rows):
for c in range(cols):
values[r][c] = random.randint(1, 100)
#Display results
print(values)
sum_2 = 0
sum_3 = 0
sum_2_list = []
sum_3_list = []
length = len(values)
for num in range(0, length):
sum = values[num][0] + values[num][1]
print('{} + {} = {}'.format(values[num][0], values[num][1], sum))
for num2 in range(0, length):
sum_2 = sum_2 + values[num2][0]
sum_3 = sum_3 + values[num2][1]
print('{} + {} + {} = {}'.format(values[0][0], values[1][0], values[2][0], sum_2))
print('{} + {} + {} = {}'.format(values[0][1], values[1][1], values[2][1], sum_3))
Related
I'm wondering if anyone knows any tools which allow me to count the frequency of amino acids at any specific position in a multiple-sequence alignment.
For example if I had three sequences:
Species 1 - MMRSA
Species 2 - MMLSA
Species 3 - MMRTA
I'd like for a way to search by position for the following output:
Position 1 - M = 3;
Position 2 - M = 3;
Position 3 - R = 2, L = 1;
Position 4 - S = 2, T = 1;
Position 5 - A = 3.
Thanks! I'm familiar with R and Linux, but if there's any other software that can do this I'm sure I can learn.
Using R:
x <- read.table(text = "Species 1 - MMRSA
Species 2 - MMLSA
Species 3 - MMRTA")
ixCol = 1
table(sapply(strsplit(x$V4, ""), "[", ixCol))
# M
# 3
ixCol = 4
table(sapply(strsplit(x$V4, ""), "[", ixCol))
# S T
# 2 1
Depending input file format, there are likely a purpose built bioconductor packages/functions.
That is really easy to parse, you can use any language of choice.
Here is an example in Python using a dict and Counter to assemble the data in a simple object.
from collections import defaultdict, Counter
msa = '''
Species 1 - MMRSA
Species 2 - MMLSA
Species 3 - MMRTA
'''
r = defaultdict(list) #dictionary having the sequences index as key and the list of aa found at that index as value
for line in msa.split('\n'):
line = line.strip()
if line:
sequence = line.split(' ')[-1]
for i, aa in enumerate(list(sequence)):
r[i].append(aa)
count = {k:Counter(v) for k,v in r.items()}
print(count)
#{0: Counter({'M': 3}), 1: Counter({'M': 3}), 2: Counter({'R': 2, 'L': 1}), 3: Counter({'S': 2, 'T': 1}), 4: Counter({'A': 3})}
To print the output as you specified:
for k, v in count.items():
print(f'Position {k+1} :', end=' ') #add 1 to start counting from 1 instead of 0
for aa, c in v.items():
print(f'{aa} = {c};', end=' ')
print()
It prints:
Position 1 : M = 3;
Position 2 : M = 3;
Position 3 : R = 2; L = 1;
Position 4 : S = 2; T = 1;
Position 5 : A = 3;
Can I convert the two arrays start,end into an adjacency list? or a 2d array so something like edge of a graph?
from typing import List
def max_network_rank(starts: List[int], ends: List[int], n: int) -> int:
adj = [0] * (n + 1)
for a, b in zip(starts, ends):
adj[a] += 1
adj[b] += 1
max_rank = 0
for a, b in zip(starts, ends):
max_rank = max(max_rank, adj[a] + adj[b] - 1)
return max_rank
Harshad/Niven numbers are positive numbers that are divisible by the sum of their digits. All single-digit numbers are Harshad numbers.
For example, 27 is a Harshad number as 2 + 7 = 9, and 9 is a divisor of 27.Count a(n) = the number of Niven (Harshad) numbers exceeding n (n<=1e12)
We can define the function checkHarshad(n) (Calculate sum of digits of n and check whether n%S(n)), then count the number of Harshad numbers in range [1..n]. But the program just run fast if n <= 1e7
Hiroaki Yamanouchi had a python code that calculate a(10^n), but I don't understand it, and I don't think it's helpful to my problem if n <= 1e12.
def number_of_niven_numbers(digits):
"""
- Count the number of Niven numbers
less than or equal to 10^digits.
"""
N = digits
ret = 0
cnts = [0] * (digits + 1)
for digit_sum in range(1, N * 9 + 1):
curr = [[0] * digit_sum]
curr[0][0] = 1
next_mods = [0] * digit_sum
for i in range(digit_sum):
next_mods[i] = 10 * i % digit_sum
for left_digits in range(N):
left_sum_max = min(9 * left_digits, digit_sum)
next = [[0] * digit_sum for _ in range(min(left_sum_max + 9, digit_sum) + 1)]
for left_sum in range(left_sum_max + 1):
for left_mod in range(digit_sum):
cnt = curr[left_sum][left_mod]
if cnt == 0:
continue
next_mod_base = next_mods[left_mod]
for next_digit in range(min(min(digit_sum, 9), digit_sum - left_sum) + 1):
next_sum = left_sum + next_digit
next_mod = next_mod_base + next_digit
if next_mod >= digit_sum:
next_mod -= digit_sum
next[next_sum][next_mod] += cnt
curr = next
if digit_sum < len(curr):
cnts[left_digits + 1] += curr[digit_sum][0]
return cnts[N] + 1
So, how can we calculate a(n) up to 1e12?
I have the following algorithm that adds t consecutive numbers starting at s.
Addup (s,t)
Make result = s
For i from s to t-1
Result = Result + s + i
Return Result
So Addup (3,3) = 3+4+5 = 12 & Addup (4,5) = 4+5+6+7+8
How can this be made recursive?
Q
In Python, for example:
def Addup(s,t):
if t == 1:
return s
else:
return s + Addup(s+1, t-1)
I am trying to make a plot of three column vectors right now in order to make a three dimensional surface plot, but the output is just a discontinuous line. Any help is appreciated, code is below. Apologies in advance for the confusing variable names.
co = 29;
BF = .0446;
WPW = 50;
E = [0:0.01:2]; //sets up a column vector
p = [];
WBR =[];
w = [];
t = 8.64E13;
delta = [0:0.5:100];
R =[];
DeltaMu = [];
Total = [];
//begin program iteration through k; change "k" value in for loop to change
//the number of iterations the program runs over, and thus the amount
//of degrees of freedom
k = 200;
for i = 1:k,
I = 12.5 + 0.167*i;
mu = I/co;
sigma = .11*mu;
cdf = cdfnor("PQ", E, mu*ones(E), sigma*ones(E));
n = 201; // variable over which the loop is iterated
pdf = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = cdf(n) - cdf(n-1); //assigns a value to the temp variable every pass
pdf(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
temp = cdf(n);
pdf(n) = temp;
n = 201;
while n > 1,
a = exp(-WPW*exp(-delta(n)*(1-E)));
n = n-1;
end
n = 201; // variable over which the loop is iterated
prob = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = a(n)*pdf(n); //assigns a value to the temp variable every pass
prob(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
WBR(i) = sum(prob)*BF
w(i) = mu
end
//begin program iteration through k; change "k" value in for loop to change
//the number of iterations the program runs over, and thus the amount
//of degrees of freedom
k = 200;
for i = 1:k,
mu = .5*i;
sigma = .1*mu;
cdf = cdfnor("PQ", delta, mu*ones(delta), sigma*ones(delta));
n = 201; // variable over which the loop is iterated
pdf = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = cdf(n) - cdf(n-1); //assigns a value to the temp variable every pass
pdf(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
temp = cdf(n);
p = 1-(exp(-t*exp(-delta)));
n = 201; // variable over which the loop is iterated
Psw = zeros(201,1); // sets up an appendable matrix of all zeros
temp = 0; //initiates a temporary integer variable
while n > 1,
temp = p(n)*pdf(n); //assigns a value to the temp variable every pass
Psw(n) = temp; //assigns the temp value to the nth slot in the
//column vector, works through the matrix backwards
n = n-1; //iterates the while loop on n
end //breaks from while loop after n hits 1
R(i) = sum(Psw)
DeltaMu(i) = mu
end
n = 200;
while n > 1,
Total(n) = WBR(n) + R(n);
n = n-1;
end
xdel(winsid()); //close any open graphs
plot3d(WBR, R, Total)
To plot a surface with plot3d, you need:
a vector of x-values
a vector of y-values
a matrix of z-values, where the (i,j) entry will determine the height over the point (x(i),y(j))
Toy example:
plot3d([1 2 3], [2 3 4], [0 1 2; 2 3 2; 0 2 1])
There is no mathematically reasonable to make a surface plot from three column vectors. What you could do with them is draw a parametric curve, which uses three vectors for x,y,z coordinates:
param3d(WBR, R, Total)
With your data, the result is still unspectacular because of the high dynamic range with the arrays. Consider plotting on the logarithmic scale.