text:
#File Name: barcodeCounter.jl
#Author: Henry C. Wong (wongh@wustl.edu,henrycwong@mst.edu,cywongx@gmail.com)
#Descr:     Julia file that finds the barcodes of fastq file based on a given spacer sequence
#Dependecies:
#  Julia 1.4.2
#  FASTX
#  DelimitedFiles
#
#  Arguments: barcodeDictionary.tsv *.fastq *.info
#    1) barcode dictionary in a tsv format where the first row are barcodes and the second row are names
#    2) a fastq file
#    3) a file that contains the spacer sequence (and only the spacer sequence)

include("barcodeCounterFuncts.jl");
using DelimitedFiles
using FASTX

#read barcode (dictionary) file
grid = readBarcodeFile(ARGS[1]);
#spin off barcodes and sequence names into their own file
barcodeArray = grid[:,1];
names = grid[:,2];
#create empty array to fill counts with
counts = fill(0,length(names));
#create dictionaries to align names, barcodes, and counts
countDict = Dict(barcodeArray .=> counts);
nameDict = Dict(barcodeArray .=> names);

#fill dictionaries with data from fastq file
fillDict(ARGS[2],countDict,nameDict);
#merge both dictionaries to prepare to output data to file
contentArray = syncDictsToArray(countDict,nameDict);
#sort combined matrix by descending order
sortedContentArr = sortslices(contentArray,dims=1,by=x->x[3],rev=true);
#create file name
fileName = createOutputFileName(ARGS[2]);
#write to file
writeResultsToFile(sortedContentArr,fileName);

max_stars_repo_name: HenryCWong/genomic-barcode-counter

max_issues_repo_name: HenryCWong/genomic-barcode-counter

max_forks_repo_name: HenryCWong/genomic-barcode-counter


text:
println("==============flag! bb_user_limits.jl======================================")

function isbreak_mip_gap(tree)
    if isdefined(tree, :incumbent) && !tree.options.all_solutions
        incu = tree.incumbent
        b = tree.best_bound
        f = incu.objval
        gap = abs(b-f)/abs(f)
        # if f and g are 0 the gap would be NaN but it's gap=0
        if isapprox(b,f,atol=tree.options.atol)
            gap = 0 # doesn't really equal 0 but should break
        end
        if gap <= tree.options.mip_gap
            default_opts = get_default_options()
            if tree.options.mip_gap > default_opts.mip_gap
                tree.limit = :MipGap
            end
            return true
        end
    end
    return false
end

"""
    isbreak_new_incumbent_limits(tree)

Return true if mip_gap or best_obj_stop is reached
"""
function isbreak_new_incumbent_limits(tree)
    if !isnan(tree.options.best_obj_stop)
        inc_val = tree.incumbent.objval
        bos = tree.options.best_obj_stop
        sense = tree.m.obj_sense
        if (sense == :Min && inc_val <= bos) || (sense == :Max && inc_val >= bos)
            tree.limit = :BestObjStop
            return true
        end
    end

    return isbreak_mip_gap(tree)
end

"""
    isbreak_time_limit!(tree)

Check if time limit is reached and  set or update the Incumbent
"""
function isbreak_time_limit!(tree)
    if !isnan(tree.options.time_limit) && time()-tree.start_time >= tree.options.time_limit
        tree.limit = :Time
        return true
    end
    return false
end

"""
    isbreak_after_step!(tree)

Check if break...
Break if
- solution limit is reached
- time limit is reached
"""
function isbreak_after_step!(tree)
    # maybe break on solution_limit (can be higher if two solutions found in last step)
    if tree.options.solution_limit > 0 && tree.nsolutions >= tree.options.solution_limit
        tree.limit = :EnoughSolutions
        return true
    end
    if isbreak_time_limit!(tree)
        return true
    end

    return false
end

ext: jl


text:
#
# Types supporting structural definition of models and their components
#

# Objects with a `name` attribute
@class NamedObj <: MimiClass begin
    name::Symbol
end

"""
    nameof(obj::NamedDef) = obj.name

Return the name of `def`.  `NamedDef`s include `DatumDef`, `ComponentDef`, and `CompositeComponentDef`
"""
Base.nameof(obj::AbstractNamedObj) = obj.name

# Deprecate old definition in favor of standard name
@deprecate name(obj::AbstractNamedObj) nameof(obj)

# Similar structure is used for variables and parameters (parameters merely adds `default`)
@class mutable DatumDef <: NamedObj begin
    comp_path::Union{Nothing, ComponentPath}
    datatype::DataType
    dim_names::Vector{Symbol}
    description::String
    unit::String
end

Base.pathof(obj::AbstractDatumDef) = obj.comp_path

@class mutable VariableDef <: DatumDef

@class mutable ParameterDef <: DatumDef begin
    # ParameterDef adds a default value, which can be specified in @defcomp
    default::Any
end

@class mutable ComponentDef <: NamedObj begin
    comp_id::Union{Nothing, ComponentId}    # allow anonynous top-level (composite) ComponentDefs (must be referenced by a ModelDef)
    comp_path::Union{Nothing, ComponentPath}
    dim_dict::OrderedDict{Symbol, Union{Nothing, Dimension}}
    namespace::OrderedDict{Symbol, Any}
    first::Union{Nothing, Int}
    last::Union{Nothing, Int}
    is_uniform::Bool

    # Store a reference to the AbstractCompositeComponent that contains this comp def.
    # That type is defined later, so we declare Any here. Parent is `nothing` for
    # detached (i.e., "template") components and is set when added to a composite.
    parent::Any

    function ComponentDef(self::ComponentDef, comp_id::Nothing)
        error("Leaf ComponentDef objects must have a valid ComponentId name (not nothing)")
    end

    # ComponentDefs are created "empty". Elements are subsequently added.
    function ComponentDef(self::AbstractComponentDef, comp_id::Union{Nothing, ComponentId}=nothing;
                          name::Union{Nothing, Symbol}=nothing)
        if comp_id === nothing
            # ModelDefs are anonymous, but since they're gensym'd, they can claim the Mimi package
            comp_id = ComponentId(Mimi, @or(name, gensym(nameof(typeof(self)))))
        end

        name = @or(name, comp_id.comp_name)
        NamedObj(self, name)

        self.comp_id = comp_id

        # self.comp_path = nothing    # this is set in add_comp!() and ModelDef()
        self.comp_path = ComponentPath(name)    # this is set in add_comp!() and ModelDef()

        self.dim_dict  = OrderedDict{Symbol, Union{Nothing, Dimension}}()
        self.namespace = OrderedDict{Symbol, Any}()
        self.first = self.last = nothing
        self.is_uniform = true
        self.parent = nothing
        return self
    end

    function ComponentDef(comp_id::Union{Nothing, ComponentId};
                          name::Union{Nothing, Symbol}=nothing)
        self = new()
        return ComponentDef(self, comp_id; name=name)
    end
end

ns(obj::AbstractComponentDef) = obj.namespace
comp_id(obj::AbstractComponentDef) = obj.comp_id
Base.pathof(obj::AbstractComponentDef) = obj.comp_path
dim_dict(obj::AbstractComponentDef) = obj.dim_dict
first_period(obj::AbstractComponentDef) = obj.first
last_period(obj::AbstractComponentDef) = obj.last
isuniform(obj::AbstractComponentDef) = obj.is_uniform

Base.parent(obj::AbstractComponentDef) = obj.parent

# Used by @defcomposite to communicate subcomponent information
struct SubComponent <: MimiStruct
    module_obj::Union{Nothing, Module}
    comp_name::Symbol
    alias::Union{Nothing, Symbol}
end

# UnnamedReferences are stored in CompositeParameterDefs or CompositeVariableDefs
# to point to subcomponents' parameters or variables.
struct UnnamedReference
    comp_name::Symbol   # name of the referenced subcomponent
    datum_name::Symbol  # name of the parameter or variable in the subcomponent's namespace
end

@class CompositeParameterDef <: ParameterDef begin
    refs::Vector{UnnamedReference}
end

# Create a CompositeParameterDef from a list of compdefs/pnames
function CompositeParameterDef(name::Symbol, comp_path::ComponentPath, pairs::Vector{Pair{T, Symbol}}, kwargs) where T <: AbstractComponentDef
    # Create the necessary references
    refs = [UnnamedReference(nameof(comp), param_name) for (comp, param_name) in pairs]

    # Unpack the kwargs
    datatype = kwargs[:datatype]
    dim_names = kwargs[:dim_names]
    description = kwargs[:description]
    unit = kwargs[:unit]
    default = kwargs[:default]

    return CompositeParameterDef(name, comp_path, datatype, dim_names, description, unit, default, refs)
end

# Create a CompositeParameterDef from one subcomponent's ParameterDef (used by import_params!)
function CompositeParameterDef(obj, param_ref)
    subcomp_name = param_ref.comp_name
    pname = param_ref.datum_name

    pardef = obj.namespace[subcomp_name].namespace[pname]
    return CompositeParameterDef(pname, pathof(obj), pardef.datatype, pardef.dim_names, pardef.description, pardef.unit, pardef.default, [param_ref])
