Esempio n. 1
0
 protected InterMineBag createEmployeeList() throws Exception {
   ObjectStoreWriter osw = null;
   try {
     Profile superUser = im.getProfileManager().getSuperuserProfile();
     osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest");
     Employee e1 = new Employee();
     e1.setName("Employee1");
     Employee e2 = new Employee();
     e2.setName("Employee2");
     Department d1 = new Department();
     d1.setName("department");
     Company company = new CompanyShadow();
     company.setName("company");
     Contractor contractor = new Contractor();
     contractor.setName("contractor");
     osw.store(contractor);
     company.addContractors(contractor);
     osw.store(company);
     d1.setCompany(company);
     osw.store(d1);
     e1.setDepartment(d1);
     e2.setDepartment(d1);
     osw.store(e1);
     osw.store(e2);
     InterMineBag list = superUser.createBag("employeeList", "Employee", "", im.getClassKeys());
     Collection<Integer> ids = new ArrayList<Integer>();
     ids.add(e1.getId());
     ids.add(e2.getId());
     list.addIdsToBag(ids, "Employee");
     return list;
   } finally {
     osw.close();
   }
 }
 @BeforeClass
 public static void loadData() {
   made = 0;
   try {
     osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest");
     Model m = osw.getModel();
     osw.beginTransaction();
     int scale = MAX_SCALE;
     try {
       for (int j = 20; j <= 90; j += 10) {
         int count = generator.nextInt(scale) + MIN_BIN; // 10 * (j % 100);
         expected.put(j, Long.valueOf(count));
         for (int i = 0; i < count; i++) {
           Types thing = DynamicUtil.createObject(Types.class);
           thing.setIntType(generator.nextInt(10) + j);
           thing.setDoubleType((generator.nextGaussian() * 5d) + 30d);
           thing.setName("histothing" + String.format("%06d", made++));
           osw.store(thing);
         }
       }
       osw.commitTransaction();
     } catch (ObjectStoreException e) {
       osw.abortTransaction();
       throw e;
     }
   } catch (ObjectStoreException e) {
     LOG.error(e);
   }
   showHistogram("Expected", expected);
   System.out.printf("Made %d things\n", made);
 }
  /**
   * Populate the SequenceFeature.locatedFeatures() collection for: Gene, Transcript, Exon and CDS
   *
   * @throws Exception if anything goes wrong
   */
  public void populateCollection() throws Exception {
    Map<String, SOTerm> soTerms = populateSOTermMap(osw);
    Query q = getAllParents();
    Results res = osw.getObjectStore().execute(q);
    Iterator<Object> resIter = res.iterator();
    osw.beginTransaction();
    int parentCount = 0;
    int childCount = 0;

    while (resIter.hasNext()) {
      ResultsRow<InterMineObject> rr = (ResultsRow<InterMineObject>) resIter.next();
      InterMineObject parent = rr.get(0);
      SOTerm soTerm = (SOTerm) rr.get(1);
      InterMineObject o = PostProcessUtil.cloneInterMineObject(parent);
      Set<InterMineObject> newCollection = getChildFeatures(soTerms, soTerm, o);
      if (newCollection != null && !newCollection.isEmpty()) {
        o.setFieldValue(TARGET_COLLECTION, newCollection);
        osw.store(o);
        parentCount++;
        childCount += newCollection.size();
      }
    }
    osw.commitTransaction();
    LOG.info("Stored " + childCount + " child features for " + parentCount + " parent features");
  }
Esempio n. 4
0
 protected InterMineBag createCompanyList() throws Exception {
   ObjectStoreWriter osw = null;
   try {
     osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.unittest");
     Profile superUser = im.getProfileManager().getSuperuserProfile();
     Company c1 = new CompanyShadow();
     c1.setName("CompanyA");
     Company c2 = new CompanyShadow();
     c2.setName("CompanyB");
     osw.store(c1);
     osw.store(c2);
     InterMineBag list = superUser.createBag("companyList", "Company", "", im.getClassKeys());
     Collection<Integer> ids = new ArrayList<Integer>();
     ids.add(c1.getId());
     ids.add(c2.getId());
     list.addIdsToBag(ids, "Company");
     return list;
   } finally {
     osw.close();
   }
 }
  private void createOverlapTestData() throws Exception {
    Chromosome chr = (Chromosome) DynamicUtil.createObject(Collections.singleton(Chromosome.class));
    chr.setPrimaryIdentifier("X");
    chr.setLength(new Integer(1000));
    chr.setId(new Integer(101));

    Set<InterMineObject> toStore = new HashSet<InterMineObject>();

    toStore.add(chr);

    int[][] exonInfo = {
      {1000, 1, 1},
      {1001, 2, 10},
      {1002, 10, 15},
      {1003, 16, 19},
      {1004, 16, 19},
      {1005, 20, 29},
      {1006, 30, 100},
      {1007, 30, 34},
      {1008, 32, 95},
      {1009, 38, 53},
      {1010, 40, 50},
      {1011, 44, 44},
      {1012, 54, 54},
      {1013, 54, 54},
      {1014, 60, 70},
      {1015, 120, 140},
      {1016, 141, 145},
      {1017, 146, 180},
      {1018, 220, 240},
      {1019, 240, 245},
      {1020, 245, 280},
    };

    Exon[] exons = new Exon[exonInfo.length];
    Location[] exonLocs = new Location[exonInfo.length];

    for (int i = 0; i < exons.length; i++) {
      exons[i] = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
      int exonId = exonInfo[i][0];
      int start = exonInfo[i][1];
      int end = exonInfo[i][2];
      exons[i].setId(new Integer(exonId));
      exons[i].setLength(new Integer(end - start + 1));
      exons[i].setChromosome(chr);
      exonLocs[i] = createLocation(chr, exons[i], "1", start, end, Location.class);
      exonLocs[i].setId(new Integer(1000 + exonId));
    }

    ReversePrimer rp =
        (ReversePrimer) DynamicUtil.createObject(Collections.singleton(ReversePrimer.class));
    rp.setId(new Integer(3000));
    rp.setLength(new Integer(100));
    rp.setChromosome(chr);

    Location rpLoc = createLocation(chr, rp, "1", 1, 100, Location.class);
    rpLoc.setId(new Integer(3001));

    toStore.add(rp);
    toStore.add(rpLoc);
    toStore.addAll(Arrays.asList(exons));
    toStore.addAll(Arrays.asList(exonLocs));

    for (InterMineObject imo : toStore) {
      osw.store(imo);
    }
  }
  public void testSetChromosomeLocationsAndLengths() throws Exception {
    Chromosome chr1 =
        (Chromosome) DynamicUtil.createObject(Collections.singleton(Chromosome.class));
    chr1.setPrimaryIdentifier("1");
    chr1.setId(new Integer(101));
    Chromosome chr2 =
        (Chromosome) DynamicUtil.createObject(Collections.singleton(Chromosome.class));
    chr1.setPrimaryIdentifier("2");
    chr1.setId(new Integer(102));

    Exon exon1 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon1.setId(new Integer(107));
    exon1.setLength(new Integer(1000));
    Exon exon2 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon2.setId(new Integer(108));
    Exon exon3 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon3.setId(new Integer(109));

    // exon 2 has two chromosome locations, shouldn't get chromosome[Location] references
    Location exon1OnChr = createLocation(chr1, exon1, "1", 51, 100, Location.class);
    exon1OnChr.setId(new Integer(1010));
    Location exon2OnChr = createLocation(chr2, exon2, "1", 201, 250, Location.class);
    exon2OnChr.setId(new Integer(1011));
    Location exon2OnChrDup = createLocation(chr1, exon2, "1", 501, 550, Location.class);
    exon2OnChrDup.setId(new Integer(1012));
    Location exon3OnChr = createLocation(chr2, exon3, "1", 601, 650, Location.class);
    exon3OnChr.setId(new Integer(1013));

    Set<InterMineObject> toStore =
        new HashSet<InterMineObject>(
            Arrays.asList(
                new InterMineObject[] {
                  chr1, chr2, exon1, exon2, exon3, exon1OnChr, exon2OnChr, exon2OnChrDup, exon3OnChr
                }));

    for (InterMineObject imo : toStore) {
      osw.store(imo);
    }

    CalculateLocations cl = new CalculateLocations(osw);
    cl.setChromosomeLocationsAndLengths();

    ObjectStore os = osw.getObjectStore();
    Exon resExon1 = (Exon) os.getObjectById(new Integer(107));
    Exon resExon2 = (Exon) os.getObjectById(new Integer(108));
    Exon resExon3 = (Exon) os.getObjectById(new Integer(109));

    assertEquals(chr1.getId(), resExon1.getChromosome().getId());
    assertEquals(exon1OnChr.getId(), resExon1.getChromosomeLocation().getId());

    assertNull(resExon2.getChromosome());
    assertNull(resExon2.getChromosomeLocation());

    assertEquals(chr2.getId(), resExon3.getChromosome().getId());
    assertEquals(exon3OnChr.getId(), resExon3.getChromosomeLocation().getId());

    // exon1 has length set so should stay as 1000, exon3 should get length 50 set from location
    assertEquals(new Integer(1000), resExon1.getLength());
    assertEquals(new Integer(50), resExon3.getLength());
    // nothing done to exon2
    assertNull(resExon2.getLength());
  }
  public void testCreateSpanningLocations() throws Exception {
    Exon exon1 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon1.setId(new Integer(107));
    Exon exon2 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon2.setId(new Integer(108));
    Exon exon3 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class));
    exon3.setId(new Integer(109));

    Location exon1OnChr = createLocation(getChromosome(), exon1, "1", 51, 100, Location.class);
    exon1OnChr.setId(new Integer(1010));
    Location exon2OnChr = createLocation(getChromosome(), exon2, "1", 201, 250, Location.class);
    exon2OnChr.setId(new Integer(1011));
    Location exon3OnChr = createLocation(getChromosome(), exon3, "1", 201, 400, Location.class);
    exon3OnChr.setId(new Integer(1012));

    Transcript trans1 =
        (Transcript) DynamicUtil.createObject(Collections.singleton(Transcript.class));
    trans1.setId(new Integer(201));

    Transcript trans2 =
        (Transcript) DynamicUtil.createObject(Collections.singleton(Transcript.class));
    trans2.setId(new Integer(202));

    Location trans2OnChr = createLocation(getChromosome(), trans2, "1", 61, 300, Location.class);

    Gene gene = (Gene) DynamicUtil.createObject(Collections.singleton(Gene.class));
    gene.setId(new Integer(301));

    exon1.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans1})));
    exon2.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans1})));

    // the location of exon3 should be ignored by createSpanningLocations() because trans2
    // already has a location
    exon3.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans2})));

    trans1.setGene(gene);
    trans2.setGene(gene);

    Set<InterMineObject> toStore =
        new HashSet<InterMineObject>(
            Arrays.asList(
                new InterMineObject[] {
                  getChromosome(),
                  gene,
                  trans1,
                  trans2,
                  exon1,
                  exon2,
                  exon3,
                  exon1OnChr,
                  exon2OnChr,
                  trans2OnChr
                }));

    for (InterMineObject imo : toStore) {
      osw.store(imo);
    }

    CalculateLocations cl = new CalculateLocations(osw);
    cl.createSpanningLocations("Transcript", "Exon", "exons");
    cl.createSpanningLocations("Gene", "Transcript", "transcripts");

    ObjectStore os = osw.getObjectStore();
    Transcript resTrans1 = (Transcript) os.getObjectById(new Integer(201));

    Assert.assertEquals(1, resTrans1.getLocations().size());
    Location resTrans1Location = (Location) resTrans1.getLocations().iterator().next();
    Assert.assertEquals(51, resTrans1Location.getStart().intValue());
    Assert.assertEquals(250, resTrans1Location.getEnd().intValue());

    Transcript resTrans2 = (Transcript) os.getObjectById(new Integer(202));

    Assert.assertEquals(1, resTrans2.getLocations().size());
    Location resTrans2Location = (Location) resTrans2.getLocations().iterator().next();
    Assert.assertEquals(61, resTrans2Location.getStart().intValue());
    Assert.assertEquals(300, resTrans2Location.getEnd().intValue());

    Gene resGene = (Gene) os.getObjectById(new Integer(301));
    Assert.assertEquals(1, resGene.getLocations().size());
    Location resGeneLocation = (Location) resGene.getLocations().iterator().next();
    Assert.assertEquals(51, resGeneLocation.getStart().intValue());
    Assert.assertEquals(300, resGeneLocation.getEnd().intValue());
  }
