          chr: chr,
          "Mb": mb,
          symbol: symbol
-      })
+      }
+    end
+
+    Repo.all(query) |> Enum.map(from_tuple_to_structure)
+
   end
 
   def marker_info(species,marker) do
-      query = """
-SELECT Geno.Chr, Geno.Mb, Species.Id,Geno.source FROM Geno, Species
-WHERE Species.Name = '#{species}'
-AND Geno.Name = '#{marker}'
-     """
-    {:ok, rows} = DB.query(query)
-    for r <- rows, do: ( {chr_name,chr_len,species_id,source} = r;
+#       query = """
+# SELECT Geno.Chr, Geno.Mb, Species.Id,Geno.source FROM Geno, Species
+# WHERE Species.Name = '#{species}'
+# AND Geno.Name = '#{marker}'
+#      """
+     query = from tab_species in Species,
+     join: geno in Geno,
+     on: tab_species."SpeciesId" == geno."SpeciesId",
+     where: tab_species."Name" == ^species and geno."Name" == ^marker,
+     select: {geno."Chr", geno."Mb", tab_species.id, geno."Source"}
+
+    # {:ok, rows} = DB.query(query)
+
+    from_tuple_to_structure = fn(query_result) ->
+      {chr_name,chr_len,species_id,source} = query_result
       %{
         species: species,
         species_id: species_id,

  end

  def species do
    {:ok, rows} = DB.query("SELECT speciesid,name,fullname FROM Species")
    for r <- rows, do: ( {id,name,fullname} = r; [id,name,fullname] )
  end

  def groups(species) do
    subq =
      case use_type(species) do
        { :integer, i } -> "Species.id = #{i}"
        { :string, s }  -> "Species.Name = '#{s}'"
      end

    # note this query can be simplified
    query = """
SELECT distinct InbredSet.id,InbredSet.Name,InbredSet.FullName
FROM InbredSet,Species,ProbeFreeze,GenoFreeze,PublishFreeze
WHERE #{subq}
and InbredSet.SpeciesId = Species.Id and InbredSet.Name != 'BXD300'
and (PublishFreeze.InbredSetId = InbredSet.Id
     or GenoFreeze.InbredSetId = InbredSet.Id
     or ProbeFreeze.InbredSetId = InbredSet.Id)
"""
    {:ok, rows} = DB.query(query)
    for r <- rows, do: ( {id,name,full_name} = r ; [id,name,full_name] )
  end

  def group_info(group) do
    subq =
      case use_type(group) do
        { :integer, i } -> "C.id = #{i}"
        { :string, s }  -> "C.Name = '#{s}'"
      end
    query = "
SELECT DISTINCT Species.speciesid,Species.Name,C.InbredSetid,C.name,C.mappingmethodid,C.genetictype
FROM Species, InbredSet as C
WHERE #{subq} and C.SpeciesId = Species.Id"
    {:ok, rows} = DB.query(query)
    for r <- rows, do: ( {species_id,species,group_id,group_name,method_id,genetic_type} = r; [group_id,group_name,species_id,species,method_id,genetic_type] )
  end

  def chr_info(dataset_name) do
    subq =
      case use_type(dataset_name) do
        { :integer, i } -> "C.id = #{i}"
        { :string, s }  -> "C.Name = '#{s}'"
      end

      query = """
SELECT Chr_Length.Name, Length
FROM Chr_Length, InbredSet as C
WHERE #{subq}
AND Chr_Length.SpeciesId = C.SpeciesId
ORDER BY Chr_Length.OrderId
      """
    {:ok, rows} = DB.query(query)
    for r <- rows, do: ( {chr_name,chr_len} = r; [chr_name,chr_len] )
  end

  def datasets(group) do
    authorize_group(group)
    query = """
SELECT DISTINCT D.Id,D.Name,D.FullName
FROM ProbeSetFreeze AS D, ProbeFreeze as D2, InbredSet, Tissue, Species
WHERE
    InbredSet.Name = '#{group}' and
    D.ProbeFreezeId = D2.Id
    AND D2.TissueId = Tissue.Id
    AND D2.InbredSetId = InbredSet.Id
    AND D.confidentiality < 1
    AND D.public > 0
"""
    {:ok, rows} = DB.query(query)
    for r <- rows, do: ( {id,name,full_name} = r ; [id,name,full_name] )
  end

  def dataset_info(dataset_name) do
    authorize_dataset(dataset_name)
    subq =
      case use_type(dataset_name) do
        { :integer, i } -> "D.id = #{i}"
        { :string, s }  -> "D.Name = '#{s}'"
      end

    query = """
SELECT D.Id, D.Name, D.FullName, D.ShortName, D.DataScale, D2.TissueId, Tissue.Name, D.public, D.confidentiality
FROM ProbeSetFreeze as D, ProbeFreeze as D2, Tissue
WHERE #{subq}
    AND D.public > 0
    AND D.ProbeFreezeId = D2.Id
    AND D2.TissueId = Tissue.Id
    """
    {:ok, rows} = DB.query(query)
    for r <- rows, do: ( {id,name,full_name,short_name,data_scale,tissue_id,tissue_name,public,confidential} = r; [id,name,full_name,short_name,data_scale,tissue_id,tissue_name,public,confidential] )
  end

  def phenotypes(dataset_name, start, stop) do

  end

  def menu_species do
    query = from species in Species,
      select: {species."SpeciesId", species."Name", species."MenuName"}
    # {:ok, rows} = DB.query("SELECT speciesid,name,menuname FROM Species")
    # for r <- rows, do: ( {id,name,fullname} = r; [id,name,fullname] )
    Repo.all(query) |> Enum.map(&(Tuple.to_list(&1)))
  end

  def menu_groups(species) do
