Download presentation

Presentation is loading. Please wait.

Published byShayna Robart Modified about 1 year ago

1
#!/usr/bin/env python """ quadequat.py It finds the real roots of a quadratic equation Author: Antonio Deiana.... Date: 28/11/ """ from math import sqrt def findroots(a, b, c, toll): x1 = x2 = 0 error = 0 delta = b ** * a * c if delta > 0 and delta > toll: x1 = (b * -1 + sqrt(delta)) / (2 * a) x2 = (b * -1 - sqrt(delta)) / (2 * a) elif delta > 0 and delta < toll: x1 = x2 = b * -1 / (2 * a) else: error = 1 return x1, x2, error

2
def main(): a = float(raw_input("Coefficient a: ")) b = float(raw_input("Coefficient b: ")) c = float(raw_input("Coefficient c: ")) toll = 1E-6 x1, x2, error = findroots(a, b, c, toll) if error == 0: if x1 != x2: print "Root 1: " + str(x1) print "Root 2: " + str(x2) else: print "Root: " + str(x1) else: print "No real roots“ if __name__=="__main__": main()

3
""" bisection.py Compute the zero of a function with the bisection method """ def bisect(xs, xd, toll, niter): cont = rad = 0 if f(xs) * f(xd) > 0: cont = -1 return rad, cont for i in range(niter): xm = 0.5 * (xs + xd) if (xm - xs) < toll: cont = 0 rad = xm return rad, cont if f(xs) * f(xm) < 0: xd = xm else: xs = xm cont = niter return rad, cont

4
def f(x): from math import exp return x + exp(x) def main(): x0 = float(raw_input("Left margin: ")) x1 = float(raw_input("Right margin: ")) toll = float(raw_input("Tollerance: ")) niter = int(raw_input("Number of iterations: ")) if x0 < x1: xs = x0 xd = x1 else: xs = x1 xd = x0 rad, cont = bisect(xs, xd, toll, niter) if cont == 0: print "Zero at " + str(rad) elif cont < 0: print "No zero exists" else: print "No zero found in " + str(niter) + " iterations“ if __name__=="__main__": main()

5
#!/usr/bin/env python from math import sqrt def get_mean(listvalues): mean = mean2 = stdev = 0 nv = len(listvalues) for value in listvalues: mean += value mean2 += value ** 2 mean /= nv stdev = sqrt((nv / (nv - 1)) * ((mean2 / nv) - (mean ** 2))) return mean, stdev def main(): nval = int(raw_input("\nNumber of values: ")) values = [] for n in range(nval): value = float(raw_input("Value n. " + str(n+1) + ": ")) values.append(value) mean, stdev = get_mean(values) print "The arithmetic mean is %10.2f +/- %10.2f“ \ % (mean, stdev) if __name__=="__main__": main()

6
#/usr/bin/env python """ mcd.py """ def mcd(m,n): if m < n: temp = m m = n n = temp while n > 0: r = m % n m = n n = r return m def main(): m = int(raw_input("\nFirst number: ")) n = int(raw_input("Second number: ")) div = mcd(m,n) print "The MCD is " + str(div) if __name__==“__main__”: main()

7
#!/usr/bin/env python """ table2file.py """ nfile = int(raw_input("\nNumbers of files: ")) maxlenlist = 0 tablists = [] for i in range(nfile): filename = raw_input("Name " + str(i+1) + " file: ") file = open(filename, "r") values = file.readlines() file.close() tablists.append(values) if maxlenlist < len(values): maxlenlist = len(values) filename = raw_input("\nName output file: ") file = open(filename, "w") for i in range(maxlenlist): string = "" for j in range(nfile): if i < len(tablists[j]): string += tablists[j][i].strip() + " " else: string += "0.0 " print >> file, string file.close()

8
#!/usr/bin/env python """ ncbi2fasta.py Convert a file from NCBI to FASTA format ""“ filename = raw_input("\nName of input file: ") file = open(filename, "r") file_lines = file.readlines() file.close() filename = raw_input("\nName of output file: ") file = open(filename, "w") for line in file_lines: if line == "\n": continue if line[0] == ">": print >> file, line.strip() else: string = "" splitted = line.split() for i in range(1, len(splitted)): string += splitted[i] print >> file, string.upper() file.close()

9
#!/usr/bin/env python """ fltlen.py Select sequences with a length between a minimum and a maximum value """ filename = raw_input("\nFile sequences: ") file = open(filename, "r") sequname = [] sequlist = [] first = 1 for line in file.readlines(): if line == "\n": continue if line[0] == ">": if first == 1: first = 0 else: sequlist.append(sequence) sequname.append(line[1:].rstrip()) sequence = "" else: sequence += linea.strip() sequlist.append(sequence) file.close()

10
lenmin = int(raw_input("\nMinimum length of the sequence: ")) lenmax = int(raw_input("Maximum length of the sequence: ")) filename = raw_input("\nName of output file: " ) file = open(filename, "w") n = 0 for i in range(len(sequlist)): if len(sequlist[i]) > lenmin and \ len(sequlist[i]) < lenmax: n += 1 print >> file, ">" + sequname[i] print >> file, sequlist[i] print "\nN. sequences selected: " + str(n) + "\n“ file.close()

11
""" table.py Read a table of numbers and build a series of files, one for each column of the table """ filename = raw_input("\nName input file: ") file = open(filename, "r") file_lines = file.readlines() file.close() filebase = raw_input("\nPrefix output files: ") splitted = file_lines[0].split() ncol = len(splitted) for i in range(ncol): filename = filebase + ".col" + str(i) + ".txt" file = open(filename, "w") for line in file_lines: splitted = line.split() print >> file, splitted[i] file.close()

12
#!/usr/bin/env python """ freq.py Compute the frequencies of the amino acids in protein sequences ""“ import numarray alphabet =["A","R","N","D","C","Q","E","G","H","I",\ "L","K","M","F","P","S","T","W","Y","V"] alpha = {} for i in range(len(alphabet)): alpha[alphabet[i]] = i def frequencies(sequlist): naa = 0 sequf = numarray.resize([0.0], [20]) for sequence in sequlist: for i in range(len(sequence)): naa += 1 sequpar[alpha[sequence[i]]] += 1 sequf /= float(naa)

13
def main(): # import sequences from file filename = raw_input("\nFilename sequences: ") file = open(filename, "r") sequlist = [] first = 1 for line in file.readlines(): if line == "\n": continue if line[0] == ">": if first == 1: first = 0 else: sequlist.append(sequence) sequence = "" else: sequence += line.strip() sequlist.append(sequence) file.close() # compute amino acid frequencies sequf = frequencies(sequlist) # save results on file for i in range(20): print "Frequency of " + alphabet[i] + " : " + str(sequf[i]) if __name__=="__main__": main()

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google