summaryrefslogtreecommitdiff
path: root/quikr_train.py
blob: 94a8c4ebb93d2ebfeb6ccc57d94a1650fbc1ba54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import os
import sys
from subprocess import *
import platform
import argparse

def main():
  """
  You can call this script independently, and will save the 
  trained matrix as a numpy file.
  example: python quikr-train.py input.fasta 6 trained_matrix.npy
  """
  parser = argparse.ArgumentParser(description=
  " quikr_train returns a custom trained matrix that can be used with \
    the quikr function. \n You must supply a kmer. \n ")

  parser.add_argument("-i", "--input", help="path to the database", required=True)
  parser.add_argument("-o", "--output", help="path to output", required=True)
  parser.add_argument("-k", "--kmer", type=int, help="specifies which kmer to use", required=True)

  args = parser.parse_args()

  if not os.path.isfile(args.input):
    parser.error( "Input database not found")

  # call the quikr train function, save the output with np.save
  matrix = quikr_train(args.input, args.kmer)

  np.save(args.output, matrix)

  return 0

def quikr_train(input_file_location, kmer):
  """
  Takes a input fasta file, and kmer, returns a custom trained matrix
  """

  
  print "input fasta training file: " + input_file_location
  print "kmer: " + kmer

  kmer_file_name = kmer + "mers.txt"
  print kmer_file_name

  
  uname = platform.uname()[0]

  if uname == "Linux": 
    print "Detected Linux"
    input_file = Popen(["./probabilities-by-read-linux", kmer, input_file_location, kmer_file_name], stdout=PIPE) 
  elif uname == "Darwin":
    print "Detected Mac OS X" 
    input_file = Popen(["./probabilities-by-read-osx", kmer, input_file_location, kmer_file_name]) 

  # load and  normalize the matrix by dividing each element by the sum of it's column.
  matrix  = np.loadtxt(input_file.stdout)
  normalized = matrix / matrix.sum(0)

  return normalized
  


if __name__ == "__main__":
    sys.exit(main())