Advertisement
Guest User

DNA CS50

a guest
Oct 15th, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import csv
  2. import sys
  3.  
  4. database=[]
  5.  
  6.  
  7. def main():
  8.  
  9.  
  10. # TODO: Check for command-line usage
  11.  
  12. if len(sys.argv) != 3:
  13. sys.exit("Usage:FILENAME")
  14.  
  15. # TODO: Read database file into a variable
  16.  
  17. filename=sys.argv[1]
  18. with open(filename) as file:
  19. reader=csv.DictReader(file)
  20. for row in reader:
  21. database.append(row)
  22.  
  23.  
  24. # TODO: Read DNA sequence file into a variable
  25. sequences=[]
  26. filename_2=sys.argv[2]
  27. with open(filename_2) as file_2:
  28. reader_2=csv.DictReader(file_2)
  29. for row in reader_2:
  30. sequences.append(row)
  31.  
  32. # TODO: Find longest match of each STR in DNA sequence
  33.  
  34. n = len(sequences)
  35. k = len(database[0])
  36.  
  37. max_matches = max_sum(sequences, n, k)
  38.  
  39.  
  40. def max_sum(sequences,n,k):
  41. for i in range(n-k+1):
  42. current_sum=0
  43. for j in range(k):
  44. current_sum += int(sequences[i + j]['sequence'])
  45. max_sum = max(current_sum, max_sum)
  46. return max_sum
  47.  
  48.  
  49. # TODO: Check database for matching profiles
  50. for person in database:
  51. match = True
  52.  
  53. # Check each STR in the database against the sequences
  54. for str_name, str_count in person.items():
  55. if str_name == 'name':
  56. continue
  57.  
  58. str_count = int(str_count)
  59. if str_count != max_matches[str_name]:
  60. match = False
  61. break
  62.  
  63. if match:
  64. print(person['name'])
  65. break # Exit the loop if a match is found
  66.  
  67. # If no match is found
  68. else:
  69. print("No match")
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement