Example #1
0
func parseSif(cxDecoder *json.Decoder, w *csv.Writer) {

	// Edge slice used for later mapping
	var edges []cx.Edge

	// Node ID to node name map
	nodeMap := make(map[int64]string)

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error: ", err)
			return
		}

		for cxDecoder.More() {
			token, err := cxDecoder.Token()
			if err != nil {
				fmt.Println(err)
			}

			if token == "nodes" {
				processNode(cxDecoder, nodeMap)
			} else if token == "edges" {
				processEdge(cxDecoder, &edges)
			}
		}
	}

	writeSif(nodeMap, edges, *w)
}
Example #2
0
func toList(cxDecoder *json.Decoder, w io.Writer) {

	// Node ID to node name map
	nodeMap := make(map[string]int64)

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error: ", err)
			return
		}

		for cxDecoder.More() {
			token, err := cxDecoder.Token()
			if err != nil {
				fmt.Println(err)
			}

			if token == "nodes" {
				processNode(cxDecoder, nodeMap)
			}
		}
	}

	err := json.NewEncoder(w).Encode(nodeMap)
	if err != nil {
		panic("parse err")
	}
}
Example #3
0
func findFeatureArray(dec *json.Decoder) error {
	for {
		t, err := dec.Token()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		if s, ok := t.(string); ok && s == "features" && dec.More() {
			// we found the features element
			d, err := dec.Token()
			if err != nil {
				return err
			}
			if delim, ok := d.(json.Delim); ok {
				if delim.String() == "[" {
					// we have our start of the array
					break
				} else {
					// A different kind of delimiter
					return fmt.Errorf("Expected features to be an array.")
				}
			}
		}
	}

	if !dec.More() {
		return fmt.Errorf("Cannot find any features.")
	}
	return nil
}
Example #4
0
// parse one level of the json translation file, this func will also parse sublevels by calling
func readLevel(jspath string, dec *json.Decoder, tr map[string]string) {
	if jspath == "" {
		t, err := dec.Token()
		if err != nil {
			log.Fatal("translate error 1: ", err)
		}
		if fmt.Sprintf("%T:%v", t, t) != "json.Delim:{" {
			log.Fatal("translate error 1.1: bad translaton file format")
		}
		//fmt.Printf("%s> %T:%v\n", jspath, t, t)
	}

	for dec.More() {
		t1, err1 := dec.Token()
		t2, err2 := dec.Token()

		if err1 != nil {
			log.Fatal("translate error 2: ", err1)
		}

		if err2 != nil {
			log.Fatal("translate error 3: ", err2)
		}

		//fmt.Printf("%s: %T:%v => %T:%v\n", jspath, t1, t1, t2, t2)

		var newpath string
		if jspath == "" {
			newpath = fmt.Sprintf("%v", t1)
		} else {
			newpath = fmt.Sprintf("%s.%v", jspath, t1)
		}
		if fmt.Sprintf("%T:%v", t2, t2) == "json.Delim:{" {
			readLevel(newpath, dec, tr)
		} else {
			tr[newpath] = fmt.Sprintf("%v", t2)
			//fmt.Printf("%s => %v\n", newpath, t2)
		}
	}

	t, err := dec.Token()
	if err != nil {
		log.Fatal("translate error 3=4: ", err)
	}
	if fmt.Sprintf("%T:%v", t, t) != "json.Delim:}" {
		log.Fatal("translate error 4.1: bad translaton file format")
	}
	//fmt.Printf("%s< %T:%v\n", jspath, t, t)

}
Example #5
0
func processEdge(decoder *json.Decoder, edges *[]cx.Edge) {
	token, err := decoder.Token()
	if err != nil || token != arrayStart {
		return
	}

	var entry cx.Edge
	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}
		*edges = append(*edges, entry)
	}
}
Example #6
0
func (m *omap) unmarshalJSON(dec *json.Decoder) error {
	for dec.More() {
		t, err := dec.Token()
		if err != nil {
			return err
		}

		key, isKey := t.(string)
		if !isKey {
			return fmt.Errorf("%t %s is not string(expected key)", t, t)
		}

		val, err := getVal(dec)
		if err != nil {
			return err
		}
		m.Set(key, val)
	}
	return nil
}
Example #7
0
func processNode(decoder *json.Decoder, nodes map[int64]string) {
	token, err := decoder.Token()
	if err != nil || token != arrayStart {
		return
	}

	var entry cx.Node
	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}

		if entry.N == "" {
			nodes[entry.ID] = strconv.FormatInt(entry.ID, 10)
		} else {
			nodes[entry.ID] = entry.N
		}
	}

}
Example #8
0
func processNode(decoder *json.Decoder, nodes map[string]int64) {
	_, err := decoder.Token()
	if err != nil {
		return
	}

	var entry cx.Node

	for decoder.More() {
		err := decoder.Decode(&entry)
		if err != nil {
			return
		}

		if entry.N == "" {
			nodes[strconv.FormatInt(entry.ID, 10)] = entry.ID
		} else {
			nodes[entry.N] = entry.ID
		}
	}
}
Example #9
0
func run(cxDecoder *json.Decoder, w io.Writer) {

	// Initialize handlers
	handlers := initHandlers()

	// Network Object
	networkAttr := make(map[string]interface{})

	// Elements
	var nodes []cyjs.CyJSNode
	var edges []cyjs.CyJSEdge
	layout := make(map[string]interface{})
	vps := make(map[string]interface{})

	elements := cyjs.Elements{Nodes: nodes, Edges: edges}

	// Temp storage for attributes
	nodeAttrs := make(map[string]interface{})
	edgeAttrs := make(map[string]interface{})

	cxData := make(map[string]interface{})
	// Basic Cytoscape.js object
	cyjsNetwork := cyjs.CyJS{Data: networkAttr, Elements: elements,
		CxData: cxData}

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			log.Println(err)
			return
		}

		// Decode entry one-by-one.
		for cxDecoder.More() {
			var entry map[string]interface{}
			err := cxDecoder.Decode(&entry)
			if err != nil {
				log.Fatal(err)
			}

			parseCxEntry(handlers, entry, &cyjsNetwork, &nodeAttrs,
				&edgeAttrs, &layout, &vps)
		}
	}

	assignNodeAttr(cyjsNetwork.Elements.Nodes, nodeAttrs, layout)
	assignEdgeAttr(cyjsNetwork.Elements.Edges, edgeAttrs)

	// Add style to net (if style data is available...)
	if vps != nil && reflect.ValueOf(vps).IsNil() == false {
		if vps["style"] != nil {
			cyjsNetwork.Style = vps["style"].([]cyjs.SelectorEntry)
		}
	}

	jsonString, err := json.Marshal(cyjsNetwork)

	if err != nil {
		fmt.Println("ERR: ", err)
	} else {
		//		fmt.Println(string(jsonString))
		w.Write(jsonString)
	}
	//debug()
}
Example #10
0
// LoadJSON loads data from a JSON file in the database table.
//
// If src != nil, LoadJSON loads the source from src and the filename is only used
// when recording file information. The type of the argument for the src parameter
// must be []byte, or io.Reader. If src == nil, LoadJSON loads the file
// specified by filename.
func (cx *DBContext) LoadJSON(src interface{}, filename, table string) (err error) {
	var dec *json.Decoder

	if src != nil {
		switch s := src.(type) {
		case []byte:
			dec = json.NewDecoder(bytes.NewReader(s))
		case io.Reader:
			dec = json.NewDecoder(s)
		default:
			return fmt.Errorf("invalid source")
		}
	} else {
		f, err := os.Open(filename)
		if err != nil {
			return err
		}
		defer func() {
			if err1 := f.Close(); err == nil {
				err = err1
			}
		}()

		dec = json.NewDecoder(f)
	}

	// The first row must have the name of the columns.
	firstRecord := make([]string, 0)
	if err = dec.Decode(&firstRecord); err != nil {
		return err
	}

	tx, err := cx.DB.Begin()
	if err != nil {
		return err
	}
	defer func() {
		if err != nil {
			err1 := tx.Rollback()
			if err1 != nil && datautil.VERBOSE {
				log.WithFields(log.Fields{
					"file":  filename,
					"table": table,
				}).Warn(err1)
			}
		}
	}()

	insertCode := fmt.Sprintf("INSERT INTO %s (%s) VALUES(%s)",
		table,
		strings.Join(firstRecord, ", "),
		cx.bindChars(len(firstRecord)),
	)
	insert, err := tx.Prepare(insertCode)
	if err != nil {
		return fmt.Errorf("SQL code: %q\n%s", insertCode, err)
	}

	list := make([]interface{}, 0)

	for line := 2; dec.More(); line++ {
		if err = dec.Decode(&list); err != nil {
			return fmt.Errorf("%q line %d: %s", filename, line, err)
		}

		_, err = tx.Stmt(insert).Exec(list...)
		if err != nil {
			return fmt.Errorf("code: %q vars: %v => %s", insertCode, list, err)
		}
	}

	if err = insert.Close(); err != nil && datautil.VERBOSE {
		log.WithFields(log.Fields{
			"file":  filename,
			"table": table,
		}).Warn(err)
	}

	if err = tx.Commit(); err != nil {
		return err
	}
	if datautil.VERBOSE {
		log.WithFields(log.Fields{
			"file":  packer.RelativeDir(filename),
			"db":    cx.Conn.GetDBName(),
			"table": table,
		}).Info("Load JSON file")
	}
	return nil
}