similarity.cpp 2.93 KB
Newer Older
1 2 3 4 5
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
6
#include <unistd.h>
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
#include "../core/dynprog.h"
#include "../core/fasta.h"
#include "../core/lazy_msa.h"
#include "../core/similarityMatrix.h"
#include "../core/compare-all.h"
#include "../lib/json.hpp"

using namespace std ;
using json = nlohmann::json;

bool check_cgi_parameters(json &result) {
  char * requestMethod = getenv("REQUEST_METHOD");
  if(!requestMethod) {
    result["Error"] = "requestMethod";
    return false;
  } else if(strcmp(requestMethod, "POST") != 0) {
    result["Error"] = "requestMethod =/= post";
    return false;
  }
  char * contentType = getenv("CONTENT_TYPE");
  if(!contentType) {
    result["Error"] = "content_type";
    return false;
  }
  result["requestMethod"] = requestMethod;
  result["contentType"] = contentType;
  return true;
}

bool create_fasta_file(json &result, int fd) {
  char temp[1024];
  FILE *f;
  f = fdopen(fd,"w");
  if(f == NULL){
    result["Error"] = "opening tempfile";
    return false;
  }else{
    while(cin) {
      cin.getline(temp, 1024);
      fputs(temp, f);
      fputs("\n", f);
    }
  }
      
  fclose(f);
  return true;
}

int main(int argc, char* argv[])
{
  
  const char* fdata;
  //  ostringstream ost; 
  char filename[] = "/tmp/VidjilSimilarityXXXXXX";
  json result;
  bool error = false;

  bool cgi_mode;
65 66 67
  bool output_json = false;
    
  //CGI
68 69
    if (argc <= 1){
      cgi_mode=true;
70
      output_json=true;
71 72 73 74 75 76 77 78 79 80 81 82 83
      cout <<"Content-type: text/html"<<endl<<endl;

      error = ! check_cgi_parameters(result);
      if (! error) {
        int fd = mkstemp(filename);
        if (fd == -1) {
          result["Error"] = "Temporary file";
          error = true;
        } else {
          error = ! create_fasta_file(result, fd);
          fdata = filename;
        }
      }
84 85
      
    //command
86 87
    }else{
      cgi_mode=false;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      
      //help !
      if (strcmp(argv[1], "-h")==0){
          
        cout << "usage: similarity [-h] [-j] file\n\n";
        cout << "file : fasta file\n";
        cout << "-h : help\n";
        cout << "-j : json output\n";
        return 0;
      }
      
      if (argc >= 3){
        if (strcmp(argv[1], "-j")==0) {
          output_json = true;
        }
        fdata = argv[2];
      }else{
        fdata = argv[1];
      }
107 108
    }
    
109 110
    
    
111 112 113
    if (!cgi_mode) cout <<endl;

    if (! error) {
114
      Fasta fa(fdata, 1, " ", !output_json);
115 116 117 118 119
    
      list<Sequence> reads;
      reads = fa.getAll();
      
      list<string> labels;
120
      for (size_t i=0; i < reads.size(); i++) {
121 122 123 124 125
        labels.push_back(fa.label(i));
      }
      
      SimilarityMatrix matrix = compare_all(reads, labels);

126
      if (output_json) {
127 128
          json j;
          j << JsonOutputSimilarityMatrix(matrix);
129
          cout << j.dump();
130 131
          if (cgi_mode)
            unlink(fdata);
132 133 134 135 136 137 138
      }else{
        cout << RawOutputSimilarityMatrix(matrix, 90);
      }
      
    }
      
}