Skip to content

Commit

Permalink
When comparing LastModified times, allow them to differ by up to 2 se…
Browse files Browse the repository at this point in the history
…conds

Notify the user when re-creating suffix array files or the .canno file
  • Loading branch information
alchemistmatt committed Apr 10, 2018
1 parent 55e6d68 commit c1122ff
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
58 changes: 51 additions & 7 deletions src/main/java/edu/ucsd/msjava/msdbsearch/CompactFastaSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/edu/ucsd/msjava/msdbsearch/CompactSuffixArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,30 @@ 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) {
e.printStackTrace();
}
}

//System.out.println("LastModified times in the existing csarr and cnlcp files " +
// "match the LastModified time of the sequence file (" + lastModified + ")");

return true;
}

Expand Down Expand Up @@ -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;
}

}

0 comments on commit c1122ff

Please sign in to comment.