end

@class CompositeVariableDef <: VariableDef begin
    ref::UnnamedReference
end

function CompositeVariableDef(name::Symbol, comp_path::ComponentPath, subcomp::AbstractComponentDef, vname::Symbol)
    vardef = subcomp.namespace[vname]
    comp_name = subcomp.name
    return CompositeVariableDef(name, comp_path, vardef.datatype, vardef.dim_names, vardef.description, vardef.unit, UnnamedReference(comp_name, vname))
end

# Define which types can appear in the namespace dict for leaf and composite compdefs
global const LeafNamespaceElement      = AbstractDatumDef
global const CompositeDatumDef         = Union{AbstractCompositeParameterDef, AbstractCompositeVariableDef}
global const CompositeNamespaceElement = Union{AbstractComponentDef, CompositeDatumDef}
global const NamespaceElement          = Union{LeafNamespaceElement, CompositeNamespaceElement}

@class mutable CompositeComponentDef <: ComponentDef begin
    internal_param_conns::Vector{InternalParameterConnection}

    # Names of external params that the ConnectorComps will use as their :input2 parameters.
    backups::Vector{Symbol}

    sorted_comps::Union{Nothing, Vector{Symbol}}

    function CompositeComponentDef(comp_id::Union{Nothing, ComponentId}=nothing)
        self = new()
        CompositeComponentDef(self, comp_id)
        return self
    end

    function CompositeComponentDef(self::AbstractCompositeComponentDef, comp_id::Union{Nothing, ComponentId}=nothing)
        ComponentDef(self, comp_id) # call superclass' initializer

        self.comp_path = ComponentPath(self.name)
        self.internal_param_conns = Vector{InternalParameterConnection}()
        self.backups = Vector{Symbol}()
        self.sorted_comps = nothing
    end
end

# Used by @defcomposite
function CompositeComponentDef(comp_id::ComponentId, alias::Symbol, subcomps::Vector{SubComponent},
                               calling_module::Module)
    # @info "CompositeComponentDef($comp_id, $alias, $subcomps)"
    composite = CompositeComponentDef(comp_id)

    for c in subcomps
        # @info "subcomp $c: module: $(printable(c.module_obj)), calling module: $(nameof(calling_module))"
        comp_module = @or(c.module_obj, calling_module)
        subcomp_id = ComponentId(comp_module, c.comp_name)
        subcomp = compdef(subcomp_id)
        add_comp!(composite, subcomp, @or(c.alias, c.comp_name))
    end
    return composite
end

add_backup!(obj::AbstractCompositeComponentDef, backup) = push!(obj.backups, backup)

internal_param_conns(obj::AbstractCompositeComponentDef) = obj.internal_param_conns

is_leaf(c::AbstractComponentDef) = true
is_leaf(c::AbstractCompositeComponentDef) = false
is_composite(c::AbstractComponentDef) = !is_leaf(c)

ComponentPath(obj::AbstractCompositeComponentDef, name::Symbol) = ComponentPath(obj.comp_path, name)

ComponentPath(obj::AbstractCompositeComponentDef, path::AbstractString) = comp_path(obj, path)

ComponentPath(obj::AbstractCompositeComponentDef, names::Symbol...) = ComponentPath(obj.comp_path.names..., names...)

@class mutable ModelDef <: CompositeComponentDef begin
    external_param_conns::Vector{ExternalParameterConnection}
    external_params::Dict{Symbol, ModelParameter}

    number_type::DataType
    dirty::Bool

    function ModelDef(number_type::DataType=Float64)
        self = new()
        CompositeComponentDef(self)  # call super's initializer

        ext_conns  = Vector{ExternalParameterConnection}()
        ext_params = Dict{Symbol, ModelParameter}()

        # N.B. @class-generated method
        return ModelDef(self, ext_conns, ext_params, number_type, false)
    end
end

external_param_conns(md::ModelDef) = md.external_param_conns

external_params(md::ModelDef) = md.external_params

#
# Reference types offer a more convenient syntax for interrogating Components.
#

# A container for a component, for interacting with it within a model.
@class ComponentReference <: MimiClass begin
    parent::AbstractComponentDef
    comp_path::ComponentPath
end

# Define access methods via getfield() since we override dot syntax
Base.parent(comp_ref::AbstractComponentReference) = getfield(comp_ref, :parent)
Base.pathof(comp_ref::AbstractComponentReference) = getfield(comp_ref, :comp_path)

