github.com/vertgenlab/gonomics@v1.0.0/cmd/vcfFormat/vcfFormat_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/csv"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/vertgenlab/gonomics/exception"
    10  	"github.com/vertgenlab/gonomics/fileio"
    11  )
    12  
    13  var VcfFormatTests = []struct {
    14  	InFile        string
    15  	OutFile       string
    16  	ExpectedFile  string
    17  	EnsemblToUCSC bool
    18  	UCSCToEnsembl bool
    19  	FixVcfRecords bool
    20  	Ref           string
    21  	ClearInfo     bool
    22  }{
    23  	{"testdata/test.UCSC.vcf", "testdata/tmp.UCSCtoEnsembl.vcf", "testdata/test.Ensembl.vcf", false, true, false, "", false},
    24  	{"testdata/test.Ensembl.vcf", "testdata/tmp.EnsemblToUCSC.vcf", "testdata/test.UCSC.vcf", true, false, false, "", false},
    25  	{"testdata/test.UCSC.vcf", "testdata/tmp.UCSCnoInfo.vcf", "testdata/expected.noInfo.vcf", false, false, false, "", true},
    26  	{"testdata/test.broken.vcf", "testdata/tmp.fixed.vcf", "testdata/expected.fixed.vcf", false, false, true, "testdata/test.fa", false},
    27  }
    28  
    29  func TestVcfFormat(t *testing.T) {
    30  	var err error
    31  	for _, v := range VcfFormatTests {
    32  		vcfFormat(v.InFile, v.OutFile, v.EnsemblToUCSC, v.UCSCToEnsembl, v.FixVcfRecords, v.Ref, v.ClearInfo, false)
    33  		if !fileio.AreEqual(v.OutFile, v.ExpectedFile) {
    34  			t.Errorf("Error in VcfFormat. Output does not match expected.")
    35  		} else {
    36  			err = os.Remove(v.OutFile)
    37  			exception.PanicOnErr(err)
    38  		}
    39  	}
    40  }
    41  
    42  // TODO: better table tests.
    43  func TestVcfTable(t *testing.T) {
    44  	infile := "testdata/test_table.vcf"
    45  	outfile := "testdata/actual_table.csv"
    46  	expected := "testdata/table_expected.csv"
    47  	vcfFormat(infile, outfile, false, false, false, "", false, true)
    48  
    49  	actualFile := fileio.EasyOpen(outfile)
    50  	expectedFile := fileio.EasyOpen(expected)
    51  	actualReader := csv.NewReader(actualFile)
    52  	expectedReader := csv.NewReader(expectedFile)
    53  
    54  	var actualData, expectedData [][]string
    55  	var err error
    56  	expectedData, err = expectedReader.ReadAll()
    57  	exception.PanicOnErr(err)
    58  	actualData, err = actualReader.ReadAll()
    59  	if err != nil {
    60  		t.Error("problem writing csv file")
    61  	}
    62  
    63  	var actualOrdering []int = make([]int, len(expectedData[0]))
    64  
    65  	// get order to deal with map randomness
    66  	for i := range expectedData[0] {
    67  		for j := range actualData[0] {
    68  			if expectedData[0][i] == actualData[0][j] {
    69  				actualOrdering[j] = i
    70  			}
    71  		}
    72  	}
    73  
    74  	// reorder actual
    75  	for i := range actualData {
    76  		newLine := make([]string, len(actualData[i]))
    77  		for j := range actualData[i] {
    78  			newLine[actualOrdering[j]] = actualData[i][j]
    79  		}
    80  		actualData[i] = newLine
    81  	}
    82  
    83  	if len(expectedData) != len(actualData) {
    84  		t.Error("problem writing csv file")
    85  	}
    86  
    87  	for i := range expectedData {
    88  		if strings.Join(expectedData[i], "") != strings.Join(actualData[i], "") {
    89  			t.Error("problem with vcf formatting to csv")
    90  		}
    91  	}
    92  
    93  	err = actualFile.Close()
    94  	exception.PanicOnErr(err)
    95  	err = expectedFile.Close()
    96  	exception.PanicOnErr(err)
    97  
    98  	if !t.Failed() {
    99  		err = os.Remove(outfile)
   100  		exception.PanicOnErr(err)
   101  	}
   102  }