ivandre

DNA

Apr 25th, 2020
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. from sys import argv, exit
  2. import csv
  3. import cs50
  4.  
  5.  
  6. def main():
  7.     if len(argv) != 3:
  8.         print("Usage: python dna.py data.csv sequence.txt")
  9.         exit(1)
  10.     database = open(argv[1], "r")
  11.     readerDatabase = csv.DictReader(database)
  12.     genTypeList = readerDatabase.fieldnames
  13.     textFile = open(argv[2], "r")
  14.     sequence = textFile.read()
  15.         #creation of a dictionary with gentypes and empty lists
  16.     record = {name: [] for name in genTypeList}
  17.     #eliminate first element containing name to make it easier work with
  18.     #the genes types
  19.     record.pop('name')
  20.     counter = 0
  21.         #i------8
  22.         #iteration through each gentype
  23.     for i in range(1, len(genTypeList)):
  24.         for j in range(len(sequence)):
  25.             counter = 0
  26.             #string slicing
  27.             gen = sequence[j:j + len(genTypeList[i])]
  28.             if gen == genTypeList[i]:
  29.                 counter = counter + 1
  30.                 record[genTypeList[i]].append(counter)
  31.                 while (True):
  32.                     j = j + len(genTypeList[i])
  33.                     #previous slice
  34.                     tmp = gen
  35.                     #posterior slice
  36.                     gen = sequence[j:j + len(genTypeList[i])]
  37.                     if tmp != gen:
  38.                         break
  39.                     else:
  40.                         counter = counter + 1
  41.                         record[genTypeList[i]].append(counter)
  42.             else:
  43.                 continue
  44.  
  45.     for names in record:
  46.         if len(record[names]) != 0:
  47.             maxx = max(record[names])
  48.             record[names] = maxx
  49.         else:
  50.             print("No match")
  51.             return
  52.     #variable match will count how many times the genes matches with the records
  53.     #of the sequence
  54.     match = 0
  55.     for linea in readerDatabase:
  56.         #iteration over each type of gen AGATC, AATG ...
  57.         match = 0
  58.         for iteration in range(1, len(genTypeList)):
  59.             a = int(linea[genTypeList[iteration]])
  60.             b = int(record[genTypeList[iteration]])
  61.             if a == b:
  62.                 match = match + 1
  63.                 if match == len(genTypeList) - 1:
  64.                     print(linea["name"])
  65.                     return
  66.                 else:
  67.                     continue
  68.     #assuming we dont have more than 2 matches between two
  69.     #persons
  70.     if match <= 2:
  71.         print("No match")
  72.         return
  73.  
  74.     textFile.close()
  75.     database.close()
  76.  
  77.  
  78. main()
Advertisement
Add Comment
Please, Sign In to add comment