4242import org .labkey .api .sequenceanalysis .SequenceOutputFile ;
4343import org .labkey .api .sequenceanalysis .pipeline .ReferenceGenome ;
4444import org .labkey .api .sequenceanalysis .pipeline .SequencePipelineService ;
45+ import org .labkey .api .sequenceanalysis .run .PicardWrapper ;
4546import org .labkey .api .sequenceanalysis .run .SimpleScriptWrapper ;
4647import org .labkey .api .settings .AppProps ;
4748import org .labkey .api .util .FileType ;
@@ -587,8 +588,7 @@ public boolean matchesTrackSelector(List<String> toTest)
587588
588589 public String getJsonTrackId ()
589590 {
590- final File finalLocation = getLocationOfProcessedTrack (false );
591- return finalLocation == null ? null : finalLocation .getName ();
591+ return getSourceFileName ();
592592 }
593593
594594 private JSONObject getBamOrCramTrack (Logger log , ExpData targetFile , ReferenceGenome rg )
@@ -627,6 +627,11 @@ private JSONObject getBamOrCramTrack(Logger log, ExpData targetFile, ReferenceGe
627627 {{
628628 put ("location" , new JSONObject ()
629629 {{
630+ if (!new File (targetFile .getFile () + ".bai" ).exists ())
631+ {
632+ log .error ("Track lacks an index: {}, expected: {}" , getObjectId (), targetFile .getFile ().getPath () + ".bai" );
633+ }
634+
630635 put ("uri" , url + ".bai" );
631636 }});
632637 put ("indexType" , "BAI" );
@@ -646,6 +651,11 @@ private JSONObject getBamOrCramTrack(Logger log, ExpData targetFile, ReferenceGe
646651 }});
647652 put ("craiLocation" , new JSONObject ()
648653 {{
654+ if (!new File (targetFile .getFile () + ".bai" ).exists ())
655+ {
656+ log .error ("Track lacks an index: {}, expected: {}" , getObjectId (), targetFile .getFile ().getPath () + ".crai" );
657+ }
658+
649659 put ("uri" , url + ".crai" );
650660 }});
651661 put ("sequenceAdapter" , JBrowseSession .getBgZippedIndexedFastaAdapter (rg ));
@@ -771,6 +781,11 @@ private JSONObject getTabixTrack(User u, Logger log, ExpData targetFile, Referen
771781 return null ;
772782 }
773783
784+ if (!new File (gzipped .getPath () + ".tbi" ).exists ())
785+ {
786+ log .error ("Track lacks an index: {}, expected: {}" , getObjectId (), gzipped .getPath () + ".tbi" );
787+ }
788+
774789 ret .put ("adapter" , new JSONObject (){{
775790 put ("type" , adapterType );
776791 put (prefix + "GzLocation" , new JSONObject (){{
@@ -790,12 +805,12 @@ private JSONObject getTabixTrack(User u, Logger log, ExpData targetFile, Referen
790805
791806 public boolean needsProcessing ()
792807 {
793- return (needsGzip () && !isGzipped ()) || doIndex () || shouldHaveFreeTextSearch ();
808+ return (needsGzip () && !isGzipped ()) || shouldBeReSorted () || doIndex () || shouldHaveFreeTextSearch ();
794809 }
795810
796811 public boolean shouldBeCopiedToProcessDir ()
797812 {
798- return (needsGzip () && !isGzipped ());
813+ return (needsGzip () && !isGzipped ()) || shouldBeReSorted () ;
799814 }
800815
801816 public boolean isGzipped ()
@@ -828,10 +843,16 @@ public File prepareResource(User u, Logger log, boolean throwIfNotPrepared, bool
828843 throw new PipelineJobException ("No ExpData for JsonFile: " + getObjectId ());
829844 }
830845
831- final File processedTrackFile = getLocationOfProcessedTrack (true );
832- final File processedTrackDir = processedTrackFile .getParentFile ();
846+ File processedTrackFile = getLocationOfProcessedTrack (true );
847+ final File processedTrackDir = processedTrackFile == null ? null : processedTrackFile .getParentFile ();
848+ if (processedTrackFile == null )
849+ {
850+ processedTrackFile = expData .getFile ();
851+ log .debug ("Track does not require processing or indexing, using original location: " + processedTrackFile .getPath ());
852+ }
853+
833854 File targetFile = expData .getFile ();
834- if (needsGzip () && !isGzipped ())
855+ if (( needsGzip () && !isGzipped ()) || shouldBeReSorted ())
835856 {
836857 //need to gzip and tabix index:
837858 if (processedTrackFile .exists () && !SequencePipelineService .get ().hasMinLineCount (processedTrackFile , 1 ))
@@ -896,8 +917,30 @@ public File prepareResource(User u, Logger log, boolean throwIfNotPrepared, bool
896917 createIndex (targetFile , log , idx , throwIfNotPrepared );
897918 }
898919
920+ if (TRACK_TYPES .bam .getFileType ().isType (targetFile ) || TRACK_TYPES .cram .getFileType ().isType (targetFile ))
921+ {
922+ File fileIdx = SequenceAnalysisService .get ().getExpectedBamOrCramIndex (targetFile );
923+ if (!fileIdx .exists ())
924+ {
925+ if (throwIfNotPrepared )
926+ {
927+ throw new IllegalStateException ("This track should have been previously indexed: " + targetFile .getName ());
928+ }
929+
930+ if (PicardWrapper .getPicardJar (false ) != null )
931+ {
932+ SequenceAnalysisService .get ().ensureBamOrCramIdx (targetFile , log , false );
933+ }
934+ }
935+ }
936+
899937 if (doIndex ())
900938 {
939+ if (processedTrackDir == null )
940+ {
941+ throw new PipelineJobException ("processedTrackDir should not be null" );
942+ }
943+
901944 File trixDir = FileUtil .appendName (processedTrackDir , "trix" );
902945 if (forceReprocess && trixDir .exists ())
903946 {
@@ -1154,6 +1197,17 @@ else if (TRACK_TYPES.gff.getFileType().isType(finalLocation) || TRACK_TYPES.gtf.
11541197 }
11551198 }
11561199
1200+ private boolean shouldBeReSorted ()
1201+ {
1202+ String sourceFilename = getSourceFileName ();
1203+ if (sourceFilename == null )
1204+ {
1205+ return false ;
1206+ }
1207+
1208+ return TRACK_TYPES .gff .getFileType ().isType (sourceFilename ) || TRACK_TYPES .gtf .getFileType ().isType (sourceFilename ) || TRACK_TYPES .bed .getFileType ().isType (sourceFilename );
1209+ }
1210+
11571211 public File getLocationOfProcessedTrack (boolean createDir )
11581212 {
11591213 ExpData expData = getExpData ();
@@ -1163,6 +1217,11 @@ public File getLocationOfProcessedTrack(boolean createDir)
11631217 }
11641218
11651219 File trackDir = getBaseDir ();
1220+ if (trackDir == null )
1221+ {
1222+ return null ;
1223+ }
1224+
11661225 if (createDir && !trackDir .exists ())
11671226 {
11681227 trackDir .mkdirs ();
0 commit comments