test_that("Parsing AIRR-Seq works", {

  test_parse=data.frame(
    sequence_id="AAAGCAAAGTGGTAAT-1_contig_2",
    sequence="GGGGAGCTCTGGGAGACGAGCCCAGCACTGGAAGTCGCCGGTGTTTCCATTCGGTGATCAGCACTGAACACAGAGGACTCACCATGGAGTTTGGGCTGAGCTGGGTTTTCCTCGTTGCTCTTTTAAGAGGTGTCCAGTGTCAGGTGCAGCTGGTGGAGTCTGGGGGAGGCGTGGTCCAGCCTGGGAGGTCCCTGAGACTCTCCTGTGCAGCCTCTGGATTCACCTTCAGTAGCTATGCTATGCACTGGGTCCGCCAGGCTCCAGGCAAGGGGCTGGAGTGGGTGGCAGTTATATCATATGATGGAAGTAATAAATACTACGTAGACTCCGTGAAGGGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGTGCGAGAGATATCTCGCAGGCTGGTAAGCACTACTACTACGGTATGGACGTCTGGGGCCAAGGGACCACGGTCACCGTCTCCTCAGCACCCACCAAGGCTCCGGATGTGTTCCCCATCATATCAGGGTGCAGACACCCAAAGGATAACAGCCCTGTGGTCCTGGCATGCTTGATAACTGGGTACCACC",
    sequence_alignment="CAGGTGCAGCTGGTGGAGTCTGGGGGA...GGCGTGGTCCAGCCTGGGAGGTCCCTGAGACTCTCCTGTGCAGCCTCTGGATTCACCTTC............AGTAGCTATGCTATGCACTGGGTCCGCCAGGCTCCAGGCAAGGGGCTGGAGTGGGTGGCAGTTATATCATATGAT......GGAAGTAATAAATACTACGTAGACTCCGTGAAG...GGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGTGCGAGAGATATCTCGCAGGCTGGTAAGCACTACTACTACGGTATGGACGTCTGGGGCCAAGGGACCACGGTCACCGTCTCCTCA",
    germline_alignment="CAGGTGCAGCTGGTGGAGTCTGGGGGA...GGCGTGGTCCAGCCTGGGAGGTCCCTGAGACTCTCCTGTGCAGCCTCTGGATTCACCTTC............AGTAGCTATGCTATGCACTGGGTCCGCCAGGCTCCAGGCAAGGGGCTGGAGTGGGTGGCAGTTATATCATATGAT......GGAAGTAATAAATACTACGCAGACTCCGTGAAG...GGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGTGCGAGAGANNNNNNNNNGGCTGGTANNNACTACTACTACGGTATGGACGTCTGGGGCCAAGGGACCACGGTCACCGTCTCCTCA",
    v_call="IGHV3-30*04,IGHV3-30-3*03",
    j_call="IGHJ6*02",
    fwr1="CAGGTGCAGCTGGTGGAGTCTGGGGGA...GGCGTGGTCCAGCCTGGGAGGTCCCTGAGACTCTCCTGTGCAGCCTCT",
    cdr1="GGATTCACCTTC............AGTAGCTATGCT",
    fwr2="ATGCACTGGGTCCGCCAGGCTCCAGGCAAGGGGCTGGAGTGGGTGGCAGTT",
    cdr2="ATATCATATGAT......GGAAGTAATAAA",
    fwr3="TACTACGTAGACTCCGTGAAG...GGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGT",
    cdr3="GCGAGAGATATCTCGCAGGCTGGTAAGCACTACTACTACGGTATGGACGTC",
    fwr4="TGGGGCCAAGGGACCACGGTCACCGTCTCCTCA"
  )

  tmpf <- tempfile(fileext = "")
  write.table(test_parse, file = paste0(tmpf,".tsv"),
              sep = "\t", row.names = FALSE, col.names = TRUE,
              quote = FALSE)

  AbSolution:::parse_AIRRSeq_file(
    file=basename(tmpf),
    group="Test_group",
    patient="Test_patient",
    subgroup="Test_subgroup",
    sample="Test_sample",
    input_path=paste0(dirname(tmpf),"/"),
    C_region_included=T,
    FWR1partial=T,
    FWR4partial=F,
    D_gene=F,
    repertoire="BCR",
    output_path=paste0(dirname(tmpf),"/"),
    is_example=F
  )

  outfile=paste(paste0(dirname(tmpf),"/"), "IMGT_parsed_index.txt", sep = "")
  expect_true(file.exists(outfile))
  expect_gt(file.info(outfile)$size, 0)

  df <- read.table(outfile, sep = "\t", header = TRUE, stringsAsFactors = FALSE)

  expect_true(all(c("ID", "V_region", "J_region", "NT_CDR3") %in% colnames(df)))
  expect_true(all(df$Group == "Test_group"))

  expect_equal(c("TACTACGCAGACTCCGTGAAGGGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGT"),
               df$NT_FWR3[1])
  expect_equal(c("TACTACGTAGACTCCGTGAAGGGCCGATTCACCATCTCCAGAGACAATTCCAAGAACACGCTGTATCTGCAAATGAACAGCCTGAGAGCTGAGGACACGGCTGTGTATTACTGT"),
               df$NT_FWR3[2])
})
