Exemplo n.º 1
0
 /**
  * Gets a list of the affected genes.
  *
  * @param selected is the list of selected genes.
  * @return the list of affected genes.
  */
 public ArrayList<ArrayList<Gene>> getAffected(ArrayList<Gene> selected) {
   ArrayList<ArrayList<Gene>> results = new ArrayList<ArrayList<Gene>>();
   for (ArrayList<Gene> x : causes) {
     boolean conditions = true;
     // check if the list of conditions are met.
     int j = 0;
     while (conditions == true && j < x.size()) {
       boolean gene_found = false;
       int i = 0;
       Gene gene_x = x.get(j);
       // check input list against condision
       while (gene_found == false && i < selected.size()) {
         if (gene_x.equals(selected.get(i))) {
           gene_found = true;
         }
         i++;
       }
       if (gene_found == false) {
         conditions = false;
       }
       j++;
     }
     if (conditions == true) {
       results.add(x);
     }
   }
   return results;
 }
Exemplo n.º 2
0
 /**
  * Checks if any of the conditions for the disease are met.
  *
  * @param selected is the list of selected genes.
  * @return true if conditions met.
  */
 public boolean isAffected(ArrayList<Gene> selected) {
   for (ArrayList<Gene> x : causes) {
     boolean conditions = true;
     // check if the list of conditions are met.
     int j = 0;
     while (conditions == true && j < x.size()) { // goes through sub list
       Gene gene_x = x.get(j); // gets current gene
       boolean gene_found = false;
       int i = 0;
       // check input list against condition
       while (gene_found == false && i < selected.size()) {
         if (gene_x.equals(selected.get(i))) {
           gene_found = true;
         }
         i++;
       }
       if (gene_found == false) {
         conditions = false;
       }
       j++;
     }
     if (conditions == true) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 3
0
 /**
  * Provides implementation-independent means for creating new Gene instances. The new instance
  * that is created and returned should be setup with any implementation-dependent configuration
  * that this Gene instance is setup with (aside from the actual value, of course). For example, if
  * this Gene were setup with bounds on its value, then the Gene instance returned from this method
  * should also be setup with those same bounds. This is important, as the JGAP core will invoke
  * this method on each Gene in the sample Chromosome in order to create each new Gene in the same
  * respective gene position for a new Chromosome.
  *
  * @return a new Gene instance of the same type and with the same setup as this concrete Gene
  * @author Neil Rostan
  * @author Klaus Meffert
  * @since 2.6 (since 1.0 in IntegerGene)
  */
 public Gene newGene() {
   Gene result = newGeneInternal();
   result.setConstraintChecker(getConstraintChecker());
   result.setEnergy(getEnergy());
   /** @todo clone app.data */
   result.setApplicationData(getApplicationData());
   return result;
 }
Exemplo n.º 4
0
 /**
  * Verifies the state of the chromosome. Especially takes care of the given constraint checker.
  *
  * @param a_constraintChecker the constraint checker to verify
  * @throws InvalidConfigurationException
  * @author Klaus Meffert
  * @since 2.5
  */
 protected void verify(IGeneConstraintChecker a_constraintChecker)
     throws InvalidConfigurationException {
   if (a_constraintChecker != null && getGenes() != null) {
     int len = getGenes().length;
     for (int i = 0; i < len; i++) {
       Gene gene = getGene(i);
       if (!a_constraintChecker.verify(gene, null, this, i)) {
         throw new InvalidConfigurationException(
             "The gene type "
                 + gene.getClass().getName()
                 + " is not allowed to be used in the chromosome due to the"
                 + " constraint checker used.");
       }
     }
   }
 }
Exemplo n.º 5
0
 /**
  * Retrieve a hash code for this Chromosome. Does not considers the order of the Genes for all
  * cases (especially when gene is empty).
  *
  * @return the hash code of this Chromosome
  * @author Neil Rotstan
  * @author Klaus Meffert
  * @since 1.0
  */
 public int hashCode() {
   // Do what java.util.AbstractList does.
   // ------------------------------------
   int geneHashcode;
   int hashCode = 1;
   if (getGenes() != null) {
     int size = size();
     for (int i = 0; i < size; i++) {
       Gene gene = getGene(i);
       if (gene == null) {
         geneHashcode = -55;
       } else {
         geneHashcode = gene.hashCode();
       }
       hashCode = 31 * hashCode + geneHashcode;
     }
   }
   return hashCode;
 }
Exemplo n.º 6
0
 /**
  * Compares this Gene with the given object and returns true if the other object is a Gene of the
  * same type and has the same value (allele) as this Gene. Otherwise it returns false.
  *
  * @param a_other the object to compare to this Gene for equality
  * @return true if this Gene is equal to the given object, false otherwise
  * @author Klaus Meffert
  * @since 1.1
  */
 public boolean equals(final Object a_other) {
   try {
     int result = compareTo(a_other);
     if (result == 0) {
       if (isCompareApplicationData()) {
         Gene otherGene = (Gene) a_other;
         int resultAppData =
             compareApplicationData(getApplicationData(), otherGene.getApplicationData());
         return resultAppData == 0;
       } else {
         return true;
       }
     } else {
       return false;
     }
   } catch (ClassCastException e) {
     // If the other object isn't an Gene of current type
     // (like IntegerGene for IntegerGene's), then we're not equal.
     // -----------------------------------------------------------
     return false;
   }
 }
Exemplo n.º 7
0
 protected void initFromGene(Gene a_sampleGene) {
   // Do sanity checking to make sure the parameters we were
   // given are valid.
   // ------------------------------------------------------
   if (a_sampleGene == null) {
     throw new IllegalArgumentException("Sample Gene cannot be null.");
   }
   // Populate the array of genes it with new Gene instances
   // created from the sample gene.
   // ------------------------------------------------------
   int size = size();
   for (int i = 0; i < size; i++) {
     setGene(i, a_sampleGene.newGene());
   }
 }
Exemplo n.º 8
0
  private void addGene(
      Map<String, Gene> map, String gene, String chr, long start, long end, long len) {

    if (map.containsKey(gene)) {
      Gene geneObj = map.get(gene);
      // move start
      if (geneObj.getStart() > start) {
        geneObj.setStart(start);
      }
      // move end
      if (geneObj.getEnd() < end) {
        geneObj.setEnd(end);
      }
      // update length
      geneObj.setLen(geneObj.getLen() + len);
    } else {
      Gene geneObj = new Gene(gene, chr, start, end, len);
      map.put(gene, geneObj);
    }
  }
 /**
  * Unmarshall a Chromosome instance from a given XML Element representation.
  *
  * @param a_activeConfiguration current Configuration object
  * @param a_xmlElement the XML Element representation of the Chromosome
  * @return a new Chromosome instance setup with the data from the XML Element representation
  * @throws ImproperXMLException if the given Element is improperly structured or missing data
  * @throws UnsupportedRepresentationException if the actively configured Gene implementation does
  *     not support the string representation of the alleles used in the given XML document
  * @throws GeneCreationException if there is a problem creating or populating a Gene instance
  * @author Neil Rotstan
  * @since 1.0
  */
 public static Gene[] getGenesFromElement(
     Configuration a_activeConfiguration, Element a_xmlElement)
     throws ImproperXMLException, UnsupportedRepresentationException, GeneCreationException {
   // Do some sanity checking. Make sure the XML Element isn't null and
   // that it in fact represents a set of genes.
   // -----------------------------------------------------------------
   if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(GENES_TAG))) {
     throw new ImproperXMLException(
         "Unable to build Chromosome instance from XML Element: "
             + "given Element is not a 'genes' element.");
   }
   List genes = Collections.synchronizedList(new ArrayList());
   // Extract the nested gene elements.
   // ---------------------------------
   NodeList geneElements = a_xmlElement.getElementsByTagName(GENE_TAG);
   if (geneElements == null) {
     throw new ImproperXMLException(
         "Unable to build Gene instances from XML Element: "
             + "'"
             + GENE_TAG
             + "'"
             + " sub-elements not found.");
   }
   // For each gene, get the class attribute so we know what class
   // to instantiate to represent the gene instance, and then find
   // the child text node, which is where the string representation
   // of the allele is located, and extract the representation.
   // -------------------------------------------------------------
   int numberOfGeneNodes = geneElements.getLength();
   for (int i = 0; i < numberOfGeneNodes; i++) {
     Element thisGeneElement = (Element) geneElements.item(i);
     thisGeneElement.normalize();
     // Fetch the class attribute and create an instance of that
     // class to represent the current gene.
     // --------------------------------------------------------
     String geneClassName = thisGeneElement.getAttribute(CLASS_ATTRIBUTE);
     Gene thisGeneObject;
     Class geneClass = null;
     try {
       geneClass = Class.forName(geneClassName);
       try {
         Constructor constr = geneClass.getConstructor(new Class[] {Configuration.class});
         thisGeneObject = (Gene) constr.newInstance(new Object[] {a_activeConfiguration});
       } catch (NoSuchMethodException nsme) {
         // Try it by calling method newGeneInternal.
         // -----------------------------------------
         Constructor constr = geneClass.getConstructor(new Class[] {});
         thisGeneObject = (Gene) constr.newInstance(new Object[] {});
         thisGeneObject =
             (Gene)
                 PrivateAccessor.invoke(
                     thisGeneObject, "newGeneInternal", new Class[] {}, new Object[] {});
       }
     } catch (Throwable e) {
       throw new GeneCreationException(geneClass, e);
     }
     // Find the text node and fetch the string representation of
     // the allele.
     // ---------------------------------------------------------
     NodeList children = thisGeneElement.getChildNodes();
     int childrenSize = children.getLength();
     String alleleRepresentation = null;
     for (int j = 0; j < childrenSize; j++) {
       Element alleleElem = (Element) children.item(j);
       if (alleleElem.getTagName().equals(ALLELE_TAG)) {
         alleleRepresentation = alleleElem.getAttribute("value");
       }
       if (children.item(j).getNodeType() == Node.TEXT_NODE) {
         // We found the text node. Extract the representation.
         // ---------------------------------------------------
         alleleRepresentation = children.item(j).getNodeValue();
         break;
       }
     }
     // Sanity check: Make sure the representation isn't null.
     // ------------------------------------------------------
     if (alleleRepresentation == null) {
       throw new ImproperXMLException(
           "Unable to build Gene instance from XML Element: "
               + "value (allele) is missing representation.");
     }
     // Now set the value of the gene to that reflect the
     // string representation.
     // -------------------------------------------------
     try {
       thisGeneObject.setValueFromPersistentRepresentation(alleleRepresentation);
     } catch (UnsupportedOperationException e) {
       throw new GeneCreationException(
           "Unable to build Gene because it does not support the "
               + "setValueFromPersistentRepresentation() method.");
     }
     // Finally, add the current gene object to the list of genes.
     // ----------------------------------------------------------
     genes.add(thisGeneObject);
   }
   return (Gene[]) genes.toArray(new Gene[genes.size()]);
 }
 /**
  * @param a_gene Gene
  * @param a_xmlDocument Document
  * @return Element
  * @author Klaus Meffert
  * @since 2.0
  */
 private static Element representAlleleAsElement(final Gene a_gene, final Document a_xmlDocument) {
   Element alleleElement = a_xmlDocument.createElement(ALLELE_TAG);
   alleleElement.setAttribute("class", a_gene.getClass().getName());
   alleleElement.setAttribute("value", a_gene.getPersistentRepresentation());
   return alleleElement;
 }