User Tools

Site Tools


script:ex7

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
script:ex7 [2018/10/15 15:53]
dokuroot
script:ex7 [2018/10/15 16:00] (current)
dokuroot
Line 1: Line 1:
-=== Exercise8 ​Code ===+=== Exercise 7 Code ===
  
-<file python ​exercise8.py>+<file python ​exercise7.py>
  
-# Answer key to files notebook, exercise ​8+# Answer key to files notebook, exercise ​7
  
-'''​ +#7a 
- 8eMake a list, my_list, containing three values. Check that the object you +def start_codon(file_name): 
-  created is a list using the type() function. +    ​try:​ 
-'''​+        infile = open(file_name
 +    ​except:​ 
 +        return ​'File could not be opened' 
 +    with infile: 
 +            for line in infile: 
 +                if 'ATG' in line: 
 +                    return line 
 +            return -1
  
-my_list = [1,2,3] +start_codon('​seq.txt'​           
-type(my_list)+
  
  
-'''​ +#7b 
-8fMake the following three lists into a single matrix+def max_from_file(file_name): 
-''​'+    ​try:​ 
 +        infile = open(file_name) 
 +    except: 
 +        return -1 
 +    with infile: 
 +        greatest = infile.readline() 
 +        for line in infile: 
 +            if line > greatest: 
 +                greatest = line 
 +        return greatest 
 +     
 +max_from_file('numbers.txt')
  
-list1 = [1, 2, 3] 
-list2 = [4, 5, 6] 
-list3 = [7, 8, 9] 
-matrix = [list1, list2, list3] 
-matrix 
  
 +#7c
 +def transcriber(input_file_name,​ output_file_name):​
 +    try:
 +        infile = open(input_file_name)
 +        outfile = open(output_file_name,​ '​w'​)
 +    except:
 +        return -1
 +    revcomp = ''​
 +    with infile, outfile:
 +        for line in infile: ​   ​
 +            if '>'​ in line:
 +                outfile.write(line)
 +            else:
 +                line = line.replace('​T',​ '​U'​)
 +                outfile.write(line)
  
-# 8g) Using list indicesextract the value in row 2, column 2 from the matrix +transcriber('​sequences.txt'​'rna.txt')
-# in 8f.+
  
-matrix[1][1] 
  
 +#7d
 +def rev_comp(input_file_name,​ output_file_name):​
 +    try:
 +        infile = open(input_file_name)
 +        outfile = open(output_file_name,​ '​w'​)
 +    except:
 +        return -1
 +    revcomp = ''​
 +    with infile, outfile:
 +        nts = '​ACTG'​
 +        comps = '​TGAC'​
 +        translation_table = str.maketrans(nts,​ comps)
 +        for line in infile:
 +            line =line.rstrip()
 +            if '>'​ in line:
 +                outfile.write(line + '​rev_comp'​ + '​\n'​)
 +            else:
 +                revcomp = line.translate(translation_table)
 +                revcomp = revcomp[::​-1]
 +                outfile.write(revcomp + '​\n'​)
  
-# 8h) Using slicingextract the first two rows from the matrix in 8f.+rev_comp('​sequences.txt'​'​revcomp_sequencest.txt')
  
-matrix[:2] 
  
 +#7e
 +def motif_finder_line_by_line(file_name,​ motif):
 +    try:
 +        infile = open(file_name)
 +    except:
 +        return -1
 +    with infile:
 +        counts = 0
 +        for line in infile:
 +            if '>'​ in line:
 +                continue
 +            line = line.rstrip()
 +            for i in range(0, len(line)):
 +                if line[i:​i+len(motif)] == motif:
 +                    counts += 1
 +        return counts
  
-# 8i) Using an indexextract the last row of the matrix in 8f, assuming you don't know how many rows are in the matrix:+motif_finder('​c_elegans_chrI.fa'​, 'tagggcactt'​) ​           ​
  
-matrix[-1] 
  
 +#7f
 +def motif_finder_all(file_name,​ motif):
 +    try:
 +        infile = open(file_name)
 +    except:
 +        return -1
 +    with infile:
 +        counts = 0
 +        file = infile.read()
 +        file = file.replace('​\n',​ ''​)
 +        for i in range(0, len(file)):
 +            if file[i:​i+len(motif)] == motif:
 +                counts += 1
 +        return counts
  
-# 8j) Write a function, element_replace(matrixn, i, c), that replaces the element in the nth row and ith column with +motif_finder('​c_elegans_chrI.fa'​'​tagggcactt'​)     
-    ​value c and returns a new matrix with the revision.+
  
-def element_replace(matrix,​ n, i, c): 
-    new_matrix = matrix 
-    new_matrix[n-1][i-1] = c 
-    return new_matrix 
- 
-element_replace(matrix,​ 1, 2, '​a'​) 
- 
- 
-# 8k) Write a function called sum_rows(matrix) that sums the elements in each row in a matrix and returns the values as a 
-#     list. Hint: use a nested for loop. The outer for loop will iterate through each row and the nested for loop will 
-#     ​iterate through each element in each row. 
- 
-def sum_rows(matrix):​ 
-    totals_list = [] 
-    for i in range(len(matrix)):​ 
-        total = 0 
-        for n in range(len(matrix)):​ 
-            total += matrix[i][n] 
-        totals_list.append(total) 
-    return totals_list 
-        ​ 
-sum_rows(matrix) 
- 
- 
-# 8l) Write a function called sum_columns(matrix) that sums the elements in each column in a matrix and returns the values 
-#     as a list. 
- 
-def sum_columns(matrix):​ 
-    totals_list = [] 
-    for i in range(len(matrix)):​ 
-        total = 0 
-        for n in range(len(matrix[i])):​ 
-            total += matrix[n][i] 
-        totals_list.append(total) 
-    return totals_list 
-        ​ 
-sum_columns(matrix) 
  
 </​file>​ </​file>​
script/ex7.txt · Last modified: 2018/10/15 16:00 by dokuroot