Search in sources :

Example 1 with Diff

use of com.sksamuel.diffpatch.DiffMatchPatch.Diff in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method createUnpatch.

private LinkedList<Patch> createUnpatch(LinkedList<Patch> patches) {
    LinkedList<Patch> result = new LinkedList<>();
    for (int patchIndex = 0, patchesLength = patches.size(); patchIndex < patchesLength; patchIndex++) {
        Patch patch = patches.get(patchIndex);
        Patch unpatch = new Patch();
        unpatch.length1 = patch.length1;
        unpatch.length2 = patch.length2;
        unpatch.start1 = patch.start1;
        unpatch.start2 = patch.start2;
        for (int diffIndex = 0, diffsLength = patch.diffs.size(); diffIndex < diffsLength; diffIndex++) {
            Diff diff = patch.diffs.get(diffIndex);
            switch(diff.operation) {
                case DELETE:
                    patch.diffs.add(new Diff(Operation.INSERT, diff.text));
                    break;
                case INSERT:
                    patch.diffs.add(new Diff(Operation.DELETE, diff.text));
                    break;
                case EQUAL:
                    patch.diffs.add(new Diff(diff.operation, diff.text));
                    break;
            }
        }
        result.add(unpatch);
    }
    return result;
}
Also used : Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) LinkedList(java.util.LinkedList)

Example 2 with Diff

use of com.sksamuel.diffpatch.DiffMatchPatch.Diff in project irida by phac-nml.

the class SamplePairer method organizeFiles.

/**
 * Organize files according to whether they should be paired up
 *
 * @param files
 *            List of {@link MultipartFile}s uploaded
 * @return Map of {@link Path}s to uploaded sequence files,
 * 			where the key is the common prefix of two paired files,
 * 			or the full file name of a single sequence file
 */
private static Map<String, List<MultipartFile>> organizeFiles(List<MultipartFile> files) {
    Map<String, List<MultipartFile>> organizedFiles = new HashMap<>();
    MultipartFile file1, file2;
    // Want to skip files that have already been organized
    Set<MultipartFile> wasChecked = new HashSet<>();
    // check all uploaded files to see if they should be paired or left single
    for (int i = 0; i < files.size(); i++) {
        file1 = files.get(i);
        boolean pair = false;
        if (!wasChecked.contains(file1)) {
            for (int j = i + 1; j < files.size() && !pair; j++) {
                file2 = files.get(j);
                if (!wasChecked.contains(file2)) {
                    MultipartFile[] filePair = null;
                    List<Diff> diffs = diff.diff_main(file1.getOriginalFilename(), file2.getOriginalFilename());
                    // diffs[3] = common suffix (e.g. ".fastq")
                    if (diffs.size() == 4) {
                        String file1ID = diffs.get(1).text;
                        String file2ID = diffs.get(2).text;
                        // situation.
                        if ((Stream.of(forwardMatches).anyMatch(x -> file1ID.contains(x)) && Stream.of(reverseMatches).anyMatch(x -> file2ID.contains(x))) || (Stream.of(reverseMatches).anyMatch(x -> file1ID.contains(x)) && Stream.of(forwardMatches).anyMatch(x -> file2ID.contains(x)))) {
                            filePair = new MultipartFile[] { file1, file2 };
                        }
                    }
                    if (filePair != null) {
                        pair = true;
                        organizedFiles.put(diffs.get(0).text, Arrays.asList(filePair));
                        logger.trace("Uploaded files [" + filePair[0].getName() + ", " + filePair[1].getName() + "] were paired.");
                        wasChecked.add(file2);
                    }
                }
            }
            if (!pair) {
                MultipartFile[] singleFile = { file1 };
                organizedFiles.put(file1.getOriginalFilename(), Arrays.asList(singleFile));
                logger.trace("Uploaded file [" + file1.getName() + "] was not paired");
            }
        }
        wasChecked.add(file1);
    }
    return organizedFiles;
}
Also used : DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) java.util(java.util) Logger(org.slf4j.Logger) Stream(java.util.stream.Stream) Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) LoggerFactory(org.slf4j.LoggerFactory) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) Path(java.nio.file.Path) IridaSequenceFilePair(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair) Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 3 with Diff

use of com.sksamuel.diffpatch.DiffMatchPatch.Diff in project muikku by otavanopisto.

the class HtmlMaterialCleaner method createPatch.

private String createPatch(String oldHtml, String newHtml) {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Diff> diffs = diffMatchPatch.diff_main(oldHtml, newHtml);
    diffMatchPatch.diff_cleanupEfficiency(diffs);
    LinkedList<Patch> patch = diffMatchPatch.patch_make(diffs);
    return diffMatchPatch.patch_toText(patch);
}
Also used : Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch)

Aggregations

DiffMatchPatch (com.sksamuel.diffpatch.DiffMatchPatch)3 Diff (com.sksamuel.diffpatch.DiffMatchPatch.Diff)3 Patch (com.sksamuel.diffpatch.DiffMatchPatch.Patch)2 IridaSequenceFilePair (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 java.util (java.util)1 LinkedList (java.util.LinkedList)1 Stream (java.util.stream.Stream)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1