Esempio n. 8
0
  /**
   * Read the UTRs collection of MRNA then set the fivePrimeUTR and threePrimeUTR fields with the
   * corresponding UTRs.
   *
   * @throws Exception if anything goes wrong
   */
  public void createUtrRefs() throws Exception {
    long startTime = System.currentTimeMillis();
    Query q = new Query();
    q.setDistinct(false);

    QueryClass qcMRNA = new QueryClass(model.getClassDescriptorByName("MRNA").getType());
    q.addFrom(qcMRNA);
    q.addToSelect(qcMRNA);
    q.addToOrderBy(qcMRNA);

    QueryClass qcUTR = new QueryClass(model.getClassDescriptorByName("UTR").getType());
    q.addFrom(qcUTR);
    q.addToSelect(qcUTR);
    q.addToOrderBy(qcUTR);

    QueryCollectionReference mrnaUtrsRef = new QueryCollectionReference(qcMRNA, "UTRs");
    ContainsConstraint mrnaUtrsConstraint =
        new ContainsConstraint(mrnaUtrsRef, ConstraintOp.CONTAINS, qcUTR);

    QueryObjectReference fivePrimeRef = new QueryObjectReference(qcMRNA, "fivePrimeUTR");
    ContainsConstraint fivePrimeNullConstraint =
        new ContainsConstraint(fivePrimeRef, ConstraintOp.IS_NULL);
    QueryObjectReference threePrimeRef = new QueryObjectReference(qcMRNA, "threePrimeUTR");
    ContainsConstraint threePrimeNullConstraint =
        new ContainsConstraint(threePrimeRef, ConstraintOp.IS_NULL);

    ConstraintSet cs = new ConstraintSet(ConstraintOp.AND);
    cs.addConstraint(mrnaUtrsConstraint);
    cs.addConstraint(fivePrimeNullConstraint);
    cs.addConstraint(threePrimeNullConstraint);

    q.setConstraint(cs);

    ObjectStore os = osw.getObjectStore();

    ((ObjectStoreInterMineImpl) os).precompute(q, Constants.PRECOMPUTE_CATEGORY);
    Results res = os.execute(q, 500, true, true, true);

    int count = 0;
    InterMineObject lastMRNA = null;

    InterMineObject fivePrimeUTR = null;
    InterMineObject threePrimeUTR = null;

    osw.beginTransaction();

    Class<? extends FastPathObject> fivePrimeUTRCls =
        model.getClassDescriptorByName("FivePrimeUTR").getType();

    Iterator<?> resIter = res.iterator();
    while (resIter.hasNext()) {
      ResultsRow<?> rr = (ResultsRow<?>) resIter.next();
      InterMineObject mrna = (InterMineObject) rr.get(0);
      InterMineObject utr = (InterMineObject) rr.get(1);

      if (lastMRNA != null && !mrna.getId().equals(lastMRNA.getId())) {
        // clone so we don't change the ObjectStore cache
        InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA);
        if (fivePrimeUTR != null) {
          tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR);
          fivePrimeUTR = null;
        }
        if (threePrimeUTR != null) {
          tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR);
          threePrimeUTR = null;
        }
        osw.store(tempMRNA);
        count++;
      }

      if (DynamicUtil.isInstance(utr, fivePrimeUTRCls)) {
        fivePrimeUTR = utr;
      } else {
        threePrimeUTR = utr;
      }

      lastMRNA = mrna;
    }

    if (lastMRNA != null) {
      // clone so we don't change the ObjectStore cache
      InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA);
      tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR);
      tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR);
      osw.store(tempMRNA);
      count++;
    }
    LOG.info(
        "Stored MRNA "
            + count
            + " times ("
            + count * 2
            + " fields set)"
            + " - took "
            + (System.currentTimeMillis() - startTime)
            + " ms.");
    osw.commitTransaction();

    // now ANALYSE tables relating to class that has been altered - may be rows added
    // to indirection tables
    if (osw instanceof ObjectStoreWriterInterMineImpl) {
      ClassDescriptor cld = model.getClassDescriptorByName("MRNA");
      DatabaseUtil.analyse(((ObjectStoreWriterInterMineImpl) osw).getDatabase(), cld, false);
    }
  }
