Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to run a Demo #61

Merged
merged 4 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
tools:ignore="GoogleAppIndexingWarning"
android:usesCleartextTraffic="true">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -28,6 +29,7 @@
<activity android:name=".activity.DownloadActivity" />
<activity android:name=".activity.MinITActivity" />
<activity android:name=".activity.TerminalActivity" />
<activity android:name=".activity.DemoActivity" />
</application>

</manifest>
80 changes: 75 additions & 5 deletions app/src/main/java/com/mobilegenomics/f5n/GUIConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public static void printList() {
}
}

public static void configureSteps(Context context) {
public static void configureSteps(Context context, String folderPath) {
// clear() vs new check what is better
steps = new ArrayList<>();
for (PipelineStep pipelineStep : selectedPipelineSteps) {
ArrayList<Argument> arguments = configureArguments(context, pipelineStep);
ArrayList<Argument> arguments = configureArguments(context, pipelineStep, folderPath);
Step step = new Step(pipelineStep, arguments);
steps.add(step);
}
Expand Down Expand Up @@ -125,7 +125,8 @@ public static String getLinkedFileArgument(String fileName) {
return linkedFileArguments.containsKey(fileName) ? linkedFileArguments.get(fileName) : "";
}

private static ArrayList<Argument> configureArguments(Context context, PipelineStep pipelineStep) {
private static ArrayList<Argument> configureArguments(Context context, PipelineStep pipelineStep,
String folderPath) {
int rawFile = 0;
switch (pipelineStep) {
case MINIMAP2_SEQUENCE_ALIGNMENT:
Expand All @@ -150,18 +151,87 @@ private static ArrayList<Argument> configureArguments(Context context, PipelineS
Log.e(TAG, "Invalid Pipeline Step");
break;
}
return buildArgumentsFromJson(context, rawFile);
return buildArgumentsFromJson(context, rawFile, folderPath);
}

private static ArrayList<Argument> buildArgumentsFromJson(Context context, @RawRes int file) {
private static ArrayList<Argument> buildArgumentsFromJson(Context context, @RawRes int file, String folderPath) {
ArrayList<Argument> arguments = new ArrayList<>();
JsonObject argsJson = JSONFileHelper.rawtoJsonObject(context, file);
JsonArray argsJsonArray = argsJson.getAsJsonArray("args");
for (JsonElement element : argsJsonArray) {
Argument argument = new Gson().fromJson(element, Argument.class);
if (folderPath != null) {
configureDemoArgsFilePath(argument, folderPath);
}
arguments.add(argument);
}
return arguments;
}

private static void configureDemoArgsFilePath(Argument argument, String folder) {

if (argument.isFile()) {
argument.setSetByUser(true);
}

// minimap2 input/output files
if (argument.getArgID().equals("MINIMAP2_REF_FILE")) {
argument.setArgValue(folder + "/draft.fa");
}
if (argument.getArgID().equals("MINIMAP2_QUERY_FILE")) {
argument.setArgValue(folder + "/reads.fasta");
}
if (argument.getArgID().equals("MINIMAP2_OUTPUT_FILE")) {
argument.setArgValue(folder + "/minimap2-out.sam");
}

// samtools input/output files
if (argument.getArgID().equals("SAMTOOL_SORT_INPUT_FILE")) {
argument.setArgValue(folder + "/minimap2-out.sam");
}
if (argument.getArgID().equals("SAMTOOL_SORT_OUTPUT_FILE")) {
argument.setArgValue(folder + "/reads.sorted.bam");
}

if (argument.getArgID().equals("SAMTOOL_INDEX_INPUT_FILE")) {
argument.setArgValue(folder + "/reads.sorted.bam");
}

// f5c index input/output files
if (argument.getArgID().equals("F5C_INDEX_FAST5_FILE")) {
argument.setArgValue(folder + "/fast5_files");
}
if (argument.getArgID().equals("F5C_INDEX_FASTA_FILE")) {
argument.setArgValue(folder + "/reads.fasta");
}

// f5c methylation input/output files
if (argument.getArgID().equals("F5C_METH_FASTA_FILE")) {
argument.setArgValue(folder + "/reads.fasta");
}
if (argument.getArgID().equals("F5C_METH_SORTED_FILE")) {
argument.setArgValue(folder + "/reads.sorted.bam");
}
if (argument.getArgID().equals("F5C_METH_REF_FILE")) {
argument.setArgValue(folder + "/draft.fa");
}
if (argument.getArgID().equals("F5C_METH_OUTPUT_FILE")) {
argument.setArgValue(folder + "/f5c-methylation.tsv");
}

// f5c event alignment input/output files
if (argument.getArgID().equals("F5C_ALIGN_FASTA_FILE")) {
argument.setArgValue(folder + "/reads.fasta");
}
if (argument.getArgID().equals("F5C_ALIGN_SORTED_FILE")) {
argument.setArgValue(folder + "/reads.sorted.bam");
}
if (argument.getArgID().equals("F5C_ALIGN_REF_FILE")) {
argument.setArgValue(folder + "/draft.fa");
}
if (argument.getArgID().equals("F5C_ALIGN_OUTPUT_FILE")) {
argument.setArgValue(folder + "/f5c-event-alignment.txt");
}
}

}
207 changes: 207 additions & 0 deletions app/src/main/java/com/mobilegenomics/f5n/activity/DemoActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package com.mobilegenomics.f5n.activity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.PipelineStep;
import com.mobilegenomics.f5n.support.Decompress;
import com.mobilegenomics.f5n.support.TimeFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class DemoActivity extends AppCompatActivity {

private static final String TAG = DemoActivity.class.getSimpleName();

private static final String ecoliDataSetURL = "https://zanojmobiapps.com/_tmp/genome/ecoli/ecoli-data-set.zip";

private static final String folderName = "mobile-genomics";

private static final String fileName = "ecoli-data-set.zip";

private long downloadID;

private LinearLayout linearLayout;

private TextView txtLogView;

private Button btnRunPipeline;

private File logFile;

private FileOutputStream fOut;

private OutputStreamWriter myOutWriter;

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_vertical);

String dirPath = Environment.getExternalStorageDirectory() + "/" + folderName;
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}

logFile = new File(dir.getAbsolutePath() + "/f5n-log.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
Toast.makeText(this, "Error creating log file, Please check permissions", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Error : " + e);
}
}

registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

try {
fOut = new FileOutputStream(logFile, true);
myOutWriter = new OutputStreamWriter(fOut);
String header = "----------- Log for Demo app session " + TimeFormat
.millisToDateTime(System.currentTimeMillis())
+ " -----------\n";
myOutWriter.append(header);
} catch (Exception e) {
Log.e(TAG, "Error : " + e);
}

linearLayout = findViewById(R.id.vertical_linear_layout);

TextView txtInfo = new TextView(this);
txtInfo.setText(getResources().getString(R.string.pipeline_description));
linearLayout.addView(txtInfo);

TextView txtSkipDownload = new TextView(this);
txtSkipDownload.setText(
"If you have already downloaded and extracted the ecoli data set to main-storage/mobile-genomics folder, you can skip Download & Extract");
txtSkipDownload.setPadding(0, 10, 0, 0);
linearLayout.addView(txtSkipDownload);

Button btnDownloadAndExtract = new Button(this);
btnDownloadAndExtract.setText("Download & Extract");
btnDownloadAndExtract.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
btnDownloadAndExtract.setEnabled(false);
downloadDataSet(ecoliDataSetURL);
}
});
linearLayout.addView(btnDownloadAndExtract);