function ComponentReference(parent::AbstractComponentDef, name::Symbol)
    return ComponentReference(parent, ComponentPath(pathof(parent), name))
end

# A container for a variable within a component, to improve connect_param! aesthetics,
# by supporting subscripting notation via getindex & setindex .
@class VariableReference <: ComponentReference begin
    var_name::Symbol
end

var_name(comp_ref::VariableReference) = getfield(comp_ref, :var_name)


text:
module Quantica

# Use README as the docstring of the module:
using Base.Threads: Iterators
@doc read(joinpath(dirname(@__DIR__), "README.md"), String) Quantica

using Requires

function __init__()
      @require GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" include("plot_makie.jl")
      @require VegaLite = "112f6efa-9a02-5b7d-90c0-432ed331239a" include("plot_vegalite.jl")
end

using StaticArrays, NearestNeighbors, SparseArrays, LinearAlgebra, OffsetArrays,
      ProgressMeter, Random, SuiteSparse

using ExprTools

using SparseArrays: getcolptr, AbstractSparseMatrix, AbstractSparseMatrixCSC

using Statistics: mean

using Compat # for use of argmin/argmax in bandstructure.jl

export sublat, bravais, lattice, dims, supercell, unitcell,
       hopping, onsite, @onsite!, @hopping!, @block!, parameters, siteselector, hopselector, nrange,
       sitepositions, siteindices, not,
       ket, ketmodel, randomkets, basiskets,
       hamiltonian, parametric, bloch, bloch!, similarmatrix,
       flatten, unflatten, orbitalstructure, wrap, transform!, combine,
       spectrum, bandstructure, diagonalizer, cuboid, isometric, splitbands,
       bands, vertices, minima, maxima, gapedge, gap, isinband,
       energies, states, degeneracy,
       momentaKPM, dosKPM, averageKPM, densityKPM, bandrangeKPM,
       greens, greensolver, Schur1D, proj_DACP, DACPdiagonaliser

export RegionPresets, RP, LatticePresets, LP, HamiltonianPresets, HP

export LinearAlgebraPackage, ArpackPackage, ArnoldiMethodPackage, KrylovKitPackage

export @SMatrix, @SVector, SMatrix, SVector, SA

export ishermitian, I

export SparseMatrixCSC

const NameType = Symbol
const nametype = Symbol

const TOOMANYITERS = 10^8

include("iterators.jl")
include("tools.jl")
include("presets.jl")
include("lattice.jl")
include("model.jl")
include("hamiltonian.jl")
include("ket.jl")
include("parametric.jl")
include("slice.jl")
include("mesh.jl")
include("diagonalizer.jl")
include("bandstructure.jl")
include("KPM.jl")
include("DACP.jl")
include("effective.jl")
include("greens.jl")
include("convert.jl")

end

max_stars_repo_name: pablosanjose/Quantica

max_issues_repo_name: pablosanjose/Quantica

max_forks_repo_name: pablosanjose/Quantica


text:
"""
    psf(Environment[,cent])

Calculate frequency-domain point spread function using the Environment struct to access
steeringvectors. Optionally, supply the index where the psf is centered, default is (N/2)+1.
"""
function psf(E::Environment,cent::Int64=floor(Int,E.N/2)+1)
    @unpack steeringvec,M,N,Nf,fn = E
    p = Array{Float64, 2}(undef, N, Nf)
    @views @inbounds for j in 1:Nf
        AeroAcoustics.psf_col!(p[:,j],steeringvec.arr[:,:,j],cent)
    end
    return FreqArray(p,fn)
end