Esempio n. 9
0
  /**
   * Add a collection of objects of type X to objects of type Y by using a connecting class. Eg. Add
   * a collection of Protein objects to Gene by examining the Transcript objects in the transcripts
   * collection of the Gene, which would use a query like: SELECT DISTINCT gene FROM Gene AS gene,
   * Transcript AS transcript, Protein AS protein WHERE (gene.transcripts CONTAINS transcript AND
   * transcript.protein CONTAINS protein) ORDER BY gene and then set protected gene.protein (if
   * created BioEntity1 -&gt; BioEntity2 -&gt; BioEntity3 ==&gt; BioEntity1 -&gt; BioEntity3
   *
   * @param firstClsName the first class in the query
   * @param firstClassFieldName the field in the firstClass which should contain the connectingClass
   * @param connectingClsName the class referred to by firstClass.sourceFieldName
   * @param connectingClassFieldName the field in connectingClass which should contain secondClass
   * @param secondClsName the class referred to by connectingClass.connectingClassFieldName
   * @param createFieldName the collection field in the secondClass - the collection to create/set
   * @param createInFirstClass if true create the new collection field in firstClass, otherwise
   *     create in secondClass
   * @throws Exception if anything goes wrong
   */
  protected void insertCollectionField(
      String firstClsName,
      String firstClassFieldName,
      String connectingClsName,
      String connectingClassFieldName,
      String secondClsName,
      String createFieldName,
      boolean createInFirstClass)
      throws Exception {
    InterMineObject lastDestObject = null;
    Set<InterMineObject> newCollection = new HashSet<InterMineObject>();

    String insertMessage =
        "insertCollectionField("
            + firstClsName
            + ", "
            + firstClassFieldName
            + ", "
            + connectingClsName
            + ", "
            + connectingClassFieldName
            + ","
            + secondClsName
            + ", "
            + createFieldName
            + ", "
            + createInFirstClass
            + ")";

    // Check that classes and fields specified exist in model
    try {
      String errorMessage = "Not performing " + insertMessage;
      PostProcessUtil.checkFieldExists(model, firstClsName, firstClassFieldName, errorMessage);
      PostProcessUtil.checkFieldExists(
          model, connectingClsName, connectingClassFieldName, errorMessage);
      PostProcessUtil.checkFieldExists(model, secondClsName, createFieldName, errorMessage);
    } catch (MetaDataException e) {
      return;
    }

    LOG.info("Beginning " + insertMessage);
    long startTime = System.currentTimeMillis();

    // if this is a many to many collection we can use ObjectStore.addToCollection which will
    // write directly to the database.
    boolean manyToMany = false;
    ClassDescriptor destCld;
    if (createInFirstClass) {
      destCld = model.getClassDescriptorByName(firstClsName);
    } else {
      destCld = model.getClassDescriptorByName(secondClsName);
    }
    CollectionDescriptor col = destCld.getCollectionDescriptorByName(createFieldName);
    if (col == null) {
      String msg =
          "Error running post-process `create-references` for `"
              + createFieldName
              + "` since this collection doesn't exist in the model.";
      LOG.error(msg);
      return;
    }

    if (col.relationType() == CollectionDescriptor.M_N_RELATION) {
      manyToMany = true;
    }

    Iterator<ResultsRow<InterMineObject>> resIter =
        PostProcessUtil.findConnectingClasses(
            osw.getObjectStore(),
            model.getClassDescriptorByName(firstClsName).getType(),
            firstClassFieldName,
            model.getClassDescriptorByName(connectingClsName).getType(),
            connectingClassFieldName,
            model.getClassDescriptorByName(secondClsName).getType(),
            createInFirstClass);

    // results will be firstClass ; destClass (ordered by firstClass)
    osw.beginTransaction();
    int count = 0;

    while (resIter.hasNext()) {
      ResultsRow<InterMineObject> rr = resIter.next();

      InterMineObject thisSourceObject;
      InterMineObject thisDestObject;

      if (createInFirstClass) {
        thisDestObject = rr.get(0);
        thisSourceObject = rr.get(1);
      } else {
        thisDestObject = rr.get(1);
        thisSourceObject = rr.get(0);
      }

      if (!manyToMany
          && (lastDestObject == null || !thisDestObject.getId().equals(lastDestObject.getId()))) {

        if (lastDestObject != null) {
          try {
            InterMineObject tempObject = PostProcessUtil.cloneInterMineObject(lastDestObject);
            Set<InterMineObject> oldCollection =
                (Set<InterMineObject>) tempObject.getFieldValue(createFieldName);
            newCollection.addAll(oldCollection);
            tempObject.setFieldValue(createFieldName, newCollection);
            count += newCollection.size();
            osw.store(tempObject);
          } catch (IllegalAccessException e) {
            LOG.error(
                "Object with ID "
                    + thisDestObject.getId()
                    + " has no "
                    + createFieldName
                    + " field",
                e);
          }
        }

        newCollection = new HashSet<InterMineObject>();
      }

      if (manyToMany) {
        osw.addToCollection(
            thisDestObject.getId(), destCld.getType(), createFieldName, thisSourceObject.getId());
      } else {
        newCollection.add(thisSourceObject);
      }

      lastDestObject = thisDestObject;
    }

    if (!manyToMany && lastDestObject != null) {
      // clone so we don't change the ObjectStore cache
      InterMineObject tempObject = PostProcessUtil.cloneInterMineObject(lastDestObject);
      tempObject.setFieldValue(createFieldName, newCollection);
      count += newCollection.size();
      osw.store(tempObject);
    }
    LOG.info(
        "Finished: created "
            + count
            + " references in "
            + secondClsName
            + " to "
            + firstClsName
            + " via "
            + connectingClsName
            + " - took "
            + (System.currentTimeMillis() - startTime)
            + " ms.");
    osw.commitTransaction();

    // now ANALYSE tables relation to class that has been altered - may be rows added
    // to indirection tables
    if (osw instanceof ObjectStoreWriterInterMineImpl) {
      ClassDescriptor cld = model.getClassDescriptorByName(secondClsName);
      DatabaseUtil.analyse(((ObjectStoreWriterInterMineImpl) osw).getDatabase(), cld, false);
    }
  }
