<commit_before>class ApplicationController < ActionController::Base

  # Beware, The following code sucks ass, trying to make sense of it is not a good idea.
  # All code was hacked together with no understanding on how to use R

  R_ECHO = true

  def upload_csv
    file_input = params[:file]

    if !file_input || !file_input.tempfile || !file_input.tempfile.path
      return render :text => 'failed to receive file'
    end

    header_line = ''
    File.open(file_input.tempfile.path) { |f| header_line = f.readline }
    headers_pre = header_line.split(',')

    @headers = []

    # because: fuck new lines!
    headers_pre.each do |header|
      @headers.push(header.gsub("\n", ''))
    end

    render :json => @headers
  end

  def index
    render
  end

  def time_fuck(time)

    if time
      days = time['days'] ? time['days'] * 1440 : 0
      hours = time['hours'] ? time['hours'] * 60 : 0
      mins = time['minutes'] ? time['minutes'] : 0
      days+hours+mins
    else
      0
    end
  end

  def get_results
    require 'rinruby'
    require 'csv'
    require 'time'


    time_start = Time.now

    myr = RinRuby.new(:echo => R_ECHO)

    # ---------- setup

    file = params[:file]
    headers_parsed = JSON[params[:headers]]
    treatments = JSON[params[:options]]


    if !file || !file.tempfile || !file.tempfile.path
      return render :text => 'failed to receive file'
    end


    timestamp = DateTime.now.strftime('%Q')

    # puts "writing to tmp-#{timestamp}.csv"
    #
    # CSV.open("public/tmp/tmp-#{timestamp}.csv", 'wb') do |csv|
    #   csv << %w(expression experiment gene treatment time rep)
    #
    #   treatments.each do |option|
    #
    #     id = headers_parsed.select { |h| h['treatment'] == 'ID' }[0]
    #
    #     gene_col = headers_parsed.index(id)
    #
    #     # filter out unused and ID
    #     filter_output = headers_parsed.select { |h| option != 'ID' && option != 'UNUSED' && h['treatment'] == option }
    #
    #     # for each filter_output add col to expression
    #
    #     if filter_output.length > 0
    #       filter_output.each do |header|
    #
    #         col_index = headers_parsed.index(header)
    #
    #         puts "processing col #{col_index}..."
    #
    #         CSV.foreach(file.tempfile.path, :headers => true) { |row|
    #           expression = row[col_index]
    #           experiment = header['name']
    #           gene = row[gene_col]
    #           treatment = header['treatment']
    #           time = time_fuck(header['time'])
    #           repetition = header['repetition']
    #           csv << %W(#{expression} #{experiment} #{gene} #{treatment} #{time} #{repetition})
    #         }
    #       end
    #     end
    #   end
    # end

    timestamp = '1412253936218'

    myr.eval <<EOF

    graphs <- vector()

    output_folder <- 'public/graphs/'

    source('gene_nets/functions.R')
    load_libraries()

    raw_filename = "public/tmp/tmp-#{timestamp}.csv"
    data <- read.csv(raw_filename, header = TRUE)

    data[data == 0] <- 0

    treatments <- unique(data$treatment)

    mock_array <- list()

    euler_array <- list()

    for(t in treatments){

      print(paste0("Current working on: ", t))<commit_msg>non fake test<commit_after>class ApplicationController < ActionController::Base

  # Beware, The following code sucks ass, trying to make sense of it is not a good idea.
  # All code was hacked together with no understanding on how to use R

  R_ECHO = true

  def upload_csv
    file_input = params[:file]

    if !file_input || !file_input.tempfile || !file_input.tempfile.path
      return render :text => 'failed to receive file'
    end

    header_line = ''
    File.open(file_input.tempfile.path) { |f| header_line = f.readline }
    headers_pre = header_line.split(',')

    @headers = []

    # because: fuck new lines!
    headers_pre.each do |header|
      @headers.push(header.gsub("\n", ''))
    end

    render :json => @headers
  end

  def index
    render
  end

  def time_fuck(time)

    if time
      days = time['days'] ? time['days'] * 1440 : 0
      hours = time['hours'] ? time['hours'] * 60 : 0
      mins = time['minutes'] ? time['minutes'] : 0
      days+hours+mins
    else
      0
    end
  end

  def get_results
    require 'rinruby'
    require 'csv'
    require 'time'


    time_start = Time.now

    myr = RinRuby.new(:echo => R_ECHO)

    # ---------- setup

    file = params[:file]
    headers_parsed = JSON[params[:headers]]
    treatments = JSON[params[:options]]


    if !file || !file.tempfile || !file.tempfile.path
      return render :text => 'failed to receive file'
    end


    timestamp = DateTime.now.strftime('%Q')

    puts "writing to tmp-#{timestamp}.csv"

    CSV.open("public/tmp/tmp-#{timestamp}.csv", 'wb') do |csv|
      csv << %w(expression experiment gene treatment time rep)

      treatments.each do |option|

        id = headers_parsed.select { |h| h['treatment'] == 'ID' }[0]

        gene_col = headers_parsed.index(id)

        # filter out unused and ID
        filter_output = headers_parsed.select { |h| option != 'ID' && option != 'UNUSED' && h['treatment'] == option }

        # for each filter_output add col to expression

        if filter_output.length > 0
          filter_output.each do |header|

            col_index = headers_parsed.index(header)

            puts "processing col #{col_index}..."

            CSV.foreach(file.tempfile.path, :headers => true) { |row|
              expression = row[col_index]
              experiment = header['name']
              gene = row[gene_col]
              treatment = header['treatment']
              time = time_fuck(header['time'])
              repetition = header['repetition']
              csv << %W(#{expression} #{experiment} #{gene} #{treatment} #{time} #{repetition})
            }
          end
        end
      end
    end

    # timestamp = '1412253936218'

    myr.eval <<EOF

    graphs <- vector()

    output_folder <- 'public/graphs/'

    source('gene_nets/functions.R')
    load_libraries()

    raw_filename = "public/tmp/tmp-#{timestamp}.csv"
    data <- read.csv(raw_filename, header = TRUE)

    data[data == 0] <- 0

    treatments <- unique(data$treatment)

    mock_array <- list()

    euler_array <- list()

    for(t in treatments){

      print(paste0("Current working on: ", t))
{'fraction_non_alphanumeric': 0.13513936736611337, 'fraction_numerical': 0.008769182586908864, 'mean_word_length': 2.3161993769470404, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1873878', 'n_tokens_mistral': 2137, 'n_tokens_neox': 1974, 'n_words': 503}