"""
    psf_col!(p,steeringvec,cent)

Calculate single frequency point spread function as a column vector.
"""
function psf_col!(p::A,steeringvec::B,cent::Int64) where {T<:AbstractFloat, N, A<:AbstractArray{T,1}, B<:AbstractArray{Complex{T},N}}
    M = size(steeringvec,1)
    @views p .= M^2 .*abs2.(steeringvec'*steeringvec[:,cent])
end


text:
module MultinomialRegression

export fit, predict, L1, L2, BoxRegularizer

include("regularization.jl")
include("fitpredict.jl")

using .regularization  # Independent
using .fitpredict      # Depends on: regularization

end


text:
export iauTr
"""
Transpose an r-matrix.

This function is part of the International Astronomical Union's
SOFA (Standards Of Fundamental Astronomy) software collection.

Status:  vector/matrix support function.

Given:
   r        double[3][3]    r-matrix

Returned:
   rt       double[3][3]    transpose

Note:
   It is permissible for r and rt to be the same array.

Called:
   iauCr        copy r-matrix

This revision:  2013 June 18

SOFA release 2018-01-30

Copyright (C) 2018 IAU SOFA Board.  See notes at end.
"""

# void iauTr(double r[3][3], double rt[3][3])

function iauTr(r::AbstractMatrix{<:Real})
   rt = zeros(Float64, 3, 3)

   ccall((:iauTr, libsofa_c), Cvoid,
       (Ptr{Cdouble}, Ptr{Cdouble}),
       convert(Array{Float64, 2}, r'),
       rt)

   return SMatrix{3,3}(rt')
end


text:
import DataFrames: DataFrame

# using Stars
include("convert.jl")

"""
    st_shrink(ga::AbstractGeoArray, mask::Nothing=nothing; missval=NaN)
    st_shrink(ga::AbstractGeoArray, mask::AbstractGeoArray)
    st_shrink(ga::AbstractGeoArray, mask::AbstractArray{<:Real,2})

Only true values in `mask` will be kept.

## Arguments
- `mask`:
    + `AbstractGeoArray`: returned by `get_mask`
    + ``
"""
st_shrink(ga::AbstractGeoArray) = st_shrink(ga, !isnan(ga[1]))

st_shrink(ga::AbstractGeoArray, mask::AbstractGeoArray;) =
    st_shrink(ga, mask.A[:, :, 1])

function st_shrink(ga::AbstractGeoArray, mask::AbstractArray{<:Real,2})
    ind = findall(mask)
    # ind_vec = LinearIndices(mat)[ind]
    rows = map(x -> x[1], ind) # x, long
    cols = map(x -> x[2], ind) # y, lat

    I_x = seq(Range(rows)...) # get the Range of `rows` and `cols`
    I_y = seq(Range(cols)...)
    ga[I_x, I_y]
end

## TODO:
# ind2mask

function mask2ind(r_mask::AbstractGeoArray{Bool})
    ind = findall(r_mask.A)
    LinearIndices(r_mask.A)[ind]
end

"""
d_coord = st_as_sf(mask)
"""
function maskCoords(r_mask::AbstractGeoArray{Bool})
    LON, LAT = st_coords(r_mask)
    ind = mask2ind(r_mask)
    DataFrame(id=ind, lon=LON[ind], lat=LAT[ind])
end

"""
    st_as_sf(ga::AbstractGeoArray, mask::Union{Nothing, AbstractArray{Bool, 2}} = nothing)
    st_as_sf(file::AbstractString, mask = nothing)

If the input is a file path, `st_shrink` will be applied.
"""
function st_as_sf(ga::AbstractGeoArray, mask::AbstractArray{Bool,2})
    ind = findall(mask)
    # ind_vec = LinearIndices(mat)[ind]
    ntime = size(ga, 3)
    res = zeros(eltype(ga.A), length(ind), ntime)
    @views for i = 1:ntime
        mat = ga.A[:, :, i]
        res[:, i] = mat[ind]
    end
    res
end
st_as_sf(ga::AbstractGeoArray, mask::Nothing=nothing) = st_as_sf(ga, .!isnan.(ga.A[:, :, 1]))
st_as_sf(ga::AbstractGeoArray, mask::AbstractGeoArray) = st_as_sf(ga, mask.A[:, :, 1])

# st_as_sf(ga::AbstractGeoArray, mask::AbstractArray{<:Bool,2}; kw...)

# keep enough information for the reverse operation
# rast(ga, vals = mask) # mat, mask

# function st_as_sf(file::AbstractString; missval=0)
#     ga = rast(file)
#     ga2 = st_shrink(ga)
#     st_as_sf(ga2; missval=missval)
# end

# mask is 3d boolean array
function st_as_sf(file::AbstractString, mask::AbstractGeoArray,
    mask_shrink=nothing)

    ga = rast(file)
    if mask_shrink !== nothing
        ga = st_shrink(ga, mask_shrink)
    end
    st_as_sf(ga, mask)
end

# the inverse operation

export rast2df, st_shrink, st_as_sf, maskCoords


text:
function test_random(AT)
    @testset "Random" begin
        @testset "rand" begin  # uniform
            for T in (Float32, Float64, Int64, Int32), d in (2, (2,2))
                A = AT{T}(undef, d)
                B = copy(A)
                rand!(A)
                rand!(B)
                @test !any(A .== B)
            end
        end
    end
end