Esempio n. 10
0
  /**
   * Add a reference to and object of type X in objects of type Y by using a connecting class. Eg.
   * Add a reference to Gene objects in Exon by examining the Transcript objects in the transcripts
   * collection of the Gene, which would use a query like: SELECT DISTINCT gene FROM Gene AS gene,
   * Transcript AS transcript, Exon AS exon WHERE (gene.transcripts CONTAINS transcript AND
   * transcript.exons CONTAINS exon) ORDER BY gene and then set exon.gene
   *
   * <p>in overview we are doing: BioEntity1 -&gt; BioEntity2 -&gt; BioEntity3 ==&gt; BioEntitiy1
   * -&gt; BioEntity3
   *
   * @param sourceClsName the first class in the query
   * @param sourceClassFieldName the field in the sourceClass which should contain the
   *     connectingClass
   * @param connectingClsName the class referred to by sourceClass.sourceFieldName
   * @param connectingClassFieldName the field in connectingClass which should contain
   *     destinationClass
   * @param destinationClsName the class referred to by connectingClass.connectingClassFieldName
   * @param createFieldName the reference field in the destinationClass - the collection to
   *     create/set
   * @throws Exception if anything goes wrong
   */
  protected void insertReferenceField(
      String sourceClsName,
      String sourceClassFieldName,
      String connectingClsName,
      String connectingClassFieldName,
      String destinationClsName,
      String createFieldName)
      throws Exception {

    String insertMessage =
        "insertReferences("
            + sourceClsName
            + ", "
            + sourceClassFieldName
            + ", "
            + connectingClsName
            + ", "
            + connectingClassFieldName
            + ","
            + destinationClsName
            + ", "
            + createFieldName
            + ")";

    // Check that classes and fields specified exist in model
    try {
      String errorMessage = "Not performing " + insertMessage;
      PostProcessUtil.checkFieldExists(model, sourceClsName, sourceClassFieldName, errorMessage);
      PostProcessUtil.checkFieldExists(
          model, connectingClsName, connectingClassFieldName, errorMessage);
      PostProcessUtil.checkFieldExists(model, destinationClsName, createFieldName, errorMessage);
    } catch (MetaDataException e) {
      return;
    }

    LOG.info("Beginning " + insertMessage);
    long startTime = System.currentTimeMillis();

    Iterator<ResultsRow<InterMineObject>> resIter =
        PostProcessUtil.findConnectingClasses(
            osw.getObjectStore(),
            model.getClassDescriptorByName(sourceClsName).getType(),
            sourceClassFieldName,
            model.getClassDescriptorByName(connectingClsName).getType(),
            connectingClassFieldName,
            model.getClassDescriptorByName(destinationClsName).getType(),
            true);

    // results will be sourceClass ; destClass (ordered by sourceClass)
    osw.beginTransaction();

    int count = 0;

    while (resIter.hasNext()) {
      ResultsRow<InterMineObject> rr = resIter.next();
      InterMineObject thisSourceObject = rr.get(0);
      InterMineObject thisDestObject = rr.get(1);

      try {
        // clone so we don't change the ObjectStore cache
        InterMineObject tempObject = PostProcessUtil.cloneInterMineObject(thisDestObject);
        tempObject.setFieldValue(createFieldName, thisSourceObject);
        count++;
        if (count % 10000 == 0) {
          LOG.info(
              "Created "
                  + count
                  + " references in "
                  + destinationClsName
                  + " to "
                  + sourceClsName
                  + " via "
                  + connectingClsName);
        }
        osw.store(tempObject);
      } catch (IllegalAccessException e) {
        LOG.error(
            "Object with ID: " + thisDestObject.getId() + " has no " + createFieldName + " field");
      }
    }

    LOG.info(
        "Finished: created "
            + count
            + " references in "
            + destinationClsName
            + " to "
            + sourceClsName
            + " via "
            + connectingClsName
            + " - took "
            + (System.currentTimeMillis() - startTime)
            + " ms.");
    osw.commitTransaction();

    // now ANALYSE tables relation to class that has been altered - may be rows added
    // to indirection tables
    if (osw instanceof ObjectStoreWriterInterMineImpl) {
      ClassDescriptor cld = model.getClassDescriptorByName(destinationClsName);
      DatabaseUtil.analyse(((ObjectStoreWriterInterMineImpl) osw).getDatabase(), cld, false);
    }
  }