From c1122ff15e558db1aee49991218dc7ea7752615b Mon Sep 17 00:00:00 2001 From: alchemistmatt Date: Mon, 9 Apr 2018 18:28:59 -0700 Subject: [PATCH] When comparing LastModified times, allow them to differ by up to 2 seconds Notify the user when re-creating suffix array files or the .canno file --- .../msdbsearch/CompactFastaSequence.java | 58 ++++++++++++++++--- .../msjava/msdbsearch/CompactSuffixArray.java | 36 +++++++++++- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/ucsd/msjava/msdbsearch/CompactFastaSequence.java b/src/main/java/edu/ucsd/msjava/msdbsearch/CompactFastaSequence.java index c7506a66..d264244e 100644 --- a/src/main/java/edu/ucsd/msjava/msdbsearch/CompactFastaSequence.java +++ b/src/main/java/edu/ucsd/msjava/msdbsearch/CompactFastaSequence.java @@ -123,16 +123,60 @@ private CompactFastaSequence(String filepath, String alphabet) { seqIdSignature = readSequence(); } - if (metaIdSignature == null || seqIdSignature == null - || metaIdSignature.getFormatId() != COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID - || seqIdSignature.getFormatId() != COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID - || metaIdSignature.getId() != seqIdSignature.getId() - || metaIdSignature.getLastModified() != lastModified - || seqIdSignature.getLastModified() != lastModified - ) { + boolean indexingRequired = false; + + if (metaIdSignature == null || seqIdSignature == null) { + System.out.println("Re-creating the .canno file since metaIdSignature is null or seqIdSignature is null"); + indexingRequired = true; + } + + if (metaIdSignature.getFormatId() != COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID) { + System.out.println("Re-creating the .canno file since the metaIdSignature is not " + + COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID + ", it is " + metaIdSignature.getFormatId()); + indexingRequired = true; + } + + if (seqIdSignature.getFormatId() != COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID) { + System.out.println("Re-creating the .canno file since the seqIdSignature is not " + + COMPACT_FASTA_SEQUENCE_FILE_FORMAT_ID + ", it is " + seqIdSignature.getFormatId()); + indexingRequired = true; + } + + if (metaIdSignature.getId() != seqIdSignature.getId()) { + System.out.println("Re-creating the .canno file since the metaIdSignature ID " + + "doesn't match seqIdSignature ID: " + + metaIdSignature.getId() + " vs. " + seqIdSignature.getId()); + indexingRequired = true; + } + + if (metaIdSignature.getLastModified() != seqIdSignature.getLastModified()) { + System.out.println("Re-creating the .canno file since metaIdSignature LastModified " + + "doesn't match seqIdSignature LastModified: " + + metaIdSignature.getLastModified() + " vs. " + seqIdSignature.getLastModified()); + indexingRequired = true; + } + + if (!CompactSuffixArray.NearlyEqualFileTimes(metaIdSignature.getLastModified(), lastModified)) { + System.out.println("Re-creating the .canno file since metaIdSignature LastModified " + + "is not within 2 seconds of the file modification time on disk: " + + "Expected " + metaIdSignature.getLastModified() + " but actually " + lastModified); + indexingRequired = true; + } + + if (indexingRequired) { createObjectFromRawFile(filepath, alphabet); metaIdSignature = readMetaInfo(); seqIdSignature = readSequence(); + } else { + /* + System.out.println("Metadata matches; no need to re-index"); + + System.out.println("metaIdSignature ID: " + metaIdSignature.getId()); + System.out.println("seqIdSignature ID: " + seqIdSignature.getId()); + System.out.println("metaIdSignature LastModified: " + metaIdSignature.getLastModified()); + System.out.println("seqIdSignature LastModified: " + seqIdSignature.getLastModified()); + System.out.println("FASTA LastModified on disk: " + lastModified + " for " + filepath); + */ } initializeAlphabet(this.alphabetString); diff --git a/src/main/java/edu/ucsd/msjava/msdbsearch/CompactSuffixArray.java b/src/main/java/edu/ucsd/msjava/msdbsearch/CompactSuffixArray.java index d277a6d8..0e05e2b3 100644 --- a/src/main/java/edu/ucsd/msjava/msdbsearch/CompactSuffixArray.java +++ b/src/main/java/edu/ucsd/msjava/msdbsearch/CompactSuffixArray.java @@ -148,8 +148,20 @@ private boolean isCompactSuffixArrayValid(long lastModified) { int id = raf.readInt(); raf.close(); - if (lastModifiedRecorded != lastModified || id != COMPACT_SUFFIX_ARRAY_FILE_FORMAT_ID) + if (!NearlyEqualFileTimes(lastModifiedRecorded, lastModified)) { + System.out.println("Re-creating suffix array files since the cached LastModified time is not within 2 seconds " + + "of the LastModified time of the sequence file:" + + " Time cached in " + f.getName() + " is " + lastModifiedRecorded + + " while the sequence file has " + lastModified); return false; + } + + if (id != COMPACT_SUFFIX_ARRAY_FILE_FORMAT_ID) { + System.out.println("Re-creating suffix array files since " + f.getName() + + " has file format ID " + id + " instead of " + COMPACT_SUFFIX_ARRAY_FILE_FORMAT_ID); + return false; + } + } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { @@ -157,6 +169,9 @@ private boolean isCompactSuffixArrayValid(long lastModified) { } } + //System.out.println("LastModified times in the existing csarr and cnlcp files " + + // "match the LastModified time of the sequence file (" + lastModified + ")"); + return true; } @@ -484,4 +499,23 @@ public void measureNominalMassError(AminoAcidSet aaSet) throws Exception { indices.close(); nlcps.close(); } + + /** + * Compares two timestamps (typically the lastModified value for a file) + * If they agree within 2 seconds, returns True, otherwise false + * @param time1 First file time (milliseconds since 1/1/1970) + * @param time2 Second file time (milliseconds since 1/1/1970) + * @return True if the times agree within 2 seconds + */ + public static boolean NearlyEqualFileTimes(long time1, long time2) + { + double timeDiffSeconds = (time1 - time2) / 1000.0; + if (Math.abs(timeDiffSeconds) <= 2.05) + { + return true; + } + + return false; + } + }