diana.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. DiaNA - 2020 - by psy (epsylon@riseup.net)
  5. You should have received a copy of the GNU General Public License along
  6. with DiaNA; if not, write to the Free Software Foundation, Inc., 51
  7. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8. """
  9. VERSION = "v0.3_beta"
  10. RELEASE = "19032020"
  11. SOURCE1 = "https://code.03c8.net/epsylon/diana"
  12. SOURCE2 = "https://github.com/epsylon/diana"
  13. CONTACT = "epsylon@riseup.net - (https://03c8.net)"
  14. """
  15. DNA-equiv:
  16. A <-> T
  17. C <-> G
  18. """
  19. import re, os, glob, random, time, math
  20. brain_path = "resources/BRAIN/brain.in" # in/out brain-tmp file
  21. genomes_path = 'datasets/' # genome datasets raw data
  22. genomes_list_path = "datasets/genome.list" # genome list
  23. universal_primer_list_path = "resources/PATTERNS/UPL.list" # UPL list
  24. dna_codons_list_path = "resources/PATTERNS/DNAcodon.list" # DNA codon list
  25. genomes = {} # main sources dict: genome_name
  26. seeds_checked = [] # list used for random checked patterns
  27. repeats = {} # repetitions 'tmp' dict: genome_name:(repets,pattern)
  28. known_patterns = [] # list used for known patterns
  29. max_length = 50 # [MAX. LENGTH] for range [PATTERN]
  30. def convert_size(size):
  31. if (size == 0):
  32. return '0 B'
  33. size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
  34. i = int(math.floor(math.log(size,1024)))
  35. p = math.pow(1024,i)
  36. s = round(size/p,2)
  37. return s, size_name[i]
  38. def search_pattern_with_human():
  39. pattern = input("[HUMAN] [SEARCH] Pattern (ex: attacg): ").upper()
  40. print("\n"+"-"*5 + "\n")
  41. create_new_pattern(pattern) # create new pattern
  42. def try_pattern_against_all_genomes_by_genome(pattern):
  43. for k, v in genomes.items():
  44. if pattern in v:
  45. t = len(re.findall(pattern, v))
  46. repeats[k] = t, pattern # create dict: genome = times, pattern
  47. def try_pattern_against_all_genomes_by_pattern(pattern, index):
  48. p_index = 0 # pattern index
  49. for k, v in genomes.items():
  50. if pattern in v:
  51. p_index = p_index + 1
  52. t = len(re.findall(pattern, v))
  53. repeats[index,p_index] = pattern, k, t # create dict: index, p_index = pattern, genome, times
  54. def sanitize_dna_pattern(pattern):
  55. valid_pattern = True
  56. for c in pattern:
  57. if c == "A":
  58. pass
  59. elif c == "T":
  60. pass
  61. elif c == "G":
  62. pass
  63. elif c == "C":
  64. pass
  65. elif c == "N":
  66. pass
  67. else:
  68. valid_pattern = False
  69. return valid_pattern
  70. def teach_ai():
  71. mode = input("[TRAIN-AI] MODE -> (H)uman, (A)utomata: ").upper()
  72. if not os.path.isfile(brain_path):
  73. create_initial_seed_file()
  74. if mode == "H": # human mode
  75. teach_ai_human_mode()
  76. else: # libre AI
  77. teach_ai_automata_mode() # automata mode
  78. def teach_ai_human_mode(): # search/discard patterns with human interaction & generate local database
  79. search_patterns_lesson_with_a_human()
  80. def search_patterns_lesson_with_a_human():
  81. print("\n"+"-"*30)
  82. print("\n[TRAIN-AI] [HUMAN] [STOP] this mode; just entering whatever invalid pattern (ex: 'exit' or 'q').\n")
  83. key = "K" # continue
  84. while key == "K":
  85. pattern = input("[TRAIN-AI] [HUMAN] [LOOP] [SEARCH] Pattern (ex: attacg): ").upper()
  86. print("\n"+"-"*5 + "\n")
  87. key = search_pattern_on_lesson(pattern)
  88. if key == "Z": # stop
  89. break
  90. def search_pattern_on_lesson(pattern):
  91. valid_pattern = sanitize_dna_pattern(pattern)
  92. if valid_pattern == True:
  93. key = search_pattern_on_local_database(pattern) # search pattern on local database
  94. else:
  95. print("[ERROR] -> Invalid DNA pattern ... [EXITING!]\n")
  96. key = "Z" # stop
  97. return key
  98. def search_pattern_on_local_database(pattern):
  99. f=open(brain_path, 'r')
  100. memory = f.read().replace('\n',' ')
  101. f.close()
  102. patterns_known = 0
  103. if not "'"+pattern+"'" in memory: # always create new patterns
  104. create_new_pattern(pattern) # create new pattern
  105. patterns_known = patterns_known + 1
  106. else:
  107. for k, v in genomes.items(): # create patterns found for new genomes
  108. if k not in memory:
  109. create_new_pattern(pattern) # create new pattern
  110. patterns_known = patterns_known + 1
  111. if patterns_known == 0:
  112. print("[TRAIN-AI] [AUTOMATA] [LOOP] [RESULTS] -ALREADY- [LEARNED!] ... -> [GOING FOR NEXT!]\n")
  113. print("-"*5 + "\n")
  114. key = "K" # continue
  115. return key
  116. def create_initial_seed_file():
  117. f=open(brain_path, 'w')
  118. f.write("")
  119. f.close()
  120. def create_new_pattern(pattern): # append it to brain
  121. valid_pattern = sanitize_dna_pattern(pattern)
  122. if valid_pattern == True:
  123. if pattern not in known_patterns:
  124. known_patterns.append(pattern)
  125. try_pattern_against_all_genomes_by_genome(pattern) # generate repeats dict
  126. patterns_found = 0
  127. for k, v in repeats.items(): # list patterns found to output
  128. print (" *", k +":", "-> ",v,"")
  129. patterns_found = patterns_found + 1
  130. print("")
  131. if patterns_found == 0:
  132. print("[INFO] -> Not any found! ... [EXITING!]\n")
  133. else:
  134. f=open(brain_path, 'a')
  135. f.write(str(repeats)+os.linesep) # add dict as str
  136. f.close()
  137. else:
  138. print("[ERROR] -> Invalid DNA pattern ... [EXITING!]\n")
  139. def teach_ai_automata_mode(): # search patterns by bruteforcing ranges & generate local database
  140. search_patterns_lesson_with_an_ai()
  141. def search_patterns_lesson_with_an_ai():
  142. print("\n"+"-"*30)
  143. print("\n[TRAIN-AI] [AUTOMATA] [STOP] this mode; pressing 'CTRL+z'.\n")
  144. ranges = input("[TRAIN-AI] [AUTOMATA] [SEARCH] Set range (x<y) for pattern deep searching (ex: 2-8): ")
  145. print ("")
  146. valid_range, ranged_permutations = check_for_deep_searching_ranges(ranges)
  147. if str(valid_range) == "OK!":
  148. ranged_ending = False
  149. print("-"*15)
  150. print("\n[TRAIN-AI] [AUTOMATA] [SEARCH] Number of [PERMUTATIONS] estimated: [ "+str(ranged_permutations)+" ]\n")
  151. print("-"*15+"\n")
  152. num_pat = 0
  153. while ranged_ending == False: # try to STOP it using: CTRL-z
  154. try:
  155. pattern, ranged_ending = generate_random_pattern(ranges, ranged_permutations) # generate random seed
  156. if pattern:
  157. num_pat = num_pat + 1
  158. print("[TRAIN-AI] [AUTOMATA] [LOOP] [SEARCH] Generating [RANDOM!] ["+str(num_pat)+"/"+str(ranged_permutations)+"] pattern: [ " + str(pattern) + " ]\n")
  159. if not num_pat == ranged_permutations:
  160. search_pattern_on_lesson(pattern)
  161. else:
  162. search_pattern_on_lesson(pattern)
  163. print("[TRAIN-AI] [AUTOMATA] [RESULTS]: REVIEWED -> [ "+str(ranged_permutations)+" PERMUTATIONS ] ... -> [EXITING!]\n")
  164. ranged_ending = True
  165. except:
  166. pass
  167. else:
  168. print("-"*15+"\n")
  169. print("[TRAIN-AI] [AUTOMATA] [ERROR] -> [INVALID!] Deep Learning [RANGE] -> "+valid_range+" ... [EXITING!]\n")
  170. def generate_random_pattern(ranges, ranged_permutations):
  171. ranged_length = 0
  172. try:
  173. range_low = int(ranges.split("-")[0])
  174. range_high = int(ranges.split("-")[1])
  175. for i in range(range_low, range_high+1):
  176. ranged_length = ranged_length + 1
  177. if ranged_length == ranged_permutations: # all possible variables have been bruteforced/checked! -> exit
  178. pattern = None
  179. ranged_ending = True
  180. return pattern, ranged_ending
  181. else:
  182. ranged_ending = False
  183. seed = [random.randrange(0, 4) for _ in range(i)] # generate "random" seed
  184. if seed not in seeds_checked:
  185. seeds_checked.append(seed)
  186. pattern = ""
  187. for n in seed:
  188. if n == 0:
  189. pattern += "A"
  190. elif n == 1:
  191. pattern += "C"
  192. elif n == 2:
  193. pattern += "T"
  194. else:
  195. pattern += "G"
  196. return pattern, ranged_ending
  197. except:
  198. print("[TRAIN-AI] [AUTOMATA] [ERROR] -> [INVALID!] Deep Learning [RANGE] ... [EXITING!]\n")
  199. pattern = None
  200. ranged_ending = True
  201. return pattern, ranged_ending
  202. def check_for_deep_searching_ranges(ranges):
  203. try:
  204. range_low = ranges.split("-")[0]
  205. range_high = ranges.split("-")[1]
  206. except:
  207. valid_range = "'bad format'"
  208. try:
  209. range_low = int(range_low)
  210. except:
  211. valid_range = "'low range' should be an integer"
  212. try:
  213. range_high = int(range_high)
  214. except:
  215. valid_range = "'high range' should be an integer"
  216. try:
  217. if range_low < range_high:
  218. if range_low > 1: # always range > 1
  219. valid_range = "OK!"
  220. else:
  221. valid_range = "'low range' should be > than 1"
  222. else:
  223. valid_range = "'low range' should be < than 'high range'"
  224. except:
  225. valid_range = "'bad format'"
  226. try:
  227. ranged_permutations = math_ranged_permutations(range_low, range_high)
  228. except:
  229. ranged_permutations = 0
  230. valid_range = "'bad format'"
  231. return valid_range, ranged_permutations
  232. def math_ranged_permutations(range_low, range_high): # calculate ranged_permutations
  233. ranged_permutations = 0
  234. for i in range(range_low, range_high+1):
  235. ranged_permutations = ranged_permutations + (4**i)
  236. return ranged_permutations
  237. def libre_ai(): # show statistics / download new genomes / keep crossing new genomes with local database / search for new patterns (non stop!)
  238. if not os.path.isfile(brain_path):
  239. create_initial_seed_file()
  240. memory = examine_stored_brain_memory()
  241. if memory != "":
  242. #print("[LIBRE-AI] [STOP] this mode; pressing 'CTRL+z'.\n")
  243. libre_ai_show_statistics(memory) # show statistics
  244. def libre_ai_show_statistics(memory):
  245. print("[LIBRE-AI] [REPORTING] [STATISTICS] ... -> [STARTING!]\n")
  246. print("-"*15 + "\n")
  247. total_genomes = 0
  248. total_adenine = 0
  249. total_guanine = 0
  250. total_cytosine = 0
  251. total_thymine = 0
  252. total_any = 0
  253. total_patterns = 0
  254. secuence_length = 0
  255. secuences_length_list = {}
  256. largest = None
  257. largest_len = 0
  258. shortest_len = 0
  259. average = None
  260. shortest = None
  261. for k, v in genomes.items():
  262. secuence_length = len(v)
  263. secuences_length_list[k] = str(secuence_length)
  264. total_genomes = total_genomes + 1
  265. total_adenine = total_adenine + v.count("A")
  266. total_guanine = total_guanine + v.count("G")
  267. total_cytosine = total_cytosine + v.count("C")
  268. total_thymine = total_thymine + v.count("T")
  269. total_any = total_any + v.count("N")
  270. path = genomes_path # genome datasets raw data
  271. l = glob.glob(genomes_path+"*") # black magic!
  272. latest_collection_file = max(l, key=os.path.getctime)
  273. latest_collection_date = time.ctime(os.path.getmtime(latest_collection_file))
  274. total_nucleotids = [total_adenine, total_guanine, total_cytosine, total_thymine, total_any]
  275. num_total_nucleotids = total_adenine + total_guanine + total_cytosine + total_thymine + total_any
  276. nucleotid_more_present = max(total_nucleotids)
  277. print("[LIBRE-AI] [REPORTING] -STORAGE- [STATISTICS]: \n")
  278. extract_storage_sizes()
  279. print(" * [LATEST UPDATE]: '"+str(latest_collection_date)+"'\n")
  280. print(" + File: '"+str(latest_collection_file)+"'\n")
  281. print("-"*5 + "\n")
  282. print("[LIBRE-AI] [REPORTING] -COLLECTION- [STATISTICS]: \n")
  283. extract_total_patterns_learned_from_local(memory)
  284. print("\n"+"-"*5 + "\n")
  285. print("[LIBRE-AI] [REPORTING] -ANALYSIS- [STATISTICS]: \n")
  286. print(" * Total [DNA SECUENCES]: [ "+str(total_genomes)+" ]\n")
  287. largest = 0
  288. largest_pattern_name = []
  289. largest_pattern_size = []
  290. for k, v in secuences_length_list.items():
  291. if int(v) > int(largest):
  292. largest = v
  293. largest_pattern_name.append(k)
  294. largest_pattern_size.append(largest)
  295. for p in largest_pattern_name:
  296. largest_pattern_name = p
  297. for s in largest_pattern_size:
  298. largest_pattern_size = s
  299. print(" + [LARGEST] : "+str(largest_pattern_name)+ " [ "+str(largest_pattern_size)+" bp linear RNA ]")
  300. prev_shortest = None
  301. shortest_pattern_name = []
  302. shortest_pattern_size = []
  303. for k, v in secuences_length_list.items():
  304. if prev_shortest == None:
  305. shortest = v
  306. shortest_pattern_name.append(k)
  307. shortest_pattern_size.append(shortest)
  308. prev_shortest = True
  309. else:
  310. if int(v) < int(shortest):
  311. shortest = v
  312. shortest_pattern_name.append(k)
  313. shortest_pattern_size.append(shortest)
  314. for p in shortest_pattern_name:
  315. shortest_pattern_name = p
  316. for s in shortest_pattern_size:
  317. shortest_pattern_size = s
  318. print(" + [SHORTEST]: "+str(shortest_pattern_name)+ " [ "+str(shortest_pattern_size)+" bp linear RNA ]\n")
  319. print(" * Total [NUCLEOTIDS]: [ "+str(num_total_nucleotids)+" ]\n")
  320. if nucleotid_more_present == total_adenine:
  321. print(" + [A] Adenine : "+str(total_adenine)+" <- [MAX]")
  322. else:
  323. print(" + [A] Adenine : "+str(total_adenine))
  324. if nucleotid_more_present == total_guanine:
  325. print(" + [G] Guanine : "+str(total_guanine)+" <- [MAX]")
  326. else:
  327. print(" + [G] Guanine : "+str(total_guanine))
  328. if nucleotid_more_present == total_cytosine:
  329. print(" + [C] Cytosine : "+str(total_cytosine)+" <- [MAX]")
  330. else:
  331. print(" + [C] Cytosine : "+str(total_cytosine))
  332. if nucleotid_more_present == total_thymine:
  333. print(" + [T] Thymine : "+str(total_thymine)+" <- [MAX]")
  334. else:
  335. print(" + [T] Thymine : "+str(total_thymine))
  336. if total_any > 0:
  337. if nucleotid_more_present == total_any:
  338. print(" + [N] *ANY* : "+str(total_any)+" <- [MAX]\n")
  339. else:
  340. print(" + [N] *ANY* : "+str(total_any)+"\n")
  341. print("-"*5 + "\n")
  342. extract_pattern_most_present_local(memory)
  343. def convert_memory_to_dict(memory): # [index] = genome_name, pattern, num_rep
  344. memory_dict = {}
  345. index = 0
  346. for m in memory:
  347. regex_record = "'(.+?)': (.+?), '(.+?)'" # regex magics! - extract first each record
  348. pattern_record = re.compile(regex_record)
  349. record = re.findall(pattern_record, m)
  350. for r in record: # now extract each field
  351. index = index + 1
  352. name = str(r).split("', '(")[0]
  353. genome_name = str(name).split("'")[1]
  354. repeats = str(r).split("', '(")[1]
  355. genome_repeats = str(repeats).split("',")[0]
  356. pattern = str(repeats).split("',")[1]
  357. genome_pattern = pattern.replace(" ", "")
  358. genome_pattern = genome_pattern.replace("'", "")
  359. genome_pattern = genome_pattern.replace(")", "")
  360. memory_dict[index] = genome_name, genome_pattern, genome_repeats # generate memory_dict!
  361. return memory_dict
  362. def extract_pattern_most_present_local(memory):
  363. memory_dict = convert_memory_to_dict(memory)
  364. if genomes:
  365. try:
  366. f=open(dna_codons_list_path, 'r')
  367. codons = f.readlines()
  368. f.close()
  369. except:
  370. pass
  371. print("[LIBRE-AI] [REPORTING] -RESEARCHING- [STATISTICS]: \n")
  372. total_genomes = 0
  373. for k, v in genomes.items():
  374. total_genomes = total_genomes + 1
  375. if memory_dict:
  376. total_patterns = 0
  377. for m in memory:
  378. total_patterns = total_patterns + 1 # counter used for known patterns
  379. max_size_pattern_name, less_size_pattern_name, biggest_pattern_name, biggest_pattern_size, smaller_pattern_name, smaller_pattern_size, total_patterns_all_genomes, most_present_patterns_by_len_list, less_present_patterns_by_len_list = extract_patterns_most_found_in_all_genomes(memory_dict)
  380. print(" * Trying -[ "+str(total_patterns)+" ]- [PATTERNS LEARNED!] against -[ "+str(total_genomes)+ " ]- [DNA SECUENCES]:")
  381. if total_patterns_all_genomes:
  382. print("\n + Total [PATTERNS FOUND!]: [ "+str(total_patterns_all_genomes)+" ]")
  383. biggest_pattern_name_codon = None
  384. for c in codons:
  385. if c.split(":")[0] == str(biggest_pattern_name):
  386. biggest_pattern_name_codon = str(c.split(":")[1].replace("\n",""))
  387. print("\n - [MOST-PRESENT!]: [ "+str(biggest_pattern_size)+" ] time(s) -> [ "+str(biggest_pattern_name)+" ] "+str(biggest_pattern_name_codon)+"\n")
  388. if biggest_pattern_name_codon == None:
  389. print("\n - [MOST-PRESENT!]: [ "+str(biggest_pattern_size)+" ] time(s) -> [ "+str(biggest_pattern_name)+" ]\n")
  390. other_pattern_name_codon = None
  391. for k, v in most_present_patterns_by_len_list.items():
  392. for c in codons:
  393. if c.split(":")[0] == str(v[0]):
  394. other_pattern_name_codon = str(c.split(":")[1].replace("\n",""))
  395. print(" * [length = "+str(k)+"] : [ "+str(v[1])+" ] time(s) -> [ "+str(v[0])+" ] "+str(other_pattern_name_codon))
  396. if other_pattern_name_codon == None:
  397. print(" * [length = "+str(k)+"] : [ "+str(v[1])+" ] time(s) -> [ "+str(v[0])+" ]")
  398. other_pattern_name_codon = None
  399. smaller_pattern_name_codon = None
  400. for c in codons:
  401. if c.split(":")[0] == str(smaller_pattern_name):
  402. smaller_pattern_name_codon = str(c.split(":")[1].replace("\n",""))
  403. print("\n - [LESS-PRESENT!]: [ "+str(smaller_pattern_size)+" ] time(s) -> [ "+str(smaller_pattern_name)+" ] "+str(smaller_pattern_name_codon)+"\n")
  404. if smaller_pattern_name_codon == None:
  405. print("\n - [LESS-PRESENT!]: [ "+str(smaller_pattern_size)+" ] time(s) -> [ "+str(smaller_pattern_name)+" ]\n")
  406. other_pattern_name_codon = None
  407. for n, m in less_present_patterns_by_len_list.items():
  408. for c in codons:
  409. if c.split(":")[0] == str(m[0]):
  410. other_pattern_name_codon = str(c.split(":")[1].replace("\n",""))
  411. print(" * [length = "+str(n)+"] : [ "+str(m[1])+" ] time(s) -> [ "+str(m[0])+" ] "+str(other_pattern_name_codon))
  412. if other_pattern_name_codon == None:
  413. print(" * [length = "+str(n)+"] : [ "+str(m[1])+" ] time(s) -> [ "+str(m[0])+" ]")
  414. other_pattern_name_codon = None
  415. max_size_pattern_name = max(most_present_patterns_by_len_list, key=most_present_patterns_by_len_list.get)
  416. less_size_pattern_name = min(most_present_patterns_by_len_list, key=most_present_patterns_by_len_list.get)
  417. print("\n - [LARGEST] : [ "+str(max_size_pattern_name)+" ] bp linear RNA")
  418. print(" - [SHORTEST]: [ "+str(less_size_pattern_name)+" ] bp linear RNA\n")
  419. else:
  420. print("\n + Total [PATTERNS FOUND!]: [ 0 ]\n")
  421. try:
  422. f=open(universal_primer_list_path, 'r')
  423. UPL = f.readlines()
  424. f.close()
  425. if UPL:
  426. extract_potential_primer_pairs(UPL, total_genomes, codons)
  427. except:
  428. pass
  429. if codons:
  430. extract_potential_dna_codons(codons, total_genomes)
  431. def extract_potential_primer_pairs(UPL, total_genomes, codons):
  432. total_universal_primer_pairs = 0
  433. total_primer_pairs_found = 0
  434. primer_pairs_found_list = {}
  435. for pp in UPL:
  436. total_universal_primer_pairs = total_universal_primer_pairs + 1
  437. for k, v in genomes.items():
  438. pair_name = pp.split(":")[1].upper().replace("\n","")
  439. pair_sec = pp.split(":")[0]
  440. if str(pair_name) in str(v.upper()):
  441. pair_times = v.count(pair_name)
  442. total_primer_pairs_found += pair_times
  443. primer_pairs_found_list[pair_sec] = pair_name, total_primer_pairs_found
  444. print(" * Trying -[ "+str(total_universal_primer_pairs)+" ]- [UNIVERSAL PRIMER PAIRS!] against -[ "+str(total_genomes)+ " ]- [DNA SECUENCES]:")
  445. if total_primer_pairs_found:
  446. total_primer_pairs_found_list = 0
  447. for m, n in primer_pairs_found_list.items():
  448. total_primer_pairs_found_list = total_primer_pairs_found_list + n[1]
  449. print("\n + Total [UNIVERSAL PRIMER PAIRS FOUND!]: [ "+str(total_primer_pairs_found_list)+" ]\n")
  450. for m, n in primer_pairs_found_list.items():
  451. print(" * "+str(m)+" -> [ "+str(n[0])+" ] : [ "+str(n[1])+" ] time(s)")
  452. print ("")
  453. else:
  454. print("\n + Total [UNIVERSAL PRIMER PAIRS FOUND!]: [ 0 ]\n")
  455. def extract_potential_dna_codons(codons, total_genomes):
  456. total_codons = 0
  457. total_codons_found = 0
  458. codons_found_list = {}
  459. codons_found_list_by_codon = {}
  460. for c in codons:
  461. total_codons = total_codons + 1
  462. for k, v in genomes.items():
  463. codon_name = c.split(":")[0].upper().replace("\n","")
  464. if str(codon_name) in str(v.upper()):
  465. codons_times = v.count(codon_name)
  466. total_codons_found += codons_times
  467. codons_found_list[codons_times] = c.split(":")[0], str(c.split(":")[1]), k
  468. print(" * Trying -[ "+str(total_codons)+" ]- [PATTERN CODONS!] against -[ "+str(total_genomes)+ " ]- [DNA SECUENCES]:")
  469. if total_codons_found:
  470. for m, n in codons_found_list.items():
  471. codon_sec = str(n[0])
  472. codon_name = str(n[1].replace("\n",""))
  473. if not codon_sec in codons_found_list_by_codon.keys():
  474. codons_found_list_by_codon[codon_sec] = codon_name, m
  475. else:
  476. for r, s in codons_found_list_by_codon.items():
  477. if codon_sec == r:
  478. new_v = s[1] + m
  479. codons_found_list_by_codon[codon_sec] = codon_name, new_v
  480. codons_found_list_by_name = {}
  481. for g,z in codons_found_list_by_codon.items():
  482. if not z[0] in codons_found_list_by_name.keys():
  483. codons_found_list_by_name[z[0]]= z[1]
  484. else:
  485. for e, q in codons_found_list_by_name.items():
  486. if z[0] == e:
  487. new_s = q + z[1]
  488. codons_found_list_by_name[z[0]] = new_s
  489. total_codons_by_codon = 0
  490. for p, f in codons_found_list_by_name.items():
  491. total_codons_by_codon = total_codons_by_codon + f
  492. print("\n + Total [PATTERN CODONS FOUND!]: [ "+str(total_codons_by_codon)+" ]\n")
  493. for p, f in codons_found_list_by_name.items():
  494. print(" * "+str(p)+" : "+str(f)+" time(s)")
  495. print ("")
  496. else:
  497. print("\n + Total [PATTERN CODONS FOUND!]: [ 0 ]\n")
  498. def extract_patterns_most_found_in_all_genomes(memory_dict):
  499. present_patterns = []
  500. for m, p in memory_dict.items():
  501. pattern = p[1]
  502. if pattern not in present_patterns:
  503. present_patterns.append(pattern)
  504. index = 0 # genome num index
  505. for pattern in present_patterns:
  506. index = index + 1
  507. try_pattern_against_all_genomes_by_pattern(pattern, index)
  508. total_patterns_all_genomes = 0
  509. largest_size_by_pattern = {}
  510. largest_size_by_pattern_index = 0
  511. for k,v in repeats.items():
  512. largest_size_by_pattern_index = largest_size_by_pattern_index + 1
  513. total_patterns_all_genomes = total_patterns_all_genomes + v[2] # total patterns all genomes
  514. largest_size_by_pattern[largest_size_by_pattern_index] = v[0], v[2]
  515. total_patterns_by_pattern = 0
  516. list_total_patterns_by_pattern = {}
  517. for i, v in largest_size_by_pattern.items():
  518. total_patterns_by_pattern = total_patterns_by_pattern + v[1]
  519. list_total_patterns_by_pattern[v[0]] = total_patterns_by_pattern
  520. biggest_pattern_name = None
  521. biggest_pattern_size = 0
  522. smaller_pattern_name = None
  523. smaller_pattern_size = 0
  524. max_size_pattern = 0
  525. for r, z in list_total_patterns_by_pattern.items():
  526. pattern_length = len(r)
  527. if pattern_length > max_size_pattern:
  528. max_size_pattern_name = r
  529. if biggest_pattern_name == None:
  530. biggest_pattern_name = r
  531. smaller_pattern_name = r
  532. biggest_pattern_size = z
  533. smaller_pattern_size = z
  534. less_size_pattern_name = r
  535. less_size_pattern_size = z
  536. else:
  537. if pattern_length < less_size_pattern_size:
  538. less_size_pattern_size = pattern_length
  539. less_size_pattern_name = r
  540. if z > biggest_pattern_size:
  541. biggest_pattern_name = r
  542. biggest_pattern_size = biggest_pattern_size + z
  543. else:
  544. if z < smaller_pattern_size:
  545. smaller_pattern_name = r
  546. smaller_pattern_size = z
  547. most_present_patterns_by_len_list = extract_most_present_pattern_by_len(list_total_patterns_by_pattern)
  548. less_present_patterns_by_len_list = extract_less_present_pattern_by_len(list_total_patterns_by_pattern)
  549. return max_size_pattern_name, less_size_pattern_name, biggest_pattern_name, biggest_pattern_size, smaller_pattern_name, smaller_pattern_size, total_patterns_all_genomes, most_present_patterns_by_len_list, less_present_patterns_by_len_list
  550. def extract_most_present_pattern_by_len(list_total_patterns_by_pattern):
  551. most_present_patterns_by_len_list = {}
  552. for k, v in list_total_patterns_by_pattern.items():
  553. pattern_len = len(k)
  554. if pattern_len in most_present_patterns_by_len_list.keys():
  555. if v > most_present_patterns_by_len_list[pattern_len][1]:
  556. most_present_patterns_by_len_list[pattern_len] = k, v
  557. else:
  558. most_present_patterns_by_len_list[pattern_len] = k, v
  559. return most_present_patterns_by_len_list
  560. def extract_less_present_pattern_by_len(list_total_patterns_by_pattern):
  561. less_present_patterns_by_len_list = {}
  562. for k, v in list_total_patterns_by_pattern.items():
  563. pattern_len = len(k)
  564. if pattern_len in less_present_patterns_by_len_list.keys():
  565. if v < less_present_patterns_by_len_list[pattern_len][1]:
  566. less_present_patterns_by_len_list[pattern_len] = k, v
  567. else:
  568. less_present_patterns_by_len_list[pattern_len] = k, v
  569. return less_present_patterns_by_len_list
  570. def extract_storage_sizes():
  571. total_dataset_size = 0
  572. total_files_size = 0
  573. total_list_size = 0
  574. for file in glob.iglob(genomes_path + '*/*/*', recursive=True): # extract datasets sizes
  575. if(file.endswith(".genome")):
  576. total_dataset_size = total_dataset_size + len(file)
  577. try:
  578. f=open(brain_path, "r") # extract brain sizes
  579. total_brain_size = len(f.read())
  580. f.close()
  581. except:
  582. total_brain_size = 0
  583. try:
  584. f=open(genomes_list_path, "r") # extract genomes list sizes
  585. total_list_size = len(f.read())
  586. f.close()
  587. except:
  588. total_list_size = 0
  589. if total_dataset_size > 0:
  590. total_files_size = int(total_files_size) + int(total_dataset_size)
  591. dataset_s, dataset_size_name = convert_size(total_dataset_size)
  592. total_dataset_size = '%s %s' % (dataset_s,dataset_size_name)
  593. if total_brain_size > 0:
  594. total_files_size = int(total_files_size) + int(total_brain_size)
  595. brain_s, brain_size_name = convert_size(total_brain_size)
  596. total_brain_size = '%s %s' % (brain_s,brain_size_name)
  597. if total_list_size > 0:
  598. total_files_size = int(total_files_size) + int(total_list_size)
  599. list_s, list_size_name = convert_size(total_list_size)
  600. total_list_size = '%s %s' % (list_s,list_size_name)
  601. total_s, total_size_name = convert_size(total_files_size)
  602. total_files_size = '%s %s' % (total_s,total_size_name)
  603. print(" * Total [FILE SIZES]: "+str(total_files_size)+"\n")
  604. if total_dataset_size:
  605. print(" + [DATASET]: "+str(total_dataset_size)+"\n")
  606. if total_list_size:
  607. print(" + [LIST]: "+str(total_list_size)+"\n")
  608. if total_brain_size:
  609. print(" + [BRAIN]: "+str(total_brain_size)+"\n")
  610. def extract_total_patterns_learned_from_local(memory):
  611. total_patterns = 0
  612. for m in memory:
  613. total_patterns = total_patterns + 1
  614. print(" * [SETTINGS] Using [MAX. LENGTH] for range [PATTERN] = [ "+str(max_length)+" ]\n")
  615. if total_patterns > 0:
  616. print(" + [PATTERNS LEARNED!]: [ "+str(total_patterns)+" ]\n")
  617. else:
  618. print(" + [PATTERNS LEARNED!]: [ "+str(total_patterns)+" ]")
  619. generate_pattern_len_report_structure(memory)
  620. return memory
  621. def list_genomes_on_database():
  622. print("[LIST] [REPORTING] [DNA SECUENCES] ... -> [STARTING!]\n")
  623. f=open(dna_codons_list_path, 'r')
  624. codons = f.readlines()
  625. f.close()
  626. print("-"*15 + "\n")
  627. f=open(genomes_list_path, 'w')
  628. for k, v in genomes.items():
  629. print ("*"+str(k)+ "-> [ "+str(len(v))+" bp linear RNA ]")
  630. print (" + [A] Adenine :", str(v.count("A")))
  631. print (" + [G] Guanine :", str(v.count("G")))
  632. print (" + [C] Cytosine :", str(v.count("C")))
  633. print (" + [T] Thymine :", str(v.count("T")))
  634. f.write(str("*"+ str(k)+ " -> [ "+str(len(v))+"bp linear RNA ]\n"))
  635. f.write(str(" + [A] Adenine : " + str(v.count("A"))+"\n"))
  636. f.write(str(" + [G] Guanine : " + str(v.count("G"))+"\n"))
  637. f.write(str(" + [C] Cytosine : " + str(v.count("C"))+"\n"))
  638. f.write(str(" + [T] Thymine : " + str(v.count("T"))+"\n"))
  639. if v.count("N") > 0:
  640. print (" + [N] *ANY* :", str(v.count("N")))
  641. f.write(str(" + [N] *ANY* : "+ str(v.count("N"))+"\n"))
  642. for c in codons:
  643. codon_sec = str(c.split(":")[0])
  644. codon_name = str(c.split(":")[1].replace("\n",""))
  645. codon_counter = str(v.count(str(c.split(":")[0])))
  646. print (" + ["+codon_sec+"] "+codon_name+" :", codon_counter)
  647. f.write(str(" + ["+codon_sec+"] "+codon_name+" : "+ codon_counter)+"\n")
  648. print ("")
  649. f.write("\n")
  650. print("-"*15 + "\n")
  651. print ("[LIST] [INFO] [SAVED!] at: '"+str(genomes_list_path)+"'... -> [EXITING!]\n")
  652. f.close()
  653. def examine_stored_brain_memory():
  654. memory = [] # list used as hot-memory
  655. f=open(brain_path, 'r')
  656. for line in f.readlines():
  657. if line not in memory:
  658. memory.append(line)
  659. f.close()
  660. if memory == "": # first time run!
  661. print ("[LIBRE-AI] [INFO] Not any [BRAIN] present ... -> [BUILDING ONE!]\n")
  662. print("-"*15 + "\n")
  663. for i in range(2, 11+1):
  664. seed = [random.randrange(0, 4) for _ in range(i)] # generate "static" genesis seed
  665. if seed not in seeds_checked:
  666. seeds_checked.append(seed)
  667. pattern = ""
  668. for n in seed:
  669. if n == 0:
  670. pattern += "A"
  671. elif n == 1:
  672. pattern += "C"
  673. elif n == 2:
  674. pattern += "T"
  675. else:
  676. pattern += "G"
  677. print("[LIBRE-AI] [SEARCH] Generating [RANDOM] pattern: " + str(pattern) + "\n")
  678. create_new_pattern(pattern) # create new pattern
  679. print("-"*15 + "\n")
  680. print ("[LIBRE-AI] [INFO] A new [BRAIN] has been created !!! ... -> [ADVANCING!]\n")
  681. f=open(brain_path, 'r')
  682. memory = f.read().replace('\n',' ')
  683. f.close()
  684. return memory
  685. def generate_pattern_len_report_structure(memory):
  686. pattern_len_1 = 0 # related with [MAX. LENGTH] range
  687. pattern_len_2 = 0
  688. pattern_len_3 = 0
  689. pattern_len_4 = 0
  690. pattern_len_5 = 0
  691. pattern_len_6 = 0
  692. pattern_len_7 = 0
  693. pattern_len_8 = 0
  694. pattern_len_9 = 0
  695. pattern_len_10 = 0
  696. pattern_len_11 = 0
  697. pattern_len_12 = 0
  698. pattern_len_13 = 0
  699. pattern_len_14 = 0
  700. pattern_len_15 = 0
  701. pattern_len_16 = 0
  702. pattern_len_17 = 0
  703. pattern_len_18 = 0
  704. pattern_len_19 = 0
  705. pattern_len_20 = 0
  706. pattern_len_21 = 0
  707. pattern_len_22 = 0
  708. pattern_len_23 = 0
  709. pattern_len_24 = 0
  710. pattern_len_25 = 0
  711. pattern_len_26 = 0
  712. pattern_len_27 = 0
  713. pattern_len_28 = 0
  714. pattern_len_29 = 0
  715. pattern_len_30 = 0
  716. pattern_len_31 = 0
  717. pattern_len_32 = 0
  718. pattern_len_33 = 0
  719. pattern_len_34 = 0
  720. pattern_len_35 = 0
  721. pattern_len_36 = 0
  722. pattern_len_37 = 0
  723. pattern_len_38 = 0
  724. pattern_len_39 = 0
  725. pattern_len_40 = 0
  726. pattern_len_41 = 0
  727. pattern_len_42 = 0
  728. pattern_len_43 = 0
  729. pattern_len_44 = 0
  730. pattern_len_45 = 0
  731. pattern_len_46 = 0
  732. pattern_len_47 = 0
  733. pattern_len_48 = 0
  734. pattern_len_49 = 0
  735. pattern_len_50 = 0
  736. for m in memory:
  737. try:
  738. pattern_len = m.split(", '")[1]
  739. pattern_len = pattern_len.split("')")[0]
  740. pattern_len = len(pattern_len)
  741. except:
  742. pattern_len = 0 # discard!
  743. if pattern_len == 1:
  744. pattern_len_1 = pattern_len_1 + 1
  745. elif pattern_len == 2:
  746. pattern_len_2 = pattern_len_2 + 1
  747. elif pattern_len == 3:
  748. pattern_len_3 = pattern_len_3 + 1
  749. elif pattern_len == 4:
  750. pattern_len_4 = pattern_len_4 + 1
  751. elif pattern_len == 5:
  752. pattern_len_5 = pattern_len_5 + 1
  753. elif pattern_len == 6:
  754. pattern_len_6 = pattern_len_6 + 1
  755. elif pattern_len == 7:
  756. pattern_len_7 = pattern_len_7 + 1
  757. elif pattern_len == 8:
  758. pattern_len_8 = pattern_len_8 + 1
  759. elif pattern_len == 9:
  760. pattern_len_9 = pattern_len_9 + 1
  761. elif pattern_len == 10:
  762. pattern_len_10 = pattern_len_10 + 1
  763. elif pattern_len == 11:
  764. pattern_len_11 = pattern_len_11 + 1
  765. elif pattern_len == 12:
  766. pattern_len_12 = pattern_len_12 + 1
  767. elif pattern_len == 13:
  768. pattern_len_13 = pattern_len_13 + 1
  769. elif pattern_len == 14:
  770. pattern_len_14 = pattern_len_14 + 1
  771. elif pattern_len == 15:
  772. pattern_len_15 = pattern_len_15 + 1
  773. elif pattern_len == 16:
  774. pattern_len_16 = pattern_len_16 + 1
  775. elif pattern_len == 17:
  776. pattern_len_17 = pattern_len_17 + 1
  777. elif pattern_len == 18:
  778. pattern_len_18 = pattern_len_18 + 1
  779. elif pattern_len == 19:
  780. pattern_len_19 = pattern_len_19 + 1
  781. elif pattern_len == 20:
  782. pattern_len_20 = pattern_len_20 + 1
  783. elif pattern_len == 21:
  784. pattern_len_21 = pattern_len_21 + 1
  785. elif pattern_len == 22:
  786. pattern_len_22 = pattern_len_22 + 1
  787. elif pattern_len == 23:
  788. pattern_len_23 = pattern_len_23 + 1
  789. elif pattern_len == 24:
  790. pattern_len_24 = pattern_len_24 + 1
  791. elif pattern_len == 25:
  792. pattern_len_25 = pattern_len_25 + 1
  793. elif pattern_len == 26:
  794. pattern_len_26 = pattern_len_26 + 1
  795. elif pattern_len == 27:
  796. pattern_len_27 = pattern_len_27 + 1
  797. elif pattern_len == 28:
  798. pattern_len_28 = pattern_len_28 + 1
  799. elif pattern_len == 29:
  800. pattern_len_29 = pattern_len_29 + 1
  801. elif pattern_len == 30:
  802. pattern_len_30 = pattern_len_30 + 1
  803. elif pattern_len == 31:
  804. pattern_len_31 = pattern_len_31 + 1
  805. elif pattern_len == 32:
  806. pattern_len_32 = pattern_len_32 + 1
  807. elif pattern_len == 33:
  808. pattern_len_33 = pattern_len_33 + 1
  809. elif pattern_len == 34:
  810. pattern_len_34 = pattern_len_34 + 1
  811. elif pattern_len == 35:
  812. pattern_len_35 = pattern_len_35 + 1
  813. elif pattern_len == 36:
  814. pattern_len_36 = pattern_len_36 + 1
  815. elif pattern_len == 37:
  816. pattern_len_37 = pattern_len_37 + 1
  817. elif pattern_len == 38:
  818. pattern_len_38 = pattern_len_38 + 1
  819. elif pattern_len == 39:
  820. pattern_len_39 = pattern_len_39 + 1
  821. elif pattern_len == 40:
  822. pattern_len_40 = pattern_len_40 + 1
  823. elif pattern_len == 41:
  824. pattern_len_41 = pattern_len_41 + 1
  825. elif pattern_len == 42:
  826. pattern_len_42 = pattern_len_42 + 1
  827. elif pattern_len == 43:
  828. pattern_len_43 = pattern_len_43 + 1
  829. elif pattern_len == 44:
  830. pattern_len_44 = pattern_len_44 + 1
  831. elif pattern_len == 45:
  832. pattern_len_45 = pattern_len_45 + 1
  833. elif pattern_len == 46:
  834. pattern_len_46 = pattern_len_46 + 1
  835. elif pattern_len == 47:
  836. pattern_len_47 = pattern_len_47 + 1
  837. elif pattern_len == 48:
  838. pattern_len_48 = pattern_len_48 + 1
  839. elif pattern_len == 49:
  840. pattern_len_49 = pattern_len_49 + 1
  841. elif pattern_len == 50:
  842. pattern_len_50 = pattern_len_50 + 1
  843. else:
  844. pass
  845. if pattern_len_1 > 0:
  846. print(" - [length = 1] : [ "+str(pattern_len_1)+" ]")
  847. if pattern_len_2 > 0:
  848. print(" - [length = 2] : [ "+str(pattern_len_2)+" ]")
  849. if pattern_len_3 > 0:
  850. print(" - [length = 3] : [ "+str(pattern_len_3)+" ]")
  851. if pattern_len_4 > 0:
  852. print(" - [length = 4] : [ "+str(pattern_len_4)+" ]")
  853. if pattern_len_5 > 0:
  854. print(" - [length = 5] : [ "+str(pattern_len_5)+" ]")
  855. if pattern_len_6 > 0:
  856. print(" - [length = 6] : [ "+str(pattern_len_6)+" ]")
  857. if pattern_len_7 > 0:
  858. print(" - [length = 7] : [ "+str(pattern_len_7)+" ]")
  859. if pattern_len_8 > 0:
  860. print(" - [length = 8] : [ "+str(pattern_len_8)+" ]")
  861. if pattern_len_9 > 0:
  862. print(" - [length = 9] : [ "+str(pattern_len_9)+" ]")
  863. if pattern_len_10 > 0:
  864. print(" - [length = 10]: [ "+str(pattern_len_10)+" ]")
  865. if pattern_len_11 > 0:
  866. print(" - [length = 11]: [ "+str(pattern_len_11)+" ]")
  867. if pattern_len_12 > 0:
  868. print(" - [length = 12]: [ "+str(pattern_len_12)+" ]")
  869. if pattern_len_13 > 0:
  870. print(" - [length = 13]: [ "+str(pattern_len_13)+" ]")
  871. if pattern_len_14 > 0:
  872. print(" - [length = 14]: [ "+str(pattern_len_14)+" ]")
  873. if pattern_len_15 > 0:
  874. print(" - [length = 15]: [ "+str(pattern_len_15)+" ]")
  875. if pattern_len_16 > 0:
  876. print(" - [length = 16]: [ "+str(pattern_len_16)+" ]")
  877. if pattern_len_17 > 0:
  878. print(" - [length = 17]: [ "+str(pattern_len_17)+" ]")
  879. if pattern_len_18 > 0:
  880. print(" - [length = 18]: [ "+str(pattern_len_18)+" ]")
  881. if pattern_len_19 > 0:
  882. print(" - [length = 19]: [ "+str(pattern_len_19)+" ]")
  883. if pattern_len_20 > 0:
  884. print(" - [length = 20]: [ "+str(pattern_len_20)+" ]")
  885. if pattern_len_21 > 0:
  886. print(" - [length = 21]: [ "+str(pattern_len_21)+" ]")
  887. if pattern_len_22 > 0:
  888. print(" - [length = 22]: [ "+str(pattern_len_22)+" ]")
  889. if pattern_len_23 > 0:
  890. print(" - [length = 23]: [ "+str(pattern_len_23)+" ]")
  891. if pattern_len_24 > 0:
  892. print(" - [length = 24]: [ "+str(pattern_len_24)+" ]")
  893. if pattern_len_25 > 0:
  894. print(" - [length = 25]: [ "+str(pattern_len_25)+" ]")
  895. if pattern_len_26 > 0:
  896. print(" - [length = 26]: [ "+str(pattern_len_26)+" ]")
  897. if pattern_len_27 > 0:
  898. print(" - [length = 27]: [ "+str(pattern_len_27)+" ]")
  899. if pattern_len_28 > 0:
  900. print(" - [length = 28]: [ "+str(pattern_len_28)+" ]")
  901. if pattern_len_29 > 0:
  902. print(" - [length = 29]: [ "+str(pattern_len_29)+" ]")
  903. if pattern_len_30 > 0:
  904. print(" - [length = 30]: [ "+str(pattern_len_30)+" ]")
  905. if pattern_len_31 > 0:
  906. print(" - [length = 31]: [ "+str(pattern_len_31)+" ]")
  907. if pattern_len_32 > 0:
  908. print(" - [length = 32]: [ "+str(pattern_len_32)+" ]")
  909. if pattern_len_33 > 0:
  910. print(" - [length = 33]: [ "+str(pattern_len_33)+" ]")
  911. if pattern_len_34 > 0:
  912. print(" - [length = 34]: [ "+str(pattern_len_34)+" ]")
  913. if pattern_len_35 > 0:
  914. print(" - [length = 35]: [ "+str(pattern_len_35)+" ]")
  915. if pattern_len_36 > 0:
  916. print(" - [length = 36]: [ "+str(pattern_len_36)+" ]")
  917. if pattern_len_37 > 0:
  918. print(" - [length = 37]: [ "+str(pattern_len_37)+" ]")
  919. if pattern_len_38 > 0:
  920. print(" - [length = 38]: [ "+str(pattern_len_38)+" ]")
  921. if pattern_len_39 > 0:
  922. print(" - [length = 39]: [ "+str(pattern_len_39)+" ]")
  923. if pattern_len_40 > 0:
  924. print(" - [length = 40]: [ "+str(pattern_len_40)+" ]")
  925. if pattern_len_41 > 0:
  926. print(" - [length = 41]: [ "+str(pattern_len_41)+" ]")
  927. if pattern_len_42 > 0:
  928. print(" - [length = 42]: [ "+str(pattern_len_42)+" ]")
  929. if pattern_len_43 > 0:
  930. print(" - [length = 43]: [ "+str(pattern_len_43)+" ]")
  931. if pattern_len_44 > 0:
  932. print(" - [length = 44]: [ "+str(pattern_len_44)+" ]")
  933. if pattern_len_45 > 0:
  934. print(" - [length = 45]: [ "+str(pattern_len_45)+" ]")
  935. if pattern_len_46 > 0:
  936. print(" - [length = 46]: [ "+str(pattern_len_46)+" ]")
  937. if pattern_len_47 > 0:
  938. print(" - [length = 47]: [ "+str(pattern_len_47)+" ]")
  939. if pattern_len_48 > 0:
  940. print(" - [length = 48]: [ "+str(pattern_len_48)+" ]")
  941. if pattern_len_49 > 0:
  942. print(" - [length = 49]: [ "+str(pattern_len_49)+" ]")
  943. if pattern_len_50 > 0:
  944. print(" - [length = 50]: [ "+str(pattern_len_50)+" ]")
  945. def print_banner():
  946. print("\n"+"="*50)
  947. print(" ____ _ _ _ _ ")
  948. print("| _ \(_) __ _| \ | | / \ ")
  949. print("| | | | |/ _` | \| | / _ \ ")
  950. print("| |_| | | (_| | |\ |/ ___ \ ")
  951. print("|____/|_|\__,_|_| \_/_/ \_\ by psy")
  952. print('\n"Search and Recognize patterns in DNA sequences"')
  953. print("\n"+"="*50)
  954. print("+ GENOMES DETECTED:", str(num_files))
  955. print("="*50)
  956. print("\n"+"-"*15+"\n")
  957. print(" * VERSION: ")
  958. print(" + "+VERSION+" - (rev:"+RELEASE+")")
  959. print("\n * SOURCES:")
  960. print(" + "+SOURCE1)
  961. print(" + "+SOURCE2)
  962. print("\n * CONTACT: ")
  963. print(" + "+CONTACT+"\n")
  964. print("-"*15+"\n")
  965. print("="*50)
  966. # sub_init #
  967. num_files=0
  968. for file in glob.iglob(genomes_path + '**/*', recursive=True):
  969. if(file.endswith(".genome")):
  970. num_files = num_files + 1
  971. f=open(file, 'r')
  972. genome = f.read().replace('\n',' ')
  973. genomes[file.replace("datasets/","")] = genome.upper() # add genome to main dict
  974. f.close()
  975. print_banner() # show banner
  976. option = input("\n+ CHOOSE: (S)earch, (L)ist, (T)rain or (R)eport: ").upper()
  977. print("")
  978. print("="*50+"\n")
  979. if option == "S": # search pattern
  980. search_pattern_with_human()
  981. elif option == "L": # list genomes
  982. list_genomes_on_database()
  983. elif option == "T": # teach AI
  984. teach_ai()
  985. else: # libre AI
  986. libre_ai()
  987. print ("="*50+"\n")