Skip to content
This repository has been archived by the owner on Mar 20, 2020. It is now read-only.

I23 haplo db #25

Merged
merged 16 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
c0f193e
#4 WIP: Changing over to database style access data models while main…
josiahseaman Jul 23, 2019
36b2e76
#1 WIP: Working on Django definitions with Bryan
josiahseaman Jul 24, 2019
b49ac65
Merge branch 'django_sliced_graph' into i23_haplo_db
josiahseaman Aug 14, 2019
650736e
#23 WIP: Transitioning all code over to using Django Database and que…
josiahseaman Aug 14, 2019
d94bb94
#23 Use Django test runner which creates a temporary database.
josiahseaman Aug 15, 2019
9324a35
#4 WIP: Changing over to database style access data models while main…
josiahseaman Jul 23, 2019
d6db4b7
#1 WIP: Working on Django definitions with Bryan
josiahseaman Jul 24, 2019
3d558b6
#23 WIP: Transitioning all code over to using Django Database and que…
josiahseaman Aug 14, 2019
29b84d2
#23 Use Django test runner which creates a temporary database.
josiahseaman Aug 15, 2019
d70fb50
Merge remote-tracking branch 'origin/i23_haplo_db' into i23_haplo_db
josiahseaman Aug 15, 2019
40811bd
#23 Graph.test.GFATest is migrated to DB versions. All GFATests pass…
josiahseaman Aug 19, 2019
b891006
#23 MAJOR: Removed Obsolete Classes: Slice, Graph. Replaced Slices e…
josiahseaman Aug 19, 2019
645591c
#13 Brought in Toshiyuki's updates to sort.py using forward_paths and…
josiahseaman Aug 20, 2019
dbc5d07
#23 Testing Travis builds with Django
josiahseaman Aug 20, 2019
6dc055c
#23 Testing Travis builds with Django: removed erroneous django_templ…
josiahseaman Aug 20, 2019
f454c6a
Testing Travis builds with Django: using Django test runner
josiahseaman Aug 20, 2019
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
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ dist: xenial

language: python
python:
- 3.7
- 3.7.3

# Command to install dependencies, e.g. pip install -r requirements_dev.txt --use-mirrors
install: pip install -r requirements_dev.txt
install:
- pip install -r requirements_dev.txt
- python manage.py migrate

env:
- DJANGO_VERSION=2.2.1
- DJANGO_SETTINGS_MODULE=vgbrowser.settings

# Command to run tests, e.g. python setup.py test
script: python -m unittest
script:
- python manage.py test
87 changes: 25 additions & 62 deletions Graph/gfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def topologicalSort(self):


class GFA:
def __init__(self, gfa: gfapy.Gfa):
def __init__(self, gfa: gfapy.Gfa, source_path: str):
self.gfa = gfa
self.source_path = source_path

# @classmethod
# def load_from_pickle(cls, file: str):
Expand All @@ -77,15 +78,14 @@ def load_from_xg(cls, file: str, xg_bin: str):
process.wait()
if process.returncode != 0:
raise OSError()
graph = cls(gfa)
instance = cls(gfa, file)
process.stdout.close()
return graph
return instance

@classmethod
def load_from_gfa(cls, file: str):
gfa = gfapy.Gfa.from_file(file)
graph = cls(gfa)
return graph
return cls(gfa, file)

# def save_as_pickle(self, outfile: str):
# with open(outfile, 'wb') as pickle_file:
Expand All @@ -103,67 +103,30 @@ def save_as_gfa(self, file: str):
self.gfa.to_file(file)

@classmethod
def from_graph(cls, graph: Graph):
def from_graph(cls, graph: GraphGenome):
"""Constructs the lines of a GFA file listing paths, then sequence nodes in arbitrary order."""
gfa = gfapy.Gfa()
for path in graph.paths:
node_series = ",".join([traverse.node.id + traverse.strand for traverse in path.nodes])
node_series = ",".join([traverse.node.name + traverse.strand for traverse in path.nodes])
gfa.add_line('\t'.join(['P', path.accession, node_series, ",".join(['*' for _ in path.nodes])]))
for node in graph.nodes.values(): # in no particular order
gfa.add_line('\t'.join(['S', str(node.id), node.seq]))
return cls(gfa)

@property
def to_paths(self) -> List[Path]:
node_hash = {}
for node in graph.nodes: # in no particular order
gfa.add_line('\t'.join(['S', str(node.name), node.seq]))
return cls(gfa, "from Graph")

def to_paths(self) -> GraphGenome:
graph = self.to_graph()
return graph.paths

def to_graph(self) -> GraphGenome:
"""Create parent object for this genome and save it in the database.
This can create duplicates appended in Paths if it is called twice."""
gdb = GraphGenome.objects.get_or_create(name=self.source_path)[0]
for segment in self.gfa.segments:
node_id = segment.name + "+"
node = Node(segment.sequence, [])
node_hash[node_id] = node

node_id = segment.name + "-"
node = Node(segment.sequence, [])
node_hash[node_id] = node
Node.objects.get_or_create(seq=segment.sequence, name=(segment.name), graph=gdb)

paths = []
for path in self.gfa.paths:
nodes = []
for node in path.segment_names:
node_index = NodeTraversal(Node(node_hash[node.name + node.orient].seq, [], node.name), node.orient)
nodes.append(node_index)
paths.append(Path(path.name, nodes))

return paths

@property
def to_graph(self):
# Extract all paths into graph
path_names = [p.name for p in self.gfa.paths]
graph = Graph(path_names) # Paths can be empty at start
for path in self.gfa.paths:
for node in path.segment_names:
graph.append_node_to_path(node.name, node.orient, path.name)
for segment in self.gfa.segments:
graph.nodes[segment.name].seq = segment.sequence
graph.paths = self.to_paths
return graph
# IMPORTANT: It's not clear to Josiah how much of the below is necessary, so it's being left unmodified.


'''
class XGWrapper:
@staticmethod
def save(gfa):
pass

@staticmethod
def load(gfa):
pass

class GraphStack:
def __init__(graphs: List[Graph]):
self.graphs = graphs
'''

if __name__ == "__main__":
location_of_xg = sys.argv[0]
p = Path(accession=path.name, graph=gdb)
p.save()
p.append_gfa_nodes(path.segment_names)
return gdb

51 changes: 51 additions & 0 deletions Graph/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 2.2.1 on 2019-08-14 14:28

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='GraphGenome',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=1000)),
],
),
migrations.CreateModel(
name='Node',
fields=[
('seq', models.CharField(blank=True, max_length=255)),
('name', models.CharField(max_length=15, primary_key=True, serialize=False)),
('graph', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Graph.GraphGenome')),
],
options={
'unique_together': {('graph', 'name')},
},
),
migrations.CreateModel(
name='Path',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('accession', models.CharField(max_length=1000, unique=True)),
('graph', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Graph.GraphGenome')),
],
),
migrations.CreateModel(
name='NodeTraversal',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order', models.IntegerField(help_text='Defines the order a path lists traversals')),
('strand', models.CharField(choices=[('+', '+'), ('-', '-')], default='+', max_length=1)),
('node', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Graph.Node')),
('path', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Graph.Path')),
],
),
]
22 changes: 22 additions & 0 deletions Graph/migrations/0002_Path_unique_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.1 on 2019-08-19 17:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('Graph', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='path',
name='accession',
field=models.CharField(max_length=1000),
),
migrations.AlterUniqueTogether(
name='path',
unique_together={('graph', 'accession')},
),
]
Loading