txtLogView = new TextView(this);
// txtLogView.setTextSize(f);
linearLayout.addView(txtLogView);

btnRunPipeline = new Button(this);
btnRunPipeline.setText("Run Pipeline");
btnRunPipeline.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
for (PipelineStep step : PipelineStep.values()) {
GUIConfiguration.addPipelineStep(step);
}
GUIConfiguration.printList();
GUIConfiguration.configureSteps(DemoActivity.this,
Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName.substring(0, fileName.lastIndexOf(".")));
startActivity(new Intent(DemoActivity.this, ConfirmationActivity.class));
}
});
linearLayout.addView(btnRunPipeline);

}

private void downloadDataSet(String url) {
// TODO check wifi connectivity

writeToLogFile("Downloading data set...\n");

DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);

try {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(
Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName));

downloadID = downloadmanager.enqueue(request);
} catch (Exception e) {
Toast.makeText(this, "Invalid URL", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Exception: " + e);
writeToLogFile("[Error] Downloading data set: " + e + "\n");
}
}

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Toast.makeText(DemoActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
writeToLogFile("Downloading data set completed\n");
extractZip(new File(Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName));
}
}
};

@Override
protected void onStop() {
super.onStop();
unregisterReceiver(onDownloadComplete);
try {
myOutWriter.append("-------------------- End of Log --------------------\n\n");
myOutWriter.flush();
myOutWriter.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private void extractZip(File file) {
writeToLogFile("Extracting data set...\n");
Decompress decompress = new Decompress(DemoActivity.this, file);
decompress.unzip();
writeToLogFile("Extracting data set completed\n");
btnRunPipeline.setVisibility(View.VISIBLE);
}

private void writeToLogFile(String lines) {
txtLogView.append(lines);
try {
myOutWriter.append(lines);
myOutWriter.flush();
} catch (IOException e) {
Log.e(TAG, "Error : " + e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.support.PermissionResultCallback;
import com.mobilegenomics.f5n.support.PermissionUtils;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements
Expand Down Expand Up @@ -57,6 +55,10 @@ public void startMinITMode(View view) {
startActivity(new Intent(MainActivity.this, MinITActivity.class));
}

public void startDemoMode(View view) {
startActivity(new Intent(MainActivity.this, DemoActivity.class));
}

/////////////////////////////
// Permission functions
/////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.PipelineStep;

import java.util.ArrayList;

public class PipelineActivity extends AppCompatActivity {
Expand Down Expand Up @@ -73,7 +71,7 @@ private void initPipelineSteps(int mode) {
}
if (!clickedNone) {
GUIConfiguration.printList();
GUIConfiguration.configureSteps(PipelineActivity.this);
GUIConfiguration.configureSteps(PipelineActivity.this, null);
if (mode == MODE_GUI) {
startActivity(new Intent(PipelineActivity.this, StepActivity.class));
} else {
Expand Down
Loading