Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • cd7dada
  • /
  • mcorr-bam
  • /
  • codon.go
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:2002e10cd8fa29c302e36aea4d83fb8af6b611c4
directory badge
swh:1:dir:972e117fbeb516c70f0394109be3c56032a160bd

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
codon.go
package main

import (
	"github.com/mingzhi/ncbiftp/taxonomy"
)

// Codon stores a codon value, the position in a genome and the read id.
type Codon struct {
	Seq     string
	ReadID  string
	GenePos int
}

// ContainsGap return true if '-' in a sequence.
func (c Codon) ContainsGap() bool {
	for _, b := range c.Seq {
		if b == '-' {
			return true
		}
	}
	return false
}

// CodonPile stores a pile of Codon, which are at a particular genome position.
type CodonPile struct {
	genePos  int
	codonMap map[string]Codon
}

// NewCodonPile return a new CodonPile.
func NewCodonPile() *CodonPile {
	return &CodonPile{codonMap: make(map[string]Codon)}
}

// Add appends a new Codon.
func (cp *CodonPile) Add(c Codon) {
	cp.genePos = c.GenePos
	cp.codonMap[c.ReadID] = c
}

// LookUp search a codon by ReadName. If not found, it returns nil.
func (cp *CodonPile) LookUp(readID string) Codon {
	return cp.codonMap[readID]
}

// Len return the lenght of pileup Codons.
func (cp *CodonPile) Len() int {
	return len(cp.codonMap)
}

// GenePos return the gene position.
func (cp *CodonPile) GenePos() int {
	return cp.genePos
}

// CodonGene represents a gene with an array of CodonPile.
type CodonGene struct {
	CodonPiles []*CodonPile
}

// NewCodonGene return a new CodonGene.
func NewCodonGene() *CodonGene {
	return &CodonGene{}
}

// AddCodon add a codon.
func (cg *CodonGene) AddCodon(c Codon) {
	for len(cg.CodonPiles) <= c.GenePos {
		cg.CodonPiles = append(cg.CodonPiles, NewCodonPile())
	}
	cg.CodonPiles[c.GenePos].Add(c)
}

// DepthAt return the pile depth at position i.
func (cg *CodonGene) DepthAt(i int) int {
	if len(cg.CodonPiles) <= i {
		return 0
	}
	return cg.CodonPiles[i].Len()
}

// Len returns length of CodonPile array.
func (cg *CodonGene) Len() int {
	return len(cg.CodonPiles)
}

// CodonPair stores a pair of Codon
type CodonPair struct {
	A, B Codon
}

// PairCodonAt pairs codons at positions i and j.
func (cg *CodonGene) PairCodonAt(i, j int) (pairs []CodonPair) {
	if i >= len(cg.CodonPiles) || j >= len(cg.CodonPiles) {
		return
	}

	if i > j {
		j, i = i, j
	}

	pile1 := cg.CodonPiles[i]
	if i == j {
		for _, codon := range pile1.codonMap {
			pairs = append(pairs, CodonPair{A: codon, B: codon})
		}
	}
	pile2 := cg.CodonPiles[j]
	for readID, codon1 := range pile1.codonMap {
		codon2 := pile2.LookUp(readID)
		if codon2.ReadID != "" {
			pairs = append(pairs, CodonPair{A: codon1, B: codon2})
		}
	}
	return
}

// SynoumousSplitCodonPairs split codon pairs into synoumous pairs.
func SynoumousSplitCodonPairs(codonPairs []CodonPair, codeTable *taxonomy.GeneticCode) [][]CodonPair {
	var splittedPairs [][]CodonPair
	var aaArray []string
	for _, codonPair := range codonPairs {
		hasGap := false
		for _, codon := range []Codon{codonPair.A, codonPair.B} {
			for _, b := range codon.Seq {
				if !isATGC(byte(b)) {
					hasGap = true
					break
				}
			}
			if hasGap {
				break
			}
		}

		if hasGap {
			continue
		}

		a := codeTable.Table[codonPair.A.Seq]
		b := codeTable.Table[codonPair.B.Seq]
		ab := string([]byte{a, b})
		index := -1
		for i, aa := range aaArray {
			if aa == ab {
				index = i
			}
		}
		if index == -1 {
			index = len(aaArray)
			aaArray = append(aaArray, ab)
			splittedPairs = append(splittedPairs, []CodonPair{})
		}
		splittedPairs[index] = append(splittedPairs[index], codonPair)
	}
	return splittedPairs
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API